diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 17:03:20 -0700 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 17:03:20 -0700 |
| commit | f404d991aa53ed81855e51b597bfe1d5c2288b42 (patch) | |
| tree | d2ce081cc722eccc089591c63ea0a345b4b4701c /src | |
| parent | 1bd7f709f5217b248fcb3a4c7be2eeca84fec795 (diff) | |
| download | stage-f404d991aa53ed81855e51b597bfe1d5c2288b42.tar.gz stage-f404d991aa53ed81855e51b597bfe1d5c2288b42.tar.bz2 stage-f404d991aa53ed81855e51b597bfe1d5c2288b42.tar.xz stage-f404d991aa53ed81855e51b597bfe1d5c2288b42.zip | |
Most operators work, as well as if/then/else.
Loops should be very easy, but we don't have lists working yet, next is
scope selection.
Diffstat (limited to 'src')
| -rw-r--r-- | src/gamestate.cpp | 83 | ||||
| -rw-r--r-- | src/main.cpp | 3 | ||||
| -rw-r--r-- | src/parser.l | 2 | ||||
| -rw-r--r-- | src/situation.cpp | 6 | ||||
| -rw-r--r-- | src/variable.cpp | 165 | ||||
| -rw-r--r-- | src/variable.h | 1 |
6 files changed, 250 insertions, 10 deletions
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 4cab5c1..5577c17 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
| @@ -118,12 +118,17 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
| 118 | { | 118 | { |
| 119 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) | 119 | for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) |
| 120 | { | 120 | { |
| 121 | sio << "Stack: " << lStack << sio.nl; | 121 | // sio << "Stack: " << lStack << sio.nl; |
| 122 | switch( (*i)->getType() ) | 122 | switch( (*i)->getType() ) |
| 123 | { | 123 | { |
| 124 | // tLeaf | 124 | // tLeaf |
| 125 | case AstNode::tNot: | 125 | case AstNode::tNot: |
| 126 | case AstNode::tComp: | 126 | case AstNode::tComp: |
| 127 | { | ||
| 128 | push( popDeref() == popDeref() ); | ||
| 129 | } | ||
| 130 | break; | ||
| 131 | |||
| 127 | case AstNode::tCompGt: | 132 | case AstNode::tCompGt: |
| 128 | case AstNode::tCompLt: | 133 | case AstNode::tCompLt: |
| 129 | case AstNode::tCompGtEq: | 134 | case AstNode::tCompGtEq: |
| @@ -147,13 +152,65 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
| 147 | break; | 152 | break; |
| 148 | 153 | ||
| 149 | case AstNode::tMinus: | 154 | case AstNode::tMinus: |
| 155 | { | ||
| 156 | Variable y = popDeref(); | ||
| 157 | Variable x = popDeref(); | ||
| 158 | lStack.push( x - y ); | ||
| 159 | } | ||
| 160 | break; | ||
| 161 | |||
| 150 | case AstNode::tDivide: | 162 | case AstNode::tDivide: |
| 163 | { | ||
| 164 | Variable y = popDeref(); | ||
| 165 | Variable x = popDeref(); | ||
| 166 | lStack.push( x / y ); | ||
| 167 | } | ||
| 168 | break; | ||
| 169 | |||
| 151 | case AstNode::tMultiply: | 170 | case AstNode::tMultiply: |
| 171 | { | ||
| 172 | Variable y = popDeref(); | ||
| 173 | Variable x = popDeref(); | ||
| 174 | lStack.push( x * y ); | ||
| 175 | } | ||
| 176 | break; | ||
| 177 | |||
| 152 | case AstNode::tPlusStore: | 178 | case AstNode::tPlusStore: |
| 179 | { | ||
| 180 | Variable y = popDeref(); | ||
| 181 | Variable x = pop(); | ||
| 182 | setVariable( x.getString(), getVariable( x.getString() ) + y ); | ||
| 183 | } | ||
| 184 | break; | ||
| 185 | |||
| 153 | case AstNode::tMinusStore: | 186 | case AstNode::tMinusStore: |
| 187 | { | ||
| 188 | Variable y = popDeref(); | ||
| 189 | Variable x = pop(); | ||
| 190 | setVariable( x.getString(), getVariable( x.getString() ) - y ); | ||
| 191 | } | ||
| 192 | break; | ||
| 193 | |||
| 154 | case AstNode::tDivideStore: | 194 | case AstNode::tDivideStore: |
| 195 | { | ||
| 196 | Variable y = popDeref(); | ||
| 197 | Variable x = pop(); | ||
| 198 | setVariable( x.getString(), getVariable( x.getString() ) / y ); | ||
| 199 | } | ||
| 200 | break; | ||
| 201 | |||
| 155 | case AstNode::tMultiplyStore: | 202 | case AstNode::tMultiplyStore: |
| 203 | { | ||
| 204 | Variable y = popDeref(); | ||
| 205 | Variable x = pop(); | ||
| 206 | setVariable( x.getString(), getVariable( x.getString() ) * y ); | ||
| 207 | } | ||
| 208 | break; | ||
| 209 | |||
| 156 | case AstNode::tNegate: | 210 | case AstNode::tNegate: |
| 211 | push( -popDeref() ); | ||
| 212 | break; | ||
| 213 | |||
| 157 | case AstNode::tIn: | 214 | case AstNode::tIn: |
| 158 | 215 | ||
| 159 | // tLeafLiteral | 216 | // tLeafLiteral |
| @@ -172,6 +229,30 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
| 172 | // tBranch | 229 | // tBranch |
| 173 | case AstNode::tScope: | 230 | case AstNode::tScope: |
| 174 | case AstNode::tIf: | 231 | case AstNode::tIf: |
| 232 | { | ||
| 233 | AstBranch::NodeList lIf = | ||
| 234 | dynamic_cast<const AstBranch *>(*i)->getNodeList(); | ||
| 235 | AstBranch::NodeList::const_iterator iIf = lIf.begin(); | ||
| 236 | parse( dynamic_cast<const AstBranch *>(*iIf)->getNodeList() ); | ||
| 237 | Variable v = popDeref(); | ||
| 238 | if( v.getType() != Variable::tBool ) | ||
| 239 | throw Bu::ExceptionBase("conditional did not evaluate to boolean."); | ||
| 240 | iIf++; | ||
| 241 | if( v.getBool() ) | ||
| 242 | { | ||
| 243 | parse( dynamic_cast<const AstBranch *>(*iIf)-> | ||
| 244 | getNodeList() ); | ||
| 245 | } | ||
| 246 | else | ||
| 247 | { | ||
| 248 | iIf++; | ||
| 249 | if( iIf ) | ||
| 250 | parse( dynamic_cast<const AstBranch *>(*iIf)-> | ||
| 251 | getNodeList() ); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | break; | ||
| 255 | |||
| 175 | case AstNode::tForEach: | 256 | case AstNode::tForEach: |
| 176 | case AstNode::tWhile: | 257 | case AstNode::tWhile: |
| 177 | break; | 258 | break; |
diff --git a/src/main.cpp b/src/main.cpp index 8fe5247..273158b 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
| @@ -30,6 +30,9 @@ int main( int argc, char *argv[] ) | |||
| 30 | GameState gs( pGame ); | 30 | GameState gs( pGame ); |
| 31 | gs.init(); | 31 | gs.init(); |
| 32 | 32 | ||
| 33 | gs.gotoSituation("stuff"); | ||
| 34 | gs.gotoSituation("start"); | ||
| 35 | |||
| 33 | return 0; | 36 | return 0; |
| 34 | } | 37 | } |
| 35 | 38 | ||
diff --git a/src/parser.l b/src/parser.l index 3491d27..1743dd6 100644 --- a/src/parser.l +++ b/src/parser.l | |||
| @@ -76,7 +76,9 @@ null { return tokNull; } | |||
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | ([1-9][0-9]*)?\.[0-9]* { | 78 | ([1-9][0-9]*)?\.[0-9]* { |
| 79 | printf("Parsing float: %s\n", yytext ); | ||
| 79 | yylval->dValue = strtod( yytext, NULL ); | 80 | yylval->dValue = strtod( yytext, NULL ); |
| 81 | printf("Final float: %f\n", yylval->dValue ); | ||
| 80 | return tokFloat; | 82 | return tokFloat; |
| 81 | } | 83 | } |
| 82 | 84 | ||
diff --git a/src/situation.cpp b/src/situation.cpp index d7f98aa..8221b50 100644 --- a/src/situation.cpp +++ b/src/situation.cpp | |||
| @@ -37,11 +37,13 @@ void Situation::exec( class GameState &gState, Situation::Mode m ) | |||
| 37 | switch( m ) | 37 | switch( m ) |
| 38 | { | 38 | { |
| 39 | case modeSetup: | 39 | case modeSetup: |
| 40 | gState.parse( pAstSetup ); | 40 | if( pAstSetup ) |
| 41 | gState.parse( pAstSetup ); | ||
| 41 | break; | 42 | break; |
| 42 | 43 | ||
| 43 | case modeEnter: | 44 | case modeEnter: |
| 44 | gState.parse( pAstEnter ); | 45 | if( pAstEnter ) |
| 46 | gState.parse( pAstEnter ); | ||
| 45 | break; | 47 | break; |
| 46 | } | 48 | } |
| 47 | } | 49 | } |
diff --git a/src/variable.cpp b/src/variable.cpp index b7899f1..d5bde2e 100644 --- a/src/variable.cpp +++ b/src/variable.cpp | |||
| @@ -349,11 +349,162 @@ Variable Variable::operator+( const Variable &rhs ) const | |||
| 349 | } | 349 | } |
| 350 | } | 350 | } |
| 351 | 351 | ||
| 352 | /* | 352 | Variable Variable::operator-( const Variable &rhs ) const |
| 353 | Variable Variable::operator-( const Variable &rhs ) const; | 353 | { |
| 354 | Variable Variable::operator*( const Variable &rhs ) const; | 354 | if( eType != rhs.eType ) |
| 355 | Variable Variable::operator/( const Variable &rhs ) const; | 355 | { |
| 356 | */ | 356 | throw VariableException("Subtracting between dissimilar types is not yet supported."); |
| 357 | } | ||
| 358 | else | ||
| 359 | { | ||
| 360 | switch( eType ) | ||
| 361 | { | ||
| 362 | case tNull: | ||
| 363 | throw VariableException("You cannot subtract nulls."); | ||
| 364 | |||
| 365 | case tBool: | ||
| 366 | throw VariableException("You cannot subtract booleans."); | ||
| 367 | |||
| 368 | case tInt: | ||
| 369 | return Variable( iValue - rhs.iValue ); | ||
| 370 | |||
| 371 | case tFloat: | ||
| 372 | return Variable( fValue - rhs.fValue ); | ||
| 373 | |||
| 374 | case tString: | ||
| 375 | throw VariableException("You cannot subtract strings."); | ||
| 376 | |||
| 377 | case tList: | ||
| 378 | throw VariableException("You cannot subtract lists...yet."); | ||
| 379 | // TODO: make this work | ||
| 380 | |||
| 381 | case tDictionary: | ||
| 382 | throw VariableException("You cannot subtract dictionaries."); | ||
| 383 | break; | ||
| 384 | |||
| 385 | case tSituation: | ||
| 386 | throw VariableException("You cannot subtract situations."); | ||
| 387 | |||
| 388 | case tVariable: | ||
| 389 | throw VariableException("You cannot subtract variables."); | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | Variable Variable::operator*( const Variable &rhs ) const | ||
| 395 | { | ||
| 396 | if( eType != rhs.eType ) | ||
| 397 | { | ||
| 398 | throw VariableException("Subtracting between dissimilar types is not yet supported."); | ||
| 399 | } | ||
| 400 | else | ||
| 401 | { | ||
| 402 | switch( eType ) | ||
| 403 | { | ||
| 404 | case tNull: | ||
| 405 | throw VariableException("You cannot multiply nulls."); | ||
| 406 | |||
| 407 | case tBool: | ||
| 408 | throw VariableException("You cannot multiply booleans."); | ||
| 409 | |||
| 410 | case tInt: | ||
| 411 | return Variable( iValue * rhs.iValue ); | ||
| 412 | |||
| 413 | case tFloat: | ||
| 414 | return Variable( fValue * rhs.fValue ); | ||
| 415 | |||
| 416 | case tString: | ||
| 417 | throw VariableException("You cannot multiply strings."); | ||
| 418 | |||
| 419 | case tList: | ||
| 420 | throw VariableException("You cannot multiply lists."); | ||
| 421 | |||
| 422 | case tDictionary: | ||
| 423 | throw VariableException("You cannot multiply dictionaries."); | ||
| 424 | break; | ||
| 425 | |||
| 426 | case tSituation: | ||
| 427 | throw VariableException("You cannot multiply situations."); | ||
| 428 | |||
| 429 | case tVariable: | ||
| 430 | throw VariableException("You cannot multiply variables."); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | } | ||
| 434 | |||
| 435 | Variable Variable::operator/( const Variable &rhs ) const | ||
| 436 | { | ||
| 437 | if( eType != rhs.eType ) | ||
| 438 | { | ||
| 439 | throw VariableException("Subtracting between dissimilar types is not yet supported."); | ||
| 440 | } | ||
| 441 | else | ||
| 442 | { | ||
| 443 | switch( eType ) | ||
| 444 | { | ||
| 445 | case tNull: | ||
| 446 | throw VariableException("You cannot divide nulls."); | ||
| 447 | |||
| 448 | case tBool: | ||
| 449 | throw VariableException("You cannot divide booleans."); | ||
| 450 | |||
| 451 | case tInt: | ||
| 452 | return Variable( iValue / rhs.iValue ); | ||
| 453 | |||
| 454 | case tFloat: | ||
| 455 | return Variable( fValue / rhs.fValue ); | ||
| 456 | |||
| 457 | case tString: | ||
| 458 | throw VariableException("You cannot divide strings."); | ||
| 459 | |||
| 460 | case tList: | ||
| 461 | throw VariableException("You cannot divide lists."); | ||
| 462 | |||
| 463 | case tDictionary: | ||
| 464 | throw VariableException("You cannot divide dictionaries."); | ||
| 465 | break; | ||
| 466 | |||
| 467 | case tSituation: | ||
| 468 | throw VariableException("You cannot divide situations."); | ||
| 469 | |||
| 470 | case tVariable: | ||
| 471 | throw VariableException("You cannot divide variables."); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | Variable Variable::operator-() const | ||
| 477 | { | ||
| 478 | switch( eType ) | ||
| 479 | { | ||
| 480 | case tNull: | ||
| 481 | throw VariableException("You cannot negate nulls."); | ||
| 482 | |||
| 483 | case tBool: | ||
| 484 | throw VariableException("You cannot negate booleans."); | ||
| 485 | |||
| 486 | case tInt: | ||
| 487 | return Variable( -iValue ); | ||
| 488 | |||
| 489 | case tFloat: | ||
| 490 | return Variable( -fValue ); | ||
| 491 | |||
| 492 | case tString: | ||
| 493 | throw VariableException("You cannot negate strings."); | ||
| 494 | |||
| 495 | case tList: | ||
| 496 | throw VariableException("You cannot negate lists."); | ||
| 497 | |||
| 498 | case tDictionary: | ||
| 499 | throw VariableException("You cannot negate dictionaries."); | ||
| 500 | |||
| 501 | case tSituation: | ||
| 502 | throw VariableException("You cannot negate situations."); | ||
| 503 | |||
| 504 | case tVariable: | ||
| 505 | throw VariableException("You cannot negate variables."); | ||
| 506 | } | ||
| 507 | } | ||
| 357 | 508 | ||
| 358 | bool Variable::operator==( const Variable &rhs ) const | 509 | bool Variable::operator==( const Variable &rhs ) const |
| 359 | { | 510 | { |
| @@ -482,10 +633,10 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) | |||
| 482 | return f << v.bValue; | 633 | return f << v.bValue; |
| 483 | 634 | ||
| 484 | case Variable::tInt: | 635 | case Variable::tInt: |
| 485 | return f << v.iValue; | 636 | return f << v.iValue << "i"; |
| 486 | 637 | ||
| 487 | case Variable::tFloat: | 638 | case Variable::tFloat: |
| 488 | return f << v.fValue; | 639 | return f << v.fValue << "f"; |
| 489 | 640 | ||
| 490 | case Variable::tString: | 641 | case Variable::tString: |
| 491 | return f << '"' << *v.sValue << '"'; | 642 | return f << '"' << *v.sValue << '"'; |
diff --git a/src/variable.h b/src/variable.h index e1bc30c..42dd865 100644 --- a/src/variable.h +++ b/src/variable.h | |||
| @@ -55,6 +55,7 @@ public: | |||
| 55 | Variable operator-( const Variable &rhs ) const; | 55 | Variable operator-( const Variable &rhs ) const; |
| 56 | Variable operator*( const Variable &rhs ) const; | 56 | Variable operator*( const Variable &rhs ) const; |
| 57 | Variable operator/( const Variable &rhs ) const; | 57 | Variable operator/( const Variable &rhs ) const; |
| 58 | Variable operator-() const; | ||
| 58 | bool operator==( const Variable &rhs ) const; | 59 | bool operator==( const Variable &rhs ) const; |
| 59 | bool operator!=( const Variable &rhs ) const; | 60 | bool operator!=( const Variable &rhs ) const; |
| 60 | bool operator>( const Variable &rhs ) const; | 61 | bool operator>( const Variable &rhs ) const; |
