From 3a0f68b527cc85bd9847127085415739af784eca Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Jan 2012 23:58:34 -0700 Subject: Fixes for +=, -=, *=, /= --- src/gamestate.cpp | 14 ++---- src/variable.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- test.stage | 32 ++++-------- 3 files changed, 155 insertions(+), 37 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 57dbe2f..285c151 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -427,34 +427,28 @@ void GameState::parse( const AstBranch::NodeList &lCode ) case AstNode::tPlusStore: { Variable y = popDeref(); - Variable x = pop(); - deref( x ) += y; + deref( lStack.peek(), true ) += y; } break; case AstNode::tMinusStore: { Variable y = popDeref(); - Variable x = pop(); - deref( x ) -= y; + deref( lStack.peek(), true ) -= y; } break; case AstNode::tDivideStore: { Variable y = popDeref(); - Variable x = pop(); - VariableRef r = x.getVariableRef(); - setVariable( r.sName, getVariable( r.sName, r.sid ) / y, r.sid ); + deref( lStack.peek(), true ) /= y; } break; case AstNode::tMultiplyStore: { Variable y = popDeref(); - Variable x = pop(); - VariableRef r = x.getVariableRef(); - setVariable( r.sName, getVariable( r.sName, r.sid ) * y, r.sid ); + deref( lStack.peek(), true ) *= y; } break; diff --git a/src/variable.cpp b/src/variable.cpp index 2ea8334..68a1778 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -417,7 +417,7 @@ Variable &Variable::operator-=( const Variable &rhs ) break; default: - throw VariableException("Int += invalid..."); + throw VariableException("Int -= invalid..."); } break; @@ -445,10 +445,146 @@ Variable &Variable::operator-=( const Variable &rhs ) return *this; } -/* -Variable &Variable::operator*=( const Variable &rhs ); -Variable &Variable::operator/=( const Variable &rhs ); -*/ +Variable &Variable::operator*=( const Variable &rhs ) +{ + switch( eType ) + { + case tNull: + throw VariableException("You cannot multiply nulls."); + + case tBool: + throw VariableException("You cannot multiply bools."); + + case tInt: + switch( rhs.eType ) + { + case tInt: + iValue *= rhs.iValue; + break; + + case tFloat: + { + double dTmp = iValue; + eType = tFloat; + fValue = dTmp * rhs.fValue; + } + break; + + default: + throw VariableException("Int *= invalid..."); + } + break; + + case tFloat: + switch( rhs.eType ) + { + case tInt: + fValue *= rhs.iValue; + break; + + case tFloat: + fValue *= rhs.fValue; + break; + + default: + throw VariableException("Int *= invalid..."); + } + break; + + case tString: + throw VariableException("You cannot multiply strings."); + break; + + case tSituation: + throw VariableException("You cannot multiply situations."); + break; + + case tVariable: + throw VariableException("You cannot multiply variable names."); + break; + + case tList: + throw VariableException("You cannot multiply lists."); + break; + + case tDictionary: + throw VariableException("You cannot multiply dictionaries."); + break; + } + + return *this; +} + +Variable &Variable::operator/=( const Variable &rhs ) +{ + switch( eType ) + { + case tNull: + throw VariableException("You cannot divide nulls."); + + case tBool: + throw VariableException("You cannot divide bools."); + + case tInt: + switch( rhs.eType ) + { + case tInt: + iValue /= rhs.iValue; + break; + + case tFloat: + { + double dTmp = iValue; + eType = tFloat; + fValue = dTmp / rhs.fValue; + } + break; + + default: + throw VariableException("Int /= invalid..."); + } + break; + + case tFloat: + switch( rhs.eType ) + { + case tInt: + fValue /= rhs.iValue; + break; + + case tFloat: + fValue /= rhs.fValue; + break; + + default: + throw VariableException("Int /= invalid..."); + } + break; + + case tString: + throw VariableException("You cannot divide strings."); + break; + + case tSituation: + throw VariableException("You cannot divide situations."); + break; + + case tVariable: + throw VariableException("You cannot divide variable names."); + break; + + case tList: + throw VariableException("You cannot divide lists."); + break; + + case tDictionary: + throw VariableException("You cannot divide dictionaries."); + break; + } + + return *this; +} + Variable Variable::operator+( const Variable &rhs ) const { if( eType != rhs.eType ) diff --git a/test.stage b/test.stage index b963b2b..654a1e8 100644 --- a/test.stage +++ b/test.stage @@ -13,32 +13,20 @@ global } } -function subfunction() -{ - if true then - { - return(); - } - display("subfunction()"); -} - -function hello() -{ - if true == true then - { - subfunction(); - display("Yeah!"); - return(); - } - display("hello()"); -} - situation <> { setup { - hello(); - display("situation"); + x = 10; + display( x ); + x /= 2; + display( x ); + x *= 4; + display( x ); + x -= 10; + display( x ); + x += 20; + display( x ); exit(); } -- cgit v1.2.3