From 11554a9e5c92a98129b42598495abe7503d9208c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 8 Feb 2012 00:22:34 -0700 Subject: Everything seems to work now, goto, return, etc. --- src/gamestate.cpp | 89 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 36 deletions(-) (limited to 'src/gamestate.cpp') diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 38c6612..0434e6d 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -237,10 +237,10 @@ class Situation *GameState::getCurSituation() { return pGame->getSituation( sCurSituation ); } - +/* void GameState::run( class AstBranch *pAst ) { - pushScope( pAst ); +// pushScope( pAst ); run(); // if( lsLocal.getSize() == iDepth ) // delete lsLocal.peekPop(); @@ -256,7 +256,7 @@ void GameState::pushScope( class AstBranch *pAst ) // int iDepth = lsLocal.getSize(); sProg.push( pAst->getNodeList().begin() ); } - +*/ void GameState::init() { Variable vStart = pGame->getParam("start"); @@ -264,7 +264,6 @@ void GameState::init() throw Bu::ExceptionBase("game.start is not set to a situation name."); gotoSituation( vStart.getString() ); - run(); } void GameState::gotoSituation( const Bu::String &sName ) @@ -276,17 +275,23 @@ void GameState::gotoSituation( const Bu::String &sName ) Situation *pSit = pGame->getSituation( sName ); sCurSituation = sName; + sGoto.clear(); if( !hsSituation.has( sName ) ) { hsSituation.insert( sName, new Scope() ); - pSit->exec( *this, Situation::modeEnter ); pSit->exec( *this, Situation::modeSetup ); + if( !sGoto.isSet() ) + pSit->exec( *this, Situation::modeEnter ); } else { pSit->exec( *this, Situation::modeEnter ); } + if( sGoto.isSet() ) + { + gotoSituation( sGoto ); + } } void GameState::callFunction( const Bu::String &sName ) @@ -308,11 +313,17 @@ void GameState::execCommand( const Bu::String &sCmd ) return; } } + + if( sGoto.isSet() ) + gotoSituation( sGoto ); } void GameState::execOption( int idx ) { pGame->getSituation( sCurSituation )->execOption( *this, idx ); + + if( sGoto.isSet() ) + gotoSituation( sGoto ); } bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) @@ -523,14 +534,27 @@ Variable GameState::popDeref() return v; } -void GameState::run() +void GameState::run( const class AstBranch *pAst, bool bNewScope ) { - bEscape = false; + if( pAst->getType() != AstNode::tScope ) + throw Bu::ExceptionBase("Nope, nothing doing, you can't run a non-scope AstBranch."); + + run( pAst->getNodeList(), bNewScope ); +} + +void GameState::run( const AstBranch::NodeList &lNode, bool bNewScope ) +{ + ProgramStack sProg; + + if( bNewScope ) + lsLocal.push( new Scope() ); + sProg.push( lNode.begin() ); + while( !sProg.isEmpty() ) { ProgramCounter &i = sProg.peek(); - sio << "Stack: " << lStack << sio.nl; - sio << "exec: " << (*i)->getType() << sio.nl; +// sio << "Stack: " << lStack << sio.nl; +// sio << "exec: " << (*i)->getType() << sio.nl; switch( (*i)->getType() ) { // tLeaf @@ -712,10 +736,8 @@ void GameState::run() Variable x = popDeref(); if( x.getType() != Variable::tSituation ) throw Bu::ExceptionBase("You cannot goto anything but a situation."); - sProg.clear(); - gotoSituation( x.getString() ); - bEscape = true; - continue; + sGoto = x.getString(); + return; } break; @@ -729,11 +751,9 @@ void GameState::run() break; case AstNode::tReturn: - sProg.pop(); -// bReturnOnly = true; -// bEscape = true; -// return; - break; + if( bNewScope ) + lsLocal.pop(); + return; case AstNode::tAppend: { @@ -791,18 +811,10 @@ void GameState::run() break; case AstNode::tFuncCall: - sio << "Calling function now: " << sio.nl; - { - ProgramCounter c = i; - i++; callFunction( - dynamic_cast(*c) + dynamic_cast(*i) ->getValue().getString() ); - } - continue; - bReturnOnly = false; - bEscape = false; break; // tBranch @@ -838,7 +850,7 @@ void GameState::run() } } break; -/* + case AstNode::tForEach: { AstBranch::NodeList lFe = @@ -868,7 +880,8 @@ void GameState::run() getValue().getVariableRef(); } - parse( dynamic_cast(*iIn)->getNodeList() ); + run( dynamic_cast(*iIn), + false ); Variable vIn = popDeref(); const AstBranch::NodeList &rDo = @@ -883,7 +896,7 @@ void GameState::run() if( bUseKey ) setVariable( vrKey.sName, i.getKey(), vrKey.sid ); setVariable( vrValue.sName, i.getValue(), vrValue.sid ); - parse( rDo ); + run( rDo, false ); } } else if( vIn.getType() == Variable::tList ) @@ -897,7 +910,7 @@ void GameState::run() rList.begin(); i; i++ ) { setVariable( vrValue.sName, *i, vrValue.sid ); - parse( rDo ); + run( rDo, false ); } } } @@ -912,27 +925,31 @@ void GameState::run() for(;;) { - parse( dynamic_cast(*iTest)-> - getNodeList() ); + run( dynamic_cast(*iTest)-> + getNodeList(), false ); Variable v = popDeref(); if( v.getType() != Variable::tBool ) throw Bu::ExceptionBase("conditional did not evaluate to boolean."); if( !v.getBool() ) break; - parse( dynamic_cast(*iDo)-> - getNodeList() ); + run( dynamic_cast(*iDo)-> + getNodeList(), false ); } } break; - */ } + if( sGoto.isSet() ) + return; + ++sProg.peek(); while( !sProg.isEmpty() && !sProg.peek() ) { sProg.pop(); } } + if( bNewScope ) + lsLocal.pop(); } -- cgit v1.2.3