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