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. --- demo.stage | 116 ++++++++++++++++++++++++++++++++++--------- src/functiondisplay.cpp | 8 +-- src/gamestate.cpp | 10 ++-- src/variable.cpp | 71 +++++++++++++++++++++++++- test.stage | 129 +++++------------------------------------------- 5 files changed, 187 insertions(+), 147 deletions(-) diff --git a/demo.stage b/demo.stage index 67c6063..00e7d94 100644 --- a/demo.stage +++ b/demo.stage @@ -7,65 +7,135 @@ game.start = <>; global { - command: "take" item + command: "go" dir { - if item in situation.items then + if exists(situation.exits) then { - player.inventory += item; - situation.items -= item; - display('''You pickup the ''' + item + - ''' and put it in your pocket.'''); - } - else - { - display('''You don't see anything like that here.'''); + if dir in situation.exits then + { + goto( situation.exits[dir] ); + } } + display('''You're not really sure how to do that...'''); } - command: "use" item + command: "exits" { - if not item in player.inventory then + if exists(situation.exits) then { - display('''You don't have anything like that in your pocket.'''); + out = "Obvious exits are: "; + bFirst = true; + for each dir : dest in situation.exits do + { + if bFirst then + { + bFirst = false; + } + else + { + out += ", "; + } + out += dir; + } + display( out ); } else { + display("There are no obvious exits."); } } + + // You should always have a global exit, quit, escape, something for + // dev-testing at least. + command: "exit" + { + exit(); + } +} + +function square( x ) +{ + return( x*x ); } -situation <
> +function sillyDisplay( txt, extra ) { - command: "there" hi + display("!~! " + txt + " !~!"); + if extra then { + display("And then some extra!"); } + else + { + display("...no extra for you"); + } +} + +function myGoto( txt ) +{ + display( txt ); + goto( txt ); +} + +function getThing() +{ + display( situation.thing ); +} + +situation <> +{ setup { - player.inventory = []; + goto( <> ); } + enter { } } -function hello() +situation <> { - bob = 55 * 2 + 1; + setup + { + situation.exits = { + 'south': <>, + 'east': <> + }; + } + enter + { + display('''You are in the dining room, it's very nice and warm. There + is a big table and...stuff. Looks like the living room is south, and + the kitchen is to the east.'''); + } } -situation <> +situation <> { + setup + { + situation.exits = { + 'north': <> + }; + } enter { - display('''You are here, there is a wrench sitting on the table.'''); - test("use", bob ); + display('''Living room!'''); } } -situation <> +situation <> { + setup + { + situation.exits = { + 'west': <> + }; + } + enter { - goto( <> ); + display('''Kitchen!'''); } } 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 ); */ diff --git a/test.stage b/test.stage index 00e7d94..9e5c00a 100644 --- a/test.stage +++ b/test.stage @@ -1,5 +1,5 @@ -game.title = "Demo game"; +game.title = "Code Test"; game.author = "Mike Buland"; game.version = 1; game.revision = 0; @@ -7,135 +7,32 @@ game.start = <>; global { - command: "go" dir - { - if exists(situation.exits) then - { - if dir in situation.exits then - { - goto( situation.exits[dir] ); - } - } - display('''You're not really sure how to do that...'''); - } - - command: "exits" - { - if exists(situation.exits) then - { - out = "Obvious exits are: "; - bFirst = true; - for each dir : dest in situation.exits do - { - if bFirst then - { - bFirst = false; - } - else - { - out += ", "; - } - out += dir; - } - display( out ); - } - else - { - display("There are no obvious exits."); - } - } - - // You should always have a global exit, quit, escape, something for - // dev-testing at least. command: "exit" { exit(); } } -function square( x ) -{ - return( x*x ); -} - -function sillyDisplay( txt, extra ) -{ - display("!~! " + txt + " !~!"); - if extra then - { - display("And then some extra!"); - } - else - { - display("...no extra for you"); - } -} - -function myGoto( txt ) -{ - display( txt ); - goto( txt ); -} - -function getThing() -{ - display( situation.thing ); -} - situation <> { setup { - goto( <> ); - } - - enter - { - } -} - -situation <> -{ - setup - { - situation.exits = { - 'south': <>, - 'east': <> - }; - } - enter - { - display('''You are in the dining room, it's very nice and warm. There - is a big table and...stuff. Looks like the living room is south, and - the kitchen is to the east.'''); + dict = {1: "Hello"}; + dict['bob'] = 'yup'; + display( dict ); + dict -= 1; + display( dict ); + + lst = [55]; + lst += 112; + display( lst ); + lst -= 55; + display( lst ); + exit(); } -} -situation <> -{ - setup - { - situation.exits = { - 'north': <> - }; - } enter { - display('''Living room!'''); } } -situation <> -{ - setup - { - situation.exits = { - 'west': <> - }; - } - - enter - { - display('''Kitchen!'''); - } -} -- cgit v1.2.3