summaryrefslogtreecommitdiff
path: root/src/gamestate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gamestate.cpp')
-rw-r--r--src/gamestate.cpp82
1 files changed, 51 insertions, 31 deletions
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
241void GameState::parse( class AstBranch *pAst ) 241void GameState::run( class AstBranch *pAst )
242{ 242{
243 pushScope( pAst );
244 run();
245// if( lsLocal.getSize() == iDepth )
246// delete lsLocal.peekPop();
247}
248
249void 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
253void GameState::init() 260void 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
262void GameState::gotoSituation( const Bu::String &sName ) 270void 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
284void GameState::callFunction( const Bu::String &sName ) 292void GameState::callFunction( const Bu::String &sName )
@@ -515,11 +523,12 @@ Variable GameState::popDeref()
515 return v; 523 return v;
516} 524}
517 525
518void GameState::parse( const AstBranch::NodeList &lCode ) 526void 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}