From 9de9d4e733ce872806c569334af4c9ace01db203 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 6 Feb 2012 12:22:27 -0700 Subject: Important bugfix in stack code. Thanks david! --- src/gamebuilder.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'src/gamebuilder.cpp') diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index bfeffec..857c348 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -19,7 +19,8 @@ GameBuilder::GameBuilder() : pCurRoot( NULL ), pCurCmd( NULL ), pCurFnc( NULL ), - pCurSit( NULL ) + pCurSit( NULL ), + iStackHeight( 0 ) { pGame = new Game(); } @@ -49,6 +50,15 @@ void GameBuilder::parse( const Bu::String &sFile ) fclose( in ); } +void GameBuilder::endCmpltExpr() +{ + while( iStackHeight > 0 ) + { + addNode( AstNode::tPop ); + } + iStackHeight = 0; +} + void GameBuilder::setLiteral( const Variable &v ) { vLiteral = v; @@ -91,9 +101,12 @@ void GameBuilder::endFunctionParams() void GameBuilder::endFunction() { //sio << "Function ended: " << *pCurRoot << sio.nl; + + pCurNode->addNode( new AstLeafLiteral( Variable::tNull ) ); pCurFnc->setAst( pCurRoot ); pCurRoot = pCurNode = NULL; pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); + iStackHeight = 0; } void GameBuilder::beginSituation( const Bu::String &sName, Situation::InputType tInput ) @@ -123,6 +136,7 @@ void GameBuilder::endSituation() void GameBuilder::addNode( AstNode::Type iType ) { + stackMod( iType ); switch( iType&AstNode::tTypeMask ) { case AstNode::tBranch: @@ -145,6 +159,7 @@ void GameBuilder::addLiteral( const Variable &v ) setLiteral( v ); if( pCurNode ) { + stackMod( AstNode::tLeafLiteral ); pCurNode->addNode( new AstLeafLiteral( v ) ); } } @@ -153,6 +168,7 @@ void GameBuilder::addVarRef( const Bu::String &sName, ScopeId sid ) { if( pCurNode ) { + stackMod( AstNode::tVarName ); pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, Variable::newVariableName( sName, sid ) ) ); } } @@ -161,10 +177,78 @@ void GameBuilder::addFuncCall( const Bu::String &sName ) { if( pCurNode ) { + stackMod( AstNode::tFuncCall ); pCurNode->addNode( new AstLeafLiteral( AstNode::tFuncCall, sName ) ); } } +void GameBuilder::stackMod( AstNode::Type iType ) +{ + switch( iType ) + { + case AstNode::tNot: + case AstNode::tNegate: + case AstNode::tSwap: + break; + + case AstNode::tComp: + case AstNode::tCompGt: + case AstNode::tCompLt: + case AstNode::tCompGtEq: + case AstNode::tCompLtEq: + case AstNode::tStore: + case AstNode::tAnd: + case AstNode::tOr: + case AstNode::tPlus: + case AstNode::tMinus: + case AstNode::tDivide: + case AstNode::tMultiply: + case AstNode::tPlusStore: + case AstNode::tMinusStore: + case AstNode::tDivideStore: + case AstNode::tMultiplyStore: + case AstNode::tIn: + case AstNode::tGoto: + case AstNode::tStoreRev: + case AstNode::tReturn: // return doesn't pop anything, but it counts as it. + case AstNode::tAppend: + case AstNode::tPop: + case AstNode::tIndex: + iStackHeight--; + break; + + case AstNode::tInsert: + iStackHeight -= 2; + break; + + case AstNode::tLeafLiteral: + case AstNode::tVarName: + case AstNode::tLiteral: + case AstNode::tFuncCall: + iStackHeight++; + break; + + case AstNode::tBranch: + break; + + case AstNode::tScope: + break; + + case AstNode::tIf: + iStackHeight--; + break; + + case AstNode::tForEach: + iStackHeight -= 2; + break; + + case AstNode::tWhile: + iStackHeight -= 1; + break; + + } +} + void GameBuilder::beginGlobal() { bGlobal = true; @@ -219,6 +303,7 @@ void GameBuilder::closeCommand() pCurSit->csLocal.addCommand( pCurCmd ); } pCurCmd = NULL; + iStackHeight = 0; } void GameBuilder::beginOption( const Bu::String &sValue ) @@ -237,5 +322,6 @@ void GameBuilder::closeOption() //pCurCmd->print(); pCurSit->csLocal.addCommand( pCurCmd ); pCurCmd = NULL; + iStackHeight = 0; } -- cgit v1.2.3