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