jitcraft/AST.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 17 21:40:44 2012 -0700 (2012-07-17)
changeset 79 0206df35882c
parent 78 93e0a19c805a
child 83 79265b56316e
permissions -rw-r--r--
Implemented ! operator.
     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     _(UnaryExpression)
    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 PoolList<Expression *> 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     bool const_;
   203     ExpressionList *dimensions_;
   204 
   205   public:
   206     Parameter(Variable *var, Handle<String> tag, bool reference, bool isConst, ExpressionList *dimensions)
   207       : variable_(var),
   208         tag_(tag),
   209         reference_(reference),
   210         const_(isConst),
   211         dimensions_(dimensions)
   212     {
   213     }
   214 
   215     String *tag() const {
   216         return tag_;
   217     }
   218     Variable *variable() const {
   219         return variable_;
   220     }
   221     bool reference() const {
   222         return reference_;
   223     }
   224     ExpressionList *dimensions() const {
   225         return dimensions_;
   226     }
   227     bool isConst() const {
   228         return const_;
   229     }
   230 };
   231 
   232 class NameProxy : public Expression
   233 {
   234     ScopedRoot<String> name_;
   235     BoundName *binding_;
   236 
   237   public:
   238     NameProxy(const SourcePosition &pos, Handle<String> name, BoundName *binding)
   239       : Expression(pos),
   240         name_(name),
   241         binding_(binding)
   242     {
   243     }
   244 
   245     Kind kind() const {
   246         return kNameProxy;
   247     }
   248     String *name() const {
   249         return name_;
   250     }
   251     void accept(AstVisitor *visitor) {
   252         visitor->visit(this);
   253     }
   254     BoundName *binding() const {
   255         return binding_;
   256     }
   257     void bind(BoundName *name) {
   258         assert(!binding());
   259         binding_ = name;
   260     }
   261 };
   262 
   263 class IntegerLiteral : public Expression
   264 {
   265     int value_;
   266 
   267   public:
   268     IntegerLiteral(const SourcePosition &pos, int value)
   269       : Expression(pos),
   270         value_(value)
   271     {
   272     }
   273 
   274     Kind kind() const {
   275         return kIntegerLiteral;
   276     }
   277     int value() const {
   278         return value_;
   279     }
   280     void accept(AstVisitor *visitor) {
   281         visitor->visit(this);
   282     }
   283 };
   284 
   285 class FloatLiteral : public Expression
   286 {
   287     double value_;
   288 
   289   public:
   290     FloatLiteral(const SourcePosition &pos, double value)
   291       : Expression(pos),
   292         value_(value)
   293     {
   294     }
   295 
   296     Kind kind() const {
   297         return kFloatLiteral;
   298     }
   299     double value() const {
   300         return value_;
   301     }
   302     void accept(AstVisitor *visitor) {
   303         visitor->visit(this);
   304     }
   305 };
   306 
   307 class StringLiteral : public Expression
   308 {
   309     ScopedRoot<ByteArray> string_;
   310 
   311   public:
   312     StringLiteral(const SourcePosition &pos, ByteArray *array)
   313       : Expression(pos),
   314         string_(array)
   315     {
   316     }
   317 
   318     Kind kind() const {
   319         return kStringLiteral;
   320     }
   321     ByteArray *string() const {
   322         return string_;
   323     }
   324     void accept(AstVisitor *visitor) {
   325         visitor->visit(this);
   326     }
   327 };
   328 
   329 class UnaryExpression : public Expression
   330 {
   331     Expression *expression_;
   332     TokenKind token_;
   333 
   334   public:
   335     UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr)
   336       : Expression(pos),
   337         expression_(expr),
   338         token_(token)
   339     {
   340     }
   341 
   342     Kind kind() const {
   343         return kUnaryExpression;
   344     }
   345     Expression *expression() const {
   346         return expression_;
   347     }
   348     TokenKind token() const {
   349         return token_;
   350     }
   351     void accept(AstVisitor *visitor) {
   352         visitor->visit(this);
   353     }
   354 };
   355 
   356 class BinaryExpression : public Expression
   357 {
   358     Expression *left_;
   359     Expression *right_;
   360     TokenKind token_;
   361 
   362   public:
   363     BinaryExpression(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   364       : Expression(pos),
   365         left_(left),
   366         right_(right),
   367         token_(token)
   368     {
   369     }
   370 
   371     Kind kind() const {
   372         return kBinaryExpression;
   373     }
   374     Expression *left() const {
   375         return left_;
   376     }
   377     Expression *right() const {
   378         return right_;
   379     }
   380     TokenKind token() const {
   381         return token_;
   382     }
   383     void accept(AstVisitor *visitor) {
   384         visitor->visit(this);
   385     }
   386 };
   387 
   388 class IndexExpression : public Expression
   389 {
   390     Expression *left_;
   391     Expression *right_;
   392 
   393   public:
   394     IndexExpression(const SourcePosition &pos, Expression *left, Expression *right)
   395       : Expression(pos),
   396         left_(left),
   397         right_(right)
   398     {
   399     }
   400 
   401     Kind kind() const {
   402         return kIndexExpression;
   403     }
   404     Expression *left() const {
   405         return left_;
   406     }
   407     Expression *right() const {
   408         return right_;
   409     }
   410     void accept(AstVisitor *visitor) {
   411         visitor->visit(this);
   412     }
   413 };
   414 
   415 class CallExpression : public Expression
   416 {
   417     Expression *callee_;
   418     ExpressionList *arguments_;
   419 
   420   public:
   421     CallExpression(const SourcePosition &pos, Expression *callee, ExpressionList *arguments)
   422       : Expression(pos),
   423         callee_(callee),
   424         arguments_(arguments)
   425     {
   426     }
   427 
   428     Kind kind() const {
   429         return kCallExpression;
   430     }
   431     Expression *callee() const {
   432         return callee_;
   433     }
   434     ExpressionList *arguments() const {
   435         return arguments_;
   436     }
   437     void accept(AstVisitor *visitor) {
   438         visitor->visit(this);
   439     }
   440 };
   441 
   442 class ForStatement : public Statement
   443 {
   444     Scope *scope_;
   445     Statement *initialization_;
   446     Expression *condition_;
   447     Statement *update_;
   448     Statement *body_;
   449 
   450   public:
   451     ForStatement(const SourcePosition &pos, Scope *scope, Statement *initialization,
   452                  Expression *condition, Statement *update, Statement *body)
   453       : Statement(pos),
   454         scope_(scope),
   455         initialization_(initialization),
   456         condition_(condition),
   457         update_(update),
   458         body_(body)
   459     {
   460     }
   461 
   462     Kind kind() const {
   463         return kForStatement;
   464     }
   465     Statement *initialization() const {
   466         return initialization_;
   467     }
   468     Scope *scope() const {
   469         return scope_;
   470     }
   471     Expression *condition() const {
   472         return condition_;
   473     }
   474     Statement *update() const {
   475         return update_;
   476     }
   477     Statement *body() const {
   478         return body_;
   479     }
   480     void accept(AstVisitor *visitor) {
   481         visitor->visit(this);
   482     }
   483 };
   484 
   485 class WhileStatement : public Statement
   486 {
   487     TokenKind token_;
   488     Expression *condition_;
   489     Statement *body_;
   490 
   491   public:
   492     WhileStatement(const SourcePosition &pos, TokenKind kind, Expression *condition, Statement *body)
   493       : Statement(pos),
   494         token_(kind),
   495         condition_(condition),
   496         body_(body)
   497     {
   498     }
   499     
   500     Kind kind() const {
   501         return kWhileStatement;
   502     }
   503     TokenKind token() const {
   504         return token_;
   505     }
   506     Expression *condition() const {
   507         return condition_;
   508     }
   509     Statement *body() const {
   510         return body_;
   511     }
   512     void accept(AstVisitor *visitor) {
   513         visitor->visit(this);
   514     }
   515 };
   516 
   517 class ReturnStatement : public Statement
   518 {
   519     Expression *expression_;
   520 
   521   public:
   522     ReturnStatement(const SourcePosition &pos, Expression *expression)
   523       : Statement(pos),
   524         expression_(expression)
   525     {
   526     }
   527 
   528     Kind kind() const {
   529         return kReturnStatement;
   530     }
   531     Expression *expression() const {
   532         return expression_;
   533     }
   534     void accept(AstVisitor *visitor) {
   535         visitor->visit(this);
   536     }
   537 };
   538 
   539 class BreakStatement : public Statement
   540 {
   541   public:
   542     BreakStatement(const SourcePosition &pos)
   543       : Statement(pos)
   544     {
   545     }
   546 
   547     Kind kind() const {
   548         return kBreakStatement;
   549     }
   550     void accept(AstVisitor *visitor) {
   551         visitor->visit(this);
   552     }
   553 };
   554 
   555 class ContinueStatement : public Statement
   556 {
   557   public:
   558     ContinueStatement(const SourcePosition &pos)
   559       : Statement(pos)
   560     {
   561     }
   562 
   563     Kind kind() const {
   564         return kContinueStatement;
   565     }
   566     void accept(AstVisitor *visitor) {
   567         visitor->visit(this);
   568     }
   569 };
   570 
   571 class Assignment : public Expression
   572 {
   573     TokenKind token_;
   574     Expression *lvalue_;
   575     Expression *expression_;
   576 
   577   public:
   578     Assignment(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   579       : Expression(pos),
   580         token_(token),
   581         lvalue_(left),
   582         expression_(right)
   583     {
   584     }
   585 
   586     Kind kind() const {
   587         return kAssignment;
   588     }
   589     TokenKind token() const {
   590         return token_;
   591     }
   592     Expression *lvalue() const {
   593         return lvalue_;
   594     }
   595     Expression *expression() const {
   596         return expression_;
   597     }
   598     void accept(AstVisitor *visitor) {
   599         visitor->visit(this);
   600     }
   601 };
   602 
   603 class ExpressionStatement : public Statement
   604 {
   605     Expression *expression_;
   606 
   607   public:
   608     ExpressionStatement(Expression *expression)
   609       : Statement(expression->pos()),
   610         expression_(expression)
   611     {
   612     }
   613 
   614     Kind kind() const {
   615         return kExpressionStatement;
   616     }
   617     Expression *expression() const {
   618         return expression_;
   619     }
   620     void accept(AstVisitor *visitor) {
   621         visitor->visit(this);
   622     }
   623 };
   624 
   625 class BlockStatement : public Statement
   626 {
   627     Scope *scope_;
   628     StatementList *statements_;
   629 
   630   public:
   631     BlockStatement(const SourcePosition &pos, Scope *scope, StatementList *statements)
   632       : Statement(pos),
   633         scope_(scope),
   634         statements_(statements)
   635     {
   636     }
   637 
   638     Kind kind() const {
   639         return kBlockStatement;
   640     }
   641     Scope *scope() const {
   642         return scope_;
   643     }
   644     StatementList *statements() const {
   645         return statements_;
   646     }
   647     void accept(AstVisitor *visitor) {
   648         visitor->visit(this);
   649     }
   650 };
   651 
   652 typedef PoolList<Parameter *> ParameterList;
   653 
   654 class FunctionStatement : public Statement
   655 {
   656     Scope *scope_;
   657     NamedFunction *fun_;
   658     Statement *body_;
   659     ParameterList *parameters_;
   660     bool explicitReturn_;
   661 
   662   public:
   663     FunctionStatement(const SourcePosition &pos, Scope *scope, NamedFunction *fun, Statement *body,
   664                       ParameterList *parameters, bool explicitReturn)
   665       : Statement(pos),
   666         scope_(scope),
   667         fun_(fun),
   668         body_(body),
   669         parameters_(parameters),
   670         explicitReturn_(explicitReturn)
   671     {
   672     }
   673 
   674     Kind kind() const {
   675         return kFunctionStatement;
   676     }
   677     Scope *scope() const {
   678         return scope_;
   679     }
   680     NamedFunction *fun() const {
   681         return fun_;
   682     }
   683     Statement *body() const {
   684         return body_;
   685     }
   686     ParameterList *parameters() const {
   687         return parameters_;
   688     }
   689     void accept(AstVisitor *visitor) {
   690         visitor->visit(this);
   691     }
   692     bool hasExplicitReturn() const {
   693         return explicitReturn_;
   694     }
   695 };
   696 
   697 class IfStatement : public Statement
   698 {
   699     Expression *condition_;
   700     Statement *ifTrue_;
   701     Statement *ifFalse_;
   702 
   703   public:
   704     IfStatement(const SourcePosition &pos, Expression *condition, Statement *ifTrue)
   705       : Statement(pos),
   706         condition_(condition),
   707         ifTrue_(ifTrue),
   708         ifFalse_(NULL)
   709     {
   710     }
   711 
   712     Kind kind() const {
   713         return kIfStatement;
   714     }
   715     Expression *condition() const {
   716         return condition_;
   717     }
   718     Statement *ifTrue() const {
   719         return ifTrue_;
   720     }
   721     Statement *ifFalse() const {
   722         return ifFalse_;
   723     }
   724     void setIfFalse(Statement *ifFalse) {
   725         ifFalse_ = ifFalse;
   726     }
   727     void accept(AstVisitor *visitor) {
   728         visitor->visit(this);
   729     }
   730 };
   731 
   732 class EnumStatement : public Statement
   733 {
   734     ScopedRoot<String> name_;
   735 
   736   public:
   737     struct Entry {
   738         NamedConstant *name;
   739         Expression *expr;
   740 
   741         Entry(NamedConstant *name, Expression *expr)
   742           : name(name),
   743             expr(expr)
   744         {
   745         }
   746     };
   747 
   748     typedef PoolList<Entry> EntryList;
   749 
   750   public:
   751     EnumStatement(const SourcePosition &pos, Handle<String> name, EntryList *entries)
   752       : Statement(pos),
   753         name_(name),
   754         entries_(entries)
   755     {
   756     }
   757 
   758     Kind kind() const {
   759         return kEnumStatement;
   760     }
   761     String *name() const {
   762         return name_;
   763     }
   764     EntryList *entries() const {
   765         return entries_;
   766     }
   767     void accept(AstVisitor *visitor) {
   768         visitor->visit(this);
   769     }
   770 
   771   private:
   772     EntryList *entries_;
   773 };
   774 
   775 class IncDecExpression : public Expression
   776 {
   777     TokenKind token_;
   778     Expression *expression_;
   779     bool postfix_;
   780 
   781   public:
   782     IncDecExpression(const SourcePosition &pos, TokenKind token, Expression *expression, bool postfix)
   783       : Expression(pos),
   784         token_(token),
   785         expression_(expression),
   786         postfix_(postfix)
   787     {
   788     }
   789 
   790     Kind kind() const {
   791         return kIncDecExpression;
   792     }
   793     TokenKind token() const {
   794         return token_;
   795     }
   796     Expression *expression() const {
   797         return expression_;
   798     }
   799     bool postfix() const {
   800         return postfix_;
   801     }
   802     void accept(AstVisitor *visitor) {
   803         visitor->visit(this);
   804     }
   805 };
   806 
   807 class Scope;
   808 
   809 typedef PoolList<NameProxy *> NameProxyList;
   810 
   811 class ParseTree : public PoolObject
   812 {
   813     StatementList *statements_;
   814     Scope *scope_;
   815     NameProxyList *unbound_;
   816 
   817   public:
   818     ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
   819       : statements_(statements),
   820         scope_(scope),
   821         unbound_(unbound)
   822     {
   823     }
   824 
   825     void dump(FILE *fp);
   826 
   827     StatementList *statements() const {
   828         return statements_;
   829     }
   830     Scope *scope() const {
   831         return scope_;
   832     }
   833     NameProxyList *unbound() const {
   834         return unbound_;
   835     }
   836 };
   837 
   838 }
   839 
   840 #endif // _include_jitcraft_ast_h_