diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-02-08 00:22:34 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-02-08 00:22:34 -0700 |
commit | 11554a9e5c92a98129b42598495abe7503d9208c (patch) | |
tree | 91182c1737204d60bcbbc038f2743e60d09faedb | |
parent | 3a1a20a9bc7008abb7de241c44cc0d7a3d4a77d9 (diff) | |
download | stage-11554a9e5c92a98129b42598495abe7503d9208c.tar.gz stage-11554a9e5c92a98129b42598495abe7503d9208c.tar.bz2 stage-11554a9e5c92a98129b42598495abe7503d9208c.tar.xz stage-11554a9e5c92a98129b42598495abe7503d9208c.zip |
Everything seems to work now, goto, return, etc.
-rw-r--r-- | src/astfunction.cpp | 2 | ||||
-rw-r--r-- | src/command.cpp | 2 | ||||
-rw-r--r-- | src/game.cpp | 9 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 2 | ||||
-rw-r--r-- | src/gamestate.cpp | 89 | ||||
-rw-r--r-- | src/gamestate.h | 10 | ||||
-rw-r--r-- | src/situation.cpp | 4 |
7 files changed, 71 insertions, 47 deletions
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() | |||
16 | 16 | ||
17 | void AstFunction::call( class GameState &gState ) | 17 | void AstFunction::call( class GameState &gState ) |
18 | { | 18 | { |
19 | gState.pushScope( pAst ); | 19 | gState.run( pAst, true ); |
20 | } | 20 | } |
21 | 21 | ||
22 | void AstFunction::addParam( const Bu::String &sName ) | 22 | 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 ) | |||
90 | } | 90 | } |
91 | } | 91 | } |
92 | 92 | ||
93 | gState.run( pAst ); | 93 | gState.run( pAst, true ); |
94 | } | 94 | } |
95 | 95 | ||
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 | |||
48 | 48 | ||
49 | Situation *Game::getSituation( const Bu::String &sName ) | 49 | Situation *Game::getSituation( const Bu::String &sName ) |
50 | { | 50 | { |
51 | return hSituation.get( sName ); | 51 | try |
52 | { | ||
53 | return hSituation.get( sName ); | ||
54 | } | ||
55 | catch( Bu::HashException &e ) | ||
56 | { | ||
57 | throw Bu::ExceptionBase(("No such situation <<" + sName + ">>").getStr()); | ||
58 | } | ||
52 | } | 59 | } |
53 | 60 | ||
54 | bool Game::execCommand( class GameState &gState, const Bu::StringList &lCmd ) | 61 | 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 ) | |||
123 | 123 | ||
124 | void GameBuilder::closeSituationMode() | 124 | void GameBuilder::closeSituationMode() |
125 | { | 125 | { |
126 | sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; | 126 | // sio << "Set situation <<" << pCurSit->getName() << ">> mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; |
127 | pCurSit->setAst( pCurRoot, eCurSitMode ); | 127 | pCurSit->setAst( pCurRoot, eCurSitMode ); |
128 | pCurRoot = pCurNode = NULL; | 128 | pCurRoot = pCurNode = NULL; |
129 | } | 129 | } |
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() | |||
237 | { | 237 | { |
238 | return pGame->getSituation( sCurSituation ); | 238 | return pGame->getSituation( sCurSituation ); |
239 | } | 239 | } |
240 | 240 | /* | |
241 | void GameState::run( class AstBranch *pAst ) | 241 | void GameState::run( class AstBranch *pAst ) |
242 | { | 242 | { |
243 | pushScope( pAst ); | 243 | // pushScope( pAst ); |
244 | run(); | 244 | run(); |
245 | // if( lsLocal.getSize() == iDepth ) | 245 | // if( lsLocal.getSize() == iDepth ) |
246 | // delete lsLocal.peekPop(); | 246 | // delete lsLocal.peekPop(); |
@@ -256,7 +256,7 @@ void GameState::pushScope( class AstBranch *pAst ) | |||
256 | // int iDepth = lsLocal.getSize(); | 256 | // int iDepth = lsLocal.getSize(); |
257 | sProg.push( pAst->getNodeList().begin() ); | 257 | sProg.push( pAst->getNodeList().begin() ); |
258 | } | 258 | } |
259 | 259 | */ | |
260 | void GameState::init() | 260 | void GameState::init() |
261 | { | 261 | { |
262 | Variable vStart = pGame->getParam("start"); | 262 | Variable vStart = pGame->getParam("start"); |
@@ -264,7 +264,6 @@ void GameState::init() | |||
264 | throw Bu::ExceptionBase("game.start is not set to a situation name."); | 264 | throw Bu::ExceptionBase("game.start is not set to a situation name."); |
265 | 265 | ||
266 | gotoSituation( vStart.getString() ); | 266 | gotoSituation( vStart.getString() ); |
267 | run(); | ||
268 | } | 267 | } |
269 | 268 | ||
270 | void GameState::gotoSituation( const Bu::String &sName ) | 269 | void GameState::gotoSituation( const Bu::String &sName ) |
@@ -276,17 +275,23 @@ void GameState::gotoSituation( const Bu::String &sName ) | |||
276 | 275 | ||
277 | Situation *pSit = pGame->getSituation( sName ); | 276 | Situation *pSit = pGame->getSituation( sName ); |
278 | sCurSituation = sName; | 277 | sCurSituation = sName; |
278 | sGoto.clear(); | ||
279 | if( !hsSituation.has( sName ) ) | 279 | if( !hsSituation.has( sName ) ) |
280 | { | 280 | { |
281 | hsSituation.insert( sName, new Scope() ); | 281 | hsSituation.insert( sName, new Scope() ); |
282 | pSit->exec( *this, Situation::modeEnter ); | ||
283 | pSit->exec( *this, Situation::modeSetup ); | 282 | pSit->exec( *this, Situation::modeSetup ); |
283 | if( !sGoto.isSet() ) | ||
284 | pSit->exec( *this, Situation::modeEnter ); | ||
284 | } | 285 | } |
285 | else | 286 | else |
286 | { | 287 | { |
287 | pSit->exec( *this, Situation::modeEnter ); | 288 | pSit->exec( *this, Situation::modeEnter ); |
288 | } | 289 | } |
289 | 290 | ||
291 | if( sGoto.isSet() ) | ||
292 | { | ||
293 | gotoSituation( sGoto ); | ||
294 | } | ||
290 | } | 295 | } |
291 | 296 | ||
292 | void GameState::callFunction( const Bu::String &sName ) | 297 | void GameState::callFunction( const Bu::String &sName ) |
@@ -308,11 +313,17 @@ void GameState::execCommand( const Bu::String &sCmd ) | |||
308 | return; | 313 | return; |
309 | } | 314 | } |
310 | } | 315 | } |
316 | |||
317 | if( sGoto.isSet() ) | ||
318 | gotoSituation( sGoto ); | ||
311 | } | 319 | } |
312 | 320 | ||
313 | void GameState::execOption( int idx ) | 321 | void GameState::execOption( int idx ) |
314 | { | 322 | { |
315 | pGame->getSituation( sCurSituation )->execOption( *this, idx ); | 323 | pGame->getSituation( sCurSituation )->execOption( *this, idx ); |
324 | |||
325 | if( sGoto.isSet() ) | ||
326 | gotoSituation( sGoto ); | ||
316 | } | 327 | } |
317 | 328 | ||
318 | bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) | 329 | bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) |
@@ -523,14 +534,27 @@ Variable GameState::popDeref() | |||
523 | return v; | 534 | return v; |
524 | } | 535 | } |
525 | 536 | ||
526 | void GameState::run() | 537 | void GameState::run( const class AstBranch *pAst, bool bNewScope ) |
527 | { | 538 | { |
528 | bEscape = false; | 539 | if( pAst->getType() != AstNode::tScope ) |
540 | throw Bu::ExceptionBase("Nope, nothing doing, you can't run a non-scope AstBranch."); | ||
541 | |||
542 | run( pAst->getNodeList(), bNewScope ); | ||
543 | } | ||
544 | |||
545 | void GameState::run( const AstBranch::NodeList &lNode, bool bNewScope ) | ||
546 | { | ||
547 | ProgramStack sProg; | ||
548 | |||
549 | if( bNewScope ) | ||
550 | lsLocal.push( new Scope() ); | ||
551 | sProg.push( lNode.begin() ); | ||
552 | |||
529 | while( !sProg.isEmpty() ) | 553 | while( !sProg.isEmpty() ) |
530 | { | 554 | { |
531 | ProgramCounter &i = sProg.peek(); | 555 | ProgramCounter &i = sProg.peek(); |
532 | sio << "Stack: " << lStack << sio.nl; | 556 | // sio << "Stack: " << lStack << sio.nl; |
533 | sio << "exec: " << (*i)->getType() << sio.nl; | 557 | // sio << "exec: " << (*i)->getType() << sio.nl; |
534 | switch( (*i)->getType() ) | 558 | switch( (*i)->getType() ) |
535 | { | 559 | { |
536 | // tLeaf | 560 | // tLeaf |
@@ -712,10 +736,8 @@ void GameState::run() | |||
712 | Variable x = popDeref(); | 736 | Variable x = popDeref(); |
713 | if( x.getType() != Variable::tSituation ) | 737 | if( x.getType() != Variable::tSituation ) |
714 | throw Bu::ExceptionBase("You cannot goto anything but a situation."); | 738 | throw Bu::ExceptionBase("You cannot goto anything but a situation."); |
715 | sProg.clear(); | 739 | sGoto = x.getString(); |
716 | gotoSituation( x.getString() ); | 740 | return; |
717 | bEscape = true; | ||
718 | continue; | ||
719 | } | 741 | } |
720 | break; | 742 | break; |
721 | 743 | ||
@@ -729,11 +751,9 @@ void GameState::run() | |||
729 | break; | 751 | break; |
730 | 752 | ||
731 | case AstNode::tReturn: | 753 | case AstNode::tReturn: |
732 | sProg.pop(); | 754 | if( bNewScope ) |
733 | // bReturnOnly = true; | 755 | lsLocal.pop(); |
734 | // bEscape = true; | 756 | return; |
735 | // return; | ||
736 | break; | ||
737 | 757 | ||
738 | case AstNode::tAppend: | 758 | case AstNode::tAppend: |
739 | { | 759 | { |
@@ -791,18 +811,10 @@ void GameState::run() | |||
791 | break; | 811 | break; |
792 | 812 | ||
793 | case AstNode::tFuncCall: | 813 | case AstNode::tFuncCall: |
794 | sio << "Calling function now: " << sio.nl; | ||
795 | { | ||
796 | ProgramCounter c = i; | ||
797 | i++; | ||
798 | callFunction( | 814 | callFunction( |
799 | dynamic_cast<const AstLeafLiteral *>(*c) | 815 | dynamic_cast<const AstLeafLiteral *>(*i) |
800 | ->getValue().getString() | 816 | ->getValue().getString() |
801 | ); | 817 | ); |
802 | } | ||
803 | continue; | ||
804 | bReturnOnly = false; | ||
805 | bEscape = false; | ||
806 | break; | 818 | break; |
807 | 819 | ||
808 | // tBranch | 820 | // tBranch |
@@ -838,7 +850,7 @@ void GameState::run() | |||
838 | } | 850 | } |
839 | } | 851 | } |
840 | break; | 852 | break; |
841 | /* | 853 | |
842 | case AstNode::tForEach: | 854 | case AstNode::tForEach: |
843 | { | 855 | { |
844 | AstBranch::NodeList lFe = | 856 | AstBranch::NodeList lFe = |
@@ -868,7 +880,8 @@ void GameState::run() | |||
868 | getValue().getVariableRef(); | 880 | getValue().getVariableRef(); |
869 | } | 881 | } |
870 | 882 | ||
871 | parse( dynamic_cast<const AstBranch *>(*iIn)->getNodeList() ); | 883 | run( dynamic_cast<const AstBranch *>(*iIn), |
884 | false ); | ||
872 | Variable vIn = popDeref(); | 885 | Variable vIn = popDeref(); |
873 | 886 | ||
874 | const AstBranch::NodeList &rDo = | 887 | const AstBranch::NodeList &rDo = |
@@ -883,7 +896,7 @@ void GameState::run() | |||
883 | if( bUseKey ) | 896 | if( bUseKey ) |
884 | setVariable( vrKey.sName, i.getKey(), vrKey.sid ); | 897 | setVariable( vrKey.sName, i.getKey(), vrKey.sid ); |
885 | setVariable( vrValue.sName, i.getValue(), vrValue.sid ); | 898 | setVariable( vrValue.sName, i.getValue(), vrValue.sid ); |
886 | parse( rDo ); | 899 | run( rDo, false ); |
887 | } | 900 | } |
888 | } | 901 | } |
889 | else if( vIn.getType() == Variable::tList ) | 902 | else if( vIn.getType() == Variable::tList ) |
@@ -897,7 +910,7 @@ void GameState::run() | |||
897 | rList.begin(); i; i++ ) | 910 | rList.begin(); i; i++ ) |
898 | { | 911 | { |
899 | setVariable( vrValue.sName, *i, vrValue.sid ); | 912 | setVariable( vrValue.sName, *i, vrValue.sid ); |
900 | parse( rDo ); | 913 | run( rDo, false ); |
901 | } | 914 | } |
902 | } | 915 | } |
903 | } | 916 | } |
@@ -912,27 +925,31 @@ void GameState::run() | |||
912 | 925 | ||
913 | for(;;) | 926 | for(;;) |
914 | { | 927 | { |
915 | parse( dynamic_cast<const AstBranch *>(*iTest)-> | 928 | run( dynamic_cast<const AstBranch *>(*iTest)-> |
916 | getNodeList() ); | 929 | getNodeList(), false ); |
917 | Variable v = popDeref(); | 930 | Variable v = popDeref(); |
918 | if( v.getType() != Variable::tBool ) | 931 | if( v.getType() != Variable::tBool ) |
919 | throw Bu::ExceptionBase("conditional did not evaluate to boolean."); | 932 | throw Bu::ExceptionBase("conditional did not evaluate to boolean."); |
920 | if( !v.getBool() ) | 933 | if( !v.getBool() ) |
921 | break; | 934 | break; |
922 | 935 | ||
923 | parse( dynamic_cast<const AstBranch *>(*iDo)-> | 936 | run( dynamic_cast<const AstBranch *>(*iDo)-> |
924 | getNodeList() ); | 937 | getNodeList(), false ); |
925 | } | 938 | } |
926 | } | 939 | } |
927 | break; | 940 | break; |
928 | */ | ||
929 | } | 941 | } |
930 | 942 | ||
943 | if( sGoto.isSet() ) | ||
944 | return; | ||
945 | |||
931 | ++sProg.peek(); | 946 | ++sProg.peek(); |
932 | while( !sProg.isEmpty() && !sProg.peek() ) | 947 | while( !sProg.isEmpty() && !sProg.peek() ) |
933 | { | 948 | { |
934 | sProg.pop(); | 949 | sProg.pop(); |
935 | } | 950 | } |
936 | } | 951 | } |
952 | if( bNewScope ) | ||
953 | lsLocal.pop(); | ||
937 | } | 954 | } |
938 | 955 | ||
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: | |||
27 | 27 | ||
28 | Interface *getInterface() { return pIface; } | 28 | Interface *getInterface() { return pIface; } |
29 | 29 | ||
30 | void run( class AstBranch *pAst ); | 30 | void run( const class AstBranch *pAst, bool bNewScope ); |
31 | void pushScope( class AstBranch *pAst ); | 31 | void run( const AstBranch::NodeList &lNode, bool bNewScope ); |
32 | //void pushScope( class AstBranch *pAst ); | ||
32 | 33 | ||
33 | void init(); | 34 | void init(); |
34 | 35 | ||
@@ -57,8 +58,6 @@ public: | |||
57 | class Situation *getCurSituation(); | 58 | class Situation *getCurSituation(); |
58 | 59 | ||
59 | private: | 60 | private: |
60 | void run(); | ||
61 | |||
62 | Gats::Object *scopeToGats( const Scope *pSrc ) const; | 61 | Gats::Object *scopeToGats( const Scope *pSrc ) const; |
63 | Gats::Object *variableToGats( const Variable &rVar ) const; | 62 | Gats::Object *variableToGats( const Variable &rVar ) const; |
64 | void gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const; | 63 | void gatsToScope( Gats::Dictionary *pRoot, Scope *pSrc ) const; |
@@ -84,7 +83,8 @@ private: | |||
84 | 83 | ||
85 | typedef AstBranch::NodeList::const_iterator ProgramCounter; | 84 | typedef AstBranch::NodeList::const_iterator ProgramCounter; |
86 | typedef Bu::List<ProgramCounter> ProgramStack; | 85 | typedef Bu::List<ProgramCounter> ProgramStack; |
87 | ProgramStack sProg; | 86 | |
87 | Bu::String sGoto; | ||
88 | }; | 88 | }; |
89 | 89 | ||
90 | #endif | 90 | #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 ) | |||
39 | { | 39 | { |
40 | case modeSetup: | 40 | case modeSetup: |
41 | if( pAstSetup ) | 41 | if( pAstSetup ) |
42 | gState.pushScope( pAstSetup ); | 42 | gState.run( pAstSetup, true ); |
43 | break; | 43 | break; |
44 | 44 | ||
45 | case modeEnter: | 45 | case modeEnter: |
46 | if( pAstEnter ) | 46 | if( pAstEnter ) |
47 | gState.pushScope( pAstEnter ); | 47 | gState.run( pAstEnter, true ); |
48 | break; | 48 | break; |
49 | } | 49 | } |
50 | } | 50 | } |