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