From 1bc10d1408eb29b0675a030e029155dd046b1dd8 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Dec 2011 13:06:40 -0700 Subject: Functions can be called now, at least manually. A little more work needs to be done before they're being called from code correctly. --- src/astbranch.h | 1 + src/astfunction.cpp | 2 +- src/astfunction.h | 2 +- src/function.h | 2 +- src/functiondisplay.cpp | 9 ++++++- src/functiondisplay.h | 4 +-- src/game.cpp | 13 +++++++++ src/game.h | 5 ++++ src/gamebuilder.cpp | 2 +- src/gamestate.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ src/gamestate.h | 21 +++++++++++++++ src/scope.cpp | 4 +-- src/scope.h | 7 +++++ src/variable.cpp | 42 +++++++++++++++++++++++++++++ src/variable.h | 7 +++++ 15 files changed, 184 insertions(+), 9 deletions(-) diff --git a/src/astbranch.h b/src/astbranch.h index 8e63af5..0c558aa 100644 --- a/src/astbranch.h +++ b/src/astbranch.h @@ -16,6 +16,7 @@ public: AstBranch *getParent() const { return pParent; } typedef Bu::List NodeList; + const NodeList &getNodeList() const { return lNodes; } private: AstBranch *pParent; diff --git a/src/astfunction.cpp b/src/astfunction.cpp index df183d3..f091644 100644 --- a/src/astfunction.cpp +++ b/src/astfunction.cpp @@ -12,7 +12,7 @@ AstFunction::~AstFunction() delete pAst; } -Variable AstFunction::call( const VariableList &lParams ) +Variable AstFunction::call( class GameState &gState ) { } diff --git a/src/astfunction.h b/src/astfunction.h index e6470e4..4b2a049 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( const VariableList &lParams ); + virtual Variable call( class GameState &gState ); void addParam( const Bu::String &sName ); void setAst( class AstBranch *pAst ); diff --git a/src/function.h b/src/function.h index 1191129..c1289f4 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( const VariableList &lParams )=0; + virtual Variable call( class GameState &gState )=0; }; #endif diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp index 920eefd..16b1175 100644 --- a/src/functiondisplay.cpp +++ b/src/functiondisplay.cpp @@ -1,5 +1,10 @@ #include "functiondisplay.h" +#include +#include "gamestate.h" + +using namespace Bu; + FunctionDisplay::FunctionDisplay() { } @@ -8,7 +13,9 @@ FunctionDisplay::~FunctionDisplay() { } -Variable FunctionDisplay::call( const VariableList &lParams ) +Variable FunctionDisplay::call( class GameState &gState ) { + Variable v = gState.pop(); + sio << "Display: " << v << sio.nl; } diff --git a/src/functiondisplay.h b/src/functiondisplay.h index 2f13360..d1a63a9 100644 --- a/src/functiondisplay.h +++ b/src/functiondisplay.h @@ -3,14 +3,14 @@ #include "function.h" -class FunctionDisplay +class FunctionDisplay : public Function { public: FunctionDisplay(); virtual ~FunctionDisplay(); virtual Bu::String getName() const { return "display"; } - virtual Variable call( const VariableList &lParams ); + virtual Variable call( class GameState &gState ); }; #endif diff --git a/src/game.cpp b/src/game.cpp index f3b5828..c2bbce4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,7 +1,10 @@ #include "game.h" +#include "functiondisplay.h" + Game::Game() { + addFunction( new FunctionDisplay() ); } Game::~Game() @@ -16,3 +19,13 @@ Game::~Game() } } +Function *Game::getFunction( const Bu::String &sName ) +{ + return hFunction.get( sName ); +} + +void Game::addFunction( Function *pFunc ) +{ + hFunction.insert( pFunc->getName(), pFunc ); +} + diff --git a/src/game.h b/src/game.h index dd614cf..4abb4da 100644 --- a/src/game.h +++ b/src/game.h @@ -16,6 +16,11 @@ public: Game(); virtual ~Game(); + Function *getFunction( const Bu::String &sName ); + +private: + void addFunction( Function *pFunc ); + private: typedef Bu::Hash FunctionHash; typedef Bu::Hash SituationHash; diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 45aee50..ee85129 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -98,7 +98,7 @@ void GameBuilder::addVarRef( const Bu::String &sName ) { if( pCurNode ) { - pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, sName ) ); + pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, Variable::newVariableName( sName ) ) ); } } diff --git a/src/gamestate.cpp b/src/gamestate.cpp index fcd3433..7649cac 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,5 +1,10 @@ #include "gamestate.h" +#include "game.h" + +#include "astleaf.h" +#include "astleafliteral.h" + GameState::GameState( Game *pGame ) : pGame( pGame ) { @@ -9,3 +14,70 @@ GameState::~GameState() { } +void GameState::parse( class AstBranch *pAst ) +{ + if( pAst->getType() != AstNode::tScope ) + throw Bu::ExceptionBase("Nope, nothing doing, you can't parse a non-scope AstBranch."); + parse( pAst->getNodeList() ); +} + +void GameState::callFunction( const Bu::String &sName ) +{ + lsLocal.push( new Scope() ); + pGame->getFunction( sName )->call( *this ); + delete lsLocal.peekPop(); +} + +void GameState::parse( const AstBranch::NodeList &lCode ) +{ + for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) + { + switch( (*i)->getType() ) + { + // tLeaf + case AstNode::tNot: + case AstNode::tComp: + case AstNode::tCompGt: + case AstNode::tCompLt: + case AstNode::tCompGtEq: + case AstNode::tCompLtEq: + case AstNode::tStore: + case AstNode::tAnd: + case AstNode::tOr: + case AstNode::tPlus: + case AstNode::tMinus: + case AstNode::tDivide: + case AstNode::tMultiply: + case AstNode::tPlusStore: + case AstNode::tMinusStore: + case AstNode::tDivideStore: + case AstNode::tMultiplyStore: + case AstNode::tNegate: + case AstNode::tIn: + + // tLeafLiteral + case AstNode::tVarName: + lStack.push( dynamic_cast(*i)->getValue() ); + break; + + case AstNode::tLiteral: + lStack.push( dynamic_cast(*i)->getValue() ); + break; + + case AstNode::tFuncCall: + callFunction( + dynamic_cast(*i) + ->getValue().getString() + ); + break; + + // tBranch + case AstNode::tScope: + case AstNode::tIf: + case AstNode::tForEach: + case AstNode::tWhile: + break; + } + } +} + diff --git a/src/gamestate.h b/src/gamestate.h index 2754873..cb02322 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -1,6 +1,10 @@ #ifndef GAME_STATE_H #define GAME_STATE_H +#include "astbranch.h" +#include "variable.h" +#include "scope.h" + class Game; class GameState @@ -9,8 +13,25 @@ public: GameState( Game *pGame ); virtual ~GameState(); + void parse( class AstBranch *pAst ); + + Variable pop() { return lStack.peekPop(); } + void push( const Variable &v ) { lStack.push( v ); } + + void callFunction( const Bu::String &sName ); + private: + void parse( const AstBranch::NodeList &lCode ); + +private: + typedef Bu::List ScopeList; + typedef Bu::Hash ScopeHash; Game *pGame; + Scope sGlobal; + ScopeList lsLocal; + ScopeHash hsSituation; + + VariableList lStack; }; #endif diff --git a/src/scope.cpp b/src/scope.cpp index 45d4484..87cd050 100644 --- a/src/scope.cpp +++ b/src/scope.cpp @@ -1,5 +1,5 @@ #include "scope.h" - +/* Scope::Scope() { } @@ -7,4 +7,4 @@ Scope::Scope() Scope::~Scope() { } - +*/ diff --git a/src/scope.h b/src/scope.h index 84797f0..f352ff5 100644 --- a/src/scope.h +++ b/src/scope.h @@ -3,14 +3,21 @@ #include "variable.h" +typedef VariableHash Scope; +/* + class Scope { public: Scope(); virtual ~Scope(); + + private: VariableHash hVars; }; +*/ + #endif diff --git a/src/variable.cpp b/src/variable.cpp index adf1511..b7899f1 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -62,6 +62,33 @@ Variable Variable::newSituationName( const Bu::String &s ) return v; } +Variable Variable::newVariableName( const Bu::String &s ) +{ + Variable v( tVariable ); + (*v.sValue) = s; + return v; +} + +bool Variable::getBool() const +{ + return bValue; +} + +int64_t Variable::getInt() const +{ + return iValue; +} + +double Variable::getFloat() const +{ + return fValue; +} + +Bu::String Variable::getString() const +{ + return *sValue; +} + Variable Variable::to( Type e ) const { if( e == eType ) @@ -195,6 +222,7 @@ Variable &Variable::operator=( const Variable &rhs ) case tString: case tSituation: + case tVariable: (*sValue) = *rhs.sValue; break; @@ -261,6 +289,10 @@ Variable &Variable::operator+=( const Variable &rhs ) case tSituation: throw VariableException("You cannot add situations."); break; + + case tVariable: + throw VariableException("You cannot add variable names."); + break; } return *this; @@ -310,6 +342,9 @@ Variable Variable::operator+( const Variable &rhs ) const case tSituation: throw VariableException("You cannot add situations."); + + case tVariable: + throw VariableException("You cannot add variables."); } } } @@ -341,6 +376,7 @@ bool Variable::operator==( const Variable &rhs ) const case tString: case tSituation: + case tVariable: return (*sValue) == (*rhs.sValue); case tList: @@ -369,6 +405,7 @@ void Variable::initType() { case tString: case tSituation: + case tVariable: sValue = new Bu::String(); break; @@ -388,6 +425,7 @@ void Variable::deinitType() { case tString: case tSituation: + case tVariable: delete sValue; break; @@ -418,6 +456,7 @@ template<> uint32_t Bu::__calcHashCode( const Variable &k ) case Variable::tString: case Variable::tSituation: + case Variable::tVariable: return Bu::__calcHashCode( *k.sValue ); case Variable::tList: @@ -453,6 +492,9 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) case Variable::tSituation: return f << "<<" << *v.sValue << ">>"; + + case Variable::tVariable: + return f << "(varref:\"" << *v.sValue << "\")"; case Variable::tList: return f << *v.lValue; diff --git a/src/variable.h b/src/variable.h index 9a35d48..e1bc30c 100644 --- a/src/variable.h +++ b/src/variable.h @@ -19,6 +19,7 @@ public: tFloat, tString, tSituation, + tVariable, tList, tDictionary }; @@ -34,9 +35,15 @@ public: virtual ~Variable(); static Variable newSituationName( const Bu::String &s ); + static Variable newVariableName( const Bu::String &s ); Type getType() const { return eType; } + bool getBool() const; + int64_t getInt() const; + double getFloat() const; + Bu::String getString() const; + Variable to( Type e ) const; Variable &operator=( const Variable &rhs ); -- cgit v1.2.3