From 162ccd918698f53ef9ff7ba80091969d93aa789d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Dec 2011 14:49:02 -0700 Subject: Situation code actually processes now. Most of the AstNode types are unhandled yet. --- src/game.cpp | 10 ++++++++++ src/game.h | 2 ++ src/gamebuilder.h | 2 ++ src/gamestate.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/gamestate.h | 7 +++++++ src/main.cpp | 7 +++++++ test.stage | 3 ++- 7 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index c2bbce4..6c980ee 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -24,6 +24,16 @@ Function *Game::getFunction( const Bu::String &sName ) return hFunction.get( sName ); } +Variable Game::getParam( const Bu::String &sName ) const +{ + return hGlobalParam.get( sName ); +} + +Situation *Game::getSituation( const Bu::String &sName ) +{ + return hSituation.get( sName ); +} + void Game::addFunction( Function *pFunc ) { hFunction.insert( pFunc->getName(), pFunc ); diff --git a/src/game.h b/src/game.h index 4abb4da..675721c 100644 --- a/src/game.h +++ b/src/game.h @@ -17,6 +17,8 @@ public: virtual ~Game(); Function *getFunction( const Bu::String &sName ); + Variable getParam( const Bu::String &sName ) const; + Situation *getSituation( const Bu::String &sName ); private: void addFunction( Function *pFunc ); diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 9a0493f..50dcbb7 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -13,6 +13,8 @@ public: GameBuilder(); virtual ~GameBuilder(); + class Game *getGame() { return pGame; } + void setLiteral( const Variable &v ); void setGameParam( const Bu::String &sName ); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 25b53b8..cd6414c 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -24,6 +24,28 @@ void GameState::parse( class AstBranch *pAst ) delete lsLocal.peekPop(); } +void GameState::init() +{ + Variable vStart = pGame->getParam("start"); + if( vStart.getType() != Variable::tSituation ) + throw Bu::ExceptionBase("game.start is not set to a situation name."); + + gotoSituation( vStart.getString() ); +} + +void GameState::gotoSituation( const Bu::String &sName ) +{ + Situation *pSit = pGame->getSituation( sName ); + sCurSituation = sName; + if( !hsSituation.has( sName ) ) + { + hsSituation.insert( sName, new Scope() ); + pSit->exec( *this, Situation::modeSetup ); + } + + pSit->exec( *this, Situation::modeEnter ); +} + void GameState::callFunction( const Bu::String &sName ) { pGame->getFunction( sName )->call( *this ); @@ -74,6 +96,21 @@ void GameState::setVariable( const Bu::String &sName, const Variable &v, throw Bu::ExceptionBase("Really bad scopeid passed into setVariable"); } +Variable GameState::deref( const Variable &src ) +{ + if( src.getType() == Variable::tVariable ) + return getVariable( src.getString() ); + return src; +} + +Variable GameState::popDeref() +{ + Variable v = lStack.peekPop(); + if( v.getType() == Variable::tVariable ) + return getVariable( v.getString() ); + return v; +} + void GameState::parse( const AstBranch::NodeList &lCode ) { for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) @@ -88,9 +125,23 @@ void GameState::parse( const AstBranch::NodeList &lCode ) case AstNode::tCompGtEq: case AstNode::tCompLtEq: case AstNode::tStore: + { + Variable y = popDeref(); + Variable dst = pop(); + setVariable( dst.getString(), y ); + } + break; + case AstNode::tAnd: case AstNode::tOr: case AstNode::tPlus: + { + Variable y = popDeref(); + Variable x = popDeref(); + lStack.push( x + y ); + } + break; + case AstNode::tMinus: case AstNode::tDivide: case AstNode::tMultiply: @@ -103,9 +154,6 @@ void GameState::parse( const AstBranch::NodeList &lCode ) // tLeafLiteral case AstNode::tVarName: - lStack.push( dynamic_cast(*i)->getValue() ); - break; - case AstNode::tLiteral: lStack.push( dynamic_cast(*i)->getValue() ); break; diff --git a/src/gamestate.h b/src/gamestate.h index 7a8f81a..83d8594 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -15,7 +15,12 @@ public: void parse( class AstBranch *pAst ); + void init(); + + void gotoSituation( const Bu::String &sName ); + Variable pop() { return lStack.peekPop(); } + Variable popDeref(); void push( const Variable &v ) { lStack.push( v ); } void callFunction( const Bu::String &sName ); @@ -31,6 +36,8 @@ public: Variable getVariable( const Bu::String &sName, ScopeId id=sidLocal ); void setVariable( const Bu::String &sName, const Variable &v, ScopeId id=sidLocal ); + Variable deref( const Variable &src ); + private: void parse( const AstBranch::NodeList &lCode ); diff --git a/src/main.cpp b/src/main.cpp index 609802c..8fe5247 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,6 @@ #include "gamebuilder.h" +#include "game.h" +#include "gamestate.h" #include "parser.tab.h" typedef void *yyscan_t; @@ -23,6 +25,11 @@ int main( int argc, char *argv[] ) fclose( in ); + Game *pGame = bld.getGame(); + + GameState gs( pGame ); + gs.init(); + return 0; } diff --git a/test.stage b/test.stage index 87e4756..04d959b 100644 --- a/test.stage +++ b/test.stage @@ -17,7 +17,8 @@ situation <> { setup { - display("Hello"); + name = "Bob"; + display("Hello " + name + ''' and then some'''); } enter -- cgit v1.2.3