From 3a1a20a9bc7008abb7de241c44cc0d7a3d4a77d9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 7 Feb 2012 23:38:14 -0700 Subject: All good except loops & function returns. --- src/astfunction.cpp | 2 +- src/command.cpp | 2 +- src/gamestate.cpp | 82 +++++++++++++++++++++++++++++++++-------------------- src/gamestate.h | 9 ++++-- src/parser.y | 4 +-- src/situation.cpp | 4 +-- 6 files changed, 63 insertions(+), 40 deletions(-) diff --git a/src/astfunction.cpp b/src/astfunction.cpp index c37a70d..894a3a1 100644 --- a/src/astfunction.cpp +++ b/src/astfunction.cpp @@ -16,7 +16,7 @@ AstFunction::~AstFunction() void AstFunction::call( class GameState &gState ) { - gState.parse( pAst ); + gState.pushScope( pAst ); } void AstFunction::addParam( const Bu::String &sName ) diff --git a/src/command.cpp b/src/command.cpp index 2def02f..a114246 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -90,6 +90,6 @@ void Command::exec( class GameState &gState, const Bu::StringList &lCmd ) } } - gState.parse( pAst ); + gState.run( pAst ); } diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 99cc749..38c6612 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -238,16 +238,23 @@ class Situation *GameState::getCurSituation() return pGame->getSituation( sCurSituation ); } -void GameState::parse( class AstBranch *pAst ) +void GameState::run( class AstBranch *pAst ) { + pushScope( pAst ); + run(); +// if( lsLocal.getSize() == iDepth ) +// delete lsLocal.peekPop(); +} + +void GameState::pushScope( class AstBranch *pAst ) +{ + sio << "--> pushScope: " << pAst->getType() << sio.nl; if( pAst->getType() != AstNode::tScope ) - throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch."); + throw Bu::ExceptionBase("Nope, nothing doing, you can't run a non-scope AstBranch."); lsLocal.push( new Scope() ); - int iDepth = lsLocal.getSize(); - parse( pAst->getNodeList() ); - if( lsLocal.getSize() == iDepth ) - delete lsLocal.peekPop(); +// int iDepth = lsLocal.getSize(); + sProg.push( pAst->getNodeList().begin() ); } void GameState::init() @@ -257,6 +264,7 @@ 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 ) @@ -271,14 +279,14 @@ void GameState::gotoSituation( const Bu::String &sName ) if( !hsSituation.has( sName ) ) { hsSituation.insert( sName, new Scope() ); + pSit->exec( *this, Situation::modeEnter ); pSit->exec( *this, Situation::modeSetup ); } + else + { + pSit->exec( *this, Situation::modeEnter ); + } - // This is here in case you use a goto in a setup mode - if( bEscape ) - return; - - pSit->exec( *this, Situation::modeEnter ); } void GameState::callFunction( const Bu::String &sName ) @@ -515,11 +523,12 @@ Variable GameState::popDeref() return v; } -void GameState::parse( const AstBranch::NodeList &lCode ) +void GameState::run() { bEscape = false; - for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) + while( !sProg.isEmpty() ) { + ProgramCounter &i = sProg.peek(); sio << "Stack: " << lStack << sio.nl; sio << "exec: " << (*i)->getType() << sio.nl; switch( (*i)->getType() ) @@ -703,11 +712,10 @@ void GameState::parse( const AstBranch::NodeList &lCode ) 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; - sio << "Initial return" << sio.nl; - push( Variable( Variable::tNull ) ); - return; + continue; } break; @@ -721,9 +729,10 @@ void GameState::parse( const AstBranch::NodeList &lCode ) break; case AstNode::tReturn: - bReturnOnly = true; - bEscape = true; - return; + sProg.pop(); +// bReturnOnly = true; +// bEscape = true; +// return; break; case AstNode::tAppend: @@ -782,10 +791,16 @@ void GameState::parse( const AstBranch::NodeList &lCode ) break; case AstNode::tFuncCall: + sio << "Calling function now: " << sio.nl; + { + ProgramCounter c = i; + i++; callFunction( - dynamic_cast(*i) + dynamic_cast(*c) ->getValue().getString() ); + } + continue; bReturnOnly = false; bEscape = false; break; @@ -799,27 +814,31 @@ void GameState::parse( const AstBranch::NodeList &lCode ) { AstBranch::NodeList lIf = dynamic_cast(*i)->getNodeList(); - AstBranch::NodeList::const_iterator iIf = lIf.begin(); - parse( dynamic_cast(*iIf)->getNodeList() ); Variable v = popDeref(); if( v.getType() != Variable::tBool ) throw Bu::ExceptionBase("conditional did not evaluate to boolean."); - iIf++; + AstBranch::NodeList::const_iterator iIf = lIf.begin(); if( v.getBool() ) { - parse( dynamic_cast(*iIf)-> - getNodeList() ); + i++; + sProg.push( dynamic_cast(*iIf)-> + getNodeList().begin() ); + continue; } else { iIf++; if( iIf ) - parse( dynamic_cast(*iIf)-> - getNodeList() ); + { + i++; + sProg.push( dynamic_cast(*iIf)-> + getNodeList().begin() ); + continue; + } } } break; - +/* case AstNode::tForEach: { AstBranch::NodeList lFe = @@ -906,12 +925,13 @@ void GameState::parse( const AstBranch::NodeList &lCode ) } } break; + */ } - if( bEscape || !bRunning ) + ++sProg.peek(); + while( !sProg.isEmpty() && !sProg.peek() ) { - sio << "Returning from" << sio.nl; - return; + sProg.pop(); } } } diff --git a/src/gamestate.h b/src/gamestate.h index 4209ebf..115892a 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -27,7 +27,8 @@ public: Interface *getInterface() { return pIface; } - void parse( class AstBranch *pAst ); + void run( class AstBranch *pAst ); + void pushScope( class AstBranch *pAst ); void init(); @@ -56,7 +57,7 @@ public: class Situation *getCurSituation(); private: - void parse( const AstBranch::NodeList &lCode ); + void run(); Gats::Object *scopeToGats( const Scope *pSrc ) const; Gats::Object *variableToGats( const Variable &rVar ) const; @@ -80,6 +81,10 @@ private: Bu::String sPrompt; VariableList lStack; + + typedef AstBranch::NodeList::const_iterator ProgramCounter; + typedef Bu::List ProgramStack; + ProgramStack sProg; }; #endif diff --git a/src/parser.y b/src/parser.y index 383ab28..47c0159 100644 --- a/src/parser.y +++ b/src/parser.y @@ -200,10 +200,8 @@ forIterator: tokIdent { bld.addVarRef( *($1), sidLocal ); } ; ifbase: tokIf { - bld.addNode( AstNode::tIf ); - bld.addNode( AstNode::tScope ); } expr { - bld.closeNode(); + bld.addNode( AstNode::tIf ); bld.addNode( AstNode::tScope ); } tokThen '{' cmpltExprList '}' { bld.closeNode(); diff --git a/src/situation.cpp b/src/situation.cpp index 1d3a853..74875ac 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.parse( pAstSetup ); + gState.pushScope( pAstSetup ); break; case modeEnter: if( pAstEnter ) - gState.parse( pAstEnter ); + gState.pushScope( pAstEnter ); break; } } -- cgit v1.2.3