Added support for do/while loops.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_jitcraft_ast_h_
19 #define _include_jitcraft_ast_h_
21 #include "PoolAllocator.h"
23 #include "StringPool.h"
24 #include "NameBinding.h"
29 _(VariableDeclaration) \
38 _(ExpressionStatement) \
39 _(FunctionStatement) \
46 // Forward declarations.
47 #define _(name) class name;
53 // Interface for AST nodes.
54 class AstNode : public PoolObject
58 # define _(name) k##name,
64 virtual Kind kind() const = 0;
66 #define _(name) bool is##name() { return kind() == k##name; } \
67 name *to##name() { assert(is##name()); return (name *)this; }
71 virtual void accept(AstVisitor *visitor) = 0;
77 #define _(name) virtual void visit(name *node) = 0;
89 static NameLocation Local(VariableDeclaration *decl) {
100 VariableDeclaration *local() const {
101 assert(kind() == LOCAL);
108 VariableDeclaration *decl;
112 class Statement : public AstNode
116 class Expression : public AstNode
126 void setType(Type *type) {
134 class Declaration : public Statement
138 typedef PoolList<Statement *> StatementList;
139 typedef std::vector<Expression *> ExpressionList;
141 class VariableDeclaration : public Declaration
144 Expression *initialization_;
145 ExpressionList *postDimensions_;
149 VariableDeclaration(Variable *var, Name *tag, ExpressionList *postDimensions, Expression *initialization)
151 postDimensions_(postDimensions),
152 initialization_(initialization),
161 return kVariableDeclaration;
163 Variable *variable() const {
166 Expression *initialization() const {
167 return initialization_;
169 ExpressionList *postDimensions() const {
170 return postDimensions_;
172 void accept(AstVisitor *visitor) {
173 visitor->visit(this);
177 class NameProxy : public Expression
183 NameProxy(Name *name, BoundName *binding)
195 void accept(AstVisitor *visitor) {
196 visitor->visit(this);
198 BoundName *binding() const {
201 void bind(BoundName *name) {
207 class IntegerLiteral : public Expression
212 IntegerLiteral(int value)
218 return kIntegerLiteral;
223 void accept(AstVisitor *visitor) {
224 visitor->visit(this);
228 class FloatLiteral : public Expression
233 FloatLiteral(double value)
239 return kFloatLiteral;
241 double value() const {
244 void accept(AstVisitor *visitor) {
245 visitor->visit(this);
249 class BinaryExpression : public Expression
256 BinaryExpression(TokenKind token, Expression *left, Expression *right)
264 return kBinaryExpression;
266 Expression *left() const {
269 Expression *right() const {
272 TokenKind token() const {
275 void accept(AstVisitor *visitor) {
276 visitor->visit(this);
280 class IndexExpression : public Expression
286 IndexExpression(Expression *left, Expression *right)
293 return kIndexExpression;
295 Expression *left() const {
298 Expression *right() const {
301 void accept(AstVisitor *visitor) {
302 visitor->visit(this);
306 class CallExpression : public Expression
309 ExpressionList *arguments_;
312 CallExpression(Expression *callee, ExpressionList *arguments)
314 arguments_(arguments)
319 return kCallExpression;
321 Expression *callee() const {
324 ExpressionList *arguments() const {
327 void accept(AstVisitor *visitor) {
328 visitor->visit(this);
332 class ForStatement : public Statement
335 VariableDeclaration *declaration_;
336 Expression *condition_;
341 ForStatement(Scope *scope, VariableDeclaration *declaration,
342 Expression *condition, Statement *update, Statement *body)
344 declaration_(declaration),
345 condition_(condition),
352 return kForStatement;
354 VariableDeclaration *declaration() const {
357 Scope *scope() const {
360 Expression *condition() const {
363 Statement *update() const {
366 Statement *body() const {
369 void accept(AstVisitor *visitor) {
370 visitor->visit(this);
374 class WhileStatement : public Statement
377 Expression *condition_;
381 WhileStatement(TokenKind kind, Expression *condition, Statement *body)
383 condition_(condition),
389 return kWhileStatement;
391 TokenKind token() const {
394 Expression *condition() const {
397 Statement *body() const {
400 void accept(AstVisitor *visitor) {
401 visitor->visit(this);
405 class ReturnStatement : public Statement
407 Expression *expression_;
410 ReturnStatement(Expression *expression)
411 : expression_(expression)
416 return kReturnStatement;
418 Expression *expression() const {
421 void accept(AstVisitor *visitor) {
422 visitor->visit(this);
426 class Assignment : public Expression
429 Expression *expression_;
432 Assignment(Expression *left, Expression *right)
441 Expression *lvalue() const {
444 Expression *expression() const {
447 void accept(AstVisitor *visitor) {
448 visitor->visit(this);
452 class ExpressionStatement : public Statement
454 Expression *expression_;
457 ExpressionStatement(Expression *expression)
458 : expression_(expression)
463 return kExpressionStatement;
465 Expression *expression() const {
468 void accept(AstVisitor *visitor) {
469 visitor->visit(this);
473 class BlockStatement : public Statement
476 StatementList *statements_;
479 BlockStatement(Scope *scope, StatementList *statements)
481 statements_(statements)
486 return kBlockStatement;
488 Scope *scope() const {
491 StatementList *statements() const {
494 void accept(AstVisitor *visitor) {
495 visitor->visit(this);
499 class FunctionStatement : public Statement
506 FunctionStatement(Scope *scope, NamedFunction *fun, Statement *body)
514 return kFunctionStatement;
516 Scope *scope() const {
519 NamedFunction *fun() const {
522 Statement *body() const {
525 void accept(AstVisitor *visitor) {
526 visitor->visit(this);
530 class IfStatement : public Statement
532 Expression *condition_;
537 IfStatement(Expression *condition, Statement *ifTrue)
538 : condition_(condition),
547 Expression *condition() const {
550 Statement *ifTrue() const {
553 Statement *ifFalse() const {
556 void setIfFalse(Statement *ifFalse) {
559 void accept(AstVisitor *visitor) {
560 visitor->visit(this);
564 class EnumStatement : public Statement
573 Entry(NamedConstant *name, Expression *expr)
580 typedef std::vector<Entry> EntryList;
583 EnumStatement(Name *name, EntryList *entries)
590 return kEnumStatement;
595 EntryList *entries() const {
598 void accept(AstVisitor *visitor) {
599 visitor->visit(this);
608 typedef std::vector<NameProxy *> NameProxyList;
610 class ParseTree : public PoolObject
612 StatementList *statements_;
614 NameProxyList *unbound_;
617 ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
618 : statements_(statements),
626 StatementList *statements() const {
629 Scope *scope() const {
632 NameProxyList *unbound() const {
639 #endif // _include_jitcraft_ast_h_