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