diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/astbranch.h | 1 | ||||
-rw-r--r-- | src/astfunction.cpp | 2 | ||||
-rw-r--r-- | src/astfunction.h | 2 | ||||
-rw-r--r-- | src/function.h | 2 | ||||
-rw-r--r-- | src/functiondisplay.cpp | 9 | ||||
-rw-r--r-- | src/functiondisplay.h | 4 | ||||
-rw-r--r-- | src/game.cpp | 13 | ||||
-rw-r--r-- | src/game.h | 5 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 2 | ||||
-rw-r--r-- | src/gamestate.cpp | 72 | ||||
-rw-r--r-- | src/gamestate.h | 21 | ||||
-rw-r--r-- | src/scope.cpp | 4 | ||||
-rw-r--r-- | src/scope.h | 7 | ||||
-rw-r--r-- | src/variable.cpp | 42 | ||||
-rw-r--r-- | src/variable.h | 7 |
15 files changed, 184 insertions, 9 deletions
diff --git a/src/astbranch.h b/src/astbranch.h index 8e63af5..0c558aa 100644 --- a/src/astbranch.h +++ b/src/astbranch.h | |||
@@ -16,6 +16,7 @@ public: | |||
16 | AstBranch *getParent() const { return pParent; } | 16 | AstBranch *getParent() const { return pParent; } |
17 | 17 | ||
18 | typedef Bu::List<AstNode *> NodeList; | 18 | typedef Bu::List<AstNode *> NodeList; |
19 | const NodeList &getNodeList() const { return lNodes; } | ||
19 | 20 | ||
20 | private: | 21 | private: |
21 | AstBranch *pParent; | 22 | AstBranch *pParent; |
diff --git a/src/astfunction.cpp b/src/astfunction.cpp index df183d3..f091644 100644 --- a/src/astfunction.cpp +++ b/src/astfunction.cpp | |||
@@ -12,7 +12,7 @@ AstFunction::~AstFunction() | |||
12 | delete pAst; | 12 | delete pAst; |
13 | } | 13 | } |
14 | 14 | ||
15 | Variable AstFunction::call( const VariableList &lParams ) | 15 | Variable AstFunction::call( class GameState &gState ) |
16 | { | 16 | { |
17 | } | 17 | } |
18 | 18 | ||
diff --git a/src/astfunction.h b/src/astfunction.h index e6470e4..4b2a049 100644 --- a/src/astfunction.h +++ b/src/astfunction.h | |||
@@ -10,7 +10,7 @@ public: | |||
10 | virtual ~AstFunction(); | 10 | virtual ~AstFunction(); |
11 | 11 | ||
12 | virtual Bu::String getName() const { return sName; } | 12 | virtual Bu::String getName() const { return sName; } |
13 | virtual Variable call( const VariableList &lParams ); | 13 | virtual Variable call( class GameState &gState ); |
14 | 14 | ||
15 | void addParam( const Bu::String &sName ); | 15 | void addParam( const Bu::String &sName ); |
16 | void setAst( class AstBranch *pAst ); | 16 | void setAst( class AstBranch *pAst ); |
diff --git a/src/function.h b/src/function.h index 1191129..c1289f4 100644 --- a/src/function.h +++ b/src/function.h | |||
@@ -11,7 +11,7 @@ public: | |||
11 | virtual ~Function(); | 11 | virtual ~Function(); |
12 | 12 | ||
13 | virtual Bu::String getName() const=0; | 13 | virtual Bu::String getName() const=0; |
14 | virtual Variable call( const VariableList &lParams )=0; | 14 | virtual Variable call( class GameState &gState )=0; |
15 | }; | 15 | }; |
16 | 16 | ||
17 | #endif | 17 | #endif |
diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp index 920eefd..16b1175 100644 --- a/src/functiondisplay.cpp +++ b/src/functiondisplay.cpp | |||
@@ -1,5 +1,10 @@ | |||
1 | #include "functiondisplay.h" | 1 | #include "functiondisplay.h" |
2 | 2 | ||
3 | #include <bu/sio.h> | ||
4 | #include "gamestate.h" | ||
5 | |||
6 | using namespace Bu; | ||
7 | |||
3 | FunctionDisplay::FunctionDisplay() | 8 | FunctionDisplay::FunctionDisplay() |
4 | { | 9 | { |
5 | } | 10 | } |
@@ -8,7 +13,9 @@ FunctionDisplay::~FunctionDisplay() | |||
8 | { | 13 | { |
9 | } | 14 | } |
10 | 15 | ||
11 | Variable FunctionDisplay::call( const VariableList &lParams ) | 16 | Variable FunctionDisplay::call( class GameState &gState ) |
12 | { | 17 | { |
18 | Variable v = gState.pop(); | ||
19 | sio << "Display: " << v << sio.nl; | ||
13 | } | 20 | } |
14 | 21 | ||
diff --git a/src/functiondisplay.h b/src/functiondisplay.h index 2f13360..d1a63a9 100644 --- a/src/functiondisplay.h +++ b/src/functiondisplay.h | |||
@@ -3,14 +3,14 @@ | |||
3 | 3 | ||
4 | #include "function.h" | 4 | #include "function.h" |
5 | 5 | ||
6 | class FunctionDisplay | 6 | class FunctionDisplay : public Function |
7 | { | 7 | { |
8 | public: | 8 | public: |
9 | FunctionDisplay(); | 9 | FunctionDisplay(); |
10 | virtual ~FunctionDisplay(); | 10 | virtual ~FunctionDisplay(); |
11 | 11 | ||
12 | virtual Bu::String getName() const { return "display"; } | 12 | virtual Bu::String getName() const { return "display"; } |
13 | virtual Variable call( const VariableList &lParams ); | 13 | virtual Variable call( class GameState &gState ); |
14 | }; | 14 | }; |
15 | 15 | ||
16 | #endif | 16 | #endif |
diff --git a/src/game.cpp b/src/game.cpp index f3b5828..c2bbce4 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -1,7 +1,10 @@ | |||
1 | #include "game.h" | 1 | #include "game.h" |
2 | 2 | ||
3 | #include "functiondisplay.h" | ||
4 | |||
3 | Game::Game() | 5 | Game::Game() |
4 | { | 6 | { |
7 | addFunction( new FunctionDisplay() ); | ||
5 | } | 8 | } |
6 | 9 | ||
7 | Game::~Game() | 10 | Game::~Game() |
@@ -16,3 +19,13 @@ Game::~Game() | |||
16 | } | 19 | } |
17 | } | 20 | } |
18 | 21 | ||
22 | Function *Game::getFunction( const Bu::String &sName ) | ||
23 | { | ||
24 | return hFunction.get( sName ); | ||
25 | } | ||
26 | |||
27 | void Game::addFunction( Function *pFunc ) | ||
28 | { | ||
29 | hFunction.insert( pFunc->getName(), pFunc ); | ||
30 | } | ||
31 | |||
@@ -16,6 +16,11 @@ public: | |||
16 | Game(); | 16 | Game(); |
17 | virtual ~Game(); | 17 | virtual ~Game(); |
18 | 18 | ||
19 | Function *getFunction( const Bu::String &sName ); | ||
20 | |||
21 | private: | ||
22 | void addFunction( Function *pFunc ); | ||
23 | |||
19 | private: | 24 | private: |
20 | typedef Bu::Hash<Bu::String, Function *> FunctionHash; | 25 | typedef Bu::Hash<Bu::String, Function *> FunctionHash; |
21 | typedef Bu::Hash<Bu::String, Situation *> SituationHash; | 26 | typedef Bu::Hash<Bu::String, Situation *> SituationHash; |
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 45aee50..ee85129 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp | |||
@@ -98,7 +98,7 @@ void GameBuilder::addVarRef( const Bu::String &sName ) | |||
98 | { | 98 | { |
99 | if( pCurNode ) | 99 | if( pCurNode ) |
100 | { | 100 | { |
101 | pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, sName ) ); | 101 | pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, Variable::newVariableName( sName ) ) ); |
102 | } | 102 | } |
103 | } | 103 | } |
104 | 104 | ||
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index fcd3433..7649cac 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
@@ -1,5 +1,10 @@ | |||
1 | #include "gamestate.h" | 1 | #include "gamestate.h" |
2 | 2 | ||
3 | #include "game.h" | ||
4 | |||
5 | #include "astleaf.h" | ||
6 | #include "astleafliteral.h" | ||
7 | |||
3 | GameState::GameState( Game *pGame ) : | 8 | GameState::GameState( Game *pGame ) : |
4 | pGame( pGame ) | 9 | pGame( pGame ) |
5 | { | 10 | { |
@@ -9,3 +14,70 @@ GameState::~GameState() | |||
9 | { | 14 | { |
10 | } | 15 | } |
11 | 16 | ||
17 | void GameState::parse( class AstBranch *pAst ) | ||
18 | { | ||
19 | if( pAst->getType() != AstNode::tScope ) | ||
20 | throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch."); | ||
21 | parse( pAst->getNodeList() ); | ||
22 | } | ||
23 | |||
24 | void GameState::callFunction( const Bu::String &sName ) | ||
25 | { | ||
26 | lsLocal.push( new Scope() ); | ||
27 | pGame->getFunction( sName )->call( *this ); | ||
28 | delete lsLocal.peekPop(); | ||
29 | } | ||
30 | |||
31 | void GameState::parse( const AstBranch::NodeList &lCode ) | ||
32 | { | ||
33 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) | ||
34 | { | ||
35 | switch( (*i)->getType() ) | ||
36 | { | ||
37 | // tLeaf | ||
38 | case AstNode::tNot: | ||
39 | case AstNode::tComp: | ||
40 | case AstNode::tCompGt: | ||
41 | case AstNode::tCompLt: | ||
42 | case AstNode::tCompGtEq: | ||
43 | case AstNode::tCompLtEq: | ||
44 | case AstNode::tStore: | ||
45 | case AstNode::tAnd: | ||
46 | case AstNode::tOr: | ||
47 | case AstNode::tPlus: | ||
48 | case AstNode::tMinus: | ||
49 | case AstNode::tDivide: | ||
50 | case AstNode::tMultiply: | ||
51 | case AstNode::tPlusStore: | ||
52 | case AstNode::tMinusStore: | ||
53 | case AstNode::tDivideStore: | ||
54 | case AstNode::tMultiplyStore: | ||
55 | case AstNode::tNegate: | ||
56 | case AstNode::tIn: | ||
57 | |||
58 | // tLeafLiteral | ||
59 | case AstNode::tVarName: | ||
60 | lStack.push( dynamic_cast<const AstLeafLiteral *>(*i)->getValue() ); | ||
61 | break; | ||
62 | |||
63 | case AstNode::tLiteral: | ||
64 | lStack.push( dynamic_cast<const AstLeafLiteral *>(*i)->getValue() ); | ||
65 | break; | ||
66 | |||
67 | case AstNode::tFuncCall: | ||
68 | callFunction( | ||
69 | dynamic_cast<const AstLeafLiteral *>(*i) | ||
70 | ->getValue().getString() | ||
71 | ); | ||
72 | break; | ||
73 | |||
74 | // tBranch | ||
75 | case AstNode::tScope: | ||
76 | case AstNode::tIf: | ||
77 | case AstNode::tForEach: | ||
78 | case AstNode::tWhile: | ||
79 | break; | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
diff --git a/src/gamestate.h b/src/gamestate.h index 2754873..cb02322 100644 --- a/src/gamestate.h +++ b/src/gamestate.h | |||
@@ -1,6 +1,10 @@ | |||
1 | #ifndef GAME_STATE_H | 1 | #ifndef GAME_STATE_H |
2 | #define GAME_STATE_H | 2 | #define GAME_STATE_H |
3 | 3 | ||
4 | #include "astbranch.h" | ||
5 | #include "variable.h" | ||
6 | #include "scope.h" | ||
7 | |||
4 | class Game; | 8 | class Game; |
5 | 9 | ||
6 | class GameState | 10 | class GameState |
@@ -9,8 +13,25 @@ public: | |||
9 | GameState( Game *pGame ); | 13 | GameState( Game *pGame ); |
10 | virtual ~GameState(); | 14 | virtual ~GameState(); |
11 | 15 | ||
16 | void parse( class AstBranch *pAst ); | ||
17 | |||
18 | Variable pop() { return lStack.peekPop(); } | ||
19 | void push( const Variable &v ) { lStack.push( v ); } | ||
20 | |||
21 | void callFunction( const Bu::String &sName ); | ||
22 | |||
12 | private: | 23 | private: |
24 | void parse( const AstBranch::NodeList &lCode ); | ||
25 | |||
26 | private: | ||
27 | typedef Bu::List<Scope *> ScopeList; | ||
28 | typedef Bu::Hash<Bu::String, Scope *> ScopeHash; | ||
13 | Game *pGame; | 29 | Game *pGame; |
30 | Scope sGlobal; | ||
31 | ScopeList lsLocal; | ||
32 | ScopeHash hsSituation; | ||
33 | |||
34 | VariableList lStack; | ||
14 | }; | 35 | }; |
15 | 36 | ||
16 | #endif | 37 | #endif |
diff --git a/src/scope.cpp b/src/scope.cpp index 45d4484..87cd050 100644 --- a/src/scope.cpp +++ b/src/scope.cpp | |||
@@ -1,5 +1,5 @@ | |||
1 | #include "scope.h" | 1 | #include "scope.h" |
2 | 2 | /* | |
3 | Scope::Scope() | 3 | Scope::Scope() |
4 | { | 4 | { |
5 | } | 5 | } |
@@ -7,4 +7,4 @@ Scope::Scope() | |||
7 | Scope::~Scope() | 7 | Scope::~Scope() |
8 | { | 8 | { |
9 | } | 9 | } |
10 | 10 | */ | |
diff --git a/src/scope.h b/src/scope.h index 84797f0..f352ff5 100644 --- a/src/scope.h +++ b/src/scope.h | |||
@@ -3,14 +3,21 @@ | |||
3 | 3 | ||
4 | #include "variable.h" | 4 | #include "variable.h" |
5 | 5 | ||
6 | typedef VariableHash Scope; | ||
7 | /* | ||
8 | |||
6 | class Scope | 9 | class Scope |
7 | { | 10 | { |
8 | public: | 11 | public: |
9 | Scope(); | 12 | Scope(); |
10 | virtual ~Scope(); | 13 | virtual ~Scope(); |
11 | 14 | ||
15 | |||
16 | |||
12 | private: | 17 | private: |
13 | VariableHash hVars; | 18 | VariableHash hVars; |
14 | }; | 19 | }; |
15 | 20 | ||
21 | */ | ||
22 | |||
16 | #endif | 23 | #endif |
diff --git a/src/variable.cpp b/src/variable.cpp index adf1511..b7899f1 100644 --- a/src/variable.cpp +++ b/src/variable.cpp | |||
@@ -62,6 +62,33 @@ Variable Variable::newSituationName( const Bu::String &s ) | |||
62 | return v; | 62 | return v; |
63 | } | 63 | } |
64 | 64 | ||
65 | Variable Variable::newVariableName( const Bu::String &s ) | ||
66 | { | ||
67 | Variable v( tVariable ); | ||
68 | (*v.sValue) = s; | ||
69 | return v; | ||
70 | } | ||
71 | |||
72 | bool Variable::getBool() const | ||
73 | { | ||
74 | return bValue; | ||
75 | } | ||
76 | |||
77 | int64_t Variable::getInt() const | ||
78 | { | ||
79 | return iValue; | ||
80 | } | ||
81 | |||
82 | double Variable::getFloat() const | ||
83 | { | ||
84 | return fValue; | ||
85 | } | ||
86 | |||
87 | Bu::String Variable::getString() const | ||
88 | { | ||
89 | return *sValue; | ||
90 | } | ||
91 | |||
65 | Variable Variable::to( Type e ) const | 92 | Variable Variable::to( Type e ) const |
66 | { | 93 | { |
67 | if( e == eType ) | 94 | if( e == eType ) |
@@ -195,6 +222,7 @@ Variable &Variable::operator=( const Variable &rhs ) | |||
195 | 222 | ||
196 | case tString: | 223 | case tString: |
197 | case tSituation: | 224 | case tSituation: |
225 | case tVariable: | ||
198 | (*sValue) = *rhs.sValue; | 226 | (*sValue) = *rhs.sValue; |
199 | break; | 227 | break; |
200 | 228 | ||
@@ -261,6 +289,10 @@ Variable &Variable::operator+=( const Variable &rhs ) | |||
261 | case tSituation: | 289 | case tSituation: |
262 | throw VariableException("You cannot add situations."); | 290 | throw VariableException("You cannot add situations."); |
263 | break; | 291 | break; |
292 | |||
293 | case tVariable: | ||
294 | throw VariableException("You cannot add variable names."); | ||
295 | break; | ||
264 | } | 296 | } |
265 | 297 | ||
266 | return *this; | 298 | return *this; |
@@ -310,6 +342,9 @@ Variable Variable::operator+( const Variable &rhs ) const | |||
310 | 342 | ||
311 | case tSituation: | 343 | case tSituation: |
312 | throw VariableException("You cannot add situations."); | 344 | throw VariableException("You cannot add situations."); |
345 | |||
346 | case tVariable: | ||
347 | throw VariableException("You cannot add variables."); | ||
313 | } | 348 | } |
314 | } | 349 | } |
315 | } | 350 | } |
@@ -341,6 +376,7 @@ bool Variable::operator==( const Variable &rhs ) const | |||
341 | 376 | ||
342 | case tString: | 377 | case tString: |
343 | case tSituation: | 378 | case tSituation: |
379 | case tVariable: | ||
344 | return (*sValue) == (*rhs.sValue); | 380 | return (*sValue) == (*rhs.sValue); |
345 | 381 | ||
346 | case tList: | 382 | case tList: |
@@ -369,6 +405,7 @@ void Variable::initType() | |||
369 | { | 405 | { |
370 | case tString: | 406 | case tString: |
371 | case tSituation: | 407 | case tSituation: |
408 | case tVariable: | ||
372 | sValue = new Bu::String(); | 409 | sValue = new Bu::String(); |
373 | break; | 410 | break; |
374 | 411 | ||
@@ -388,6 +425,7 @@ void Variable::deinitType() | |||
388 | { | 425 | { |
389 | case tString: | 426 | case tString: |
390 | case tSituation: | 427 | case tSituation: |
428 | case tVariable: | ||
391 | delete sValue; | 429 | delete sValue; |
392 | break; | 430 | break; |
393 | 431 | ||
@@ -418,6 +456,7 @@ template<> uint32_t Bu::__calcHashCode<Variable>( const Variable &k ) | |||
418 | 456 | ||
419 | case Variable::tString: | 457 | case Variable::tString: |
420 | case Variable::tSituation: | 458 | case Variable::tSituation: |
459 | case Variable::tVariable: | ||
421 | return Bu::__calcHashCode( *k.sValue ); | 460 | return Bu::__calcHashCode( *k.sValue ); |
422 | 461 | ||
423 | case Variable::tList: | 462 | case Variable::tList: |
@@ -453,6 +492,9 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) | |||
453 | 492 | ||
454 | case Variable::tSituation: | 493 | case Variable::tSituation: |
455 | return f << "<<" << *v.sValue << ">>"; | 494 | return f << "<<" << *v.sValue << ">>"; |
495 | |||
496 | case Variable::tVariable: | ||
497 | return f << "(varref:\"" << *v.sValue << "\")"; | ||
456 | 498 | ||
457 | case Variable::tList: | 499 | case Variable::tList: |
458 | return f << *v.lValue; | 500 | return f << *v.lValue; |
diff --git a/src/variable.h b/src/variable.h index 9a35d48..e1bc30c 100644 --- a/src/variable.h +++ b/src/variable.h | |||
@@ -19,6 +19,7 @@ public: | |||
19 | tFloat, | 19 | tFloat, |
20 | tString, | 20 | tString, |
21 | tSituation, | 21 | tSituation, |
22 | tVariable, | ||
22 | tList, | 23 | tList, |
23 | tDictionary | 24 | tDictionary |
24 | }; | 25 | }; |
@@ -34,9 +35,15 @@ public: | |||
34 | virtual ~Variable(); | 35 | virtual ~Variable(); |
35 | 36 | ||
36 | static Variable newSituationName( const Bu::String &s ); | 37 | static Variable newSituationName( const Bu::String &s ); |
38 | static Variable newVariableName( const Bu::String &s ); | ||
37 | 39 | ||
38 | Type getType() const { return eType; } | 40 | Type getType() const { return eType; } |
39 | 41 | ||
42 | bool getBool() const; | ||
43 | int64_t getInt() const; | ||
44 | double getFloat() const; | ||
45 | Bu::String getString() const; | ||
46 | |||
40 | Variable to( Type e ) const; | 47 | Variable to( Type e ) const; |
41 | 48 | ||
42 | Variable &operator=( const Variable &rhs ); | 49 | Variable &operator=( const Variable &rhs ); |