From 9e6dc529833d17374a73bae1f30c7970f936f7cc Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Dec 2011 09:59:47 -0700 Subject: Functions exists/delete work now. --- src/astfunction.cpp | 2 +- src/astfunction.h | 2 +- src/function.h | 2 +- src/functiondelete.cpp | 19 +++++++++++++++++++ src/functiondelete.h | 16 ++++++++++++++++ src/functiondisplay.cpp | 2 +- src/functiondisplay.h | 2 +- src/functionexists.cpp | 19 +++++++++++++++++++ src/functionexists.h | 16 ++++++++++++++++ src/game.cpp | 4 ++++ src/gamestate.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/gamestate.h | 2 ++ test.stage | 4 ++++ 13 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 src/functiondelete.cpp create mode 100644 src/functiondelete.h create mode 100644 src/functionexists.cpp create mode 100644 src/functionexists.h diff --git a/src/astfunction.cpp b/src/astfunction.cpp index f5e25dc..c37a70d 100644 --- a/src/astfunction.cpp +++ b/src/astfunction.cpp @@ -14,7 +14,7 @@ AstFunction::~AstFunction() delete pAst; } -Variable AstFunction::call( class GameState &gState ) +void AstFunction::call( class GameState &gState ) { gState.parse( pAst ); } diff --git a/src/astfunction.h b/src/astfunction.h index a546b19..5e71753 100644 --- a/src/astfunction.h +++ b/src/astfunction.h @@ -10,7 +10,7 @@ public: virtual ~AstFunction(); virtual Bu::String getName() const { return sName; } - virtual Variable call( class GameState &gState ); + virtual void call( class GameState &gState ); void addParam( const Bu::String &sName ); const Bu::StringList &getParamList() { return lParam; } diff --git a/src/function.h b/src/function.h index c1289f4..6696d5a 100644 --- a/src/function.h +++ b/src/function.h @@ -11,7 +11,7 @@ public: virtual ~Function(); virtual Bu::String getName() const=0; - virtual Variable call( class GameState &gState )=0; + virtual void call( class GameState &gState )=0; }; #endif diff --git a/src/functiondelete.cpp b/src/functiondelete.cpp new file mode 100644 index 0000000..c9bfef4 --- /dev/null +++ b/src/functiondelete.cpp @@ -0,0 +1,19 @@ +#include "functiondelete.h" + +#include "gamestate.h" + +FunctionDelete::FunctionDelete() +{ +} + +FunctionDelete::~FunctionDelete() +{ +} + +void FunctionDelete::call( class GameState &gState ) +{ + Variable v = gState.pop(); + VariableRef r = v.getVariableRef(); + gState.delVariable( r.sName, r.sid ); +} + diff --git a/src/functiondelete.h b/src/functiondelete.h new file mode 100644 index 0000000..3545343 --- /dev/null +++ b/src/functiondelete.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_DELETE_H +#define FUNCTION_DELETE_H + +#include "function.h" + +class FunctionDelete : public Function +{ +public: + FunctionDelete(); + virtual ~FunctionDelete(); + + virtual Bu::String getName() const { return "delete"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp index 332a2c9..fa5b79c 100644 --- a/src/functiondisplay.cpp +++ b/src/functiondisplay.cpp @@ -13,7 +13,7 @@ FunctionDisplay::~FunctionDisplay() { } -Variable FunctionDisplay::call( class GameState &gState ) +void FunctionDisplay::call( class GameState &gState ) { Variable v = gState.popDeref(); sio << "Display: " << v << sio.nl; diff --git a/src/functiondisplay.h b/src/functiondisplay.h index d1a63a9..3b7e828 100644 --- a/src/functiondisplay.h +++ b/src/functiondisplay.h @@ -10,7 +10,7 @@ public: virtual ~FunctionDisplay(); virtual Bu::String getName() const { return "display"; } - virtual Variable call( class GameState &gState ); + virtual void call( class GameState &gState ); }; #endif diff --git a/src/functionexists.cpp b/src/functionexists.cpp new file mode 100644 index 0000000..71e03f6 --- /dev/null +++ b/src/functionexists.cpp @@ -0,0 +1,19 @@ +#include "functionexists.h" + +#include "gamestate.h" + +FunctionExists::FunctionExists() +{ +} + +FunctionExists::~FunctionExists() +{ +} + +void FunctionExists::call( class GameState &gState ) +{ + Variable v = gState.pop(); + VariableRef r = v.getVariableRef(); + gState.push( Variable( gState.hasVariable( r.sName, r.sid ) ) ); +} + diff --git a/src/functionexists.h b/src/functionexists.h new file mode 100644 index 0000000..5313331 --- /dev/null +++ b/src/functionexists.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_EXISTS_H +#define FUNCTION_EXISTS_H + +#include "function.h" + +class FunctionExists : public Function +{ +public: + FunctionExists(); + virtual ~FunctionExists(); + + virtual Bu::String getName() const { return "exists"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 6c980ee..87d82b7 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,10 +1,14 @@ #include "game.h" #include "functiondisplay.h" +#include "functionexists.h" +#include "functiondelete.h" Game::Game() { addFunction( new FunctionDisplay() ); + addFunction( new FunctionExists() ); + addFunction( new FunctionDelete() ); } Game::~Game() diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 902d0a9..a3758ce 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -65,6 +65,46 @@ void GameState::callFunction( const Bu::String &sName ) pGame->getFunction( sName )->call( *this ); } +bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) +{ + switch( id ) + { + case sidLocal: + return lsLocal.peek()->has( sName ); + + case sidGlobal: + return sGlobal.has( sName ); + + case sidPlayer: + return sPlayer.has( sName ); + + case sidSituation: + return hsSituation.get( sCurSituation )->has( sName ); + } + + throw Bu::ExceptionBase("Really bad scopeid passed into getVariable"); +} + +void GameState::delVariable( const Bu::String &sName, ScopeId id ) +{ + switch( id ) + { + case sidLocal: + return lsLocal.peek()->erase( sName ); + + case sidGlobal: + return sGlobal.erase( sName ); + + case sidPlayer: + return sPlayer.erase( sName ); + + case sidSituation: + return hsSituation.get( sCurSituation )->erase( sName ); + } + + throw Bu::ExceptionBase("Really bad scopeid passed into getVariable"); +} + Variable GameState::getVariable( const Bu::String &sName, ScopeId id ) { try diff --git a/src/gamestate.h b/src/gamestate.h index 6000419..61e18e8 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -26,6 +26,8 @@ public: void callFunction( const Bu::String &sName ); + bool hasVariable( const Bu::String &sName, ScopeId id ); + void delVariable( const Bu::String &sName, ScopeId id ); Variable getVariable( const Bu::String &sName, ScopeId id=sidLocal ); void setVariable( const Bu::String &sName, const Variable &v, ScopeId id=sidLocal ); diff --git a/test.stage b/test.stage index 516471d..060e11d 100644 --- a/test.stage +++ b/test.stage @@ -48,6 +48,10 @@ situation <> display("This is the setup phase for start, " + name); sillyDisplay( "Hello", name == player.name ); + display( exists(name) ); + delete( name ); + display( exists(name) ); + getThing(); myGoto( <> ); display("You shouldn't see this."); -- cgit v1.2.3