]> git.saurik.com Git - cycript.git/blame - Syntax.hpp
Use Lex rules to guide let [/{ parse in for loops.
[cycript.git] / Syntax.hpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
b4aa79af
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
b4aa79af 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 19**/
b4aa79af
JF
20/* }}} */
21
20052ff7
JF
22#ifndef CYCRIPT_SYNTAX_HPP
23#define CYCRIPT_SYNTAX_HPP
24
25#include <cstdio>
26#include <cstdlib>
63b4c5a8 27
efd689d8 28#include <streambuf>
5999c315 29#include <string>
c3c20102 30#include <vector>
cf7d4c69 31
da2af935 32#include "List.hpp"
00b4cb83 33#include "Location.hpp"
029bc65b 34#include "Options.hpp"
20052ff7 35#include "Pooling.hpp"
029bc65b 36
01d0f64d 37struct CYContext;
924f67b2 38
5999c315 39struct CYThing {
652ec1ba 40 virtual void Output(struct CYOutput &out) const = 0;
5999c315
JF
41};
42
652ec1ba 43struct CYOutput {
efd689d8
JF
44 std::streambuf &out_;
45 CYPosition position_;
46
029bc65b 47 CYOptions &options_;
11c1cc16
JF
48 bool pretty_;
49 unsigned indent_;
efd689d8 50 unsigned recent_;
320ce753 51 bool right_;
652ec1ba 52
96a7e5c2
JF
53 enum {
54 NoMode,
55 NoLetter,
c0bc320e 56 NoPlus,
96a7e5c2
JF
57 NoHyphen,
58 Terminated
59 } mode_;
60
efd689d8 61 CYOutput(std::streambuf &out, CYOptions &options) :
11c1cc16 62 out_(out),
029bc65b 63 options_(options),
11c1cc16 64 pretty_(false),
96a7e5c2 65 indent_(0),
efd689d8 66 recent_(0),
320ce753 67 right_(false),
96a7e5c2 68 mode_(NoMode)
652ec1ba
JF
69 {
70 }
71
96a7e5c2 72 void Check(char value);
1fdca2fa 73 void Terminate();
652ec1ba 74
efd689d8
JF
75 _finline void operator ()(char value) {
76 _assert(out_.sputc(value) != EOF);
77 recent_ = indent_;
78 if (value == '\n')
51b2dc6b 79 position_.Lines(1);
efd689d8 80 else
51b2dc6b 81 position_.Columns(1);
efd689d8
JF
82 }
83
84 _finline void operator ()(const char *data, std::streamsize size) {
85 _assert(out_.sputn(data, size) == size);
86 recent_ = indent_;
51b2dc6b 87 position_.Columns(size);
efd689d8
JF
88 }
89
90 _finline void operator ()(const char *data) {
91 return operator ()(data, strlen(data));
92 }
93
96a7e5c2
JF
94 CYOutput &operator <<(char rhs);
95 CYOutput &operator <<(const char *rhs);
96
97 _finline CYOutput &operator <<(const CYThing *rhs) {
98 if (rhs != NULL)
99 rhs->Output(*this);
652ec1ba
JF
100 return *this;
101 }
102
103 _finline CYOutput &operator <<(const CYThing &rhs) {
104 rhs.Output(*this);
105 return *this;
106 }
107};
5999c315 108
c5b15840
JF
109struct CYExpression;
110struct CYAssignment;
111
e5bc40db 112struct CYPropertyName {
c5b15840
JF
113 virtual bool Computed() const {
114 return false;
115 }
116
117 virtual bool Constructor() const {
118 return false;
119 }
120
121 virtual CYExpression *PropertyName(CYContext &context) = 0;
652ec1ba 122 virtual void PropertyName(CYOutput &out) const = 0;
e5bc40db
JF
123};
124
3b52fd1a
JF
125enum CYNeeded {
126 CYNever = -1,
127 CYSometimes = 0,
128 CYAlways = 1,
129};
130
131enum CYFlags {
132 CYNoFlags = 0,
133 CYNoBrace = (1 << 0),
134 CYNoFunction = (1 << 1),
c5b15840
JF
135 CYNoClass = (1 << 2),
136 CYNoIn = (1 << 3),
137 CYNoCall = (1 << 4),
138 CYNoRightHand = (1 << 5),
139 CYNoDangle = (1 << 6),
140 CYNoInteger = (1 << 7),
141 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
3b52fd1a
JF
142};
143
5a6d4d25
JF
144_finline CYFlags operator ~(CYFlags rhs) {
145 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
146}
147
148_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
149 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
150}
151
152_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
153 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
154}
155
156_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
157 return lhs = lhs | rhs;
158}
159
160_finline CYFlags CYLeft(CYFlags flags) {
161 return flags & ~(CYNoDangle | CYNoInteger);
162}
163
164_finline CYFlags CYRight(CYFlags flags) {
c5b15840 165 return flags & ~CYNoBFC;
5a6d4d25
JF
166}
167
168_finline CYFlags CYCenter(CYFlags flags) {
169 return CYLeft(CYRight(flags));
170}
171
efd689d8
JF
172enum CYCompactType {
173 CYCompactNone,
174 CYCompactLong,
175 CYCompactShort,
176};
177
178#define CYCompact(type) \
179 virtual CYCompactType Compact() const { \
180 return CYCompact ## type; \
181 }
182
3b52fd1a 183struct CYStatement :
b0385401
JF
184 CYNext<CYStatement>,
185 CYThing
3b52fd1a 186{
efd689d8 187 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
3b52fd1a 188 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
b0385401 189 virtual void Output(CYOutput &out) const;
3b52fd1a 190
3b52fd1a
JF
191 virtual CYStatement *Replace(CYContext &context) = 0;
192
efd689d8 193 virtual CYCompactType Compact() const = 0;
12e37ba3
JF
194 virtual CYStatement *Return();
195
3b52fd1a
JF
196 private:
197 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
198};
199
c5b15840 200typedef CYList<CYStatement> CYStatements;
63b4c5a8 201
bfd79fae
JF
202struct CYForInitializer :
203 CYStatement
204{
205 virtual CYForInitializer *Replace(CYContext &context) = 0;
206 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
207};
208
cf7d4c69 209struct CYWord :
e5bc40db 210 CYThing,
c5b15840 211 CYPropertyName
63b4c5a8 212{
cf7d4c69
JF
213 const char *word_;
214
215 CYWord(const char *word) :
216 word_(word)
217 {
218 }
219
c5b15840
JF
220 virtual bool Constructor() const {
221 return strcmp(word_, "constructor") == 0;
222 }
223
029bc65b 224 virtual const char *Word() const;
652ec1ba 225 virtual void Output(CYOutput &out) const;
e5bc40db 226
c5b15840 227 virtual CYExpression *PropertyName(CYContext &context);
652ec1ba 228 virtual void PropertyName(CYOutput &out) const;
63b4c5a8
JF
229};
230
652ec1ba 231_finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
029bc65b
JF
232 lhs << &rhs << '=';
233 return lhs << rhs.Word();
652ec1ba
JF
234}
235
7085e1ab
JF
236enum CYIdentifierKind {
237 CYIdentifierArgument,
238 CYIdentifierCatch,
239 CYIdentifierGlobal,
240 CYIdentifierLexical,
241 CYIdentifierMagic,
242 CYIdentifierOther,
243 CYIdentifierVariable,
244};
245
cf7d4c69 246struct CYIdentifier :
14ec9e00 247 CYNext<CYIdentifier>,
cf7d4c69 248 CYWord
63b4c5a8 249{
7085e1ab 250 CYLocation location_;
6a981250 251 size_t offset_;
e013809d 252 size_t usage_;
029bc65b 253
5999c315 254 CYIdentifier(const char *word) :
029bc65b 255 CYWord(word),
e013809d
JF
256 offset_(0),
257 usage_(0)
5999c315 258 {
cf7d4c69 259 }
029bc65b
JF
260
261 virtual const char *Word() const;
7085e1ab 262 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
63b4c5a8
JF
263};
264
62014ea9 265struct CYLabel :
3b52fd1a 266 CYStatement
62014ea9 267{
9e562cfc 268 CYIdentifier *name_;
3b52fd1a 269 CYStatement *statement_;
cf7d4c69 270
3b52fd1a
JF
271 CYLabel(CYIdentifier *name, CYStatement *statement) :
272 name_(name),
273 statement_(statement)
cf7d4c69
JF
274 {
275 }
fb98ac0c 276
efd689d8
JF
277 CYCompact(Short)
278
3b52fd1a
JF
279 virtual CYStatement *Replace(CYContext &context);
280 virtual void Output(CYOutput &out, CYFlags flags) const;
fb98ac0c
JF
281};
282
14ec9e00 283struct CYCStringLess :
029bc65b
JF
284 std::binary_function<const char *, const char *, bool>
285{
286 _finline bool operator ()(const char *lhs, const char *rhs) const {
287 return strcmp(lhs, rhs) < 0;
288 }
289};
290
291struct CYIdentifierValueLess :
292 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
293{
294 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
14ec9e00 295 return CYCStringLess()(lhs->Word(), rhs->Word());
029bc65b
JF
296 }
297};
298
7085e1ab
JF
299struct CYIdentifierFlags :
300 CYNext<CYIdentifierFlags>
301{
e013809d 302 CYIdentifier *identifier_;
7085e1ab
JF
303 CYIdentifierKind kind_;
304 unsigned count_;
305 unsigned offset_;
e013809d 306
7085e1ab
JF
307 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
308 CYNext<CYIdentifierFlags>(next),
309 identifier_(identifier),
310 kind_(kind),
311 count_(0),
312 offset_(0)
313 {
314 }
315};
e013809d 316
029bc65b 317struct CYScope {
61fe0c53 318 bool transparent_;
029bc65b 319 CYScope *parent_;
7085e1ab
JF
320 bool damaged_;
321 CYIdentifierFlags *shadow_;
a86e34d0 322
7085e1ab 323 CYIdentifierFlags *internal_;
029bc65b 324
50a3d79f 325 CYScope(bool transparent, CYContext &context);
0a356474 326
7085e1ab
JF
327 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
328 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
329
330 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
331 void Merge(CYContext &context, const CYIdentifierFlags *flags);
332
50a3d79f 333 void Close(CYContext &context, CYStatement *&statements);
7085e1ab
JF
334 void Close(CYContext &context);
335 void Damage();
029bc65b
JF
336};
337
a7d8b413 338struct CYScript :
3b52fd1a 339 CYThing
63b4c5a8 340{
b0385401 341 CYStatement *code_;
9e562cfc 342
a7d8b413 343 CYScript(CYStatement *code) :
b0385401 344 code_(code)
9e562cfc
JF
345 {
346 }
cf7d4c69 347
3b52fd1a 348 virtual void Replace(CYContext &context);
3b52fd1a 349 virtual void Output(CYOutput &out) const;
9e562cfc
JF
350};
351
ab2aa221 352struct CYNonLocal;
a0be43fc 353struct CYThisScope;
ab2aa221 354
2c81c6df 355struct CYContext {
6a981250 356 CYOptions &options_;
ab2aa221 357
6a981250 358 CYScope *scope_;
a0be43fc 359 CYThisScope *this_;
c5b15840 360 CYIdentifier *super_;
a0be43fc 361
ab2aa221 362 CYNonLocal *nonlocal_;
06293152 363 CYNonLocal *nextlocal_;
ab2aa221
JF
364 unsigned unique_;
365
7085e1ab
JF
366 std::vector<CYIdentifier *> replace_;
367
2eb8215d 368 CYContext(CYOptions &options) :
6a981250 369 options_(options),
ab2aa221 370 scope_(NULL),
a0be43fc 371 this_(NULL),
c5b15840 372 super_(NULL),
ab2aa221 373 nonlocal_(NULL),
06293152 374 nextlocal_(NULL),
ab2aa221 375 unique_(0)
6a981250
JF
376 {
377 }
378
b0385401
JF
379 void ReplaceAll(CYStatement *&statement) {
380 if (statement == NULL)
381 return;
382 CYStatement *next(statement->next_);
383
384 Replace(statement);
385 ReplaceAll(next);
386
387 if (statement == NULL)
388 statement = next;
389 else
390 statement->SetNext(next);
cde20a5a
JF
391 }
392
6a981250
JF
393 template <typename Type_>
394 void Replace(Type_ *&value) {
395 for (;;) if (value == NULL)
396 break;
397 else {
398 Type_ *replace(value->Replace(*this));
399 if (replace != value)
400 value = replace;
401 else break;
402 }
403 }
ab2aa221
JF
404
405 void NonLocal(CYStatement *&statements);
406 CYIdentifier *Unique();
407};
408
409struct CYNonLocal {
410 CYIdentifier *identifier_;
411
412 CYNonLocal() :
413 identifier_(NULL)
414 {
415 }
416
417 CYIdentifier *Target(CYContext &context) {
418 if (identifier_ == NULL)
419 identifier_ = context.Unique();
420 return identifier_;
421 }
6a981250
JF
422};
423
a0be43fc
JF
424struct CYThisScope :
425 CYNext<CYThisScope>
426{
427 CYIdentifier *identifier_;
428
429 CYThisScope() :
430 identifier_(NULL)
431 {
432 }
433
434 CYIdentifier *Identifier(CYContext &context) {
435 if (next_ != NULL)
436 return next_->Identifier(context);
437 if (identifier_ == NULL)
438 identifier_ = context.Unique();
439 return identifier_;
440 }
441};
442
9e562cfc 443struct CYBlock :
b0385401 444 CYStatement
9e562cfc 445{
b0385401 446 CYStatement *code_;
9e562cfc 447
b0385401
JF
448 CYBlock(CYStatement *code) :
449 code_(code)
9e562cfc 450 {
cf7d4c69 451 }
9e562cfc 452
efd689d8
JF
453 CYCompact(Short)
454
3b52fd1a
JF
455 virtual CYStatement *Replace(CYContext &context);
456
fb98ac0c 457 virtual void Output(CYOutput &out, CYFlags flags) const;
12e37ba3
JF
458
459 virtual CYStatement *Return();
cf7d4c69
JF
460};
461
7085e1ab
JF
462struct CYTarget;
463struct CYVar;
b158281e 464
7085e1ab
JF
465struct CYForInInitializer {
466 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
c8a0500b 467
7085e1ab 468 virtual CYTarget *Replace(CYContext &context) = 0;
c8a0500b 469 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
b09da87b
JF
470};
471
0afc9ba3
JF
472struct CYFunctionParameter;
473
4644480a
JF
474struct CYNumber;
475struct CYString;
476
cf7d4c69 477struct CYExpression :
96a7e5c2 478 CYThing
63b4c5a8 479{
9a39f705 480 virtual int Precedence() const = 0;
75b0a457 481
fb98ac0c
JF
482 virtual bool RightHand() const {
483 return true;
484 }
485
7085e1ab
JF
486 virtual bool Eval() const {
487 return false;
488 }
75b0a457 489
7085e1ab 490 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
6c093cce 491
96a7e5c2 492 virtual void Output(CYOutput &out) const;
652ec1ba 493 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
9a39f705 494 void Output(CYOutput &out, int precedence, CYFlags flags) const;
dea834b0 495
3b52fd1a
JF
496 virtual CYExpression *Replace(CYContext &context) = 0;
497
4644480a 498 virtual CYExpression *Primitive(CYContext &context) {
fd5cdf97 499 return NULL;
4644480a
JF
500 }
501
0afc9ba3 502 virtual CYFunctionParameter *Parameter() const;
0afc9ba3 503
4644480a
JF
504 virtual CYNumber *Number(CYContext &context) {
505 return NULL;
506 }
507
508 virtual CYString *String(CYContext &context) {
509 return NULL;
510 }
511
dea834b0
JF
512 virtual const char *Word() const {
513 return NULL;
514 }
63b4c5a8
JF
515};
516
7085e1ab
JF
517struct CYTarget :
518 CYExpression,
519 CYForInInitializer
520{
521 virtual bool RightHand() const {
522 return false;
523 }
524
525 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
526
527 virtual CYTarget *Replace(CYContext &context) = 0;
528 using CYExpression::Output;
529};
530
b09da87b
JF
531#define CYAlphabetic(value) \
532 virtual bool Alphabetic() const { \
533 return value; \
534 }
535
d35a3b07 536#define CYPrecedence(value) \
9a39f705
JF
537 static const int Precedence_ = value; \
538 virtual int Precedence() const { \
8351aa30 539 return Precedence_; \
d35a3b07
JF
540 }
541
542struct CYCompound :
543 CYExpression
544{
fd5cdf97
JF
545 CYExpression *expression_;
546 CYExpression *next_;
d35a3b07 547
b0385401 548 CYCompound(CYExpression *expression, CYExpression *next) :
fd5cdf97
JF
549 expression_(expression),
550 next_(next)
d35a3b07 551 {
fd5cdf97 552 _assert(expression_ != NULL);
b0385401 553 _assert(next != NULL);
d35a3b07
JF
554 }
555
556 CYPrecedence(17)
557
3b52fd1a 558 virtual CYExpression *Replace(CYContext &context);
652ec1ba 559 void Output(CYOutput &out, CYFlags flags) const;
e06e5ee1 560
b0385401
JF
561 virtual CYFunctionParameter *Parameter() const;
562};
563
564struct CYParenthetical :
7085e1ab 565 CYTarget
b0385401
JF
566{
567 CYExpression *expression_;
568
569 CYParenthetical(CYExpression *expression) :
570 expression_(expression)
571 {
572 }
573
574 CYPrecedence(0)
575
7085e1ab 576 virtual CYTarget *Replace(CYContext &context);
b0385401 577 void Output(CYOutput &out, CYFlags flags) const;
d35a3b07 578};
5999c315 579
c8a0500b
JF
580struct CYDeclaration;
581
3b52fd1a
JF
582struct CYFunctionParameter :
583 CYNext<CYFunctionParameter>,
584 CYThing
585{
7085e1ab 586 CYDeclaration *initialiser_;
3b52fd1a 587
7085e1ab 588 CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) :
3b52fd1a 589 CYNext<CYFunctionParameter>(next),
c8a0500b 590 initialiser_(initialiser)
4e11a430
JF
591 {
592 }
593
b0385401 594 void Replace(CYContext &context, CYStatement *&statements);
c8a0500b 595 void Output(CYOutput &out) const;
3b52fd1a
JF
596};
597
75b0a457 598struct CYComprehension :
96a7e5c2
JF
599 CYNext<CYComprehension>,
600 CYThing
75b0a457 601{
c2529502
JF
602 CYComprehension(CYComprehension *next = NULL) :
603 CYNext<CYComprehension>(next)
604 {
605 }
606
b3aa25d8
JF
607 CYComprehension *Modify(CYComprehension *next) {
608 next_ = next;
609 return this;
610 }
611
3b52fd1a
JF
612 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
613 CYFunctionParameter *Parameters(CYContext &context) const;
614 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 615 virtual void Output(CYOutput &out) const = 0;
75b0a457
JF
616};
617
618struct CYForInComprehension :
619 CYComprehension
620{
4e3c9056 621 CYDeclaration *declaration_;
75b0a457
JF
622 CYExpression *set_;
623
4e3c9056 624 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
c2529502 625 CYComprehension(next),
4e3c9056 626 declaration_(declaration),
75b0a457
JF
627 set_(set)
628 {
629 }
630
3b52fd1a
JF
631 virtual CYFunctionParameter *Parameter(CYContext &context) const;
632 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 633 virtual void Output(CYOutput &out) const;
75b0a457
JF
634};
635
d5618df7 636struct CYForOfComprehension :
75b0a457
JF
637 CYComprehension
638{
4e3c9056 639 CYDeclaration *declaration_;
75b0a457
JF
640 CYExpression *set_;
641
4e3c9056 642 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
c2529502 643 CYComprehension(next),
4e3c9056 644 declaration_(declaration),
75b0a457
JF
645 set_(set)
646 {
647 }
648
3b52fd1a
JF
649 virtual CYFunctionParameter *Parameter(CYContext &context) const;
650 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 651 virtual void Output(CYOutput &out) const;
75b0a457
JF
652};
653
654struct CYIfComprehension :
655 CYComprehension
656{
657 CYExpression *test_;
658
b3aa25d8
JF
659 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
660 CYComprehension(next),
75b0a457
JF
661 test_(test)
662 {
663 }
664
3b52fd1a
JF
665 virtual CYFunctionParameter *Parameter(CYContext &context) const;
666 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 667 virtual void Output(CYOutput &out) const;
75b0a457
JF
668};
669
670struct CYArrayComprehension :
7085e1ab 671 CYTarget
75b0a457
JF
672{
673 CYExpression *expression_;
674 CYComprehension *comprehensions_;
675
676 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
677 expression_(expression),
678 comprehensions_(comprehensions)
679 {
680 }
681
682 CYPrecedence(0)
683
7085e1ab 684 virtual CYTarget *Replace(CYContext &context);
652ec1ba 685 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
686};
687
cf7d4c69 688struct CYLiteral :
7085e1ab 689 CYTarget
63b4c5a8 690{
7085e1ab
JF
691 CYLocation location_;
692
d35a3b07 693 CYPrecedence(0)
fd5cdf97
JF
694
695 virtual CYExpression *Primitive(CYContext &context) {
696 return this;
697 }
cf7d4c69 698};
63b4c5a8 699
3b52fd1a
JF
700struct CYTrivial :
701 CYLiteral
702{
7085e1ab 703 virtual CYTarget *Replace(CYContext &context);
3b52fd1a
JF
704};
705
478d4ed0 706struct CYMagic :
7085e1ab 707 CYTarget
478d4ed0
JF
708{
709 CYPrecedence(0)
710};
711
dea834b0
JF
712struct CYRange {
713 uint64_t lo_;
714 uint64_t hi_;
715
716 CYRange(uint64_t lo, uint64_t hi) :
717 lo_(lo), hi_(hi)
718 {
719 }
720
721 bool operator [](uint8_t value) const {
722 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
723 }
724
725 void operator()(uint8_t value) {
726 if (value >> 7)
727 return;
728 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
729 }
730};
731
283e7e33 732extern CYRange DigitRange_;
dea834b0
JF
733extern CYRange WordStartRange_;
734extern CYRange WordEndRange_;
735
cf7d4c69 736struct CYString :
3b52fd1a 737 CYTrivial,
e5bc40db 738 CYPropertyName
cf7d4c69
JF
739{
740 const char *value_;
5999c315 741 size_t size_;
cf7d4c69 742
3b52fd1a
JF
743 CYString() :
744 value_(NULL),
745 size_(0)
746 {
747 }
748
749 CYString(const char *value) :
750 value_(value),
751 size_(strlen(value))
752 {
753 }
754
5999c315
JF
755 CYString(const char *value, size_t size) :
756 value_(value),
757 size_(size)
cf7d4c69
JF
758 {
759 }
760
3b52fd1a 761 CYString(const CYWord *word) :
029bc65b 762 value_(word->Word()),
5999c315 763 size_(strlen(value_))
cf7d4c69
JF
764 {
765 }
766
5999c315 767 const char *Value() const {
cf7d4c69
JF
768 return value_;
769 }
770
11c1cc16 771 virtual const char *Word() const;
dea834b0 772
4644480a
JF
773 virtual CYNumber *Number(CYContext &context);
774 virtual CYString *String(CYContext &context);
775
5db9a7f5 776 CYString *Concat(CYContext &out, CYString *rhs) const;
652ec1ba 777 virtual void Output(CYOutput &out, CYFlags flags) const;
c5b15840
JF
778
779 virtual CYExpression *PropertyName(CYContext &context);
652ec1ba 780 virtual void PropertyName(CYOutput &out) const;
63b4c5a8
JF
781};
782
fc8fc33d 783struct CYElementValue;
b900e1a4
JF
784
785struct CYSpan :
786 CYNext<CYSpan>
787{
788 CYExpression *expression_;
789 CYString *string_;
790
791 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
792 CYNext<CYSpan>(next),
793 expression_(expression),
794 string_(string)
795 {
796 }
797
fc8fc33d 798 CYElementValue *Replace(CYContext &context);
b900e1a4
JF
799};
800
801struct CYTemplate :
7085e1ab 802 CYTarget
b900e1a4
JF
803{
804 CYString *string_;
805 CYSpan *spans_;
806
807 CYTemplate(CYString *string, CYSpan *spans) :
808 string_(string),
809 spans_(spans)
810 {
811 }
812
813 CYPrecedence(0)
b900e1a4 814
7085e1ab 815 virtual CYTarget *Replace(CYContext &context);
b900e1a4
JF
816 virtual void Output(CYOutput &out, CYFlags flags) const;
817};
818
cf7d4c69 819struct CYNumber :
3b52fd1a 820 CYTrivial,
e5bc40db 821 CYPropertyName
cf7d4c69 822{
5999c315
JF
823 double value_;
824
825 CYNumber(double value) :
826 value_(value)
827 {
828 }
829
830 double Value() const {
831 return value_;
cf7d4c69
JF
832 }
833
4644480a
JF
834 virtual CYNumber *Number(CYContext &context);
835 virtual CYString *String(CYContext &context);
836
652ec1ba 837 virtual void Output(CYOutput &out, CYFlags flags) const;
c5b15840
JF
838
839 virtual CYExpression *PropertyName(CYContext &context);
840 virtual void PropertyName(CYOutput &out) const;
841};
842
843struct CYComputed :
844 CYPropertyName
845{
846 CYExpression *expression_;
847
848 CYComputed(CYExpression *expression) :
849 expression_(expression)
850 {
851 }
852
853 virtual bool Computed() const {
854 return true;
855 }
856
857 virtual CYExpression *PropertyName(CYContext &context);
652ec1ba 858 virtual void PropertyName(CYOutput &out) const;
cf7d4c69
JF
859};
860
63cd45c9 861struct CYRegEx :
3b52fd1a 862 CYTrivial
63cd45c9
JF
863{
864 const char *value_;
c8a2a786 865 size_t size_;
63cd45c9 866
c8a2a786
JF
867 CYRegEx(const char *value, size_t size) :
868 value_(value),
869 size_(size)
63cd45c9
JF
870 {
871 }
872
873 const char *Value() const {
874 return value_;
875 }
876
877 virtual void Output(CYOutput &out, CYFlags flags) const;
878};
879
cf7d4c69 880struct CYNull :
3b52fd1a 881 CYTrivial
cf7d4c69 882{
4644480a
JF
883 virtual CYNumber *Number(CYContext &context);
884 virtual CYString *String(CYContext &context);
885
652ec1ba 886 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
887};
888
889struct CYThis :
478d4ed0 890 CYMagic
cf7d4c69 891{
7085e1ab 892 virtual CYTarget *Replace(CYContext &context);
652ec1ba 893 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
894};
895
896struct CYBoolean :
3b52fd1a 897 CYTrivial
cf7d4c69 898{
42520424
JF
899 CYPrecedence(4)
900
901 virtual bool RightHand() const {
902 return true;
903 }
904
5999c315 905 virtual bool Value() const = 0;
652ec1ba 906 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
907};
908
909struct CYFalse :
cf7d4c69
JF
910 CYBoolean
911{
11c1cc16
JF
912 virtual bool Value() const {
913 return false;
914 }
4644480a
JF
915
916 virtual CYNumber *Number(CYContext &context);
917 virtual CYString *String(CYContext &context);
cf7d4c69
JF
918};
919
920struct CYTrue :
cf7d4c69
JF
921 CYBoolean
922{
11c1cc16
JF
923 virtual bool Value() const {
924 return true;
925 }
4644480a
JF
926
927 virtual CYNumber *Number(CYContext &context);
928 virtual CYString *String(CYContext &context);
cf7d4c69
JF
929};
930
931struct CYVariable :
7085e1ab 932 CYTarget
cf7d4c69
JF
933{
934 CYIdentifier *name_;
935
936 CYVariable(CYIdentifier *name) :
937 name_(name)
938 {
939 }
5999c315 940
2eb8215d
JF
941 CYVariable(const char *name) :
942 name_(new($pool) CYIdentifier(name))
943 {
944 }
945
d35a3b07
JF
946 CYPrecedence(0)
947
7085e1ab
JF
948 virtual bool Eval() const {
949 return strcmp(name_->Word(), "eval") == 0;
950 }
951
952 virtual CYTarget *Replace(CYContext &context);
652ec1ba 953 virtual void Output(CYOutput &out, CYFlags flags) const;
0afc9ba3
JF
954
955 virtual CYFunctionParameter *Parameter() const;
cf7d4c69
JF
956};
957
958struct CYPrefix :
63b4c5a8
JF
959 CYExpression
960{
961 CYExpression *rhs_;
962
cf7d4c69 963 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
964 rhs_(rhs)
965 {
966 }
5999c315 967
b09da87b 968 virtual bool Alphabetic() const = 0;
5999c315
JF
969 virtual const char *Operator() const = 0;
970
3b52fd1a
JF
971 CYPrecedence(4)
972
973 virtual CYExpression *Replace(CYContext &context);
652ec1ba 974 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
975};
976
cf7d4c69 977struct CYInfix :
63b4c5a8
JF
978 CYExpression
979{
980 CYExpression *lhs_;
981 CYExpression *rhs_;
982
cf7d4c69 983 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
984 lhs_(lhs),
985 rhs_(rhs)
986 {
987 }
5999c315 988
0ff9f149
JF
989 void SetLeft(CYExpression *lhs) {
990 lhs_ = lhs;
991 }
992
b09da87b 993 virtual bool Alphabetic() const = 0;
5999c315
JF
994 virtual const char *Operator() const = 0;
995
3b52fd1a 996 virtual CYExpression *Replace(CYContext &context);
652ec1ba 997 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
998};
999
cf7d4c69 1000struct CYPostfix :
63b4c5a8
JF
1001 CYExpression
1002{
1003 CYExpression *lhs_;
1004
cf7d4c69 1005 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
1006 lhs_(lhs)
1007 {
1008 }
5999c315
JF
1009
1010 virtual const char *Operator() const = 0;
1011
3b52fd1a
JF
1012 CYPrecedence(3)
1013
1014 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1015 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
1016};
1017
cf7d4c69 1018struct CYAssignment :
d35a3b07 1019 CYExpression
cf7d4c69 1020{
7085e1ab 1021 CYTarget *lhs_;
d35a3b07
JF
1022 CYExpression *rhs_;
1023
7085e1ab 1024 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
d35a3b07
JF
1025 lhs_(lhs),
1026 rhs_(rhs)
cf7d4c69
JF
1027 {
1028 }
5999c315 1029
8b77acf1
JF
1030 void SetRight(CYExpression *rhs) {
1031 rhs_ = rhs;
0ff9f149
JF
1032 }
1033
5999c315 1034 virtual const char *Operator() const = 0;
d35a3b07 1035
4de0686f
JF
1036 CYPrecedence(16)
1037
3b52fd1a 1038 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1039 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1040};
1041
62014ea9 1042struct CYArgument :
96a7e5c2
JF
1043 CYNext<CYArgument>,
1044 CYThing
62014ea9 1045{
cf7d4c69
JF
1046 CYWord *name_;
1047 CYExpression *value_;
cf7d4c69 1048
3b52fd1a
JF
1049 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1050 CYNext<CYArgument>(next),
1051 name_(NULL),
1052 value_(value)
1053 {
1054 }
1055
cf7d4c69 1056 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
62014ea9 1057 CYNext<CYArgument>(next),
cf7d4c69 1058 name_(name),
62014ea9 1059 value_(value)
cf7d4c69
JF
1060 {
1061 }
5999c315 1062
5192746c 1063 CYArgument *Replace(CYContext &context);
652ec1ba 1064 void Output(CYOutput &out) const;
cf7d4c69
JF
1065};
1066
5999c315
JF
1067struct CYClause :
1068 CYThing,
1069 CYNext<CYClause>
1070{
cf7d4c69 1071 CYExpression *case_;
b0385401 1072 CYStatement *code_;
cf7d4c69 1073
b0385401 1074 CYClause(CYExpression *_case, CYStatement *code) :
cf7d4c69 1075 case_(_case),
b0385401 1076 code_(code)
cf7d4c69
JF
1077 {
1078 }
1079
fa389b0f 1080 void Replace(CYContext &context);
652ec1ba 1081 virtual void Output(CYOutput &out) const;
cf7d4c69
JF
1082};
1083
62014ea9 1084struct CYElement :
96a7e5c2 1085 CYThing
fc8fc33d
JF
1086{
1087 virtual bool Elision() const = 0;
1088
1089 virtual void Replace(CYContext &context) = 0;
1090};
1091
1092struct CYElementValue :
1093 CYNext<CYElement>,
1094 CYElement
62014ea9 1095{
cf7d4c69 1096 CYExpression *value_;
cf7d4c69 1097
fc8fc33d 1098 CYElementValue(CYExpression *value, CYElement *next) :
62014ea9
JF
1099 CYNext<CYElement>(next),
1100 value_(value)
cf7d4c69
JF
1101 {
1102 }
5999c315 1103
fc8fc33d
JF
1104 virtual bool Elision() const {
1105 return value_ == NULL;
1106 }
1107
1108 virtual void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1110};
1111
1112struct CYElementSpread :
1113 CYElement
1114{
1115 CYExpression *value_;
1116
1117 CYElementSpread(CYExpression *value) :
1118 value_(value)
1119 {
1120 }
1121
1122 virtual bool Elision() const {
1123 return false;
1124 }
1125
1126 virtual void Replace(CYContext &context);
1127 virtual void Output(CYOutput &out) const;
5befe15e
JF
1128};
1129
1130struct CYArray :
1131 CYLiteral
1132{
1133 CYElement *elements_;
1134
3b52fd1a 1135 CYArray(CYElement *elements = NULL) :
5befe15e
JF
1136 elements_(elements)
1137 {
1138 }
1139
7085e1ab 1140 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1141 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1142};
1143
7085e1ab 1144struct CYDeclaration {
cf7d4c69
JF
1145 CYIdentifier *identifier_;
1146 CYExpression *initialiser_;
1147
550ee46a 1148 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
cf7d4c69
JF
1149 identifier_(identifier),
1150 initialiser_(initialiser)
1151 {
1152 }
5999c315 1153
7085e1ab 1154 CYTarget *Target(CYContext &context);
3b52fd1a 1155
7085e1ab
JF
1156 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1158};
1159
1160struct CYForLexical :
1161 CYForInInitializer
1162{
1163 bool constant_;
1164 CYDeclaration *declaration_;
1165
1166 CYForLexical(bool constant, CYDeclaration *declaration) :
1167 constant_(constant),
1168 declaration_(declaration)
1169 {
1170 }
1171
1172 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1173
1174 virtual CYTarget *Replace(CYContext &context);
1175 virtual void Output(CYOutput &out, CYFlags flags) const;
1176};
15b88a33 1177
7085e1ab
JF
1178struct CYForVariable :
1179 CYForInInitializer
1180{
1181 CYDeclaration *declaration_;
75b0a457 1182
7085e1ab
JF
1183 CYForVariable(CYDeclaration *declaration) :
1184 declaration_(declaration)
1185 {
1186 }
1187
1188 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1189
1190 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1191 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1192};
1193
1194struct CYDeclarations :
cac61857 1195 CYNext<CYDeclarations>,
15b88a33 1196 CYThing
cf7d4c69
JF
1197{
1198 CYDeclaration *declaration_;
cf7d4c69 1199
cacd1a88 1200 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
cac61857
JF
1201 CYNext<CYDeclarations>(next),
1202 declaration_(declaration)
1203 {
1204 }
1205
7085e1ab 1206 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
96a7e5c2 1207
15b88a33
JF
1208 CYArgument *Argument(CYContext &context);
1209 CYFunctionParameter *Parameter(CYContext &context);
3b52fd1a 1210
96a7e5c2 1211 virtual void Output(CYOutput &out) const;
652ec1ba 1212 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1213};
1214
1215struct CYVar :
bfd79fae 1216 CYForInitializer
cac61857
JF
1217{
1218 CYDeclarations *declarations_;
1219
1220 CYVar(CYDeclarations *declarations) :
1221 declarations_(declarations)
1222 {
1223 }
1224
efd689d8
JF
1225 CYCompact(None)
1226
bfd79fae 1227 virtual CYForInitializer *Replace(CYContext &context);
fb98ac0c 1228 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1229};
1230
ca6a1b2b 1231struct CYLet :
bfd79fae 1232 CYForInitializer
cac61857 1233{
7085e1ab 1234 bool constant_;
cac61857 1235 CYDeclarations *declarations_;
cac61857 1236
7085e1ab
JF
1237 CYLet(bool constant, CYDeclarations *declarations) :
1238 constant_(constant),
ca6a1b2b 1239 declarations_(declarations)
cf7d4c69
JF
1240 {
1241 }
5999c315 1242
ca6a1b2b 1243 CYCompact(None)
efd689d8 1244
bfd79fae 1245 virtual CYForInitializer *Replace(CYContext &context);
fb98ac0c 1246 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1247};
1248
c5b15840
JF
1249struct CYBuilder {
1250 CYList<CYDeclarations> declarations_;
1251 CYList<CYStatement> statements_;
1252
1253 operator bool() const {
1254 return statements_ != NULL;
1255 }
1256};
1257
1258struct CYProperty :
1259 CYNext<CYProperty>,
1260 CYThing
1261{
1262 CYPropertyName *name_;
1263
1264 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1265 CYNext<CYProperty>(next),
1266 name_(name)
1267 {
1268 }
1269
7b87d205
JF
1270 virtual bool Update() const;
1271
c5b15840 1272 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
a196a97a 1273 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
c5b15840 1274
a196a97a 1275 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
c5b15840
JF
1276
1277 virtual void Replace(CYContext &context) = 0;
1278 virtual void Output(CYOutput &out) const;
1279};
1280
1281struct CYPropertyValue :
1282 CYProperty
1283{
1284 CYExpression *value_;
1285
1286 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1287 CYProperty(name, next),
1288 value_(value)
1289 {
1290 }
1291
a196a97a 1292 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
c5b15840
JF
1293 virtual void Replace(CYContext &context);
1294 virtual void Output(CYOutput &out) const;
1295};
1296
cf7d4c69
JF
1297struct CYFor :
1298 CYStatement
1299{
a7d8b413 1300 CYForInitializer *initialiser_;
cf7d4c69
JF
1301 CYExpression *test_;
1302 CYExpression *increment_;
1303 CYStatement *code_;
1304
a7d8b413 1305 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
cf7d4c69
JF
1306 initialiser_(initialiser),
1307 test_(test),
1308 increment_(increment),
1309 code_(code)
1310 {
1311 }
5999c315 1312
efd689d8
JF
1313 CYCompact(Long)
1314
3b52fd1a 1315 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1316 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1317};
1318
1319struct CYForIn :
1320 CYStatement
1321{
a7d8b413 1322 CYForInInitializer *initialiser_;
cf7d4c69
JF
1323 CYExpression *set_;
1324 CYStatement *code_;
1325
a7d8b413 1326 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
cf7d4c69
JF
1327 initialiser_(initialiser),
1328 set_(set),
1329 code_(code)
1330 {
1331 }
5999c315 1332
efd689d8
JF
1333 CYCompact(Long)
1334
3b52fd1a 1335 virtual CYStatement *Replace(CYContext &context);
23111dca
JF
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1337};
1338
1339struct CYForInitialized :
1340 CYStatement
1341{
1342 CYDeclaration *declaration_;
1343 CYExpression *set_;
1344 CYStatement *code_;
1345
1346 CYForInitialized(CYDeclaration *declaration, CYExpression *set, CYStatement *code) :
1347 declaration_(declaration),
1348 set_(set),
1349 code_(code)
1350 {
1351 }
1352
1353 CYCompact(Long)
1354
1355 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1356 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1357};
1358
d5618df7 1359struct CYForOf :
75b0a457
JF
1360 CYStatement
1361{
a7d8b413 1362 CYForInInitializer *initialiser_;
75b0a457
JF
1363 CYExpression *set_;
1364 CYStatement *code_;
1365
a7d8b413 1366 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
75b0a457
JF
1367 initialiser_(initialiser),
1368 set_(set),
1369 code_(code)
1370 {
1371 }
1372
efd689d8
JF
1373 CYCompact(Long)
1374
3b52fd1a 1375 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1376 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
1377};
1378
693d501b
JF
1379struct CYObject :
1380 CYLiteral
1381{
3b52fd1a 1382 CYProperty *properties_;
693d501b 1383
ab2aa221 1384 CYObject(CYProperty *properties = NULL) :
3b52fd1a 1385 properties_(properties)
693d501b
JF
1386 {
1387 }
1388
7085e1ab 1389 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1390 void Output(CYOutput &out, CYFlags flags) const;
693d501b
JF
1391};
1392
cf7d4c69 1393struct CYMember :
7085e1ab 1394 CYTarget
cf7d4c69
JF
1395{
1396 CYExpression *object_;
1397 CYExpression *property_;
1398
1399 CYMember(CYExpression *object, CYExpression *property) :
1400 object_(object),
1401 property_(property)
1402 {
1403 }
5999c315 1404
9b5527f0
JF
1405 void SetLeft(CYExpression *object) {
1406 object_ = object;
1407 }
1408};
1409
1410struct CYDirectMember :
1411 CYMember
1412{
1413 CYDirectMember(CYExpression *object, CYExpression *property) :
1414 CYMember(object, property)
1415 {
1416 }
1417
1418 CYPrecedence(1)
1419
7085e1ab 1420 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1421 virtual void Output(CYOutput &out, CYFlags flags) const;
9b5527f0
JF
1422};
1423
1424struct CYIndirectMember :
1425 CYMember
1426{
1427 CYIndirectMember(CYExpression *object, CYExpression *property) :
1428 CYMember(object, property)
1429 {
1430 }
1431
d35a3b07
JF
1432 CYPrecedence(1)
1433
7085e1ab 1434 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1435 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1436};
1437
2eb8215d
JF
1438namespace cy {
1439namespace Syntax {
1440
1441struct New :
7085e1ab 1442 CYTarget
cf7d4c69
JF
1443{
1444 CYExpression *constructor_;
1445 CYArgument *arguments_;
1446
c5b15840 1447 New(CYExpression *constructor, CYArgument *arguments = NULL) :
cf7d4c69
JF
1448 constructor_(constructor),
1449 arguments_(arguments)
1450 {
1451 }
5999c315 1452
9a39f705 1453 virtual int Precedence() const {
fb98ac0c
JF
1454 return arguments_ == NULL ? 2 : 1;
1455 }
1456
d35a3b07 1457
7085e1ab 1458 virtual CYTarget *Replace(CYContext &context);
652ec1ba 1459 virtual void Output(CYOutput &out, CYFlags flags) const;
6c093cce 1460
7085e1ab 1461 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
cf7d4c69
JF
1462};
1463
2eb8215d
JF
1464} }
1465
7085e1ab
JF
1466struct CYApply :
1467 CYTarget
cf7d4c69 1468{
cf7d4c69
JF
1469 CYArgument *arguments_;
1470
7085e1ab 1471 CYApply(CYArgument *arguments = NULL) :
cf7d4c69
JF
1472 arguments_(arguments)
1473 {
1474 }
5999c315 1475
fb98ac0c 1476 CYPrecedence(1)
d35a3b07 1477
7085e1ab
JF
1478 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1479};
1480
1481struct CYCall :
1482 CYApply
1483{
1484 CYExpression *function_;
1485
1486 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1487 CYApply(arguments),
1488 function_(function)
1489 {
1490 }
1491
652ec1ba 1492 virtual void Output(CYOutput &out, CYFlags flags) const;
7085e1ab
JF
1493 virtual CYTarget *Replace(CYContext &context);
1494};
1495
1496struct CYEval :
1497 CYApply
1498{
1499 CYEval(CYArgument *arguments) :
1500 CYApply(arguments)
1501 {
1502 }
6c093cce 1503
7085e1ab
JF
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1505 virtual CYTarget *Replace(CYContext &context);
6c093cce
JF
1506};
1507
1508struct CYRubyProc;
1509
1510struct CYRubyBlock :
7085e1ab 1511 CYTarget
6c093cce
JF
1512{
1513 CYExpression *call_;
1514 CYRubyProc *proc_;
1515
1516 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1517 call_(call),
1518 proc_(proc)
1519 {
1520 }
1521
1522 CYPrecedence(1)
6c093cce 1523
7085e1ab 1524 virtual CYTarget *Replace(CYContext &context);
6c093cce 1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1abe53bf 1526
7085e1ab 1527 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
cf7d4c69
JF
1528};
1529
1530struct CYIf :
1531 CYStatement
1532{
1533 CYExpression *test_;
1534 CYStatement *true_;
1535 CYStatement *false_;
1536
3b52fd1a 1537 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
cf7d4c69
JF
1538 test_(test),
1539 true_(_true),
1540 false_(_false)
1541 {
1542 }
5999c315 1543
efd689d8
JF
1544 CYCompact(Long)
1545
3b52fd1a 1546 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1547 virtual void Output(CYOutput &out, CYFlags flags) const;
12e37ba3
JF
1548
1549 virtual CYStatement *Return();
cf7d4c69
JF
1550};
1551
1552struct CYDoWhile :
1553 CYStatement
1554{
1555 CYExpression *test_;
1556 CYStatement *code_;
1557
1558 CYDoWhile(CYExpression *test, CYStatement *code) :
1559 test_(test),
1560 code_(code)
1561 {
1562 }
5999c315 1563
efd689d8
JF
1564 CYCompact(None)
1565
3b52fd1a 1566 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1567 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1568};
1569
1570struct CYWhile :
1571 CYStatement
1572{
1573 CYExpression *test_;
1574 CYStatement *code_;
1575
1576 CYWhile(CYExpression *test, CYStatement *code) :
1577 test_(test),
1578 code_(code)
1579 {
1580 }
5999c315 1581
efd689d8
JF
1582 CYCompact(Long)
1583
3b52fd1a 1584 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1585 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1586};
1587
a846a8cd 1588struct CYFunction {
b09da87b 1589 CYFunctionParameter *parameters_;
b0385401 1590 CYStatement *code_;
a0be43fc 1591
ab2aa221 1592 CYNonLocal *nonlocal_;
12e37ba3 1593 bool implicit_;
a0be43fc 1594 CYThisScope this_;
c5b15840 1595 CYIdentifier *super_;
cf7d4c69 1596
c5b15840 1597 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
cf7d4c69 1598 parameters_(parameters),
b0385401 1599 code_(code),
12e37ba3 1600 nonlocal_(NULL),
c5b15840
JF
1601 implicit_(false),
1602 super_(NULL)
cf7d4c69
JF
1603 {
1604 }
5999c315 1605
c5b15840
JF
1606 void Replace(CYContext &context);
1607 void Output(CYOutput &out) const;
fb98ac0c
JF
1608};
1609
1610struct CYFunctionExpression :
1611 CYFunction,
7085e1ab 1612 CYTarget
fb98ac0c 1613{
c5b15840
JF
1614 CYIdentifier *name_;
1615
b0385401 1616 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
c5b15840
JF
1617 CYFunction(parameters, code),
1618 name_(name)
fb98ac0c
JF
1619 {
1620 }
1621
a0be43fc 1622 CYPrecedence(0)
a0be43fc 1623
7085e1ab 1624 CYTarget *Replace(CYContext &context) override;
a0be43fc
JF
1625 virtual void Output(CYOutput &out, CYFlags flags) const;
1626};
1627
a0be43fc
JF
1628struct CYFatArrow :
1629 CYFunction,
1630 CYExpression
1631{
b0385401 1632 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
c5b15840 1633 CYFunction(parameters, code)
a0be43fc
JF
1634 {
1635 }
1636
d35a3b07
JF
1637 CYPrecedence(0)
1638
7085e1ab 1639 CYExpression *Replace(CYContext &context) override;
652ec1ba 1640 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1641};
1642
6c093cce 1643struct CYRubyProc :
6ce0f978 1644 CYFunction,
7085e1ab 1645 CYTarget
6c093cce 1646{
b0385401 1647 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
c5b15840 1648 CYFunction(parameters, code)
6c093cce
JF
1649 {
1650 }
1651
c5b15840 1652 CYPrecedence(0)
c5b15840 1653
7085e1ab 1654 CYTarget *Replace(CYContext &context) override;
6c093cce
JF
1655 virtual void Output(CYOutput &out, CYFlags flags) const;
1656};
1657
fb98ac0c
JF
1658struct CYFunctionStatement :
1659 CYFunction,
b10bd496 1660 CYStatement
cf7d4c69 1661{
c5b15840
JF
1662 CYIdentifier *name_;
1663
b0385401 1664 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
c5b15840
JF
1665 CYFunction(parameters, code),
1666 name_(name)
cf7d4c69
JF
1667 {
1668 }
5999c315 1669
efd689d8
JF
1670 CYCompact(None)
1671
7085e1ab 1672 CYStatement *Replace(CYContext &context) override;
fb98ac0c 1673 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
1674};
1675
c5b15840
JF
1676struct CYPropertyMethod;
1677
1678struct CYMethod :
1679 CYFunction,
1680 CYProperty
1681{
1682 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1683 CYFunction(parameters, code),
1684 CYProperty(name, next)
1685 {
1686 }
1687
1688 virtual CYFunctionExpression *Constructor();
1689
1690 using CYProperty::Replace;
1691 virtual void Replace(CYContext &context);
1692};
1693
1694struct CYPropertyGetter :
1695 CYMethod
1696{
1697 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1698 CYMethod(name, NULL, code, next)
1699 {
1700 }
1701
a196a97a 1702 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
c5b15840
JF
1703 virtual void Output(CYOutput &out) const;
1704};
1705
1706struct CYPropertySetter :
1707 CYMethod
1708{
1709 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1710 CYMethod(name, parameters, code, next)
1711 {
1712 }
1713
a196a97a 1714 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
c5b15840
JF
1715 virtual void Output(CYOutput &out) const;
1716};
1717
1718struct CYPropertyMethod :
1719 CYMethod
1720{
1721 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1722 CYMethod(name, parameters, code, next)
1723 {
1724 }
1725
7b87d205
JF
1726 bool Update() const override;
1727
c5b15840
JF
1728 virtual CYFunctionExpression *Constructor();
1729
a196a97a 1730 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
c5b15840
JF
1731 virtual void Output(CYOutput &out) const;
1732};
1733
1734struct CYClassTail :
1735 CYThing
1736{
1737 CYExpression *extends_;
1738
1739 CYFunctionExpression *constructor_;
1740 CYList<CYProperty> instance_;
1741 CYList<CYProperty> static_;
1742
1743 CYClassTail(CYExpression *extends) :
1744 extends_(extends),
1745 constructor_(NULL)
1746 {
1747 }
1748
1749 void Output(CYOutput &out) const;
1750};
1751
1752struct CYClassExpression :
7085e1ab 1753 CYTarget
c5b15840
JF
1754{
1755 CYIdentifier *name_;
1756 CYClassTail *tail_;
1757
1758 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1759 name_(name),
1760 tail_(tail)
1761 {
1762 }
1763
1764 CYPrecedence(0)
c5b15840 1765
7085e1ab 1766 CYTarget *Replace(CYContext &context) override;
c5b15840
JF
1767 virtual void Output(CYOutput &out, CYFlags flags) const;
1768};
1769
1770struct CYClassStatement :
1771 CYStatement
1772{
1773 CYIdentifier *name_;
1774 CYClassTail *tail_;
1775
1776 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1777 name_(name),
1778 tail_(tail)
1779 {
1780 }
1781
1782 CYCompact(Long)
1783
7085e1ab 1784 CYStatement *Replace(CYContext &context) override;
c5b15840
JF
1785 virtual void Output(CYOutput &out, CYFlags flags) const;
1786};
1787
1788struct CYSuperCall :
7085e1ab 1789 CYTarget
c5b15840
JF
1790{
1791 CYArgument *arguments_;
1792
1793 CYSuperCall(CYArgument *arguments) :
1794 arguments_(arguments)
1795 {
1796 }
1797
1798 CYPrecedence(2)
c5b15840 1799
7085e1ab 1800 CYTarget *Replace(CYContext &context) override;
c5b15840
JF
1801 virtual void Output(CYOutput &out, CYFlags flags) const;
1802};
1803
1804struct CYSuperAccess :
7085e1ab 1805 CYTarget
c5b15840
JF
1806{
1807 CYExpression *property_;
1808
1809 CYSuperAccess(CYExpression *property) :
1810 property_(property)
1811 {
1812 }
1813
1814 CYPrecedence(1)
c5b15840 1815
7085e1ab 1816 CYTarget *Replace(CYContext &context) override;
c5b15840
JF
1817 virtual void Output(CYOutput &out, CYFlags flags) const;
1818};
1819
5999c315 1820struct CYExpress :
bfd79fae 1821 CYForInitializer
5999c315
JF
1822{
1823 CYExpression *expression_;
1824
1825 CYExpress(CYExpression *expression) :
1826 expression_(expression)
1827 {
fd5cdf97 1828 if (expression_ == NULL)
029bc65b 1829 throw;
5999c315
JF
1830 }
1831
efd689d8
JF
1832 CYCompact(None)
1833
bfd79fae 1834 CYForInitializer *Replace(CYContext &context) override;
fb98ac0c 1835 virtual void Output(CYOutput &out, CYFlags flags) const;
12e37ba3
JF
1836
1837 virtual CYStatement *Return();
cf7d4c69
JF
1838};
1839
1840struct CYContinue :
1841 CYStatement
1842{
1843 CYIdentifier *label_;
1844
1845 CYContinue(CYIdentifier *label) :
1846 label_(label)
1847 {
1848 }
5999c315 1849
efd689d8
JF
1850 CYCompact(Short)
1851
7085e1ab 1852 CYStatement *Replace(CYContext &context) override;
fb98ac0c 1853 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1854};
1855
1856struct CYBreak :
1857 CYStatement
1858{
1859 CYIdentifier *label_;
1860
1861 CYBreak(CYIdentifier *label) :
1862 label_(label)
1863 {
1864 }
5999c315 1865
efd689d8
JF
1866 CYCompact(Short)
1867
7085e1ab 1868 CYStatement *Replace(CYContext &context) override;
fb98ac0c 1869 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1870};
1871
1872struct CYReturn :
1873 CYStatement
1874{
1875 CYExpression *value_;
1876
1877 CYReturn(CYExpression *value) :
1878 value_(value)
1879 {
1880 }
5999c315 1881
efd689d8
JF
1882 CYCompact(None)
1883
7085e1ab 1884 CYStatement *Replace(CYContext &context) override;
9d2b125d
JF
1885 virtual void Output(CYOutput &out, CYFlags flags) const;
1886};
1887
1888struct CYYieldGenerator :
1889 CYExpression
1890{
1891 CYExpression *value_;
1892
1893 CYYieldGenerator(CYExpression *value) :
1894 value_(value)
1895 {
1896 }
1897
1898 CYPrecedence(0)
1899
7085e1ab 1900 CYExpression *Replace(CYContext &context) override;
9d2b125d
JF
1901 virtual void Output(CYOutput &out, CYFlags flags) const;
1902};
1903
1904struct CYYieldValue :
1905 CYExpression
1906{
1907 CYExpression *value_;
1908
1909 CYYieldValue(CYExpression *value) :
1910 value_(value)
1911 {
1912 }
1913
1914 CYPrecedence(0)
1915
1916 virtual CYExpression *Replace(CYContext &context);
fb98ac0c 1917 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1918};
1919
1920struct CYEmpty :
bfd79fae 1921 CYForInitializer
cf7d4c69 1922{
efd689d8
JF
1923 CYCompact(Short)
1924
bfd79fae 1925 virtual CYForInitializer *Replace(CYContext &context);
fb98ac0c 1926 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1927};
1928
96a7e5c2
JF
1929struct CYFinally :
1930 CYThing
1931{
b0385401 1932 CYStatement *code_;
b10bd496 1933
b0385401
JF
1934 CYFinally(CYStatement *code) :
1935 code_(code)
b10bd496
JF
1936 {
1937 }
1938
3b52fd1a 1939 void Replace(CYContext &context);
b10bd496
JF
1940 virtual void Output(CYOutput &out) const;
1941};
1942
3fe283c5
JF
1943struct CYTypeSpecifier :
1944 CYThing
1945{
7085e1ab 1946 virtual CYTarget *Replace(CYContext &context) = 0;
3fe283c5
JF
1947};
1948
03db6a67
JF
1949struct CYTypeError :
1950 CYTypeSpecifier
1951{
1952 CYTypeError() {
1953 }
1954
7085e1ab 1955 virtual CYTarget *Replace(CYContext &context);
03db6a67
JF
1956 virtual void Output(CYOutput &out) const;
1957};
1958
3fe283c5
JF
1959struct CYTypeVoid :
1960 CYTypeSpecifier
1961{
1962 CYTypeVoid() {
1963 }
1964
7085e1ab 1965 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
1966 virtual void Output(CYOutput &out) const;
1967};
1968
1969struct CYTypeVariable :
1970 CYTypeSpecifier
1971{
1972 CYIdentifier *name_;
1973
1974 CYTypeVariable(CYIdentifier *name) :
1975 name_(name)
1976 {
1977 }
1978
1979 CYTypeVariable(const char *name) :
1980 name_(new($pool) CYIdentifier(name))
1981 {
1982 }
1983
7085e1ab 1984 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
1985 virtual void Output(CYOutput &out) const;
1986};
1987
1988struct CYTypeUnsigned :
1989 CYTypeSpecifier
1990{
1991 CYTypeSpecifier *specifier_;
1992
1993 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1994 specifier_(specifier)
1995 {
1996 }
1997
7085e1ab 1998 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
1999 virtual void Output(CYOutput &out) const;
2000};
2001
2002struct CYTypeSigned :
2003 CYTypeSpecifier
2004{
2005 CYTypeSpecifier *specifier_;
2006
2007 CYTypeSigned(CYTypeSpecifier *specifier) :
2008 specifier_(specifier)
2009 {
2010 }
2011
7085e1ab 2012 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
2013 virtual void Output(CYOutput &out) const;
2014};
2015
2016struct CYTypeLong :
2017 CYTypeSpecifier
2018{
2019 CYTypeSpecifier *specifier_;
2020
2021 CYTypeLong(CYTypeSpecifier *specifier) :
2022 specifier_(specifier)
2023 {
2024 }
2025
7085e1ab 2026 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
2027 virtual void Output(CYOutput &out) const;
2028};
2029
2030struct CYTypeShort :
2031 CYTypeSpecifier
2032{
2033 CYTypeSpecifier *specifier_;
2034
2035 CYTypeShort(CYTypeSpecifier *specifier) :
2036 specifier_(specifier)
2037 {
2038 }
2039
7085e1ab 2040 virtual CYTarget *Replace(CYContext &context);
3fe283c5
JF
2041 virtual void Output(CYOutput &out) const;
2042};
2043
00b4cb83
JF
2044struct CYTypeFunctionWith;
2045
690cf1a8
JF
2046struct CYTypeModifier :
2047 CYNext<CYTypeModifier>
2048{
2049 CYTypeModifier(CYTypeModifier *next) :
2050 CYNext<CYTypeModifier>(next)
2051 {
2052 }
2053
9a39f705
JF
2054 virtual int Precedence() const = 0;
2055
7085e1ab
JF
2056 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2057 CYTarget *Replace(CYContext &context, CYTarget *type);
9a39f705
JF
2058
2059 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2060 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
00b4cb83
JF
2061
2062 virtual CYTypeFunctionWith *Function() { return NULL; }
690cf1a8
JF
2063};
2064
2065struct CYTypeArrayOf :
2066 CYTypeModifier
2067{
2068 CYExpression *size_;
2069
2070 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2071 CYTypeModifier(next),
2072 size_(size)
2073 {
2074 }
2075
9a39f705 2076 CYPrecedence(1)
690cf1a8 2077
7085e1ab 2078 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
9a39f705 2079 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
690cf1a8
JF
2080};
2081
2082struct CYTypeConstant :
2083 CYTypeModifier
2084{
2085 CYTypeConstant(CYTypeModifier *next = NULL) :
2086 CYTypeModifier(next)
2087 {
2088 }
2089
9a39f705 2090 CYPrecedence(0)
690cf1a8 2091
7085e1ab 2092 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
9a39f705 2093 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
690cf1a8
JF
2094};
2095
2096struct CYTypePointerTo :
2097 CYTypeModifier
2098{
2099 CYTypePointerTo(CYTypeModifier *next = NULL) :
2100 CYTypeModifier(next)
2101 {
2102 }
2103
9a39f705 2104 CYPrecedence(0)
690cf1a8 2105
7085e1ab 2106 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
9a39f705 2107 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
690cf1a8
JF
2108};
2109
9a39f705 2110struct CYTypeVolatile :
690cf1a8
JF
2111 CYTypeModifier
2112{
9a39f705
JF
2113 CYTypeVolatile(CYTypeModifier *next = NULL) :
2114 CYTypeModifier(next)
690cf1a8
JF
2115 {
2116 }
2117
9a39f705 2118 CYPrecedence(0)
690cf1a8 2119
7085e1ab 2120 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
9a39f705 2121 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
690cf1a8
JF
2122};
2123
2124struct CYTypedIdentifier :
60097023
JF
2125 CYNext<CYTypedIdentifier>,
2126 CYThing
690cf1a8 2127{
00b4cb83 2128 CYLocation location_;
690cf1a8 2129 CYIdentifier *identifier_;
3fe283c5 2130 CYTypeSpecifier *specifier_;
9a39f705 2131 CYTypeModifier *modifier_;
690cf1a8 2132
00b4cb83
JF
2133 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2134 location_(location),
690cf1a8 2135 identifier_(identifier),
3fe283c5 2136 specifier_(NULL),
9a39f705 2137 modifier_(NULL)
690cf1a8
JF
2138 {
2139 }
60097023 2140
3fe283c5 2141 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
9a39f705 2142 identifier_(NULL),
3fe283c5 2143 specifier_(specifier),
9a39f705
JF
2144 modifier_(modifier)
2145 {
2146 }
2147
2148 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2149 CYSetLast(modifier_) = modifier;
2150 return this;
2151 }
2152
7085e1ab 2153 virtual CYTarget *Replace(CYContext &context);
60097023 2154 virtual void Output(CYOutput &out) const;
00b4cb83
JF
2155
2156 CYTypeFunctionWith *Function();
690cf1a8
JF
2157};
2158
9a39f705 2159struct CYEncodedType :
7085e1ab 2160 CYTarget
9a39f705
JF
2161{
2162 CYTypedIdentifier *typed_;
2163
2164 CYEncodedType(CYTypedIdentifier *typed) :
2165 typed_(typed)
2166 {
2167 }
2168
2169 CYPrecedence(1)
2170
7085e1ab 2171 virtual CYTarget *Replace(CYContext &context);
9a39f705
JF
2172 virtual void Output(CYOutput &out, CYFlags flags) const;
2173};
2174
690cf1a8 2175struct CYTypedParameter :
9a39f705
JF
2176 CYNext<CYTypedParameter>,
2177 CYThing
690cf1a8
JF
2178{
2179 CYTypedIdentifier *typed_;
2180
2181 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2182 CYNext<CYTypedParameter>(next),
2183 typed_(typed)
2184 {
2185 }
2186
663c538f 2187 CYArgument *Argument(CYContext &context);
690cf1a8
JF
2188 CYFunctionParameter *Parameters(CYContext &context);
2189 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
9a39f705
JF
2190
2191 virtual void Output(CYOutput &out) const;
690cf1a8
JF
2192};
2193
2194struct CYLambda :
7085e1ab 2195 CYTarget
690cf1a8 2196{
9a39f705 2197 CYTypedIdentifier *typed_;
690cf1a8 2198 CYTypedParameter *parameters_;
b0385401 2199 CYStatement *code_;
690cf1a8 2200
b0385401 2201 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
9a39f705 2202 typed_(typed),
690cf1a8 2203 parameters_(parameters),
b0385401 2204 code_(code)
690cf1a8
JF
2205 {
2206 }
2207
2208 CYPrecedence(1)
2209
7085e1ab 2210 virtual CYTarget *Replace(CYContext &context);
690cf1a8
JF
2211 virtual void Output(CYOutput &out, CYFlags flags) const;
2212};
2213
7b750785
JF
2214struct CYModule :
2215 CYNext<CYModule>,
2216 CYThing
2217{
2218 CYWord *part_;
2219
2220 CYModule(CYWord *part, CYModule *next = NULL) :
2221 CYNext<CYModule>(next),
2222 part_(part)
2223 {
2224 }
2225
2226 CYString *Replace(CYContext &context, const char *separator) const;
2227 void Output(CYOutput &out) const;
2228};
2229
2230struct CYImport :
2231 CYStatement
2232{
2233 CYModule *module_;
2234
2235 CYImport(CYModule *module) :
2236 module_(module)
2237 {
2238 }
2239
efd689d8
JF
2240 CYCompact(None)
2241
7b750785
JF
2242 virtual CYStatement *Replace(CYContext &context);
2243 virtual void Output(CYOutput &out, CYFlags flags) const;
2244};
2245
c5587ed7
JF
2246struct CYExternal :
2247 CYStatement
2248{
2249 CYString *abi_;
2250 CYTypedIdentifier *typed_;
2251
2252 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2253 abi_(abi),
2254 typed_(typed)
2255 {
2256 }
2257
efd689d8
JF
2258 CYCompact(None)
2259
c5587ed7
JF
2260 virtual CYStatement *Replace(CYContext &context);
2261 virtual void Output(CYOutput &out, CYFlags flags) const;
2262};
2263
60097023
JF
2264struct CYTypeDefinition :
2265 CYStatement
2266{
2267 CYTypedIdentifier *typed_;
2268
2269 CYTypeDefinition(CYTypedIdentifier *typed) :
2270 typed_(typed)
2271 {
2272 }
2273
efd689d8
JF
2274 CYCompact(None)
2275
60097023
JF
2276 virtual CYStatement *Replace(CYContext &context);
2277 virtual void Output(CYOutput &out, CYFlags flags) const;
2278};
2279
3fe16be7
JF
2280struct CYTypeBlockWith :
2281 CYTypeModifier
2282{
2283 CYTypedParameter *parameters_;
2284
2285 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2286 CYTypeModifier(next),
2287 parameters_(parameters)
2288 {
2289 }
2290
2291 CYPrecedence(0)
2292
7085e1ab 2293 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
3fe16be7
JF
2294 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2295};
2296
663c538f
JF
2297struct CYTypeFunctionWith :
2298 CYTypeModifier
2299{
2300 CYTypedParameter *parameters_;
2301
2302 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2303 CYTypeModifier(next),
2304 parameters_(parameters)
2305 {
2306 }
2307
9a39f705 2308 CYPrecedence(1)
663c538f 2309
7085e1ab 2310 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
9a39f705 2311 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
00b4cb83
JF
2312
2313 virtual CYTypeFunctionWith *Function() { return this; }
663c538f
JF
2314};
2315
37954781
JF
2316namespace cy {
2317namespace Syntax {
2318
2319struct Catch :
2320 CYThing
2321{
2322 CYIdentifier *name_;
b0385401 2323 CYStatement *code_;
37954781 2324
b0385401 2325 Catch(CYIdentifier *name, CYStatement *code) :
37954781 2326 name_(name),
b0385401 2327 code_(code)
37954781
JF
2328 {
2329 }
2330
2331 void Replace(CYContext &context);
2332 virtual void Output(CYOutput &out) const;
2333};
2334
2335struct Try :
cf7d4c69
JF
2336 CYStatement
2337{
b0385401 2338 CYStatement *code_;
37954781 2339 Catch *catch_;
b10bd496 2340 CYFinally *finally_;
cf7d4c69 2341
b0385401
JF
2342 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2343 code_(code),
cf7d4c69
JF
2344 catch_(_catch),
2345 finally_(finally)
2346 {
2347 }
5999c315 2348
efd689d8
JF
2349 CYCompact(Short)
2350
3b52fd1a 2351 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 2352 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
2353};
2354
37954781 2355struct Throw :
cf7d4c69
JF
2356 CYStatement
2357{
2358 CYExpression *value_;
2359
ab2aa221 2360 Throw(CYExpression *value = NULL) :
cf7d4c69
JF
2361 value_(value)
2362 {
2363 }
5999c315 2364
efd689d8
JF
2365 CYCompact(None)
2366
3b52fd1a 2367 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 2368 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
2369};
2370
37954781
JF
2371} }
2372
cf7d4c69
JF
2373struct CYWith :
2374 CYStatement
2375{
2376 CYExpression *scope_;
2377 CYStatement *code_;
2378
2379 CYWith(CYExpression *scope, CYStatement *code) :
2380 scope_(scope),
2381 code_(code)
2382 {
2383 }
5999c315 2384
efd689d8
JF
2385 CYCompact(Long)
2386
3b52fd1a 2387 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 2388 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
2389};
2390
2391struct CYSwitch :
2392 CYStatement
2393{
2394 CYExpression *value_;
2395 CYClause *clauses_;
2396
2397 CYSwitch(CYExpression *value, CYClause *clauses) :
2398 value_(value),
2399 clauses_(clauses)
2400 {
2401 }
5999c315 2402
efd689d8
JF
2403 CYCompact(Long)
2404
3b52fd1a 2405 virtual CYStatement *Replace(CYContext &context);
c8a0500b
JF
2406 virtual void Output(CYOutput &out, CYFlags flags) const;
2407};
2408
2409struct CYDebugger :
2410 CYStatement
2411{
2412 CYDebugger()
2413 {
2414 }
2415
efd689d8
JF
2416 CYCompact(None)
2417
c8a0500b 2418 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 2419 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
2420};
2421
2422struct CYCondition :
2423 CYExpression
2424{
2425 CYExpression *test_;
2426 CYExpression *true_;
2427 CYExpression *false_;
2428
2429 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
91a416e4 2430 test_(test),
cf7d4c69
JF
2431 true_(_true),
2432 false_(_false)
2433 {
2434 }
5999c315 2435
d35a3b07
JF
2436 CYPrecedence(15)
2437
3b52fd1a 2438 virtual CYExpression *Replace(CYContext &context);
652ec1ba 2439 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
2440};
2441
2442struct CYAddressOf :
2443 CYPrefix
2444{
2445 CYAddressOf(CYExpression *rhs) :
2446 CYPrefix(rhs)
2447 {
2448 }
2449
2450 virtual const char *Operator() const {
2451 return "&";
2452 }
2453
b09da87b 2454 CYAlphabetic(false)
d35a3b07 2455
3b52fd1a 2456 virtual CYExpression *Replace(CYContext &context);
5999c315
JF
2457};
2458
2459struct CYIndirect :
7085e1ab 2460 CYTarget
5999c315 2461{
7085e1ab
JF
2462 CYExpression *rhs_;
2463
5999c315 2464 CYIndirect(CYExpression *rhs) :
7085e1ab 2465 rhs_(rhs)
5999c315
JF
2466 {
2467 }
2468
7085e1ab
JF
2469 // XXX: this should be checked
2470 CYPrecedence(2)
d35a3b07 2471
7085e1ab
JF
2472 virtual CYTarget *Replace(CYContext &context);
2473 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
2474};
2475
4644480a
JF
2476#define CYReplace \
2477 virtual CYExpression *Replace(CYContext &context);
2478
2479#define CYPostfix_(op, name, args...) \
cf7d4c69
JF
2480 struct CY ## name : \
2481 CYPostfix \
4644480a 2482 { args \
cf7d4c69
JF
2483 CY ## name(CYExpression *lhs) : \
2484 CYPostfix(lhs) \
2485 { \
2486 } \
5999c315
JF
2487 \
2488 virtual const char *Operator() const { \
2489 return op; \
2490 } \
cf7d4c69
JF
2491 };
2492
4644480a 2493#define CYPrefix_(alphabetic, op, name, args...) \
cf7d4c69
JF
2494 struct CY ## name : \
2495 CYPrefix \
4644480a 2496 { args \
cf7d4c69
JF
2497 CY ## name(CYExpression *rhs) : \
2498 CYPrefix(rhs) \
2499 { \
2500 } \
d35a3b07 2501 \
b09da87b 2502 CYAlphabetic(alphabetic) \
5999c315
JF
2503 \
2504 virtual const char *Operator() const { \
2505 return op; \
2506 } \
cf7d4c69
JF
2507 };
2508
4644480a 2509#define CYInfix_(alphabetic, precedence, op, name, args...) \
cf7d4c69
JF
2510 struct CY ## name : \
2511 CYInfix \
4644480a 2512 { args \
cf7d4c69
JF
2513 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2514 CYInfix(lhs, rhs) \
2515 { \
2516 } \
d35a3b07 2517 \
b09da87b 2518 CYAlphabetic(alphabetic) \
d35a3b07 2519 CYPrecedence(precedence) \
5999c315
JF
2520 \
2521 virtual const char *Operator() const { \
2522 return op; \
2523 } \
cf7d4c69
JF
2524 };
2525
4644480a 2526#define CYAssignment_(op, name, args...) \
cf7d4c69
JF
2527 struct CY ## name ## Assign : \
2528 CYAssignment \
4644480a 2529 { args \
7085e1ab 2530 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
cf7d4c69
JF
2531 CYAssignment(lhs, rhs) \
2532 { \
2533 } \
5999c315
JF
2534 \
2535 virtual const char *Operator() const { \
2536 return op; \
2537 } \
cf7d4c69
JF
2538 };
2539
2540CYPostfix_("++", PostIncrement)
2541CYPostfix_("--", PostDecrement)
2542
b09da87b
JF
2543CYPrefix_(true, "delete", Delete)
2544CYPrefix_(true, "void", Void)
2545CYPrefix_(true, "typeof", TypeOf)
2546CYPrefix_(false, "++", PreIncrement)
2547CYPrefix_(false, "--", PreDecrement)
c0bc320e 2548CYPrefix_(false, "+", Affirm)
b09da87b
JF
2549CYPrefix_(false, "-", Negate)
2550CYPrefix_(false, "~", BitwiseNot)
2551CYPrefix_(false, "!", LogicalNot)
2552
62f398e4 2553CYInfix_(false, 5, "*", Multiply, CYReplace)
b09da87b
JF
2554CYInfix_(false, 5, "/", Divide)
2555CYInfix_(false, 5, "%", Modulus)
4644480a 2556CYInfix_(false, 6, "+", Add, CYReplace)
b09da87b
JF
2557CYInfix_(false, 6, "-", Subtract)
2558CYInfix_(false, 7, "<<", ShiftLeft)
2559CYInfix_(false, 7, ">>", ShiftRightSigned)
2560CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2561CYInfix_(false, 8, "<", Less)
2562CYInfix_(false, 8, ">", Greater)
2563CYInfix_(false, 8, "<=", LessOrEqual)
2564CYInfix_(false, 8, ">=", GreaterOrEqual)
2565CYInfix_(true, 8, "instanceof", InstanceOf)
2566CYInfix_(true, 8, "in", In)
2567CYInfix_(false, 9, "==", Equal)
2568CYInfix_(false, 9, "!=", NotEqual)
2569CYInfix_(false, 9, "===", Identical)
2570CYInfix_(false, 9, "!==", NotIdentical)
2571CYInfix_(false, 10, "&", BitwiseAnd)
2572CYInfix_(false, 11, "^", BitwiseXOr)
2573CYInfix_(false, 12, "|", BitwiseOr)
2574CYInfix_(false, 13, "&&", LogicalAnd)
2575CYInfix_(false, 14, "||", LogicalOr)
cf7d4c69
JF
2576
2577CYAssignment_("=", )
2578CYAssignment_("*=", Multiply)
2579CYAssignment_("/=", Divide)
2580CYAssignment_("%=", Modulus)
2581CYAssignment_("+=", Add)
2582CYAssignment_("-=", Subtract)
2583CYAssignment_("<<=", ShiftLeft)
2584CYAssignment_(">>=", ShiftRightSigned)
2585CYAssignment_(">>>=", ShiftRightUnsigned)
2586CYAssignment_("&=", BitwiseAnd)
2587CYAssignment_("^=", BitwiseXOr)
2588CYAssignment_("|=", BitwiseOr)
2589
c5fa2867 2590#endif/*CYCRIPT_PARSER_HPP*/