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