From bae6192c54533e8da95d8ae1ed4d4eccee28c39a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 29 Dec 2011 10:27:38 -0700 Subject: Getting close to realy running. --- src/astfunction.cpp | 28 +++++++++++++++++++ src/astfunction.h | 24 ++++++++++++++++ src/astnode.cpp | 2 +- src/astnode.h | 2 +- src/command.cpp | 46 +++++++++++++++++++++++++++++++ src/command.h | 38 +++++++++++++++++++++++++ src/commandset.cpp | 21 ++++++++++++++ src/commandset.h | 20 ++++++++++++++ src/functiondisplay.cpp | 14 ++++++++++ src/functiondisplay.h | 16 +++++++++++ src/game.cpp | 8 ++++++ src/game.h | 3 +- src/gamebuilder.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++------ src/gamebuilder.h | 17 ++++++++++-- src/parser.y | 27 ++++++++++++------ test.stage | 4 +++ 16 files changed, 320 insertions(+), 23 deletions(-) create mode 100644 src/astfunction.cpp create mode 100644 src/astfunction.h create mode 100644 src/command.cpp create mode 100644 src/command.h create mode 100644 src/commandset.cpp create mode 100644 src/commandset.h create mode 100644 src/functiondisplay.cpp create mode 100644 src/functiondisplay.h diff --git a/src/astfunction.cpp b/src/astfunction.cpp new file mode 100644 index 0000000..df183d3 --- /dev/null +++ b/src/astfunction.cpp @@ -0,0 +1,28 @@ +#include "astfunction.h" +#include "astbranch.h" + +AstFunction::AstFunction( const Bu::String &sName ) : + sName( sName ), + pAst( NULL ) +{ +} + +AstFunction::~AstFunction() +{ + delete pAst; +} + +Variable AstFunction::call( const VariableList &lParams ) +{ +} + +void AstFunction::addParam( const Bu::String &sName ) +{ + lParam.append( sName ); +} + +void AstFunction::setAst( class AstBranch *pAst ) +{ + this->pAst = pAst; +} + diff --git a/src/astfunction.h b/src/astfunction.h new file mode 100644 index 0000000..e6470e4 --- /dev/null +++ b/src/astfunction.h @@ -0,0 +1,24 @@ +#ifndef AST_FUNCTION_H +#define AST_FUNCTION_H + +#include "function.h" + +class AstFunction : public Function +{ +public: + AstFunction( const Bu::String &sName ); + virtual ~AstFunction(); + + virtual Bu::String getName() const { return sName; } + virtual Variable call( const VariableList &lParams ); + + void addParam( const Bu::String &sName ); + void setAst( class AstBranch *pAst ); + +private: + Bu::String sName; + Bu::StringList lParam; + class AstBranch *pAst; +}; + +#endif diff --git a/src/astnode.cpp b/src/astnode.cpp index 6e59ab6..7191292 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp @@ -43,7 +43,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ) case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; case AstNode::tVarName: return f << "tVarName"; case AstNode::tLiteral: return f << "tLiteral"; - case AstNode::tFuncName: return f << "tFuncName"; + case AstNode::tFuncCall: return f << "tFuncCall"; case AstNode::tBranch: return f << "!tBranch!"; case AstNode::tScope: return f << "tScope"; diff --git a/src/astnode.h b/src/astnode.h index b38b895..28e5336 100644 --- a/src/astnode.h +++ b/src/astnode.h @@ -33,7 +33,7 @@ public: tLeafLiteral = 0x02000000, tVarName = 0x02000001, tLiteral = 0x02000002, - tFuncName = 0x02000003, + tFuncCall = 0x02000003, tBranch = 0x04000000, tScope = 0x04000001, diff --git a/src/command.cpp b/src/command.cpp new file mode 100644 index 0000000..5b859f7 --- /dev/null +++ b/src/command.cpp @@ -0,0 +1,46 @@ +#include "command.h" + +#include "astbranch.h" + +#include +using namespace Bu; + +Command::Command() : + pAst( NULL ) +{ +} + +Command::~Command() +{ + delete pAst; +} + +void Command::addLiteral( const Bu::String &sValue ) +{ + lChunks.append( Chunk( true, sValue ) ); +} + +void Command::addParam( const Bu::String &sValue ) +{ + lChunks.append( Chunk( false, sValue ) ); +} + +void Command::setAst( class AstBranch *pAst ) +{ + this->pAst = pAst; +} + +void Command::print() +{ + sio << "command:"; + for( ChunkList::iterator i = lChunks.begin(); i; i++ ) + { + if( (*i).bLiteral ) + sio << " \"" << (*i).sValue << "\""; + else + sio << " " << (*i).sValue; + } + + sio << sio.nl; +} + diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..f36fc14 --- /dev/null +++ b/src/command.h @@ -0,0 +1,38 @@ +#ifndef COMMAND_H +#define COMMAND_H + +#include + +class Command +{ +public: + Command(); + virtual ~Command(); + + void addLiteral( const Bu::String &sValue ); + void addParam( const Bu::String &sValue ); + + void setAst( class AstBranch *pAst ); + + void print(); + +private: + class Chunk + { + public: + Chunk( bool bLiteral, const Bu::String &sValue ) : + bLiteral( bLiteral ), sValue( sValue ) + { + } + + bool bLiteral; + Bu::String sValue; + }; + + typedef Bu::List ChunkList; + ChunkList lChunks; + + class AstBranch *pAst; +}; + +#endif diff --git a/src/commandset.cpp b/src/commandset.cpp new file mode 100644 index 0000000..ffd58a5 --- /dev/null +++ b/src/commandset.cpp @@ -0,0 +1,21 @@ +#include "commandset.h" + +#include "command.h" + +CommandSet::CommandSet() +{ +} + +CommandSet::~CommandSet() +{ + for( CommandList::iterator i = lCommand.begin(); i; i++ ) + { + delete (*i); + } +} + +void CommandSet::addCommand( class Command *pCmd ) +{ + lCommand.append( pCmd ); +} + diff --git a/src/commandset.h b/src/commandset.h new file mode 100644 index 0000000..593d529 --- /dev/null +++ b/src/commandset.h @@ -0,0 +1,20 @@ +#ifndef COMMAND_SET_H +#define COMMAND_SET_H + +#include +#include + +class CommandSet +{ +public: + CommandSet(); + virtual ~CommandSet(); + + void addCommand( class Command *pCmd ); + +private: + typedef Bu::List CommandList; + CommandList lCommand; +}; + +#endif diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp new file mode 100644 index 0000000..920eefd --- /dev/null +++ b/src/functiondisplay.cpp @@ -0,0 +1,14 @@ +#include "functiondisplay.h" + +FunctionDisplay::FunctionDisplay() +{ +} + +FunctionDisplay::~FunctionDisplay() +{ +} + +Variable FunctionDisplay::call( const VariableList &lParams ) +{ +} + diff --git a/src/functiondisplay.h b/src/functiondisplay.h new file mode 100644 index 0000000..2f13360 --- /dev/null +++ b/src/functiondisplay.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_DISPLAY_H +#define FUNCTION_DISPLAY_H + +#include "function.h" + +class FunctionDisplay +{ +public: + FunctionDisplay(); + virtual ~FunctionDisplay(); + + virtual Bu::String getName() const { return "display"; } + virtual Variable call( const VariableList &lParams ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 61fa255..f3b5828 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -6,5 +6,13 @@ Game::Game() Game::~Game() { + for( FunctionHash::iterator i = hFunction.begin(); i; i++ ) + { + delete (*i); + } + for( SituationHash::iterator i = hSituation.begin(); i; i++ ) + { + delete (*i); + } } diff --git a/src/game.h b/src/game.h index 1daa148..81247ca 100644 --- a/src/game.h +++ b/src/game.h @@ -9,6 +9,7 @@ class Game { +friend class GameBuilder; public: Game(); virtual ~Game(); @@ -16,7 +17,7 @@ public: private: typedef Bu::Hash FunctionHash; typedef Bu::Hash SituationHash; - typedef Bu::Hash ScopeHash; + VariableHash hGlobalParam; FunctionHash hFunction; SituationHash hSituation; Bu::String sCurSituation; diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 47de17d..e6c1f38 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -1,16 +1,24 @@ #include "gamebuilder.h" #include +#include "game.h" #include "astbranch.h" #include "astleaf.h" #include "astleafliteral.h" +#include "astfunction.h" +#include "command.h" using namespace Bu; GameBuilder::GameBuilder() : + pGame( NULL ), + bGlobal( false ), pCurNode( NULL ), - pCurRoot( NULL ) + pCurRoot( NULL ), + pCurCmd( NULL ), + pCurFnc( NULL ) { + pGame = new Game(); } GameBuilder::~GameBuilder() @@ -24,20 +32,28 @@ void GameBuilder::setLiteral( const Variable &v ) void GameBuilder::setGameParam( const Bu::String &sName ) { - sio << "Set game param '" << sName << "' to " << vLiteral << sio.nl; - hGameParams.insert( sName, vLiteral ); + pGame->hGlobalParam.insert( sName, vLiteral ); } void GameBuilder::beginFunction( const Bu::String &sName ) { pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); - + pCurFnc = new AstFunction( sName ); sio << "New function: " << sName << sio.nl; } +void GameBuilder::addFunctionParam( const Bu::String &sName ) +{ + pCurFnc->addParam( sName ); + sio << " - Param added '" << sName << "'" << sio.nl; +} + void GameBuilder::endFunction() { sio << "Function ended: " << *pCurRoot << sio.nl; + pCurFnc->setAst( pCurRoot ); + pCurRoot = pCurNode = NULL; + pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); } void GameBuilder::beginSituation( const Bu::String &sName ) @@ -50,11 +66,6 @@ void GameBuilder::endSituation() sio << "Situation ended." << sio.nl; } -void GameBuilder::addParam( const Bu::String &sName ) -{ - sio << " - Param added '" << sName << "'" << sio.nl; -} - void GameBuilder::addNode( AstNode::Type iType ) { switch( iType&AstNode::tTypeMask ) @@ -91,3 +102,47 @@ void GameBuilder::addVarRef( const Bu::String &sName ) } } +void GameBuilder::addFuncCall( const Bu::String &sName ) +{ + if( pCurNode ) + { + pCurNode->addNode( new AstLeafLiteral( AstNode::tFuncCall, sName ) ); + } +} + +void GameBuilder::beginGlobal() +{ + bGlobal = true; +} + +void GameBuilder::closeGlobal() +{ + bGlobal = false; +} + +void GameBuilder::beginCommand( const Bu::String &sValue ) +{ + pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); + pCurCmd = new Command(); + pCurCmd->addLiteral( sValue ); +} + +void GameBuilder::addCommandLiteral( const Bu::String &sValue ) +{ + pCurCmd->addLiteral( sValue ); +} + +void GameBuilder::addCommandParam( const Bu::String &sValue ) +{ + pCurCmd->addParam( sValue ); +} + +void GameBuilder::closeCommand() +{ + pCurCmd->print(); + sio << *pCurRoot << sio.nl; + pCurCmd->setAst( pCurRoot ); + delete pCurCmd; + pCurCmd = NULL; +} + diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 2637c37..bc32f55 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -16,23 +16,34 @@ public: void setGameParam( const Bu::String &sName ); void beginFunction( const Bu::String &sName ); + void addFunctionParam( const Bu::String &sName ); void endFunction(); void beginSituation( const Bu::String &sName ); void endSituation(); - void addParam( const Bu::String &sName ); void addNode( AstNode::Type iType ); void closeNode(); void addLiteral( const Variable &v ); void addVarRef( const Bu::String &sName ); + void addFuncCall( const Bu::String &sName ); + + void beginGlobal(); + void closeGlobal(); + + void beginCommand( const Bu::String &sValue ); + void addCommandLiteral( const Bu::String &sValue ); + void addCommandParam( const Bu::String &sValue ); + void closeCommand(); private: + class Game *pGame; + bool bGlobal; Variable vLiteral; class AstBranch *pCurNode; class AstBranch *pCurRoot; - - VariableHash hGameParams; + class Command *pCurCmd; + class AstFunction *pCurFnc; }; #endif diff --git a/src/parser.y b/src/parser.y index 90c771d..e6210d1 100644 --- a/src/parser.y +++ b/src/parser.y @@ -96,7 +96,8 @@ gameDecls: ; globalDecl: - | tokGlobal '{' globalExprList '}' + | tokGlobal '{' { bld.beginGlobal(); } globalExprList '}' + { bld.closeGlobal(); } ; globalExprList: @@ -134,11 +135,11 @@ function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ; funcParamList: - | tokIdent { bld.addParam( *($1) ); } funcParamListEx + | tokIdent { bld.addFunctionParam( *($1) ); } funcParamListEx ; funcParamListEx: - | funcParamListEx ',' tokIdent { bld.addParam( *($3) ); } + | funcParamListEx ',' tokIdent { bld.addFunctionParam( *($3) ); } ; cmpltExprList: @@ -156,7 +157,8 @@ cmpltExpr: expr ';' bld.closeNode(); bld.addNode( AstNode::tScope ); } tokDo '{' cmpltExprList '}' { - bld.closeNode(); bld.closeNode(); + bld.closeNode(); + bld.closeNode(); } ; @@ -198,7 +200,7 @@ literal: tokInt { bld.addLiteral( Variable( $1 ) ); } ; expr: literal - | tokIdent '(' listValues ')' + | tokIdent '(' funcCallParams ')' { bld.addFuncCall( *($1) ); } | varRef | varRef '=' expr { bld.addNode( AstNode::tStore ); } | varRef tokPlusAssign expr { bld.addNode( AstNode::tPlusStore ); } @@ -227,6 +229,14 @@ expr: literal | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } ; +funcCallParams: + | expr funcCallParamsEx + ; + +funcCallParamsEx: + | funcCallParamsEx ',' expr + ; + listValues: expr | listValues ',' expr ; @@ -235,12 +245,13 @@ dictValues: expr ':' expr | dictValues ',' expr ':' expr ; -commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}' +commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } + commandParamList '{' cmpltExprList '}' { bld.closeCommand(); } ; commandParamList: - | commandParamList tokString - | commandParamList tokIdent + | commandParamList tokString { bld.addCommandLiteral( *$2 ); } + | commandParamList tokIdent { bld.addCommandParam( *$2 ); } ; %% /* diff --git a/test.stage b/test.stage index a705c93..13155d3 100644 --- a/test.stage +++ b/test.stage @@ -7,6 +7,10 @@ game.start = <>; global { + command: "eat" object + { + pow(5, x*2); + } } function hello( a, b, c ) -- cgit v1.2.3