From 70076bb00bdedb57405ed2ef27e2fec172e2f38a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Dec 2011 23:07:59 -0700 Subject: Well, +=, -= on dictionaries/lists works now. --- src/functiondisplay.cpp | 8 +++--- src/gamestate.cpp | 10 ++++--- src/variable.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp index 4790844..ab37a08 100644 --- a/src/functiondisplay.cpp +++ b/src/functiondisplay.cpp @@ -15,10 +15,12 @@ FunctionDisplay::~FunctionDisplay() void FunctionDisplay::call( class GameState &gState ) { - Bu::String s = gState.popDeref().to( Variable::tString ).getString(); +// Bu::String s = gState.popDeref().to( Variable::tString ).getString(); - sio << format( s ) << sio.nl; -// sio << "Display: " << v << sio.nl; +// sio << format( s ) << sio.nl; + + Variable v = gState.popDeref(); + sio << "Display: " << v << sio.nl; } Bu::String FunctionDisplay::format( const Bu::String &sSrc ) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index bd3e638..4af49aa 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -427,8 +427,9 @@ void GameState::parse( const AstBranch::NodeList &lCode ) { Variable y = popDeref(); Variable x = pop(); - VariableRef r = x.getVariableRef(); - setVariable( r.sName, getVariable( r.sName, r.sid ) + y, r.sid ); + deref( x ) += y; +// VariableRef r = x.getVariableRef(); +// setVariable( r.sName, getVariable( r.sName, r.sid ) + y, r.sid ); } break; @@ -436,8 +437,9 @@ void GameState::parse( const AstBranch::NodeList &lCode ) { Variable y = popDeref(); Variable x = pop(); - VariableRef r = x.getVariableRef(); - setVariable( r.sName, getVariable( r.sName, r.sid ) - y, r.sid ); + deref( x ) -= y; +// VariableRef r = x.getVariableRef(); +// setVariable( r.sName, getVariable( r.sName, r.sid ) - y, r.sid ); } break; diff --git a/src/variable.cpp b/src/variable.cpp index 1100c84..0ce9793 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -363,8 +363,77 @@ Variable &Variable::operator+=( const Variable &rhs ) return *this; } +Variable &Variable::operator-=( const Variable &rhs ) +{ + switch( eType ) + { + case tNull: + throw VariableException("You cannot subtract nulls."); + + case tBool: + throw VariableException("You cannot subtract 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 subtract strings."); + break; + + case tSituation: + throw VariableException("You cannot subtract situations."); + break; + + case tVariable: + throw VariableException("You cannot subtract variable names."); + break; + + case tList: + (*lValue).erase( rhs ); + break; + + case tDictionary: + (*hValue).erase( rhs ); + break; + } + + return *this; +} + /* -Variable &Variable::operator-=( const Variable &rhs ); Variable &Variable::operator*=( const Variable &rhs ); Variable &Variable::operator/=( const Variable &rhs ); */ -- cgit v1.2.3