From 32c32bc48c8fd7cffca301b8919fb26726f6467e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 24 Dec 2011 01:08:21 -0700 Subject: Ast nearly done, builder coming along. --- demo.stage | 1 + src/astbranch.cpp | 16 ++++++++++++++++ src/astbranch.h | 20 ++++++++++++++++++++ src/astleaf.cpp | 11 +++++++++++ src/astleaf.h | 17 +++++++++++++++++ src/astnode.cpp | 10 ++++++++++ src/astnode.h | 35 +++++++++++++++++++++-------------- src/game.h | 3 --- src/gamebuilder.cpp | 21 +++++++++++++++++++++ src/gamebuilder.h | 6 +++++- src/parser.l | 3 +++ src/parser.y | 51 +++++++++++++++++++++++++++++++++++---------------- test.stage | 16 ++++++++++++++++ 13 files changed, 176 insertions(+), 34 deletions(-) create mode 100644 src/astbranch.cpp create mode 100644 src/astbranch.h create mode 100644 src/astleaf.cpp create mode 100644 src/astleaf.h create mode 100644 test.stage diff --git a/demo.stage b/demo.stage index e51c160..67c6063 100644 --- a/demo.stage +++ b/demo.stage @@ -50,6 +50,7 @@ situation <
> function hello() { + bob = 55 * 2 + 1; } situation <> diff --git a/src/astbranch.cpp b/src/astbranch.cpp new file mode 100644 index 0000000..7b31426 --- /dev/null +++ b/src/astbranch.cpp @@ -0,0 +1,16 @@ +#include "astbranch.h" + +AstBranch::AstBranch( AstNode::Type eType ) : + AstNode( eType ) +{ +} + +AstBranch::~AstBranch() +{ + for( NodeList::iterator i = lNodes.begin(); i; i++ ) + { + delete *i; + } +} + + diff --git a/src/astbranch.h b/src/astbranch.h new file mode 100644 index 0000000..72f8014 --- /dev/null +++ b/src/astbranch.h @@ -0,0 +1,20 @@ +#ifndef AST_BRANCH_H +#define AST_BRANCH_H + +#include "astnode.h" + +#include + +class AstBranch : public AstNode +{ +public: + AstBranch( Type eType ); + virtual ~AstBranch(); + + typedef Bu::List NodeList; + +private: + NodeList lNodes; +}; + +#endif diff --git a/src/astleaf.cpp b/src/astleaf.cpp new file mode 100644 index 0000000..6e29d7e --- /dev/null +++ b/src/astleaf.cpp @@ -0,0 +1,11 @@ +#include "astleaf.h" + +AstLeaf::AstLeaf( AstNode::Type eType ) : + AstNode( eType ) +{ +} + +AstLeaf::~AstLeaf() +{ +} + diff --git a/src/astleaf.h b/src/astleaf.h new file mode 100644 index 0000000..7cc9dda --- /dev/null +++ b/src/astleaf.h @@ -0,0 +1,17 @@ +#ifndef AST_LEAF_H +#define AST_LEAF_H + +#include "astnode.h" +#include "variable.h" + +class AstLeaf : public AstNode +{ +public: + AstLeaf( Type eType ); + virtual ~AstLeaf(); + +private: + Variable vValue; +}; + +#endif diff --git a/src/astnode.cpp b/src/astnode.cpp index ff73346..4e0f30e 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp @@ -1 +1,11 @@ #include "astnode.h" + +AstNode::AstNode( AstNode::Type eType ) : + eType( eType ) +{ +} + +AstNode::~AstNode() +{ +} + diff --git a/src/astnode.h b/src/astnode.h index 780e07e..9584788 100644 --- a/src/astnode.h +++ b/src/astnode.h @@ -6,31 +6,38 @@ class AstNode { public: - AstNode(); - virtual ~AstNode(); - enum Type { - tValue = 0x01000000, + tLeaf = 0x01000000, tVarName = 0x01000001, tLiteral = 0x01000002, tFuncName = 0x01000003, + tNot = 0x01000004, + tComp = 0x01000005, + tCompGt = 0x01000006, + tCompLt = 0x01000007, + tCompGtEq = 0x01000008, + tCompLtEq = 0x01000009, + tStore = 0x0100000A, + tAnd = 0x0100000B, + tOr = 0x0100000C, + tBranch = 0x02000000, tScope = 0x02000001, tIf = 0x02000002, - tElse = 0x02000003, - tNot = 0x00000004, - tComp = 0x02000005, - tCompGt = 0x02000006, - tCompLt = 0x02000007, - tCompGtEq = 0x02000008, - tCompLtEq = 0x02000009, - tStore = 0x0200000A, - tAnd = 0x0200000B, - tOr = 0x0200000C, + tForEach = 0x02000003, + tWhile = 0x02000004, + + tTypeMask = 0x03000000, }; + + AstNode( Type eType ); + virtual ~AstNode(); + + Type getType() const { return eType; } private: + Type eType; }; #endif diff --git a/src/game.h b/src/game.h index a5b1163..1daa148 100644 --- a/src/game.h +++ b/src/game.h @@ -19,9 +19,6 @@ private: typedef Bu::Hash ScopeHash; FunctionHash hFunction; SituationHash hSituation; - ScopeHash hSituationScope; - Scope sGlobal; - Scope sPlayer; Bu::String sCurSituation; }; diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 912a595..3696495 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -42,3 +42,24 @@ void GameBuilder::endSituation() sio << "Situation ended." << sio.nl; } +void GameBuilder::addParam( const Bu::String &sName ) +{ + sio << " - Param added '" << sName << "'" << sio.nl; +} + +void GameBuilder::addNode( int iType ) +{ + sio << " - Added type " << Fmt::hex() << iType << sio.nl; +} + +void GameBuilder::addLiteral( const Variable &v ) +{ + setLiteral( v ); + sio << " - Added literal " << v << sio.nl; +} + +void GameBuilder::addVarRef( const Bu::String &sName ) +{ + sio << " - Added varref '" << sName << "'" << sio.nl; +} + diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 59b4b57..26d6781 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -16,11 +16,15 @@ public: void beginFunction( const Bu::String &sName ); void endFunction(); - void beginSituation( const Bu::String &sName ); void endSituation(); + void addParam( const Bu::String &sName ); + void addNode( int iType ); + void addLiteral( const Variable &v ); + void addVarRef( const Bu::String &sName ); + private: Variable vLiteral; diff --git a/src/parser.l b/src/parser.l index 9e031eb..3491d27 100644 --- a/src/parser.l +++ b/src/parser.l @@ -36,6 +36,7 @@ player { return tokPlayer; } while { return tokWhile; } for { return tokFor; } each { return tokEach; } +do { return tokDo; } in { return tokIn; } if { return tokIf; } then { return tokThen; } @@ -45,6 +46,8 @@ goto { return tokGoto; } not { return tokNot; } setup { return tokSetup; } enter { return tokEnter; } +and { return tokAnd; } +or { return tokOr; } true { yylval->bValue = true; return tokBool; } false { yylval->bValue = false; return tokBool; } diff --git a/src/parser.y b/src/parser.y index 0b1d41b..7993927 100644 --- a/src/parser.y +++ b/src/parser.y @@ -4,6 +4,7 @@ #include #include "gamebuilder.h" +#include "astnode.h" typedef void *yyscan_t; @@ -44,9 +45,12 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %token tokSituation %token tokSetup %token tokEnter +%token tokAnd +%token tokOr %token tokWhile %token tokFor %token tokEach +%token tokDo %token tokIn %token tokIf %token tokThen @@ -77,10 +81,10 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %right tokNot %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign %right tokLtEq tokGtEq tokCmp -%left tokIn -%left '(' ')' '[' ']' -%left '*' '/' %left '-' '+' +%left '*' '/' +%left tokIn tokAnd tokOr +%left '(' ')' '[' ']' %% input: gameDecls globalDecl bodyDecl @@ -125,9 +129,17 @@ situationMode: tokSetup | tokEnter ; -function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' ')' '{' '}' { bld.endFunction(); } +function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); } ; +funcParamList: + | tokIdent { bld.addParam( *($1) ); } funcParamListEx + ; + +funcParamListEx: + | funcParamListEx ',' tokIdent { bld.addParam( *($3) ); } + ; + cmpltExprList: | cmpltExprList cmpltExpr ; @@ -135,39 +147,46 @@ cmpltExprList: cmpltExpr: expr ';' | tokGoto '(' expr ')' ';' | tokIf expr tokThen '{' cmpltExprList '}' ifnext + | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' + | tokWhile expr tokDo '{' cmpltExprList '}' ; +forIterator: tokIdent + | tokIdent ':' tokIdent + ifnext: | tokElse '{' cmpltExprList '}' | tokElse tokIf '{' cmpltExprList '}' ifnext ; -varRef: tokIdent +varRef: tokIdent { bld.addVarRef( *($1) ); } | tokPlayer '.' tokIdent | tokGlobal '.' tokIdent | tokSituation '.' tokIdent ; -literal: tokInt { bld.setLiteral( Variable( $1 ) ); } - | tokFloat { bld.setLiteral( Variable( $1 ) ); } - | tokString { bld.setLiteral( Variable( *($1) ) ); } - | tokBool { bld.setLiteral( Variable( $1 ) ); } - | tokNull { bld.setLiteral( Variable( Variable::tNull ) ); } - | tokSituationName { bld.setLiteral( Variable::newSituationName( *($1) ) ); } +literal: tokInt { bld.addLiteral( Variable( $1 ) ); } + | tokFloat { bld.addLiteral( Variable( $1 ) ); } + | tokString { bld.addLiteral( Variable( *($1) ) ); } + | tokBool { bld.addLiteral( Variable( $1 ) ); } + | tokNull { bld.addLiteral( Variable( Variable::tNull ) ); } + | tokSituationName { bld.addLiteral( Variable::newSituationName( *($1) ) ); } ; expr: literal | tokIdent '(' listValues ')' | varRef - | varRef '=' expr + | varRef '=' expr { bld.addNode( AstNode::tStore ); } | varRef tokPlusAssign expr | varRef tokMinusAssign expr | varRef tokTimesAssign expr | varRef tokDivideAssign expr - | expr '+' expr - | expr '-' expr - | expr '/' expr - | expr '*' expr + | expr '+' expr { printf(" + plus\n"); } + | expr '-' expr { printf(" + minus\n"); } + | expr '/' expr { printf(" + divide\n"); } + | expr '*' expr { printf(" + times\n"); } + | expr tokAnd expr + | expr tokOr expr | expr tokIn expr | '(' expr ')' | expr '[' expr ']' diff --git a/test.stage b/test.stage new file mode 100644 index 0000000..c242a44 --- /dev/null +++ b/test.stage @@ -0,0 +1,16 @@ + +game.title = "Demo game"; +game.author = "Mike Buland"; +game.version = 1; +game.revision = 0; +game.start = <>; + +global +{ +} + +function hello( a, b, c ) +{ + bob = 5 + 55 * 2 + 1; +} + -- cgit v1.2.3