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/astfunction.cpp | 2 +- src/command.cpp | 2 +- src/game.cpp | 9 +++++- src/gamebuilder.cpp | 2 +- src/gamestate.cpp | 89 +++++++++++++++++++++++++++++++---------------------- src/gamestate.h | 10 +++--- src/situation.cpp | 4 +-- 7 files changed, 71 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/astfunction.cpp b/src/astfunction.cpp index 894a3a1..e27e585 100644 --- a/src/astfunction.cpp +++ b/src/astfunction.cpp @@ -16,7 +16,7 @@ AstFunction::~AstFunction() void AstFunction::call( class GameState &gState ) { - gState.pushScope( pAst ); + gState.run( pAst, true ); } void AstFunction::addParam( const Bu::String &sName ) diff --git a/src/command.cpp b/src/command.cpp index a114246..dc43c24 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -90,6 +90,6 @@ void Command::exec( class GameState &gState, const Bu::StringList &lCmd ) } } - gState.run( pAst ); + gState.run( pAst, true ); } diff --git a/src/game.cpp b/src/game.cpp index 753f83b..19a31f7 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -48,7 +48,14 @@ Variable Game::getParam( const Bu::String &sName ) const Situation *Game::getSituation( const Bu::String &sName ) { - return hSituation.get( sName ); + try + { + return hSituation.get( sName ); + } + catch( Bu::HashException &e ) + { + throw Bu::ExceptionBase(("No such situation <<" + sName + ">>").getStr()); + } } bool Game::execCommand( class GameState &gState, const Bu::StringList &lCmd ) diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index ac1174a..e8ecf63 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -123,7 +123,7 @@ void GameBuilder::beginSituationMode( Situation::Mode m ) void GameBuilder::closeSituationMode() { - sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; +// sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; pCurSit->setAst( pCurRoot, eCurSitMode ); pCurRoot = pCurNode = NULL; } 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(); } diff --git a/src/gamestate.h b/src/gamestate.h index 115892a..089273b 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -27,8 +27,9 @@ public: Interface *getInterface() { return pIface; } - void run( class AstBranch *pAst ); - void pushScope( class AstBranch *pAst ); + void run( const class AstBranch *pAst, bool bNewScope ); + void run( const AstBranch::NodeList &lNode, bool bNewScope ); + //void pushScope( class AstBranch *pAst ); void init(); @@ -57,8 +58,6 @@ public: class Situation *getCurSituation(); private: - void run(); - Gats::Object *scopeToGats( const Scope *pSrc ) const; Gats::Object *variableToGats( const Variable &rVar ) const; void gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const; @@ -84,7 +83,8 @@ private: typedef AstBranch::NodeList::const_iterator ProgramCounter; typedef Bu::List ProgramStack; - ProgramStack sProg; + + Bu::String sGoto; }; #endif diff --git a/src/situation.cpp b/src/situation.cpp index 74875ac..9b8b304 100644 --- a/src/situation.cpp +++ b/src/situation.cpp @@ -39,12 +39,12 @@ void Situation::exec( class GameState &gState, Situation::Mode m ) { case modeSetup: if( pAstSetup ) - gState.pushScope( pAstSetup ); + gState.run( pAstSetup, true ); break; case modeEnter: if( pAstEnter ) - gState.pushScope( pAstEnter ); + gState.run( pAstEnter, true ); break; } } -- cgit v1.2.3