diff options
-rw-r--r-- | src/astfunction.cpp | 2 | ||||
-rw-r--r-- | src/command.cpp | 2 | ||||
-rw-r--r-- | src/gamestate.cpp | 82 | ||||
-rw-r--r-- | src/gamestate.h | 9 | ||||
-rw-r--r-- | src/parser.y | 4 | ||||
-rw-r--r-- | 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() | |||
16 | 16 | ||
17 | void AstFunction::call( class GameState &gState ) | 17 | void AstFunction::call( class GameState &gState ) |
18 | { | 18 | { |
19 | gState.parse( pAst ); | 19 | gState.pushScope( pAst ); |
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 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 ) | |||
90 | } | 90 | } |
91 | } | 91 | } |
92 | 92 | ||
93 | gState.parse( pAst ); | 93 | gState.run( pAst ); |
94 | } | 94 | } |
95 | 95 | ||
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() | |||
238 | return pGame->getSituation( sCurSituation ); | 238 | return pGame->getSituation( sCurSituation ); |
239 | } | 239 | } |
240 | 240 | ||
241 | void GameState::parse( class AstBranch *pAst ) | 241 | void GameState::run( class AstBranch *pAst ) |
242 | { | 242 | { |
243 | pushScope( pAst ); | ||
244 | run(); | ||
245 | // if( lsLocal.getSize() == iDepth ) | ||
246 | // delete lsLocal.peekPop(); | ||
247 | } | ||
248 | |||
249 | void GameState::pushScope( class AstBranch *pAst ) | ||
250 | { | ||
251 | sio << "--> pushScope: " << pAst->getType() << sio.nl; | ||
243 | if( pAst->getType() != AstNode::tScope ) | 252 | if( pAst->getType() != AstNode::tScope ) |
244 | throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch."); | 253 | throw Bu::ExceptionBase("Nope, nothing doing, you can't run a non-scope AstBranch."); |
245 | 254 | ||
246 | lsLocal.push( new Scope() ); | 255 | lsLocal.push( new Scope() ); |
247 | int iDepth = lsLocal.getSize(); | 256 | // int iDepth = lsLocal.getSize(); |
248 | parse( pAst->getNodeList() ); | 257 | sProg.push( pAst->getNodeList().begin() ); |
249 | if( lsLocal.getSize() == iDepth ) | ||
250 | delete lsLocal.peekPop(); | ||
251 | } | 258 | } |
252 | 259 | ||
253 | void GameState::init() | 260 | void GameState::init() |
@@ -257,6 +264,7 @@ void GameState::init() | |||
257 | 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."); |
258 | 265 | ||
259 | gotoSituation( vStart.getString() ); | 266 | gotoSituation( vStart.getString() ); |
267 | run(); | ||
260 | } | 268 | } |
261 | 269 | ||
262 | void GameState::gotoSituation( const Bu::String &sName ) | 270 | void GameState::gotoSituation( const Bu::String &sName ) |
@@ -271,14 +279,14 @@ void GameState::gotoSituation( const Bu::String &sName ) | |||
271 | if( !hsSituation.has( sName ) ) | 279 | if( !hsSituation.has( sName ) ) |
272 | { | 280 | { |
273 | hsSituation.insert( sName, new Scope() ); | 281 | hsSituation.insert( sName, new Scope() ); |
282 | pSit->exec( *this, Situation::modeEnter ); | ||
274 | pSit->exec( *this, Situation::modeSetup ); | 283 | pSit->exec( *this, Situation::modeSetup ); |
275 | } | 284 | } |
285 | else | ||
286 | { | ||
287 | pSit->exec( *this, Situation::modeEnter ); | ||
288 | } | ||
276 | 289 | ||
277 | // This is here in case you use a goto in a setup mode | ||
278 | if( bEscape ) | ||
279 | return; | ||
280 | |||
281 | pSit->exec( *this, Situation::modeEnter ); | ||
282 | } | 290 | } |
283 | 291 | ||
284 | void GameState::callFunction( const Bu::String &sName ) | 292 | void GameState::callFunction( const Bu::String &sName ) |
@@ -515,11 +523,12 @@ Variable GameState::popDeref() | |||
515 | return v; | 523 | return v; |
516 | } | 524 | } |
517 | 525 | ||
518 | void GameState::parse( const AstBranch::NodeList &lCode ) | 526 | void GameState::run() |
519 | { | 527 | { |
520 | bEscape = false; | 528 | bEscape = false; |
521 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) | 529 | while( !sProg.isEmpty() ) |
522 | { | 530 | { |
531 | ProgramCounter &i = sProg.peek(); | ||
523 | sio << "Stack: " << lStack << sio.nl; | 532 | sio << "Stack: " << lStack << sio.nl; |
524 | sio << "exec: " << (*i)->getType() << sio.nl; | 533 | sio << "exec: " << (*i)->getType() << sio.nl; |
525 | switch( (*i)->getType() ) | 534 | switch( (*i)->getType() ) |
@@ -703,11 +712,10 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
703 | Variable x = popDeref(); | 712 | Variable x = popDeref(); |
704 | if( x.getType() != Variable::tSituation ) | 713 | if( x.getType() != Variable::tSituation ) |
705 | throw Bu::ExceptionBase("You cannot goto anything but a situation."); | 714 | throw Bu::ExceptionBase("You cannot goto anything but a situation."); |
715 | sProg.clear(); | ||
706 | gotoSituation( x.getString() ); | 716 | gotoSituation( x.getString() ); |
707 | bEscape = true; | 717 | bEscape = true; |
708 | sio << "Initial return" << sio.nl; | 718 | continue; |
709 | push( Variable( Variable::tNull ) ); | ||
710 | return; | ||
711 | } | 719 | } |
712 | break; | 720 | break; |
713 | 721 | ||
@@ -721,9 +729,10 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
721 | break; | 729 | break; |
722 | 730 | ||
723 | case AstNode::tReturn: | 731 | case AstNode::tReturn: |
724 | bReturnOnly = true; | 732 | sProg.pop(); |
725 | bEscape = true; | 733 | // bReturnOnly = true; |
726 | return; | 734 | // bEscape = true; |
735 | // return; | ||
727 | break; | 736 | break; |
728 | 737 | ||
729 | case AstNode::tAppend: | 738 | case AstNode::tAppend: |
@@ -782,10 +791,16 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
782 | break; | 791 | break; |
783 | 792 | ||
784 | case AstNode::tFuncCall: | 793 | case AstNode::tFuncCall: |
794 | sio << "Calling function now: " << sio.nl; | ||
795 | { | ||
796 | ProgramCounter c = i; | ||
797 | i++; | ||
785 | callFunction( | 798 | callFunction( |
786 | dynamic_cast<const AstLeafLiteral *>(*i) | 799 | dynamic_cast<const AstLeafLiteral *>(*c) |
787 | ->getValue().getString() | 800 | ->getValue().getString() |
788 | ); | 801 | ); |
802 | } | ||
803 | continue; | ||
789 | bReturnOnly = false; | 804 | bReturnOnly = false; |
790 | bEscape = false; | 805 | bEscape = false; |
791 | break; | 806 | break; |
@@ -799,27 +814,31 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
799 | { | 814 | { |
800 | AstBranch::NodeList lIf = | 815 | AstBranch::NodeList lIf = |
801 | dynamic_cast<const AstBranch *>(*i)->getNodeList(); | 816 | dynamic_cast<const AstBranch *>(*i)->getNodeList(); |
802 | AstBranch::NodeList::const_iterator iIf = lIf.begin(); | ||
803 | parse( dynamic_cast<const AstBranch *>(*iIf)->getNodeList() ); | ||
804 | Variable v = popDeref(); | 817 | Variable v = popDeref(); |
805 | if( v.getType() != Variable::tBool ) | 818 | if( v.getType() != Variable::tBool ) |
806 | throw Bu::ExceptionBase("conditional did not evaluate to boolean."); | 819 | throw Bu::ExceptionBase("conditional did not evaluate to boolean."); |
807 | iIf++; | 820 | AstBranch::NodeList::const_iterator iIf = lIf.begin(); |
808 | if( v.getBool() ) | 821 | if( v.getBool() ) |
809 | { | 822 | { |
810 | parse( dynamic_cast<const AstBranch *>(*iIf)-> | 823 | i++; |
811 | getNodeList() ); | 824 | sProg.push( dynamic_cast<const AstBranch *>(*iIf)-> |
825 | getNodeList().begin() ); | ||
826 | continue; | ||
812 | } | 827 | } |
813 | else | 828 | else |
814 | { | 829 | { |
815 | iIf++; | 830 | iIf++; |
816 | if( iIf ) | 831 | if( iIf ) |
817 | parse( dynamic_cast<const AstBranch *>(*iIf)-> | 832 | { |
818 | getNodeList() ); | 833 | i++; |
834 | sProg.push( dynamic_cast<const AstBranch *>(*iIf)-> | ||
835 | getNodeList().begin() ); | ||
836 | continue; | ||
837 | } | ||
819 | } | 838 | } |
820 | } | 839 | } |
821 | break; | 840 | break; |
822 | 841 | /* | |
823 | case AstNode::tForEach: | 842 | case AstNode::tForEach: |
824 | { | 843 | { |
825 | AstBranch::NodeList lFe = | 844 | AstBranch::NodeList lFe = |
@@ -906,12 +925,13 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
906 | } | 925 | } |
907 | } | 926 | } |
908 | break; | 927 | break; |
928 | */ | ||
909 | } | 929 | } |
910 | 930 | ||
911 | if( bEscape || !bRunning ) | 931 | ++sProg.peek(); |
932 | while( !sProg.isEmpty() && !sProg.peek() ) | ||
912 | { | 933 | { |
913 | sio << "Returning from" << sio.nl; | 934 | sProg.pop(); |
914 | return; | ||
915 | } | 935 | } |
916 | } | 936 | } |
917 | } | 937 | } |
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: | |||
27 | 27 | ||
28 | Interface *getInterface() { return pIface; } | 28 | Interface *getInterface() { return pIface; } |
29 | 29 | ||
30 | void parse( class AstBranch *pAst ); | 30 | void run( class AstBranch *pAst ); |
31 | void pushScope( class AstBranch *pAst ); | ||
31 | 32 | ||
32 | void init(); | 33 | void init(); |
33 | 34 | ||
@@ -56,7 +57,7 @@ public: | |||
56 | class Situation *getCurSituation(); | 57 | class Situation *getCurSituation(); |
57 | 58 | ||
58 | private: | 59 | private: |
59 | void parse( const AstBranch::NodeList &lCode ); | 60 | void run(); |
60 | 61 | ||
61 | Gats::Object *scopeToGats( const Scope *pSrc ) const; | 62 | Gats::Object *scopeToGats( const Scope *pSrc ) const; |
62 | Gats::Object *variableToGats( const Variable &rVar ) const; | 63 | Gats::Object *variableToGats( const Variable &rVar ) const; |
@@ -80,6 +81,10 @@ private: | |||
80 | Bu::String sPrompt; | 81 | Bu::String sPrompt; |
81 | 82 | ||
82 | VariableList lStack; | 83 | VariableList lStack; |
84 | |||
85 | typedef AstBranch::NodeList::const_iterator ProgramCounter; | ||
86 | typedef Bu::List<ProgramCounter> ProgramStack; | ||
87 | ProgramStack sProg; | ||
83 | }; | 88 | }; |
84 | 89 | ||
85 | #endif | 90 | #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 ); } | |||
200 | ; | 200 | ; |
201 | 201 | ||
202 | ifbase: tokIf { | 202 | ifbase: tokIf { |
203 | bld.addNode( AstNode::tIf ); | ||
204 | bld.addNode( AstNode::tScope ); | ||
205 | } expr { | 203 | } expr { |
206 | bld.closeNode(); | 204 | bld.addNode( AstNode::tIf ); |
207 | bld.addNode( AstNode::tScope ); | 205 | bld.addNode( AstNode::tScope ); |
208 | } tokThen '{' cmpltExprList '}' { | 206 | } tokThen '{' cmpltExprList '}' { |
209 | bld.closeNode(); | 207 | 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 ) | |||
39 | { | 39 | { |
40 | case modeSetup: | 40 | case modeSetup: |
41 | if( pAstSetup ) | 41 | if( pAstSetup ) |
42 | gState.parse( pAstSetup ); | 42 | gState.pushScope( pAstSetup ); |
43 | break; | 43 | break; |
44 | 44 | ||
45 | case modeEnter: | 45 | case modeEnter: |
46 | if( pAstEnter ) | 46 | if( pAstEnter ) |
47 | gState.parse( pAstEnter ); | 47 | gState.pushScope( pAstEnter ); |
48 | break; | 48 | break; |
49 | } | 49 | } |
50 | } | 50 | } |