summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-29 13:06:40 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-29 13:06:40 -0700
commit1bc10d1408eb29b0675a030e029155dd046b1dd8 (patch)
tree6d649015bdfa69df9de1cb964906fd396b9db82b
parent79dce6268850fb1b0d76c15d5399d66bcd286e5f (diff)
downloadstage-1bc10d1408eb29b0675a030e029155dd046b1dd8.tar.gz
stage-1bc10d1408eb29b0675a030e029155dd046b1dd8.tar.bz2
stage-1bc10d1408eb29b0675a030e029155dd046b1dd8.tar.xz
stage-1bc10d1408eb29b0675a030e029155dd046b1dd8.zip
Functions can be called now, at least manually.
A little more work needs to be done before they're being called from code correctly.
-rw-r--r--src/astbranch.h1
-rw-r--r--src/astfunction.cpp2
-rw-r--r--src/astfunction.h2
-rw-r--r--src/function.h2
-rw-r--r--src/functiondisplay.cpp9
-rw-r--r--src/functiondisplay.h4
-rw-r--r--src/game.cpp13
-rw-r--r--src/game.h5
-rw-r--r--src/gamebuilder.cpp2
-rw-r--r--src/gamestate.cpp72
-rw-r--r--src/gamestate.h21
-rw-r--r--src/scope.cpp4
-rw-r--r--src/scope.h7
-rw-r--r--src/variable.cpp42
-rw-r--r--src/variable.h7
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
20private: 21private:
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
15Variable AstFunction::call( const VariableList &lParams ) 15Variable 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
6using namespace Bu;
7
3FunctionDisplay::FunctionDisplay() 8FunctionDisplay::FunctionDisplay()
4{ 9{
5} 10}
@@ -8,7 +13,9 @@ FunctionDisplay::~FunctionDisplay()
8{ 13{
9} 14}
10 15
11Variable FunctionDisplay::call( const VariableList &lParams ) 16Variable 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
6class FunctionDisplay 6class FunctionDisplay : public Function
7{ 7{
8public: 8public:
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
3Game::Game() 5Game::Game()
4{ 6{
7 addFunction( new FunctionDisplay() );
5} 8}
6 9
7Game::~Game() 10Game::~Game()
@@ -16,3 +19,13 @@ Game::~Game()
16 } 19 }
17} 20}
18 21
22Function *Game::getFunction( const Bu::String &sName )
23{
24 return hFunction.get( sName );
25}
26
27void Game::addFunction( Function *pFunc )
28{
29 hFunction.insert( pFunc->getName(), pFunc );
30}
31
diff --git a/src/game.h b/src/game.h
index dd614cf..4abb4da 100644
--- a/src/game.h
+++ b/src/game.h
@@ -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
21private:
22 void addFunction( Function *pFunc );
23
19private: 24private:
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
3GameState::GameState( Game *pGame ) : 8GameState::GameState( Game *pGame ) :
4 pGame( pGame ) 9 pGame( pGame )
5{ 10{
@@ -9,3 +14,70 @@ GameState::~GameState()
9{ 14{
10} 15}
11 16
17void 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
24void GameState::callFunction( const Bu::String &sName )
25{
26 lsLocal.push( new Scope() );
27 pGame->getFunction( sName )->call( *this );
28 delete lsLocal.peekPop();
29}
30
31void 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
4class Game; 8class Game;
5 9
6class GameState 10class 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
12private: 23private:
24 void parse( const AstBranch::NodeList &lCode );
25
26private:
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/*
3Scope::Scope() 3Scope::Scope()
4{ 4{
5} 5}
@@ -7,4 +7,4 @@ Scope::Scope()
7Scope::~Scope() 7Scope::~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
6typedef VariableHash Scope;
7/*
8
6class Scope 9class Scope
7{ 10{
8public: 11public:
9 Scope(); 12 Scope();
10 virtual ~Scope(); 13 virtual ~Scope();
11 14
15
16
12private: 17private:
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
65Variable Variable::newVariableName( const Bu::String &s )
66{
67 Variable v( tVariable );
68 (*v.sValue) = s;
69 return v;
70}
71
72bool Variable::getBool() const
73{
74 return bValue;
75}
76
77int64_t Variable::getInt() const
78{
79 return iValue;
80}
81
82double Variable::getFloat() const
83{
84 return fValue;
85}
86
87Bu::String Variable::getString() const
88{
89 return *sValue;
90}
91
65Variable Variable::to( Type e ) const 92Variable 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 );