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 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 145 insertions(+), 15 deletions(-) (limited to 'src') 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 ) -- cgit v1.2.3