Add support for float variables and basic math.
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) \
45 // Forward declarations.
46 #define _(name) class name;
52 // Interface for AST nodes.
53 class AstNode : public PoolObject
57 # define _(name) k##name,
63 virtual Kind kind() const = 0;
65 #define _(name) bool is##name() { return kind() == k##name; } \
66 name *to##name() { assert(is##name()); return (name *)this; }
70 virtual void accept(AstVisitor *visitor) = 0;
76 #define _(name) virtual void visit(name *node) = 0;
88 static NameLocation Local(VariableDeclaration *decl) {
99 VariableDeclaration *local() const {
100 assert(kind() == LOCAL);
107 VariableDeclaration *decl;
111 class Statement : public AstNode
115 class Expression : public AstNode
125 void setType(Type *type) {
133 class Declaration : public Statement
137 typedef PoolList<Statement *> StatementList;
138 typedef std::vector<Expression *> ExpressionList;
140 class VariableDeclaration : public Declaration
143 Expression *initialization_;
144 ExpressionList *postDimensions_;
148 VariableDeclaration(Variable *var, Name *tag, ExpressionList *postDimensions, Expression *initialization)
150 postDimensions_(postDimensions),
151 initialization_(initialization),
160 return kVariableDeclaration;
162 Variable *variable() const {
165 Expression *initialization() const {
166 return initialization_;
168 ExpressionList *postDimensions() const {
169 return postDimensions_;
171 void accept(AstVisitor *visitor) {
172 visitor->visit(this);
176 class NameProxy : public Expression
182 NameProxy(Name *name, BoundName *binding)
194 void accept(AstVisitor *visitor) {
195 visitor->visit(this);
197 BoundName *binding() const {
200 void bind(BoundName *name) {
206 class IntegerLiteral : public Expression
211 IntegerLiteral(int value)
217 return kIntegerLiteral;
222 void accept(AstVisitor *visitor) {
223 visitor->visit(this);
227 class FloatLiteral : public Expression
232 FloatLiteral(double value)
238 return kFloatLiteral;
240 double value() const {
243 void accept(AstVisitor *visitor) {
244 visitor->visit(this);
248 class BinaryExpression : public Expression
255 BinaryExpression(TokenKind token, Expression *left, Expression *right)
263 return kBinaryExpression;
265 Expression *left() const {
268 Expression *right() const {
271 TokenKind token() const {
274 void accept(AstVisitor *visitor) {
275 visitor->visit(this);
279 class IndexExpression : public Expression
285 IndexExpression(Expression *left, Expression *right)
292 return kIndexExpression;
294 Expression *left() const {
297 Expression *right() const {
300 void accept(AstVisitor *visitor) {
301 visitor->visit(this);
305 class CallExpression : public Expression
308 ExpressionList *arguments_;
311 CallExpression(Expression *callee, ExpressionList *arguments)
313 arguments_(arguments)
318 return kCallExpression;
320 Expression *callee() const {
323 ExpressionList *arguments() const {
326 void accept(AstVisitor *visitor) {
327 visitor->visit(this);
331 class ForStatement : public Statement
334 VariableDeclaration *declaration_;
335 Expression *condition_;
340 ForStatement(Scope *scope, VariableDeclaration *declaration,
341 Expression *condition, Statement *update, Statement *body)
343 declaration_(declaration),
344 condition_(condition),
351 return kForStatement;
353 VariableDeclaration *declaration() const {
356 Scope *scope() const {
359 Expression *condition() const {
362 Statement *update() const {
365 Statement *body() const {
368 void accept(AstVisitor *visitor) {
369 visitor->visit(this);
373 class ReturnStatement : public Statement
375 Expression *expression_;
378 ReturnStatement(Expression *expression)
379 : expression_(expression)
384 return kReturnStatement;
386 Expression *expression() const {
389 void accept(AstVisitor *visitor) {
390 visitor->visit(this);
394 class Assignment : public Expression
397 Expression *expression_;
400 Assignment(Expression *left, Expression *right)
409 Expression *lvalue() const {
412 Expression *expression() const {
415 void accept(AstVisitor *visitor) {
416 visitor->visit(this);
420 class ExpressionStatement : public Statement
422 Expression *expression_;
425 ExpressionStatement(Expression *expression)
426 : expression_(expression)
431 return kExpressionStatement;
433 Expression *expression() const {
436 void accept(AstVisitor *visitor) {
437 visitor->visit(this);
441 class BlockStatement : public Statement
444 StatementList *statements_;
447 BlockStatement(Scope *scope, StatementList *statements)
449 statements_(statements)
454 return kBlockStatement;
456 Scope *scope() const {
459 StatementList *statements() const {
462 void accept(AstVisitor *visitor) {
463 visitor->visit(this);
467 class FunctionStatement : public Statement
474 FunctionStatement(Scope *scope, NamedFunction *fun, Statement *body)
482 return kFunctionStatement;
484 Scope *scope() const {
487 NamedFunction *fun() const {
490 Statement *body() const {
493 void accept(AstVisitor *visitor) {
494 visitor->visit(this);
498 class IfStatement : public Statement
500 Expression *condition_;
505 IfStatement(Expression *condition, Statement *ifTrue)
506 : condition_(condition),
515 Expression *condition() const {
518 Statement *ifTrue() const {
521 Statement *ifFalse() const {
524 void setIfFalse(Statement *ifFalse) {
527 void accept(AstVisitor *visitor) {
528 visitor->visit(this);
532 class EnumStatement : public Statement
541 Entry(NamedConstant *name, Expression *expr)
548 typedef std::vector<Entry> EntryList;
551 EnumStatement(Name *name, EntryList *entries)
558 return kEnumStatement;
563 EntryList *entries() const {
566 void accept(AstVisitor *visitor) {
567 visitor->visit(this);
576 typedef std::vector<NameProxy *> NameProxyList;
578 class ParseTree : public PoolObject
580 StatementList *statements_;
582 NameProxyList *unbound_;
585 ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
586 : statements_(statements),
594 StatementList *statements() const {
597 Scope *scope() const {
600 NameProxyList *unbound() const {
607 #endif // _include_jitcraft_ast_h_