summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-29 14:49:02 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-29 14:49:02 -0700
commit162ccd918698f53ef9ff7ba80091969d93aa789d (patch)
treeb01d84a31a5fec69a07cdbef75fa777529dfacc7
parent533310f646f1b1a00250a361f627967c420f1eef (diff)
downloadstage-162ccd918698f53ef9ff7ba80091969d93aa789d.tar.gz
stage-162ccd918698f53ef9ff7ba80091969d93aa789d.tar.bz2
stage-162ccd918698f53ef9ff7ba80091969d93aa789d.tar.xz
stage-162ccd918698f53ef9ff7ba80091969d93aa789d.zip
Situation code actually processes now.
Most of the AstNode types are unhandled yet.
-rw-r--r--src/game.cpp10
-rw-r--r--src/game.h2
-rw-r--r--src/gamebuilder.h2
-rw-r--r--src/gamestate.cpp54
-rw-r--r--src/gamestate.h7
-rw-r--r--src/main.cpp7
-rw-r--r--test.stage3
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 )
24 return hFunction.get( sName ); 24 return hFunction.get( sName );
25} 25}
26 26
27Variable Game::getParam( const Bu::String &sName ) const
28{
29 return hGlobalParam.get( sName );
30}
31
32Situation *Game::getSituation( const Bu::String &sName )
33{
34 return hSituation.get( sName );
35}
36
27void Game::addFunction( Function *pFunc ) 37void Game::addFunction( Function *pFunc )
28{ 38{
29 hFunction.insert( pFunc->getName(), pFunc ); 39 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:
17 virtual ~Game(); 17 virtual ~Game();
18 18
19 Function *getFunction( const Bu::String &sName ); 19 Function *getFunction( const Bu::String &sName );
20 Variable getParam( const Bu::String &sName ) const;
21 Situation *getSituation( const Bu::String &sName );
20 22
21private: 23private:
22 void addFunction( Function *pFunc ); 24 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:
13 GameBuilder(); 13 GameBuilder();
14 virtual ~GameBuilder(); 14 virtual ~GameBuilder();
15 15
16 class Game *getGame() { return pGame; }
17
16 void setLiteral( const Variable &v ); 18 void setLiteral( const Variable &v );
17 void setGameParam( const Bu::String &sName ); 19 void setGameParam( const Bu::String &sName );
18 20
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 )
24 delete lsLocal.peekPop(); 24 delete lsLocal.peekPop();
25} 25}
26 26
27void GameState::init()
28{
29 Variable vStart = pGame->getParam("start");
30 if( vStart.getType() != Variable::tSituation )
31 throw Bu::ExceptionBase("game.start is not set to a situation name.");
32
33 gotoSituation( vStart.getString() );
34}
35
36void GameState::gotoSituation( const Bu::String &sName )
37{
38 Situation *pSit = pGame->getSituation( sName );
39 sCurSituation = sName;
40 if( !hsSituation.has( sName ) )
41 {
42 hsSituation.insert( sName, new Scope() );
43 pSit->exec( *this, Situation::modeSetup );
44 }
45
46 pSit->exec( *this, Situation::modeEnter );
47}
48
27void GameState::callFunction( const Bu::String &sName ) 49void GameState::callFunction( const Bu::String &sName )
28{ 50{
29 pGame->getFunction( sName )->call( *this ); 51 pGame->getFunction( sName )->call( *this );
@@ -74,6 +96,21 @@ void GameState::setVariable( const Bu::String &sName, const Variable &v,
74 throw Bu::ExceptionBase("Really bad scopeid passed into setVariable"); 96 throw Bu::ExceptionBase("Really bad scopeid passed into setVariable");
75} 97}
76 98
99Variable GameState::deref( const Variable &src )
100{
101 if( src.getType() == Variable::tVariable )
102 return getVariable( src.getString() );
103 return src;
104}
105
106Variable GameState::popDeref()
107{
108 Variable v = lStack.peekPop();
109 if( v.getType() == Variable::tVariable )
110 return getVariable( v.getString() );
111 return v;
112}
113
77void GameState::parse( const AstBranch::NodeList &lCode ) 114void GameState::parse( const AstBranch::NodeList &lCode )
78{ 115{
79 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) 116 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ )
@@ -88,9 +125,23 @@ void GameState::parse( const AstBranch::NodeList &lCode )
88 case AstNode::tCompGtEq: 125 case AstNode::tCompGtEq:
89 case AstNode::tCompLtEq: 126 case AstNode::tCompLtEq:
90 case AstNode::tStore: 127 case AstNode::tStore:
128 {
129 Variable y = popDeref();
130 Variable dst = pop();
131 setVariable( dst.getString(), y );
132 }
133 break;
134
91 case AstNode::tAnd: 135 case AstNode::tAnd:
92 case AstNode::tOr: 136 case AstNode::tOr:
93 case AstNode::tPlus: 137 case AstNode::tPlus:
138 {
139 Variable y = popDeref();
140 Variable x = popDeref();
141 lStack.push( x + y );
142 }
143 break;
144
94 case AstNode::tMinus: 145 case AstNode::tMinus:
95 case AstNode::tDivide: 146 case AstNode::tDivide:
96 case AstNode::tMultiply: 147 case AstNode::tMultiply:
@@ -103,9 +154,6 @@ void GameState::parse( const AstBranch::NodeList &lCode )
103 154
104 // tLeafLiteral 155 // tLeafLiteral
105 case AstNode::tVarName: 156 case AstNode::tVarName:
106 lStack.push( dynamic_cast<const AstLeafLiteral *>(*i)->getValue() );
107 break;
108
109 case AstNode::tLiteral: 157 case AstNode::tLiteral:
110 lStack.push( dynamic_cast<const AstLeafLiteral *>(*i)->getValue() ); 158 lStack.push( dynamic_cast<const AstLeafLiteral *>(*i)->getValue() );
111 break; 159 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:
15 15
16 void parse( class AstBranch *pAst ); 16 void parse( class AstBranch *pAst );
17 17
18 void init();
19
20 void gotoSituation( const Bu::String &sName );
21
18 Variable pop() { return lStack.peekPop(); } 22 Variable pop() { return lStack.peekPop(); }
23 Variable popDeref();
19 void push( const Variable &v ) { lStack.push( v ); } 24 void push( const Variable &v ) { lStack.push( v ); }
20 25
21 void callFunction( const Bu::String &sName ); 26 void callFunction( const Bu::String &sName );
@@ -31,6 +36,8 @@ public:
31 Variable getVariable( const Bu::String &sName, ScopeId id=sidLocal ); 36 Variable getVariable( const Bu::String &sName, ScopeId id=sidLocal );
32 void setVariable( const Bu::String &sName, const Variable &v, ScopeId id=sidLocal ); 37 void setVariable( const Bu::String &sName, const Variable &v, ScopeId id=sidLocal );
33 38
39 Variable deref( const Variable &src );
40
34private: 41private:
35 void parse( const AstBranch::NodeList &lCode ); 42 void parse( const AstBranch::NodeList &lCode );
36 43
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 @@
1#include "gamebuilder.h" 1#include "gamebuilder.h"
2#include "game.h"
3#include "gamestate.h"
2#include "parser.tab.h" 4#include "parser.tab.h"
3 5
4typedef void *yyscan_t; 6typedef void *yyscan_t;
@@ -23,6 +25,11 @@ int main( int argc, char *argv[] )
23 25
24 fclose( in ); 26 fclose( in );
25 27
28 Game *pGame = bld.getGame();
29
30 GameState gs( pGame );
31 gs.init();
32
26 return 0; 33 return 0;
27} 34}
28 35
diff --git a/test.stage b/test.stage
index 87e4756..04d959b 100644
--- a/test.stage
+++ b/test.stage
@@ -17,7 +17,8 @@ situation <<start>>
17{ 17{
18 setup 18 setup
19 { 19 {
20 display("Hello"); 20 name = "Bob";
21 display("Hello " + name + ''' and then some''');
21 } 22 }
22 23
23 enter 24 enter