From f2ee67558acbe3c418a7558587b56158d593d88d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Dec 2011 13:11:35 -0700 Subject: return, exit, lists added. You can't index them. They're linked lists right now, maybe that's not really what I want long-term, but it'll work for now... --- src/astnode.cpp | 3 +++ src/astnode.h | 3 +++ src/functionexit.cpp | 17 +++++++++++++++++ src/functionexit.h | 16 ++++++++++++++++ src/game.cpp | 2 ++ src/gamebuilder.cpp | 14 +++++++------- src/gamestate.cpp | 28 +++++++++++++++++++++++++--- src/gamestate.h | 6 ++++++ src/main.cpp | 14 ++++++++------ src/parser.l | 1 + src/parser.y | 11 +++++++---- src/variable.cpp | 4 ++++ 12 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 src/functionexit.cpp create mode 100644 src/functionexit.h (limited to 'src') diff --git a/src/astnode.cpp b/src/astnode.cpp index 99f6a2e..84f6845 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp @@ -42,6 +42,9 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ) case AstNode::tGoto: return f << "tGoto"; case AstNode::tSwap: return f << "tSwap"; case AstNode::tStoreRev: return f << "tStoreRev"; + case AstNode::tReturn: return f << "tReturn"; + case AstNode::tAppend: return f << "tAppend"; + case AstNode::tInsert: return f << "tInsert"; case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; case AstNode::tVarName: return f << "tVarName"; diff --git a/src/astnode.h b/src/astnode.h index 7b9e928..58c4a42 100644 --- a/src/astnode.h +++ b/src/astnode.h @@ -32,6 +32,9 @@ public: tGoto = 0x01000014, tSwap = 0x01000015, tStoreRev = 0x01000016, + tReturn = 0x01000017, + tAppend = 0x01000018, + tInsert = 0x01000019, tLeafLiteral = 0x02000000, tVarName = 0x02000001, diff --git a/src/functionexit.cpp b/src/functionexit.cpp new file mode 100644 index 0000000..be64f43 --- /dev/null +++ b/src/functionexit.cpp @@ -0,0 +1,17 @@ +#include "functionexit.h" + +#include "gamestate.h" + +FunctionExit::FunctionExit() +{ +} + +FunctionExit::~FunctionExit() +{ +} + +void FunctionExit::call( class GameState &gState ) +{ + gState.exit(); +} + diff --git a/src/functionexit.h b/src/functionexit.h new file mode 100644 index 0000000..c312594 --- /dev/null +++ b/src/functionexit.h @@ -0,0 +1,16 @@ +#ifndef FUNCTION_EXIT_H +#define FUNCTION_EXIT_H + +#include "function.h" + +class FunctionExit : public Function +{ +public: + FunctionExit(); + virtual ~FunctionExit(); + + virtual Bu::String getName() const { return "exit"; } + virtual void call( class GameState &gState ); +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp index b9c7e08..910cec2 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3,12 +3,14 @@ #include "functiondisplay.h" #include "functionexists.h" #include "functiondelete.h" +#include "functionexit.h" Game::Game() { addFunction( new FunctionDisplay() ); addFunction( new FunctionExists() ); addFunction( new FunctionDelete() ); + addFunction( new FunctionExit() ); } Game::~Game() diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index e84d8f7..791ba6b 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -41,13 +41,13 @@ void GameBuilder::beginFunction( const Bu::String &sName ) { pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); pCurFnc = new AstFunction( sName ); - sio << "New function: " << sName << sio.nl; + //sio << "New function: " << sName << sio.nl; } void GameBuilder::addFunctionParam( const Bu::String &sName ) { pCurFnc->addParam( sName ); - sio << " - Param added '" << sName << "'" << sio.nl; + //sio << " - Param added '" << sName << "'" << sio.nl; } void GameBuilder::endFunctionParams() @@ -68,7 +68,7 @@ void GameBuilder::endFunctionParams() void GameBuilder::endFunction() { - sio << "Function ended: " << *pCurRoot << sio.nl; + //sio << "Function ended: " << *pCurRoot << sio.nl; pCurFnc->setAst( pCurRoot ); pCurRoot = pCurNode = NULL; pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); @@ -77,7 +77,7 @@ void GameBuilder::endFunction() void GameBuilder::beginSituation( const Bu::String &sName ) { pCurSit = new Situation( sName ); - sio << "New situation: " << sName << sio.nl; + //sio << "New situation: " << sName << sio.nl; } void GameBuilder::beginSituationMode( Situation::Mode m ) @@ -88,7 +88,7 @@ void GameBuilder::beginSituationMode( Situation::Mode m ) void GameBuilder::closeSituationMode() { - sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; + //sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; pCurSit->setAst( pCurRoot, eCurSitMode ); pCurRoot = pCurNode = NULL; } @@ -96,7 +96,7 @@ void GameBuilder::closeSituationMode() void GameBuilder::endSituation() { pGame->hSituation.insert( pCurSit->getName(), pCurSit ); - sio << "Situation ended." << sio.nl; + //sio << "Situation ended." << sio.nl; } void GameBuilder::addNode( AstNode::Type iType ) @@ -185,7 +185,7 @@ void GameBuilder::closeCommand() { pCurCmd->setAst( pCurRoot ); pCurRoot = pCurNode = NULL; - pCurCmd->print(); +// pCurCmd->print(); if( bGlobal ) { pGame->csGlobal.addCommand( pCurCmd ); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index fa014ac..d45d53c 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -9,7 +9,8 @@ using namespace Bu; GameState::GameState( Game *pGame ) : - pGame( pGame ) + pGame( pGame ), + bRunning( true ) { } @@ -73,7 +74,8 @@ void GameState::execCommand( const Bu::String &sCmd ) { if( !pGame->execCommand( *this, lCmd ) ) { - throw Bu::ExceptionBase("No such command exists."); + sio << "I don't understand that command." << sio.nl; + return; } } } @@ -215,6 +217,12 @@ Bu::StringList GameState::tokenize( const Bu::String &sSrc ) return lRet; } +void GameState::exit() +{ + bRunning = false; + bEscape = true; +} + Variable GameState::popDeref() { Variable v = lStack.peekPop(); @@ -420,6 +428,17 @@ void GameState::parse( const AstBranch::NodeList &lCode ) } break; + case AstNode::tReturn: + return; + break; + + case AstNode::tAppend: + { + Variable v = popDeref(); + lStack.peek() += v; + } + break; + // tLeafLiteral case AstNode::tVarName: case AstNode::tLiteral: @@ -435,6 +454,9 @@ void GameState::parse( const AstBranch::NodeList &lCode ) // tBranch case AstNode::tScope: + throw Bu::ExceptionBase("Scope? that shouldn't be here..."); + break; + case AstNode::tIf: { AstBranch::NodeList lIf = @@ -488,7 +510,7 @@ void GameState::parse( const AstBranch::NodeList &lCode ) break; } - if( bEscape ) + if( bEscape || !bRunning ) { return; } diff --git a/src/gamestate.h b/src/gamestate.h index d88fecb..e32eeaa 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -35,6 +35,10 @@ public: Variable deref( const Variable &src ); Bu::StringList tokenize( const Bu::String &sSrc ); + void exit(); + bool isRunning() const { return bRunning; } + Bu::String getPrompt() const { return sPrompt; } + private: void parse( const AstBranch::NodeList &lCode ); @@ -49,6 +53,8 @@ private: Bu::String sCurSituation; bool bEscape; + bool bRunning; + Bu::String sPrompt; VariableList lStack; }; diff --git a/src/main.cpp b/src/main.cpp index 2b20e66..413151a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,12 +33,14 @@ int main( int argc, char *argv[] ) GameState gs( pGame ); gs.init(); - char buf[1024]; - sio << ">>" << sio.flush; - fgets( buf, 1024, stdin ); - - sio << "Read: >" << buf << "<" << sio.nl; - gs.execCommand( buf ); + while( gs.isRunning() ) + { + char buf[1024]; + sio << "command> " << sio.flush; + fgets( buf, 1024, stdin ); + + gs.execCommand( buf ); + } return 0; } diff --git a/src/parser.l b/src/parser.l index 8bc687d..7b11765 100644 --- a/src/parser.l +++ b/src/parser.l @@ -48,6 +48,7 @@ setup { return tokSetup; } enter { return tokEnter; } and { return tokAnd; } or { return tokOr; } +return { return tokReturn; } true { yylval->bValue = true; return tokBool; } false { yylval->bValue = false; return tokBool; } diff --git a/src/parser.y b/src/parser.y index 55a7968..f01e0e4 100644 --- a/src/parser.y +++ b/src/parser.y @@ -62,6 +62,7 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %token tokLtEq %token tokGtEq %token tokCmp +%token tokReturn %token tokPlusAssign %token tokMinusAssign %token tokTimesAssign @@ -150,6 +151,8 @@ cmpltExprList: ; cmpltExpr: expr ';' + | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); } + | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); } | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); } | ifbase | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' @@ -224,8 +227,8 @@ expr: literal | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); } | '(' expr ')' | expr '[' expr ']' - | '[' ']' - | '[' listValues ']' + | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } + | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' | '{' '}' | '{' dictValues '}' | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } @@ -240,8 +243,8 @@ funcCallParamsEx: | funcCallParamsEx ',' expr ; -listValues: expr - | listValues ',' expr +listValues: expr { bld.addNode( AstNode::tAppend ); } + | listValues ',' expr { bld.addNode( AstNode::tAppend ); } ; dictValues: expr ':' expr diff --git a/src/variable.cpp b/src/variable.cpp index 4c5ca4c..db7726c 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -309,6 +309,10 @@ Variable &Variable::operator+=( const Variable &rhs ) case tVariable: throw VariableException("You cannot add variable names."); break; + + case tList: + (*lValue).append( rhs ); + break; } return *this; -- cgit v1.2.3