Added support for enums.
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) \
32 _(IntegerLiteralExpression) \
37 _(ExpressionStatement) \
38 _(FunctionStatement) \
44 // Forward declarations.
45 #define _(name) class name;
51 // Interface for AST nodes.
52 class AstNode : public PoolObject
56 # define _(name) k##name,
62 virtual Kind kind() const = 0;
64 #define _(name) bool is##name() { return kind() == k##name; } \
65 name *to##name() { assert(is##name()); return (name *)this; }
69 virtual void accept(AstVisitor *visitor) = 0;
75 #define _(name) virtual void visit(name *node) = 0;
87 static NameLocation Local(VariableDeclaration *decl) {
98 VariableDeclaration *local() const {
99 assert(kind() == LOCAL);
106 VariableDeclaration *decl;
110 class Statement : public AstNode
114 class Expression : public AstNode
124 void setType(Type *type) {
132 class Declaration : public Statement
136 typedef PoolList<Statement *> StatementList;
137 typedef std::vector<Expression *> ExpressionList;
139 class VariableDeclaration : public Declaration
142 Expression *initialization_;
143 ExpressionList *postDimensions_;
146 VariableDeclaration(Variable *var, ExpressionList *postDimensions, Expression *initialization)
148 postDimensions_(postDimensions),
149 initialization_(initialization)
154 return kVariableDeclaration;
156 Variable *variable() const {
159 Expression *initialization() const {
160 return initialization_;
162 ExpressionList *postDimensions() const {
163 return postDimensions_;
165 void accept(AstVisitor *visitor) {
166 visitor->visit(this);
170 class NameProxy : public Expression
176 NameProxy(Name *name, BoundName *binding)
188 void accept(AstVisitor *visitor) {
189 visitor->visit(this);
191 BoundName *binding() const {
194 void bind(BoundName *name) {
200 class IntegerLiteralExpression : public Expression
205 IntegerLiteralExpression(int value)
211 return kIntegerLiteralExpression;
216 void accept(AstVisitor *visitor) {
217 visitor->visit(this);
221 class BinaryExpression : public Expression
228 BinaryExpression(TokenKind token, Expression *left, Expression *right)
236 return kBinaryExpression;
238 Expression *left() const {
241 Expression *right() const {
244 TokenKind token() const {
247 void accept(AstVisitor *visitor) {
248 visitor->visit(this);
252 class IndexExpression : public Expression
258 IndexExpression(Expression *left, Expression *right)
265 return kIndexExpression;
267 Expression *left() const {
270 Expression *right() const {
273 void accept(AstVisitor *visitor) {
274 visitor->visit(this);
278 class CallExpression : public Expression
281 ExpressionList *arguments_;
284 CallExpression(Expression *callee, ExpressionList *arguments)
286 arguments_(arguments)
291 return kCallExpression;
293 Expression *callee() const {
296 ExpressionList *arguments() const {
299 void accept(AstVisitor *visitor) {
300 visitor->visit(this);
304 class ForStatement : public Statement
307 VariableDeclaration *declaration_;
308 Expression *condition_;
313 ForStatement(Scope *scope, VariableDeclaration *declaration,
314 Expression *condition, Statement *update, Statement *body)
316 declaration_(declaration),
317 condition_(condition),
324 return kForStatement;
326 VariableDeclaration *declaration() const {
329 Scope *scope() const {
332 Expression *condition() const {
335 Statement *update() const {
338 Statement *body() const {
341 void accept(AstVisitor *visitor) {
342 visitor->visit(this);
346 class ReturnStatement : public Statement
348 Expression *expression_;
351 ReturnStatement(Expression *expression)
352 : expression_(expression)
357 return kReturnStatement;
359 Expression *expression() const {
362 void accept(AstVisitor *visitor) {
363 visitor->visit(this);
367 class Assignment : public Expression
370 Expression *expression_;
373 Assignment(Expression *left, Expression *right)
382 Expression *lvalue() const {
385 Expression *expression() const {
388 void accept(AstVisitor *visitor) {
389 visitor->visit(this);
393 class ExpressionStatement : public Statement
395 Expression *expression_;
398 ExpressionStatement(Expression *expression)
399 : expression_(expression)
404 return kExpressionStatement;
406 Expression *expression() const {
409 void accept(AstVisitor *visitor) {
410 visitor->visit(this);
414 class BlockStatement : public Statement
417 StatementList *statements_;
420 BlockStatement(Scope *scope, StatementList *statements)
422 statements_(statements)
427 return kBlockStatement;
429 Scope *scope() const {
432 StatementList *statements() const {
435 void accept(AstVisitor *visitor) {
436 visitor->visit(this);
440 class FunctionStatement : public Statement
447 FunctionStatement(Scope *scope, NamedFunction *fun, Statement *body)
455 return kFunctionStatement;
457 Scope *scope() const {
460 NamedFunction *fun() const {
463 Statement *body() const {
466 void accept(AstVisitor *visitor) {
467 visitor->visit(this);
471 class IfStatement : public Statement
473 Expression *condition_;
478 IfStatement(Expression *condition, Statement *ifTrue)
479 : condition_(condition),
488 Expression *condition() const {
491 Statement *ifTrue() const {
494 Statement *ifFalse() const {
497 void setIfFalse(Statement *ifFalse) {
500 void accept(AstVisitor *visitor) {
501 visitor->visit(this);
505 class EnumStatement : public Statement
514 Entry(NamedConstant *name, Expression *expr)
521 typedef std::vector<Entry> EntryList;
524 EnumStatement(Name *name, EntryList *entries)
531 return kEnumStatement;
536 EntryList *entries() const {
539 void accept(AstVisitor *visitor) {
540 visitor->visit(this);
549 typedef std::vector<NameProxy *> NameProxyList;
551 class ParseTree : public PoolObject
553 StatementList *statements_;
555 NameProxyList *unbound_;
558 ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
559 : statements_(statements),
567 StatementList *statements() const {
570 Scope *scope() const {
573 NameProxyList *unbound() const {
580 #endif // _include_jitcraft_ast_h_