jitcraft/AST.h
author David Anderson <dvander@alliedmods.net>
Wed Jun 27 02:21:56 2012 -0700 (2012-06-27)
changeset 4 633176823669
parent 2 46fc53b8fd2f
child 9 73781c5a59d7
permissions -rw-r--r--
Add support for float variables and basic math.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     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)
     9  * any later version.
    10  * 
    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.
    14  *
    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/.
    17  */
    18 #ifndef _include_jitcraft_ast_h_
    19 #define _include_jitcraft_ast_h_
    20 
    21 #include "PoolAllocator.h"
    22 #include "Token.h"
    23 #include "StringPool.h"
    24 #include "NameBinding.h"
    25 
    26 namespace jc {
    27 
    28 #define ASTKINDS(_)             \
    29     _(VariableDeclaration)      \
    30     _(ForStatement)             \
    31     _(ReturnStatement)          \
    32     _(IntegerLiteral)           \
    33     _(FloatLiteral)             \
    34     _(BinaryExpression)         \
    35     _(BlockStatement)           \
    36     _(Assignment)               \
    37     _(NameProxy)                \
    38     _(ExpressionStatement)      \
    39     _(FunctionStatement)        \
    40     _(CallExpression)           \
    41     _(IfStatement)              \
    42     _(IndexExpression)          \
    43     _(EnumStatement)
    44 
    45 // Forward declarations.
    46 #define _(name) class name;
    47 ASTKINDS(_)
    48 #undef _
    49 
    50 class AstVisitor;
    51 
    52 // Interface for AST nodes.
    53 class AstNode : public PoolObject
    54 {
    55   public:
    56     enum Kind {
    57 #       define _(name) k##name,
    58         ASTKINDS(_)
    59 #       undef _
    60         AstKind_Invalid
    61     };
    62     
    63     virtual Kind kind() const = 0;
    64 
    65 #define _(name)     bool is##name() { return kind() == k##name; }                   \
    66                     name *to##name() { assert(is##name()); return (name *)this; }
    67     ASTKINDS(_)
    68 #undef _
    69 
    70     virtual void accept(AstVisitor *visitor) = 0;
    71 };
    72 
    73 class AstVisitor
    74 {
    75   public:
    76 #define _(name) virtual void visit(name *node) = 0;
    77     ASTKINDS(_)
    78 #undef _
    79 };
    80 
    81 class NameLocation
    82 {
    83   public:
    84     enum Kind {
    85         LOCAL
    86     };
    87 
    88     static NameLocation Local(VariableDeclaration *decl) {
    89         NameLocation loc;
    90         loc.kind_ = LOCAL;
    91         loc.u_.decl = decl;
    92         return loc;
    93     }
    94 
    95     Kind kind() const {
    96         return kind_;
    97     }
    98 
    99     VariableDeclaration *local() const {
   100         assert(kind() == LOCAL);
   101         return u_.decl;
   102     }
   103 
   104   private:
   105     Kind kind_;
   106     union {
   107         VariableDeclaration *decl;
   108     } u_;
   109 };
   110 
   111 class Statement : public AstNode
   112 {
   113 };
   114 
   115 class Expression : public AstNode
   116 {
   117     Type *type_;
   118 
   119   public:
   120     Expression()
   121       : type_(NULL)
   122     {
   123     }
   124 
   125     void setType(Type *type) {
   126         type_ = type;
   127     }
   128     Type *type() const {
   129         return type_;
   130     }
   131 };
   132 
   133 class Declaration : public Statement
   134 {
   135 };
   136 
   137 typedef PoolList<Statement *> StatementList;
   138 typedef std::vector<Expression *> ExpressionList;
   139 
   140 class VariableDeclaration : public Declaration
   141 {
   142     Variable *variable_;
   143     Expression *initialization_;
   144     ExpressionList *postDimensions_;
   145     Name *tag_;
   146 
   147   public:
   148     VariableDeclaration(Variable *var, Name *tag, ExpressionList *postDimensions, Expression *initialization)
   149       : variable_(var),
   150         postDimensions_(postDimensions),
   151         initialization_(initialization),
   152         tag_(tag)
   153     {
   154     }
   155 
   156     Name *tag() const {
   157         return tag_;
   158     }
   159     Kind kind() const {
   160         return kVariableDeclaration;
   161     }
   162     Variable *variable() const {
   163         return variable_;
   164     }
   165     Expression *initialization() const {
   166         return initialization_;
   167     }
   168     ExpressionList *postDimensions() const {
   169         return postDimensions_;
   170     }
   171     void accept(AstVisitor *visitor) {
   172         visitor->visit(this);
   173     }
   174 };
   175 
   176 class NameProxy : public Expression
   177 {
   178     Name *name_;
   179     BoundName *binding_;
   180 
   181   public:
   182     NameProxy(Name *name, BoundName *binding)
   183       : name_(name),
   184         binding_(binding)
   185     {
   186     }
   187 
   188     Kind kind() const {
   189         return kNameProxy;
   190     }
   191     Name *name() const {
   192         return name_;
   193     }
   194     void accept(AstVisitor *visitor) {
   195         visitor->visit(this);
   196     }
   197     BoundName *binding() const {
   198         return binding_;
   199     }
   200     void bind(BoundName *name) {
   201         assert(!binding());
   202         binding_ = name;
   203     }
   204 };
   205 
   206 class IntegerLiteral : public Expression
   207 {
   208     int value_;
   209 
   210   public:
   211     IntegerLiteral(int value)
   212       : value_(value)
   213     {
   214     }
   215 
   216     Kind kind() const {
   217         return kIntegerLiteral;
   218     }
   219     int value() const {
   220         return value_;
   221     }
   222     void accept(AstVisitor *visitor) {
   223         visitor->visit(this);
   224     }
   225 };
   226 
   227 class FloatLiteral : public Expression
   228 {
   229     double value_;
   230 
   231   public:
   232     FloatLiteral(double value)
   233       : value_(value)
   234     {
   235     }
   236 
   237     Kind kind() const {
   238         return kFloatLiteral;
   239     }
   240     double value() const {
   241         return value_;
   242     }
   243     void accept(AstVisitor *visitor) {
   244         visitor->visit(this);
   245     }
   246 };
   247 
   248 class BinaryExpression : public Expression
   249 {
   250     Expression *left_;
   251     Expression *right_;
   252     TokenKind token_;
   253 
   254   public:
   255     BinaryExpression(TokenKind token, Expression *left, Expression *right)
   256       : token_(token),
   257         left_(left),
   258         right_(right)
   259     {
   260     }
   261 
   262     Kind kind() const {
   263         return kBinaryExpression;
   264     }
   265     Expression *left() const {
   266         return left_;
   267     }
   268     Expression *right() const {
   269         return right_;
   270     }
   271     TokenKind token() const {
   272         return token_;
   273     }
   274     void accept(AstVisitor *visitor) {
   275         visitor->visit(this);
   276     }
   277 };
   278 
   279 class IndexExpression : public Expression
   280 {
   281     Expression *left_;
   282     Expression *right_;
   283 
   284   public:
   285     IndexExpression(Expression *left, Expression *right)
   286       : left_(left),
   287         right_(right)
   288     {
   289     }
   290 
   291     Kind kind() const {
   292         return kIndexExpression;
   293     }
   294     Expression *left() const {
   295         return left_;
   296     }
   297     Expression *right() const {
   298         return right_;
   299     }
   300     void accept(AstVisitor *visitor) {
   301         visitor->visit(this);
   302     }
   303 };
   304 
   305 class CallExpression : public Expression
   306 {
   307     Expression *callee_;
   308     ExpressionList *arguments_;
   309 
   310   public:
   311     CallExpression(Expression *callee, ExpressionList *arguments)
   312       : callee_(callee),
   313         arguments_(arguments)
   314     {
   315     }
   316 
   317     Kind kind() const {
   318         return kCallExpression;
   319     }
   320     Expression *callee() const {
   321         return callee_;
   322     }
   323     ExpressionList *arguments() const {
   324         return arguments_;
   325     }
   326     void accept(AstVisitor *visitor) {
   327         visitor->visit(this);
   328     }
   329 };
   330 
   331 class ForStatement : public Statement
   332 {
   333     Scope *scope_;
   334     VariableDeclaration *declaration_;
   335     Expression *condition_;
   336     Statement *update_;
   337     Statement *body_;
   338 
   339   public:
   340     ForStatement(Scope *scope, VariableDeclaration *declaration,
   341                  Expression *condition, Statement *update, Statement *body)
   342       : scope_(scope),
   343         declaration_(declaration),
   344         condition_(condition),
   345         update_(update),
   346         body_(body)
   347     {
   348     }
   349 
   350     Kind kind() const {
   351         return kForStatement;
   352     }
   353     VariableDeclaration *declaration() const {
   354         return declaration_;
   355     }
   356     Scope *scope() const {
   357         return scope_;
   358     }
   359     Expression *condition() const {
   360         return condition_;
   361     }
   362     Statement *update() const {
   363         return update_;
   364     }
   365     Statement *body() const {
   366         return body_;
   367     }
   368     void accept(AstVisitor *visitor) {
   369         visitor->visit(this);
   370     }
   371 };
   372 
   373 class ReturnStatement : public Statement
   374 {
   375     Expression *expression_;
   376 
   377   public:
   378     ReturnStatement(Expression *expression)
   379       : expression_(expression)
   380     {
   381     }
   382 
   383     Kind kind() const {
   384         return kReturnStatement;
   385     }
   386     Expression *expression() const {
   387         return expression_;
   388     }
   389     void accept(AstVisitor *visitor) {
   390         visitor->visit(this);
   391     }
   392 };
   393 
   394 class Assignment : public Expression
   395 {
   396     Expression *lvalue_;
   397     Expression *expression_;
   398 
   399   public:
   400     Assignment(Expression *left, Expression *right)
   401       : lvalue_(left),
   402         expression_(right)
   403     {
   404     }
   405 
   406     Kind kind() const {
   407         return kAssignment;
   408     }
   409     Expression *lvalue() const {
   410         return lvalue_;
   411     }
   412     Expression *expression() const {
   413         return expression_;
   414     }
   415     void accept(AstVisitor *visitor) {
   416         visitor->visit(this);
   417     }
   418 };
   419 
   420 class ExpressionStatement : public Statement
   421 {
   422     Expression *expression_;
   423 
   424   public:
   425     ExpressionStatement(Expression *expression)
   426       : expression_(expression)
   427     {
   428     }
   429 
   430     Kind kind() const {
   431         return kExpressionStatement;
   432     }
   433     Expression *expression() const {
   434         return expression_;
   435     }
   436     void accept(AstVisitor *visitor) {
   437         visitor->visit(this);
   438     }
   439 };
   440 
   441 class BlockStatement : public Statement
   442 {
   443     Scope *scope_;
   444     StatementList *statements_;
   445 
   446   public:
   447     BlockStatement(Scope *scope, StatementList *statements)
   448       : scope_(scope),
   449         statements_(statements)
   450     {
   451     }
   452 
   453     Kind kind() const {
   454         return kBlockStatement;
   455     }
   456     Scope *scope() const {
   457         return scope_;
   458     }
   459     StatementList *statements() const {
   460         return statements_;
   461     }
   462     void accept(AstVisitor *visitor) {
   463         visitor->visit(this);
   464     }
   465 };
   466 
   467 class FunctionStatement : public Statement
   468 {
   469     Scope *scope_;
   470     NamedFunction *fun_;
   471     Statement *body_;
   472 
   473   public:
   474     FunctionStatement(Scope *scope, NamedFunction *fun, Statement *body)
   475       : scope_(scope),
   476         fun_(fun),
   477         body_(body)
   478     {
   479     }
   480 
   481     Kind kind() const {
   482         return kFunctionStatement;
   483     }
   484     Scope *scope() const {
   485         return scope_;
   486     }
   487     NamedFunction *fun() const {
   488         return fun_;
   489     }
   490     Statement *body() const {
   491         return body_;
   492     }
   493     void accept(AstVisitor *visitor) {
   494         visitor->visit(this);
   495     }
   496 };
   497 
   498 class IfStatement : public Statement
   499 {
   500     Expression *condition_;
   501     Statement *ifTrue_;
   502     Statement *ifFalse_;
   503 
   504   public:
   505     IfStatement(Expression *condition, Statement *ifTrue)
   506       : condition_(condition),
   507         ifTrue_(ifTrue),
   508         ifFalse_(NULL)
   509     {
   510     }
   511 
   512     Kind kind() const {
   513         return kIfStatement;
   514     }
   515     Expression *condition() const {
   516         return condition_;
   517     }
   518     Statement *ifTrue() const {
   519         return ifTrue_;
   520     }
   521     Statement *ifFalse() const {
   522         return ifFalse_;
   523     }
   524     void setIfFalse(Statement *ifFalse) {
   525         ifFalse_ = ifFalse;
   526     }
   527     void accept(AstVisitor *visitor) {
   528         visitor->visit(this);
   529     }
   530 };
   531 
   532 class EnumStatement : public Statement
   533 {
   534     Name *name_;
   535 
   536   public:
   537     struct Entry {
   538         NamedConstant *name;
   539         Expression *expr;
   540 
   541         Entry(NamedConstant *name, Expression *expr)
   542           : name(name),
   543             expr(expr)
   544         {
   545         }
   546     };
   547 
   548     typedef std::vector<Entry> EntryList;
   549 
   550   public:
   551     EnumStatement(Name *name, EntryList *entries)
   552       : name_(name),
   553         entries_(entries)
   554     {
   555     }
   556 
   557     Kind kind() const {
   558         return kEnumStatement;
   559     }
   560     Name *name() const {
   561         return name_;
   562     }
   563     EntryList *entries() const {
   564         return entries_;
   565     }
   566     void accept(AstVisitor *visitor) {
   567         visitor->visit(this);
   568     }
   569 
   570   private:
   571     EntryList *entries_;
   572 };
   573 
   574 class Scope;
   575 
   576 typedef std::vector<NameProxy *> NameProxyList;
   577 
   578 class ParseTree : public PoolObject
   579 {
   580     StatementList *statements_;
   581     Scope *scope_;
   582     NameProxyList *unbound_;
   583 
   584   public:
   585     ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
   586       : statements_(statements),
   587         scope_(scope),
   588         unbound_(unbound)
   589     {
   590     }
   591 
   592     void dump(FILE *fp);
   593 
   594     StatementList *statements() const {
   595         return statements_;
   596     }
   597     Scope *scope() const {
   598         return scope_;
   599     }
   600     NameProxyList *unbound() const {
   601         return unbound_;
   602     }
   603 };
   604 
   605 }
   606 
   607 #endif // _include_jitcraft_ast_h_