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