diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 22:27:59 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 22:27:59 -0700 |
commit | f66458278ce3663397fc985a1253c85b74f011e6 (patch) | |
tree | 785e2fb51a91fe277c698ea84009e0052ef6555e /src/gamestate.cpp | |
parent | f404d991aa53ed81855e51b597bfe1d5c2288b42 (diff) | |
download | stage-f66458278ce3663397fc985a1253c85b74f011e6.tar.gz stage-f66458278ce3663397fc985a1253c85b74f011e6.tar.bz2 stage-f66458278ce3663397fc985a1253c85b74f011e6.tar.xz stage-f66458278ce3663397fc985a1253c85b74f011e6.zip |
Most AstNodes work now.
Next up: loops, proper variable references with scopes, and gotos.
Diffstat (limited to 'src/gamestate.cpp')
-rw-r--r-- | src/gamestate.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 5577c17..14a7b53 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
@@ -51,7 +51,9 @@ void GameState::gotoSituation( const Bu::String &sName ) | |||
51 | 51 | ||
52 | void GameState::callFunction( const Bu::String &sName ) | 52 | void GameState::callFunction( const Bu::String &sName ) |
53 | { | 53 | { |
54 | lsLocal.push( new Scope() ); | ||
54 | pGame->getFunction( sName )->call( *this ); | 55 | pGame->getFunction( sName )->call( *this ); |
56 | delete lsLocal.peekPop(); | ||
55 | } | 57 | } |
56 | 58 | ||
57 | Variable GameState::getVariable( const Bu::String &sName, ScopeId id ) | 59 | Variable GameState::getVariable( const Bu::String &sName, ScopeId id ) |
@@ -119,10 +121,19 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
119 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) | 121 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) |
120 | { | 122 | { |
121 | // sio << "Stack: " << lStack << sio.nl; | 123 | // sio << "Stack: " << lStack << sio.nl; |
124 | // sio << "exec: " << (*i)->getType() << sio.nl; | ||
122 | switch( (*i)->getType() ) | 125 | switch( (*i)->getType() ) |
123 | { | 126 | { |
124 | // tLeaf | 127 | // tLeaf |
125 | case AstNode::tNot: | 128 | case AstNode::tNot: |
129 | { | ||
130 | Variable x = popDeref(); | ||
131 | if( x.getType() != Variable::tBool ) | ||
132 | throw Bu::ExceptionBase("Non-bool used with logical not operator."); | ||
133 | push( Variable( !x.getBool() ) ); | ||
134 | } | ||
135 | break; | ||
136 | |||
126 | case AstNode::tComp: | 137 | case AstNode::tComp: |
127 | { | 138 | { |
128 | push( popDeref() == popDeref() ); | 139 | push( popDeref() == popDeref() ); |
@@ -130,9 +141,37 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
130 | break; | 141 | break; |
131 | 142 | ||
132 | case AstNode::tCompGt: | 143 | case AstNode::tCompGt: |
144 | { | ||
145 | Variable y = popDeref(); | ||
146 | Variable x = popDeref(); | ||
147 | push( x > y ); | ||
148 | } | ||
149 | break; | ||
150 | |||
133 | case AstNode::tCompLt: | 151 | case AstNode::tCompLt: |
152 | { | ||
153 | Variable y = popDeref(); | ||
154 | Variable x = popDeref(); | ||
155 | push( x < y ); | ||
156 | } | ||
157 | break; | ||
158 | |||
134 | case AstNode::tCompGtEq: | 159 | case AstNode::tCompGtEq: |
160 | { | ||
161 | Variable y = popDeref(); | ||
162 | Variable x = popDeref(); | ||
163 | push( x >= y ); | ||
164 | } | ||
165 | break; | ||
166 | |||
135 | case AstNode::tCompLtEq: | 167 | case AstNode::tCompLtEq: |
168 | { | ||
169 | Variable y = popDeref(); | ||
170 | Variable x = popDeref(); | ||
171 | push( x <= y ); | ||
172 | } | ||
173 | break; | ||
174 | |||
136 | case AstNode::tStore: | 175 | case AstNode::tStore: |
137 | { | 176 | { |
138 | Variable y = popDeref(); | 177 | Variable y = popDeref(); |
@@ -142,7 +181,31 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
142 | break; | 181 | break; |
143 | 182 | ||
144 | case AstNode::tAnd: | 183 | case AstNode::tAnd: |
184 | { | ||
185 | Variable y = popDeref(); | ||
186 | Variable x = popDeref(); | ||
187 | if( x.getType() != Variable::tBool || | ||
188 | y.getType() != Variable::tBool ) | ||
189 | { | ||
190 | throw Bu::ExceptionBase("Non-bool used in logical AND operator."); | ||
191 | } | ||
192 | lStack.push( Variable( x.getBool() && y.getBool() ) ); | ||
193 | } | ||
194 | break; | ||
195 | |||
145 | case AstNode::tOr: | 196 | case AstNode::tOr: |
197 | { | ||
198 | Variable y = popDeref(); | ||
199 | Variable x = popDeref(); | ||
200 | if( x.getType() != Variable::tBool || | ||
201 | y.getType() != Variable::tBool ) | ||
202 | { | ||
203 | throw Bu::ExceptionBase("Non-bool used in logical OR operator."); | ||
204 | } | ||
205 | lStack.push( Variable( x.getBool() || y.getBool() ) ); | ||
206 | } | ||
207 | break; | ||
208 | |||
146 | case AstNode::tPlus: | 209 | case AstNode::tPlus: |
147 | { | 210 | { |
148 | Variable y = popDeref(); | 211 | Variable y = popDeref(); |
@@ -212,6 +275,15 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
212 | break; | 275 | break; |
213 | 276 | ||
214 | case AstNode::tIn: | 277 | case AstNode::tIn: |
278 | case AstNode::tGoto: | ||
279 | case AstNode::tSwap: | ||
280 | { | ||
281 | Variable y = pop(); | ||
282 | Variable x = pop(); | ||
283 | push( y ); | ||
284 | push( x ); | ||
285 | } | ||
286 | break; | ||
215 | 287 | ||
216 | // tLeafLiteral | 288 | // tLeafLiteral |
217 | case AstNode::tVarName: | 289 | case AstNode::tVarName: |