summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gamebuilder.cpp19
-rw-r--r--src/gamebuilder.h5
-rw-r--r--src/gamestate.cpp50
-rw-r--r--src/gamestate.h13
-rw-r--r--src/parser.y8
-rw-r--r--src/situation.cpp52
-rw-r--r--src/situation.h21
-rw-r--r--test.stage13
8 files changed, 170 insertions, 11 deletions
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp
index ee85129..6c42fb0 100644
--- a/src/gamebuilder.cpp
+++ b/src/gamebuilder.cpp
@@ -7,6 +7,7 @@
7#include "astleafliteral.h" 7#include "astleafliteral.h"
8#include "astfunction.h" 8#include "astfunction.h"
9#include "command.h" 9#include "command.h"
10#include "situation.h"
10 11
11using namespace Bu; 12using namespace Bu;
12 13
@@ -16,7 +17,8 @@ GameBuilder::GameBuilder() :
16 pCurNode( NULL ), 17 pCurNode( NULL ),
17 pCurRoot( NULL ), 18 pCurRoot( NULL ),
18 pCurCmd( NULL ), 19 pCurCmd( NULL ),
19 pCurFnc( NULL ) 20 pCurFnc( NULL ),
21 pCurSit( NULL )
20{ 22{
21 pGame = new Game(); 23 pGame = new Game();
22} 24}
@@ -58,11 +60,26 @@ void GameBuilder::endFunction()
58 60
59void GameBuilder::beginSituation( const Bu::String &sName ) 61void GameBuilder::beginSituation( const Bu::String &sName )
60{ 62{
63 pCurSit = new Situation( sName );
61 sio << "New situation: " << sName << sio.nl; 64 sio << "New situation: " << sName << sio.nl;
62} 65}
63 66
67void GameBuilder::beginSituationMode( Situation::Mode m )
68{
69 pCurNode = pCurRoot = new AstBranch( AstNode::tScope );
70 eCurSitMode = m;
71}
72
73void GameBuilder::closeSituationMode()
74{
75 sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl;
76 pCurSit->setAst( pCurRoot, eCurSitMode );
77 pCurRoot = pCurNode = NULL;
78}
79
64void GameBuilder::endSituation() 80void GameBuilder::endSituation()
65{ 81{
82 pGame->hSituation.insert( pCurSit->getName(), pCurSit );
66 sio << "Situation ended." << sio.nl; 83 sio << "Situation ended." << sio.nl;
67} 84}
68 85
diff --git a/src/gamebuilder.h b/src/gamebuilder.h
index bc32f55..9a0493f 100644
--- a/src/gamebuilder.h
+++ b/src/gamebuilder.h
@@ -5,6 +5,7 @@
5 5
6#include "variable.h" 6#include "variable.h"
7#include "astnode.h" 7#include "astnode.h"
8#include "situation.h"
8 9
9class GameBuilder 10class GameBuilder
10{ 11{
@@ -20,6 +21,8 @@ public:
20 void endFunction(); 21 void endFunction();
21 22
22 void beginSituation( const Bu::String &sName ); 23 void beginSituation( const Bu::String &sName );
24 void beginSituationMode( Situation::Mode m );
25 void closeSituationMode();
23 void endSituation(); 26 void endSituation();
24 27
25 void addNode( AstNode::Type iType ); 28 void addNode( AstNode::Type iType );
@@ -44,6 +47,8 @@ private:
44 class AstBranch *pCurRoot; 47 class AstBranch *pCurRoot;
45 class Command *pCurCmd; 48 class Command *pCurCmd;
46 class AstFunction *pCurFnc; 49 class AstFunction *pCurFnc;
50 class Situation *pCurSit;
51 Situation::Mode eCurSitMode;
47}; 52};
48 53
49#endif 54#endif
diff --git a/src/gamestate.cpp b/src/gamestate.cpp
index 7649cac..25b53b8 100644
--- a/src/gamestate.cpp
+++ b/src/gamestate.cpp
@@ -18,14 +18,60 @@ void GameState::parse( class AstBranch *pAst )
18{ 18{
19 if( pAst->getType() != AstNode::tScope ) 19 if( pAst->getType() != AstNode::tScope )
20 throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch."); 20 throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch.");
21
22 lsLocal.push( new Scope() );
21 parse( pAst->getNodeList() ); 23 parse( pAst->getNodeList() );
24 delete lsLocal.peekPop();
22} 25}
23 26
24void GameState::callFunction( const Bu::String &sName ) 27void GameState::callFunction( const Bu::String &sName )
25{ 28{
26 lsLocal.push( new Scope() );
27 pGame->getFunction( sName )->call( *this ); 29 pGame->getFunction( sName )->call( *this );
28 delete lsLocal.peekPop(); 30}
31
32Variable GameState::getVariable( const Bu::String &sName, ScopeId id )
33{
34 switch( id )
35 {
36 case sidLocal:
37 return lsLocal.peek()->get( sName );
38
39 case sidGlobal:
40 return sGlobal.get( sName );
41
42 case sidPlayer:
43 return sPlayer.get( sName );
44
45 case sidSituation:
46 return hsSituation.get( sCurSituation )->get( sName );
47 }
48
49 throw Bu::ExceptionBase("Really bad scopeid passed into getVariable");
50}
51
52void GameState::setVariable( const Bu::String &sName, const Variable &v,
53 ScopeId id )
54{
55 switch( id )
56 {
57 case sidLocal:
58 lsLocal.peek()->insert( sName, v );
59 return;
60
61 case sidGlobal:
62 sGlobal.insert( sName, v );
63 return;
64
65 case sidPlayer:
66 sPlayer.insert( sName, v );
67 return;
68
69 case sidSituation:
70 hsSituation.get( sCurSituation )->insert( sName, v );
71 return;
72 }
73
74 throw Bu::ExceptionBase("Really bad scopeid passed into setVariable");
29} 75}
30 76
31void GameState::parse( const AstBranch::NodeList &lCode ) 77void GameState::parse( const AstBranch::NodeList &lCode )
diff --git a/src/gamestate.h b/src/gamestate.h
index cb02322..7a8f81a 100644
--- a/src/gamestate.h
+++ b/src/gamestate.h
@@ -20,6 +20,17 @@ public:
20 20
21 void callFunction( const Bu::String &sName ); 21 void callFunction( const Bu::String &sName );
22 22
23 enum ScopeId
24 {
25 sidLocal,
26 sidGlobal,
27 sidPlayer,
28 sidSituation
29 };
30
31 Variable getVariable( const Bu::String &sName, ScopeId id=sidLocal );
32 void setVariable( const Bu::String &sName, const Variable &v, ScopeId id=sidLocal );
33
23private: 34private:
24 void parse( const AstBranch::NodeList &lCode ); 35 void parse( const AstBranch::NodeList &lCode );
25 36
@@ -28,8 +39,10 @@ private:
28 typedef Bu::Hash<Bu::String, Scope *> ScopeHash; 39 typedef Bu::Hash<Bu::String, Scope *> ScopeHash;
29 Game *pGame; 40 Game *pGame;
30 Scope sGlobal; 41 Scope sGlobal;
42 Scope sPlayer;
31 ScopeList lsLocal; 43 ScopeList lsLocal;
32 ScopeHash hsSituation; 44 ScopeHash hsSituation;
45 Bu::String sCurSituation;
33 46
34 VariableList lStack; 47 VariableList lStack;
35}; 48};
diff --git a/src/parser.y b/src/parser.y
index e6210d1..8f538a5 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -124,11 +124,13 @@ situationMembers:
124 | situationMembers commandDecl 124 | situationMembers commandDecl
125 ; 125 ;
126 126
127situationModeFunc: situationMode '{' cmpltExprList '}' 127situationModeFunc: situationMode '{' cmpltExprList '}' {
128 bld.closeSituationMode();
129 }
128 ; 130 ;
129 131
130situationMode: tokSetup 132situationMode: tokSetup { bld.beginSituationMode( Situation::modeSetup ); }
131 | tokEnter 133 | tokEnter { bld.beginSituationMode( Situation::modeEnter ); }
132 ; 134 ;
133 135
134function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); } 136function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); }
diff --git a/src/situation.cpp b/src/situation.cpp
index 0d24aa4..d7f98aa 100644
--- a/src/situation.cpp
+++ b/src/situation.cpp
@@ -1,10 +1,60 @@
1#include "situation.h" 1#include "situation.h"
2 2
3Situation::Situation() 3#include "astbranch.h"
4#include "gamestate.h"
5
6#include <bu/formatter.h>
7
8Situation::Situation( const Bu::String &sName ) :
9 sName( sName ),
10 pAstSetup( NULL ),
11 pAstEnter( NULL )
4{ 12{
5} 13}
6 14
7Situation::~Situation() 15Situation::~Situation()
8{ 16{
17 delete pAstSetup;
18 delete pAstEnter;
19}
20
21void Situation::setAst( class AstBranch *pAst, Situation::Mode m )
22{
23 switch( m )
24 {
25 case modeSetup:
26 pAstSetup = pAst;
27 break;
28
29 case modeEnter:
30 pAstEnter = pAst;
31 break;
32 }
33}
34
35void Situation::exec( class GameState &gState, Situation::Mode m )
36{
37 switch( m )
38 {
39 case modeSetup:
40 gState.parse( pAstSetup );
41 break;
42
43 case modeEnter:
44 gState.parse( pAstEnter );
45 break;
46 }
47}
48
49Bu::Formatter &operator<<( Bu::Formatter &f, Situation::Mode m )
50{
51 switch( m )
52 {
53 case Situation::modeSetup:
54 return f << "Setup";
55
56 case Situation::modeEnter:
57 return f << "Enter";
58 }
9} 59}
10 60
diff --git a/src/situation.h b/src/situation.h
index aed6397..4d06fe6 100644
--- a/src/situation.h
+++ b/src/situation.h
@@ -1,13 +1,32 @@
1#ifndef SITUATION_H 1#ifndef SITUATION_H
2#define SITUATION_H 2#define SITUATION_H
3 3
4#include <bu/string.h>
5
4class Situation 6class Situation
5{ 7{
6public: 8public:
7 Situation(); 9 Situation( const Bu::String &sName );
8 virtual ~Situation(); 10 virtual ~Situation();
9 11
12 Bu::String getName() const { return sName; }
13
14 enum Mode
15 {
16 modeSetup,
17 modeEnter,
18 };
19
20 void setAst( class AstBranch *pAst, Mode m );
21
22 void exec( class GameState &gState, Mode m );
23
10private: 24private:
25 Bu::String sName;
26 class AstBranch *pAstSetup;
27 class AstBranch *pAstEnter;
11}; 28};
12 29
30Bu::Formatter &operator<<( Bu::Formatter &f, Situation::Mode m );
31
13#endif 32#endif
diff --git a/test.stage b/test.stage
index 13155d3..87e4756 100644
--- a/test.stage
+++ b/test.stage
@@ -9,12 +9,19 @@ global
9{ 9{
10 command: "eat" object 10 command: "eat" object
11 { 11 {
12 pow(5, x*2); 12 display(object);
13 } 13 }
14} 14}
15 15
16function hello( a, b, c ) 16situation <<start>>
17{ 17{
18 a[x][y]; 18 setup
19 {
20 display("Hello");
21 }
22
23 enter
24 {
25 }
19} 26}
20 27