summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/astnode.cpp1
-rw-r--r--src/astnode.h1
-rw-r--r--src/functiondelete.cpp1
-rw-r--r--src/functiondisplay.cpp1
-rw-r--r--src/functionexit.cpp1
-rw-r--r--src/gamebuilder.cpp88
-rw-r--r--src/gamebuilder.h5
-rw-r--r--src/gamestate.cpp8
-rw-r--r--src/parser.y12
9 files changed, 109 insertions, 9 deletions
diff --git a/src/astnode.cpp b/src/astnode.cpp
index 2417968..81d560f 100644
--- a/src/astnode.cpp
+++ b/src/astnode.cpp
@@ -46,6 +46,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t )
46 case AstNode::tAppend: return f << "tAppend"; 46 case AstNode::tAppend: return f << "tAppend";
47 case AstNode::tInsert: return f << "tInsert"; 47 case AstNode::tInsert: return f << "tInsert";
48 case AstNode::tIndex: return f << "tIndex"; 48 case AstNode::tIndex: return f << "tIndex";
49 case AstNode::tPop: return f << "tPop";
49 50
50 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; 51 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!";
51 case AstNode::tVarName: return f << "tVarName"; 52 case AstNode::tVarName: return f << "tVarName";
diff --git a/src/astnode.h b/src/astnode.h
index a1215ed..94394f0 100644
--- a/src/astnode.h
+++ b/src/astnode.h
@@ -36,6 +36,7 @@ public:
36 tAppend = 0x01000018, 36 tAppend = 0x01000018,
37 tInsert = 0x01000019, 37 tInsert = 0x01000019,
38 tIndex = 0x0100001A, 38 tIndex = 0x0100001A,
39 tPop = 0x0100001B,
39 40
40 tLeafLiteral = 0x02000000, 41 tLeafLiteral = 0x02000000,
41 tVarName = 0x02000001, 42 tVarName = 0x02000001,
diff --git a/src/functiondelete.cpp b/src/functiondelete.cpp
index c9bfef4..070ffd4 100644
--- a/src/functiondelete.cpp
+++ b/src/functiondelete.cpp
@@ -15,5 +15,6 @@ void FunctionDelete::call( class GameState &gState )
15 Variable v = gState.pop(); 15 Variable v = gState.pop();
16 VariableRef r = v.getVariableRef(); 16 VariableRef r = v.getVariableRef();
17 gState.delVariable( r.sName, r.sid ); 17 gState.delVariable( r.sName, r.sid );
18 gState.push( Variable( Variable::tNull ) );
18} 19}
19 20
diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp
index 2f2209c..21941c0 100644
--- a/src/functiondisplay.cpp
+++ b/src/functiondisplay.cpp
@@ -22,6 +22,7 @@ void FunctionDisplay::call( class GameState &gState )
22 gState.getInterface()->display( pNode ); 22 gState.getInterface()->display( pNode );
23 delete pNode; 23 delete pNode;
24 24
25 gState.push( Variable( Variable::tNull ) );
25/* 26/*
26 Variable v = gState.popDeref(); 27 Variable v = gState.popDeref();
27 sio << "Display: " << v << sio.nl; 28 sio << "Display: " << v << sio.nl;
diff --git a/src/functionexit.cpp b/src/functionexit.cpp
index be64f43..5e0088c 100644
--- a/src/functionexit.cpp
+++ b/src/functionexit.cpp
@@ -13,5 +13,6 @@ FunctionExit::~FunctionExit()
13void FunctionExit::call( class GameState &gState ) 13void FunctionExit::call( class GameState &gState )
14{ 14{
15 gState.exit(); 15 gState.exit();
16 gState.push( Variable( Variable::tNull ) );
16} 17}
17 18
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp
index bfeffec..857c348 100644
--- a/src/gamebuilder.cpp
+++ b/src/gamebuilder.cpp
@@ -19,7 +19,8 @@ GameBuilder::GameBuilder() :
19 pCurRoot( NULL ), 19 pCurRoot( NULL ),
20 pCurCmd( NULL ), 20 pCurCmd( NULL ),
21 pCurFnc( NULL ), 21 pCurFnc( NULL ),
22 pCurSit( NULL ) 22 pCurSit( NULL ),
23 iStackHeight( 0 )
23{ 24{
24 pGame = new Game(); 25 pGame = new Game();
25} 26}
@@ -49,6 +50,15 @@ void GameBuilder::parse( const Bu::String &sFile )
49 fclose( in ); 50 fclose( in );
50} 51}
51 52
53void GameBuilder::endCmpltExpr()
54{
55 while( iStackHeight > 0 )
56 {
57 addNode( AstNode::tPop );
58 }
59 iStackHeight = 0;
60}
61
52void GameBuilder::setLiteral( const Variable &v ) 62void GameBuilder::setLiteral( const Variable &v )
53{ 63{
54 vLiteral = v; 64 vLiteral = v;
@@ -91,9 +101,12 @@ void GameBuilder::endFunctionParams()
91void GameBuilder::endFunction() 101void GameBuilder::endFunction()
92{ 102{
93 //sio << "Function ended: " << *pCurRoot << sio.nl; 103 //sio << "Function ended: " << *pCurRoot << sio.nl;
104
105 pCurNode->addNode( new AstLeafLiteral( Variable::tNull ) );
94 pCurFnc->setAst( pCurRoot ); 106 pCurFnc->setAst( pCurRoot );
95 pCurRoot = pCurNode = NULL; 107 pCurRoot = pCurNode = NULL;
96 pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); 108 pGame->hFunction.insert( pCurFnc->getName(), pCurFnc );
109 iStackHeight = 0;
97} 110}
98 111
99void GameBuilder::beginSituation( const Bu::String &sName, Situation::InputType tInput ) 112void GameBuilder::beginSituation( const Bu::String &sName, Situation::InputType tInput )
@@ -123,6 +136,7 @@ void GameBuilder::endSituation()
123 136
124void GameBuilder::addNode( AstNode::Type iType ) 137void GameBuilder::addNode( AstNode::Type iType )
125{ 138{
139 stackMod( iType );
126 switch( iType&AstNode::tTypeMask ) 140 switch( iType&AstNode::tTypeMask )
127 { 141 {
128 case AstNode::tBranch: 142 case AstNode::tBranch:
@@ -145,6 +159,7 @@ void GameBuilder::addLiteral( const Variable &v )
145 setLiteral( v ); 159 setLiteral( v );
146 if( pCurNode ) 160 if( pCurNode )
147 { 161 {
162 stackMod( AstNode::tLeafLiteral );
148 pCurNode->addNode( new AstLeafLiteral( v ) ); 163 pCurNode->addNode( new AstLeafLiteral( v ) );
149 } 164 }
150} 165}
@@ -153,6 +168,7 @@ void GameBuilder::addVarRef( const Bu::String &sName, ScopeId sid )
153{ 168{
154 if( pCurNode ) 169 if( pCurNode )
155 { 170 {
171 stackMod( AstNode::tVarName );
156 pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, Variable::newVariableName( sName, sid ) ) ); 172 pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, Variable::newVariableName( sName, sid ) ) );
157 } 173 }
158} 174}
@@ -161,10 +177,78 @@ void GameBuilder::addFuncCall( const Bu::String &sName )
161{ 177{
162 if( pCurNode ) 178 if( pCurNode )
163 { 179 {
180 stackMod( AstNode::tFuncCall );
164 pCurNode->addNode( new AstLeafLiteral( AstNode::tFuncCall, sName ) ); 181 pCurNode->addNode( new AstLeafLiteral( AstNode::tFuncCall, sName ) );
165 } 182 }
166} 183}
167 184
185void GameBuilder::stackMod( AstNode::Type iType )
186{
187 switch( iType )
188 {
189 case AstNode::tNot:
190 case AstNode::tNegate:
191 case AstNode::tSwap:
192 break;
193
194 case AstNode::tComp:
195 case AstNode::tCompGt:
196 case AstNode::tCompLt:
197 case AstNode::tCompGtEq:
198 case AstNode::tCompLtEq:
199 case AstNode::tStore:
200 case AstNode::tAnd:
201 case AstNode::tOr:
202 case AstNode::tPlus:
203 case AstNode::tMinus:
204 case AstNode::tDivide:
205 case AstNode::tMultiply:
206 case AstNode::tPlusStore:
207 case AstNode::tMinusStore:
208 case AstNode::tDivideStore:
209 case AstNode::tMultiplyStore:
210 case AstNode::tIn:
211 case AstNode::tGoto:
212 case AstNode::tStoreRev:
213 case AstNode::tReturn: // return doesn't pop anything, but it counts as it.
214 case AstNode::tAppend:
215 case AstNode::tPop:
216 case AstNode::tIndex:
217 iStackHeight--;
218 break;
219
220 case AstNode::tInsert:
221 iStackHeight -= 2;
222 break;
223
224 case AstNode::tLeafLiteral:
225 case AstNode::tVarName:
226 case AstNode::tLiteral:
227 case AstNode::tFuncCall:
228 iStackHeight++;
229 break;
230
231 case AstNode::tBranch:
232 break;
233
234 case AstNode::tScope:
235 break;
236
237 case AstNode::tIf:
238 iStackHeight--;
239 break;
240
241 case AstNode::tForEach:
242 iStackHeight -= 2;
243 break;
244
245 case AstNode::tWhile:
246 iStackHeight -= 1;
247 break;
248
249 }
250}
251
168void GameBuilder::beginGlobal() 252void GameBuilder::beginGlobal()
169{ 253{
170 bGlobal = true; 254 bGlobal = true;
@@ -219,6 +303,7 @@ void GameBuilder::closeCommand()
219 pCurSit->csLocal.addCommand( pCurCmd ); 303 pCurSit->csLocal.addCommand( pCurCmd );
220 } 304 }
221 pCurCmd = NULL; 305 pCurCmd = NULL;
306 iStackHeight = 0;
222} 307}
223 308
224void GameBuilder::beginOption( const Bu::String &sValue ) 309void GameBuilder::beginOption( const Bu::String &sValue )
@@ -237,5 +322,6 @@ void GameBuilder::closeOption()
237 //pCurCmd->print(); 322 //pCurCmd->print();
238 pCurSit->csLocal.addCommand( pCurCmd ); 323 pCurSit->csLocal.addCommand( pCurCmd );
239 pCurCmd = NULL; 324 pCurCmd = NULL;
325 iStackHeight = 0;
240} 326}
241 327
diff --git a/src/gamebuilder.h b/src/gamebuilder.h
index 80df1bc..f64f50e 100644
--- a/src/gamebuilder.h
+++ b/src/gamebuilder.h
@@ -15,6 +15,8 @@ public:
15 15
16 void parse( const Bu::String &sFile ); 16 void parse( const Bu::String &sFile );
17 17
18 void endCmpltExpr();
19
18 class Game *getGame() { return pGame; } 20 class Game *getGame() { return pGame; }
19 21
20 void setLiteral( const Variable &v ); 22 void setLiteral( const Variable &v );
@@ -36,6 +38,8 @@ public:
36 void addVarRef( const Bu::String &sName, ScopeId sid ); 38 void addVarRef( const Bu::String &sName, ScopeId sid );
37 void addFuncCall( const Bu::String &sName ); 39 void addFuncCall( const Bu::String &sName );
38 40
41 void stackMod( AstNode::Type iType );
42
39 void beginGlobal(); 43 void beginGlobal();
40 void closeGlobal(); 44 void closeGlobal();
41 45
@@ -58,6 +62,7 @@ private:
58 class AstFunction *pCurFnc; 62 class AstFunction *pCurFnc;
59 class Situation *pCurSit; 63 class Situation *pCurSit;
60 Situation::Mode eCurSitMode; 64 Situation::Mode eCurSitMode;
65 int iStackHeight;
61}; 66};
62 67
63#endif 68#endif
diff --git a/src/gamestate.cpp b/src/gamestate.cpp
index 0a82b99..aeaaaed 100644
--- a/src/gamestate.cpp
+++ b/src/gamestate.cpp
@@ -520,8 +520,8 @@ void GameState::parse( const AstBranch::NodeList &lCode )
520 bEscape = false; 520 bEscape = false;
521 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) 521 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ )
522 { 522 {
523// sio << "Stack: " << lStack << sio.nl; 523 //sio << "Stack: " << lStack << sio.nl;
524// sio << "exec: " << (*i)->getType() << sio.nl; 524 //sio << "exec: " << (*i)->getType() << sio.nl;
525 switch( (*i)->getType() ) 525 switch( (*i)->getType() )
526 { 526 {
527 // tLeaf 527 // tLeaf
@@ -748,6 +748,10 @@ void GameState::parse( const AstBranch::NodeList &lCode )
748 } 748 }
749 break; 749 break;
750 750
751 case AstNode::tPop:
752 pop();
753 break;
754
751 // tLeafLiteral 755 // tLeafLiteral
752 case AstNode::tVarName: 756 case AstNode::tVarName:
753 case AstNode::tLiteral: 757 case AstNode::tLiteral:
diff --git a/src/parser.y b/src/parser.y
index b50b897..bbe4840 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -160,10 +160,10 @@ cmpltExprList:
160 | cmpltExprList cmpltExpr 160 | cmpltExprList cmpltExpr
161 ; 161 ;
162 162
163cmpltExpr: expr ';' 163cmpltExpr: expr ';' { bld.endCmpltExpr(); }
164 | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); } 164 | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();}
165 | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); } 165 | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();}
166 | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); } 166 | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); bld.endCmpltExpr();}
167 | ifbase 167 | ifbase
168 | tokFor tokEach { 168 | tokFor tokEach {
169 bld.addNode( AstNode::tForEach ); 169 bld.addNode( AstNode::tForEach );
@@ -269,11 +269,11 @@ expr: literal
269 ; 269 ;
270 270
271funcCallParams: 271funcCallParams:
272 | expr funcCallParamsEx 272 | expr funcCallParamsEx { bld.stackMod( AstNode::tPop ); }
273 ; 273 ;
274 274
275funcCallParamsEx: 275funcCallParamsEx:
276 | funcCallParamsEx ',' expr 276 | funcCallParamsEx ',' expr { bld.stackMod( AstNode::tPop ); }
277 ; 277 ;
278 278
279listValues: expr { bld.addNode( AstNode::tAppend ); } 279listValues: expr { bld.addNode( AstNode::tAppend ); }