From 40e08192a08f55b5090d5ef28f48c74613e2e5a0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Dec 2011 00:32:12 -0700 Subject: Wow, a whole lot of code builds now. --- src/astbranch.h | 1 + src/astleafliteral.cpp | 6 ++++++ src/astleafliteral.h | 1 + src/astnode.cpp | 4 ++++ src/astnode.h | 4 ++++ src/gamebuilder.cpp | 13 +++++++++---- src/gamebuilder.h | 1 + src/parser.y | 48 ++++++++++++++++++++++++++++++++++++++---------- test.stage | 14 +++++++++++++- 9 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/astbranch.h b/src/astbranch.h index 26aac80..8e63af5 100644 --- a/src/astbranch.h +++ b/src/astbranch.h @@ -13,6 +13,7 @@ public: virtual ~AstBranch(); AstNode *addNode( AstNode *pNode ); + AstBranch *getParent() const { return pParent; } typedef Bu::List NodeList; diff --git a/src/astleafliteral.cpp b/src/astleafliteral.cpp index a2a0808..116aeb6 100644 --- a/src/astleafliteral.cpp +++ b/src/astleafliteral.cpp @@ -8,6 +8,12 @@ AstLeafLiteral::AstLeafLiteral( const Variable &v ) : { } +AstLeafLiteral::AstLeafLiteral( AstNode eType, const Variable &v ) : + AstNode( eType ), + vValue( v ) +{ +} + AstLeafLiteral::~AstLeafLiteral() { } diff --git a/src/astleafliteral.h b/src/astleafliteral.h index c8867e1..e2da3f6 100644 --- a/src/astleafliteral.h +++ b/src/astleafliteral.h @@ -9,6 +9,7 @@ class AstLeafLiteral : public AstNode friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); public: AstLeafLiteral( const Variable &v ); + AstLeafLiteral( AstNode eType, const Variable &v ); virtual ~AstLeafLiteral(); const Variable &getValue() const { return vValue; } diff --git a/src/astnode.cpp b/src/astnode.cpp index 2c94ca9..18ce122 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp @@ -29,6 +29,10 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ) case AstNode::tStore: return f << "tStore"; case AstNode::tAnd: return f << "tAnd"; case AstNode::tOr: return f << "tOr"; + case AstNode::tPlus: return f << "tPlus"; + case AstNode::tMinus: return f << "tMinus"; + case AstNode::tDivide: return f << "tDivide"; + case AstNode::tMultiply: return f << "tMultiply"; case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; case AstNode::tVarName: return f << "tVarName"; diff --git a/src/astnode.h b/src/astnode.h index 1054a2b..abbcaaf 100644 --- a/src/astnode.h +++ b/src/astnode.h @@ -19,6 +19,10 @@ public: tStore = 0x01000007, tAnd = 0x01000008, tOr = 0x01000009, + tPlus = 0x0100000A, + tMinus = 0x0100000B, + tDivide = 0x0100000C, + tMultiply = 0x0100000D, tLeafLiteral = 0x02000000, tVarName = 0x02000001, diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 44fd6c1..47de17d 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -57,8 +57,6 @@ void GameBuilder::addParam( const Bu::String &sName ) void GameBuilder::addNode( AstNode::Type iType ) { - sio << " - Added type " << Fmt::hex() << iType << sio.nl; - switch( iType&AstNode::tTypeMask ) { case AstNode::tBranch: @@ -71,6 +69,11 @@ void GameBuilder::addNode( AstNode::Type iType ) } } +void GameBuilder::closeNode() +{ + pCurNode = pCurNode->getParent(); +} + void GameBuilder::addLiteral( const Variable &v ) { setLiteral( v ); @@ -78,11 +81,13 @@ void GameBuilder::addLiteral( const Variable &v ) { pCurNode->addNode( new AstLeafLiteral( v ) ); } - sio << " - Added literal " << v << sio.nl; } void GameBuilder::addVarRef( const Bu::String &sName ) { - sio << " - Added varref '" << sName << "'" << sio.nl; + if( pCurNode ) + { + pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, sName ) ); + } } diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 51ff27a..2637c37 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -23,6 +23,7 @@ public: void addParam( const Bu::String &sName ); void addNode( AstNode::Type iType ); + void closeNode(); void addLiteral( const Variable &v ); void addVarRef( const Bu::String &sName ); diff --git a/src/parser.y b/src/parser.y index 7993927..fbada5a 100644 --- a/src/parser.y +++ b/src/parser.y @@ -80,10 +80,10 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %right tokNot %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign -%right tokLtEq tokGtEq tokCmp %left '-' '+' %left '*' '/' %left tokIn tokAnd tokOr +%right '<' '>' tokLtEq tokGtEq tokCmp %left '(' ')' '[' ']' %% @@ -146,17 +146,40 @@ cmpltExprList: cmpltExpr: expr ';' | tokGoto '(' expr ')' ';' - | tokIf expr tokThen '{' cmpltExprList '}' ifnext + | ifbase | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' - | tokWhile expr tokDo '{' cmpltExprList '}' + | tokWhile { + bld.addNode( AstNode::tWhile ); + bld.addNode( AstNode::tScope ); + } expr { + bld.closeNode(); + bld.addNode( AstNode::tScope ); + } tokDo '{' cmpltExprList '}' { + bld.closeNode(); bld.closeNode(); + } ; forIterator: tokIdent | tokIdent ':' tokIdent +ifbase: tokIf { + bld.addNode( AstNode::tIf ); + bld.addNode( AstNode::tScope ); + } expr { + bld.closeNode(); + bld.addNode( AstNode::tScope ); + } tokThen '{' cmpltExprList '}' { + bld.closeNode(); + } ifnext { + bld.closeNode(); + } + ; + ifnext: - | tokElse '{' cmpltExprList '}' - | tokElse tokIf '{' cmpltExprList '}' ifnext + | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' { + bld.closeNode(); + } + | tokElse { bld.addNode( AstNode::tScope ); } ifbase ; varRef: tokIdent { bld.addVarRef( *($1) ); } @@ -177,17 +200,22 @@ expr: literal | tokIdent '(' listValues ')' | varRef | varRef '=' expr { bld.addNode( AstNode::tStore ); } - | varRef tokPlusAssign expr + | varRef tokPlusAssign expr | varRef tokMinusAssign expr | varRef tokTimesAssign expr | varRef tokDivideAssign expr - | expr '+' expr { printf(" + plus\n"); } - | expr '-' expr { printf(" + minus\n"); } - | expr '/' expr { printf(" + divide\n"); } - | expr '*' expr { printf(" + times\n"); } + | expr '+' expr { bld.addNode( AstNode::tPlus ); } + | expr '-' expr { bld.addNode( AstNode::tMinus ); } + | expr '/' expr { bld.addNode( AstNode::tDivide ); } + | expr '*' expr { bld.addNode( AstNode::tMultiply ); } | expr tokAnd expr | expr tokOr expr | expr tokIn expr + | expr tokCmp expr { bld.addNode( AstNode::tComp ); } + | expr '<' expr { bld.addNode( AstNode::tCompLt ); } + | expr '>' expr { bld.addNode( AstNode::tCompGt ); } + | expr tokLtEq expr { bld.addNode( AstNode::tCompLtEq ); } + | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); } | '(' expr ')' | expr '[' expr ']' | '[' ']' diff --git a/test.stage b/test.stage index c242a44..a1c6ab3 100644 --- a/test.stage +++ b/test.stage @@ -11,6 +11,18 @@ global function hello( a, b, c ) { - bob = 5 + 55 * 2 + 1; + joe = a; + if b < c then + { + sam = b * c + 1; + } + else if something <= c then + { + x * 43; + } + while true do + { + bob = 5 + 55 * 2 + 1; + } } -- cgit v1.2.3