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