From f404d991aa53ed81855e51b597bfe1d5c2288b42 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Dec 2011 17:03:20 -0700 Subject: 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. --- src/gamestate.cpp | 83 ++++++++++++++++++++++++++- src/main.cpp | 3 + src/parser.l | 2 + src/situation.cpp | 6 +- src/variable.cpp | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/variable.h | 1 + test.stage | 13 ++++- 7 files changed, 262 insertions(+), 11 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 ) { for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) { - sio << "Stack: " << lStack << sio.nl; +// sio << "Stack: " << lStack << sio.nl; switch( (*i)->getType() ) { // tLeaf case AstNode::tNot: case AstNode::tComp: + { + push( popDeref() == popDeref() ); + } + break; + case AstNode::tCompGt: case AstNode::tCompLt: case AstNode::tCompGtEq: @@ -147,13 +152,65 @@ void GameState::parse( const AstBranch::NodeList &lCode ) break; case AstNode::tMinus: + { + Variable y = popDeref(); + Variable x = popDeref(); + lStack.push( x - y ); + } + break; + case AstNode::tDivide: + { + Variable y = popDeref(); + Variable x = popDeref(); + lStack.push( x / y ); + } + break; + case AstNode::tMultiply: + { + Variable y = popDeref(); + Variable x = popDeref(); + lStack.push( x * y ); + } + break; + case AstNode::tPlusStore: + { + Variable y = popDeref(); + Variable x = pop(); + setVariable( x.getString(), getVariable( x.getString() ) + y ); + } + break; + case AstNode::tMinusStore: + { + Variable y = popDeref(); + Variable x = pop(); + setVariable( x.getString(), getVariable( x.getString() ) - y ); + } + break; + case AstNode::tDivideStore: + { + Variable y = popDeref(); + Variable x = pop(); + setVariable( x.getString(), getVariable( x.getString() ) / y ); + } + break; + case AstNode::tMultiplyStore: + { + Variable y = popDeref(); + Variable x = pop(); + setVariable( x.getString(), getVariable( x.getString() ) * y ); + } + break; + case AstNode::tNegate: + push( -popDeref() ); + break; + case AstNode::tIn: // tLeafLiteral @@ -172,6 +229,30 @@ void GameState::parse( const AstBranch::NodeList &lCode ) // tBranch case AstNode::tScope: case AstNode::tIf: + { + AstBranch::NodeList lIf = + dynamic_cast(*i)->getNodeList(); + AstBranch::NodeList::const_iterator iIf = lIf.begin(); + parse( dynamic_cast(*iIf)->getNodeList() ); + Variable v = popDeref(); + if( v.getType() != Variable::tBool ) + throw Bu::ExceptionBase("conditional did not evaluate to boolean."); + iIf++; + if( v.getBool() ) + { + parse( dynamic_cast(*iIf)-> + getNodeList() ); + } + else + { + iIf++; + if( iIf ) + parse( dynamic_cast(*iIf)-> + getNodeList() ); + } + } + break; + case AstNode::tForEach: case AstNode::tWhile: 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[] ) GameState gs( pGame ); gs.init(); + gs.gotoSituation("stuff"); + gs.gotoSituation("start"); + return 0; } 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; } } ([1-9][0-9]*)?\.[0-9]* { + printf("Parsing float: %s\n", yytext ); yylval->dValue = strtod( yytext, NULL ); + printf("Final float: %f\n", yylval->dValue ); return tokFloat; } 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 ) switch( m ) { case modeSetup: - gState.parse( pAstSetup ); + if( pAstSetup ) + gState.parse( pAstSetup ); break; case modeEnter: - gState.parse( pAstEnter ); + if( pAstEnter ) + gState.parse( pAstEnter ); break; } } 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 } } -/* -Variable Variable::operator-( const Variable &rhs ) const; -Variable Variable::operator*( const Variable &rhs ) const; -Variable Variable::operator/( const Variable &rhs ) const; -*/ +Variable Variable::operator-( const Variable &rhs ) const +{ + if( eType != rhs.eType ) + { + throw VariableException("Subtracting between dissimilar types is not yet supported."); + } + else + { + switch( eType ) + { + case tNull: + throw VariableException("You cannot subtract nulls."); + + case tBool: + throw VariableException("You cannot subtract booleans."); + + case tInt: + return Variable( iValue - rhs.iValue ); + + case tFloat: + return Variable( fValue - rhs.fValue ); + + case tString: + throw VariableException("You cannot subtract strings."); + + case tList: + throw VariableException("You cannot subtract lists...yet."); + // TODO: make this work + + case tDictionary: + throw VariableException("You cannot subtract dictionaries."); + break; + + case tSituation: + throw VariableException("You cannot subtract situations."); + + case tVariable: + throw VariableException("You cannot subtract variables."); + } + } +} + +Variable Variable::operator*( const Variable &rhs ) const +{ + if( eType != rhs.eType ) + { + throw VariableException("Subtracting between dissimilar types is not yet supported."); + } + else + { + switch( eType ) + { + case tNull: + throw VariableException("You cannot multiply nulls."); + + case tBool: + throw VariableException("You cannot multiply booleans."); + + case tInt: + return Variable( iValue * rhs.iValue ); + + case tFloat: + return Variable( fValue * rhs.fValue ); + + case tString: + throw VariableException("You cannot multiply strings."); + + case tList: + throw VariableException("You cannot multiply lists."); + + case tDictionary: + throw VariableException("You cannot multiply dictionaries."); + break; + + case tSituation: + throw VariableException("You cannot multiply situations."); + + case tVariable: + throw VariableException("You cannot multiply variables."); + } + } +} + +Variable Variable::operator/( const Variable &rhs ) const +{ + if( eType != rhs.eType ) + { + throw VariableException("Subtracting between dissimilar types is not yet supported."); + } + else + { + switch( eType ) + { + case tNull: + throw VariableException("You cannot divide nulls."); + + case tBool: + throw VariableException("You cannot divide booleans."); + + case tInt: + return Variable( iValue / rhs.iValue ); + + case tFloat: + return Variable( fValue / rhs.fValue ); + + case tString: + throw VariableException("You cannot divide strings."); + + case tList: + throw VariableException("You cannot divide lists."); + + case tDictionary: + throw VariableException("You cannot divide dictionaries."); + break; + + case tSituation: + throw VariableException("You cannot divide situations."); + + case tVariable: + throw VariableException("You cannot divide variables."); + } + } +} + +Variable Variable::operator-() const +{ + switch( eType ) + { + case tNull: + throw VariableException("You cannot negate nulls."); + + case tBool: + throw VariableException("You cannot negate booleans."); + + case tInt: + return Variable( -iValue ); + + case tFloat: + return Variable( -fValue ); + + case tString: + throw VariableException("You cannot negate strings."); + + case tList: + throw VariableException("You cannot negate lists."); + + case tDictionary: + throw VariableException("You cannot negate dictionaries."); + + case tSituation: + throw VariableException("You cannot negate situations."); + + case tVariable: + throw VariableException("You cannot negate variables."); + } +} bool Variable::operator==( const Variable &rhs ) const { @@ -482,10 +633,10 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) return f << v.bValue; case Variable::tInt: - return f << v.iValue; + return f << v.iValue << "i"; case Variable::tFloat: - return f << v.fValue; + return f << v.fValue << "f"; case Variable::tString: 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: Variable operator-( const Variable &rhs ) const; Variable operator*( const Variable &rhs ) const; Variable operator/( const Variable &rhs ) const; + Variable operator-() const; bool operator==( const Variable &rhs ) const; bool operator!=( const Variable &rhs ) const; bool operator>( const Variable &rhs ) const; diff --git a/test.stage b/test.stage index 2b6e567..75bff27 100644 --- a/test.stage +++ b/test.stage @@ -18,8 +18,19 @@ situation <> setup { name = "Bob"; + name += " The Man"; display("This is the setup phase for start, " + name); - display("???"); + display( 5 == 6 ); + display( 5 == 5 ); + + if name == "Bob The Man" then + { + display("You are bob"); + } + else + { + display("You are not bob"); + } } enter -- cgit v1.2.3