diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-30 13:11:35 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-30 13:11:35 -0700 |
commit | f2ee67558acbe3c418a7558587b56158d593d88d (patch) | |
tree | d0b306fc26679fee672df740acdbdc0ede3c50dd /src | |
parent | e112d781ea5ffc4186e7d70dba595b5a233335a8 (diff) | |
download | stage-f2ee67558acbe3c418a7558587b56158d593d88d.tar.gz stage-f2ee67558acbe3c418a7558587b56158d593d88d.tar.bz2 stage-f2ee67558acbe3c418a7558587b56158d593d88d.tar.xz stage-f2ee67558acbe3c418a7558587b56158d593d88d.zip |
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...
Diffstat (limited to 'src')
-rw-r--r-- | src/astnode.cpp | 3 | ||||
-rw-r--r-- | src/astnode.h | 3 | ||||
-rw-r--r-- | src/functionexit.cpp | 17 | ||||
-rw-r--r-- | src/functionexit.h | 16 | ||||
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 14 | ||||
-rw-r--r-- | src/gamestate.cpp | 28 | ||||
-rw-r--r-- | src/gamestate.h | 6 | ||||
-rw-r--r-- | src/main.cpp | 14 | ||||
-rw-r--r-- | src/parser.l | 1 | ||||
-rw-r--r-- | src/parser.y | 11 | ||||
-rw-r--r-- | src/variable.cpp | 4 |
12 files changed, 99 insertions, 20 deletions
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 ) | |||
42 | case AstNode::tGoto: return f << "tGoto"; | 42 | case AstNode::tGoto: return f << "tGoto"; |
43 | case AstNode::tSwap: return f << "tSwap"; | 43 | case AstNode::tSwap: return f << "tSwap"; |
44 | case AstNode::tStoreRev: return f << "tStoreRev"; | 44 | case AstNode::tStoreRev: return f << "tStoreRev"; |
45 | case AstNode::tReturn: return f << "tReturn"; | ||
46 | case AstNode::tAppend: return f << "tAppend"; | ||
47 | case AstNode::tInsert: return f << "tInsert"; | ||
45 | 48 | ||
46 | case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; | 49 | case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; |
47 | case AstNode::tVarName: return f << "tVarName"; | 50 | 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: | |||
32 | tGoto = 0x01000014, | 32 | tGoto = 0x01000014, |
33 | tSwap = 0x01000015, | 33 | tSwap = 0x01000015, |
34 | tStoreRev = 0x01000016, | 34 | tStoreRev = 0x01000016, |
35 | tReturn = 0x01000017, | ||
36 | tAppend = 0x01000018, | ||
37 | tInsert = 0x01000019, | ||
35 | 38 | ||
36 | tLeafLiteral = 0x02000000, | 39 | tLeafLiteral = 0x02000000, |
37 | tVarName = 0x02000001, | 40 | 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 @@ | |||
1 | #include "functionexit.h" | ||
2 | |||
3 | #include "gamestate.h" | ||
4 | |||
5 | FunctionExit::FunctionExit() | ||
6 | { | ||
7 | } | ||
8 | |||
9 | FunctionExit::~FunctionExit() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | void FunctionExit::call( class GameState &gState ) | ||
14 | { | ||
15 | gState.exit(); | ||
16 | } | ||
17 | |||
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 @@ | |||
1 | #ifndef FUNCTION_EXIT_H | ||
2 | #define FUNCTION_EXIT_H | ||
3 | |||
4 | #include "function.h" | ||
5 | |||
6 | class FunctionExit : public Function | ||
7 | { | ||
8 | public: | ||
9 | FunctionExit(); | ||
10 | virtual ~FunctionExit(); | ||
11 | |||
12 | virtual Bu::String getName() const { return "exit"; } | ||
13 | virtual void call( class GameState &gState ); | ||
14 | }; | ||
15 | |||
16 | #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 @@ | |||
3 | #include "functiondisplay.h" | 3 | #include "functiondisplay.h" |
4 | #include "functionexists.h" | 4 | #include "functionexists.h" |
5 | #include "functiondelete.h" | 5 | #include "functiondelete.h" |
6 | #include "functionexit.h" | ||
6 | 7 | ||
7 | Game::Game() | 8 | Game::Game() |
8 | { | 9 | { |
9 | addFunction( new FunctionDisplay() ); | 10 | addFunction( new FunctionDisplay() ); |
10 | addFunction( new FunctionExists() ); | 11 | addFunction( new FunctionExists() ); |
11 | addFunction( new FunctionDelete() ); | 12 | addFunction( new FunctionDelete() ); |
13 | addFunction( new FunctionExit() ); | ||
12 | } | 14 | } |
13 | 15 | ||
14 | Game::~Game() | 16 | 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 ) | |||
41 | { | 41 | { |
42 | pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); | 42 | pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); |
43 | pCurFnc = new AstFunction( sName ); | 43 | pCurFnc = new AstFunction( sName ); |
44 | sio << "New function: " << sName << sio.nl; | 44 | //sio << "New function: " << sName << sio.nl; |
45 | } | 45 | } |
46 | 46 | ||
47 | void GameBuilder::addFunctionParam( const Bu::String &sName ) | 47 | void GameBuilder::addFunctionParam( const Bu::String &sName ) |
48 | { | 48 | { |
49 | pCurFnc->addParam( sName ); | 49 | pCurFnc->addParam( sName ); |
50 | sio << " - Param added '" << sName << "'" << sio.nl; | 50 | //sio << " - Param added '" << sName << "'" << sio.nl; |
51 | } | 51 | } |
52 | 52 | ||
53 | void GameBuilder::endFunctionParams() | 53 | void GameBuilder::endFunctionParams() |
@@ -68,7 +68,7 @@ void GameBuilder::endFunctionParams() | |||
68 | 68 | ||
69 | void GameBuilder::endFunction() | 69 | void GameBuilder::endFunction() |
70 | { | 70 | { |
71 | sio << "Function ended: " << *pCurRoot << sio.nl; | 71 | //sio << "Function ended: " << *pCurRoot << sio.nl; |
72 | pCurFnc->setAst( pCurRoot ); | 72 | pCurFnc->setAst( pCurRoot ); |
73 | pCurRoot = pCurNode = NULL; | 73 | pCurRoot = pCurNode = NULL; |
74 | pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); | 74 | pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); |
@@ -77,7 +77,7 @@ void GameBuilder::endFunction() | |||
77 | void GameBuilder::beginSituation( const Bu::String &sName ) | 77 | void GameBuilder::beginSituation( const Bu::String &sName ) |
78 | { | 78 | { |
79 | pCurSit = new Situation( sName ); | 79 | pCurSit = new Situation( sName ); |
80 | sio << "New situation: " << sName << sio.nl; | 80 | //sio << "New situation: " << sName << sio.nl; |
81 | } | 81 | } |
82 | 82 | ||
83 | void GameBuilder::beginSituationMode( Situation::Mode m ) | 83 | void GameBuilder::beginSituationMode( Situation::Mode m ) |
@@ -88,7 +88,7 @@ void GameBuilder::beginSituationMode( Situation::Mode m ) | |||
88 | 88 | ||
89 | void GameBuilder::closeSituationMode() | 89 | void GameBuilder::closeSituationMode() |
90 | { | 90 | { |
91 | sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; | 91 | //sio << "Set situation mode " << eCurSitMode << " to " << *pCurRoot << sio.nl; |
92 | pCurSit->setAst( pCurRoot, eCurSitMode ); | 92 | pCurSit->setAst( pCurRoot, eCurSitMode ); |
93 | pCurRoot = pCurNode = NULL; | 93 | pCurRoot = pCurNode = NULL; |
94 | } | 94 | } |
@@ -96,7 +96,7 @@ void GameBuilder::closeSituationMode() | |||
96 | void GameBuilder::endSituation() | 96 | void GameBuilder::endSituation() |
97 | { | 97 | { |
98 | pGame->hSituation.insert( pCurSit->getName(), pCurSit ); | 98 | pGame->hSituation.insert( pCurSit->getName(), pCurSit ); |
99 | sio << "Situation ended." << sio.nl; | 99 | //sio << "Situation ended." << sio.nl; |
100 | } | 100 | } |
101 | 101 | ||
102 | void GameBuilder::addNode( AstNode::Type iType ) | 102 | void GameBuilder::addNode( AstNode::Type iType ) |
@@ -185,7 +185,7 @@ void GameBuilder::closeCommand() | |||
185 | { | 185 | { |
186 | pCurCmd->setAst( pCurRoot ); | 186 | pCurCmd->setAst( pCurRoot ); |
187 | pCurRoot = pCurNode = NULL; | 187 | pCurRoot = pCurNode = NULL; |
188 | pCurCmd->print(); | 188 | // pCurCmd->print(); |
189 | if( bGlobal ) | 189 | if( bGlobal ) |
190 | { | 190 | { |
191 | pGame->csGlobal.addCommand( pCurCmd ); | 191 | 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 @@ | |||
9 | using namespace Bu; | 9 | using namespace Bu; |
10 | 10 | ||
11 | GameState::GameState( Game *pGame ) : | 11 | GameState::GameState( Game *pGame ) : |
12 | pGame( pGame ) | 12 | pGame( pGame ), |
13 | bRunning( true ) | ||
13 | { | 14 | { |
14 | } | 15 | } |
15 | 16 | ||
@@ -73,7 +74,8 @@ void GameState::execCommand( const Bu::String &sCmd ) | |||
73 | { | 74 | { |
74 | if( !pGame->execCommand( *this, lCmd ) ) | 75 | if( !pGame->execCommand( *this, lCmd ) ) |
75 | { | 76 | { |
76 | throw Bu::ExceptionBase("No such command exists."); | 77 | sio << "I don't understand that command." << sio.nl; |
78 | return; | ||
77 | } | 79 | } |
78 | } | 80 | } |
79 | } | 81 | } |
@@ -215,6 +217,12 @@ Bu::StringList GameState::tokenize( const Bu::String &sSrc ) | |||
215 | return lRet; | 217 | return lRet; |
216 | } | 218 | } |
217 | 219 | ||
220 | void GameState::exit() | ||
221 | { | ||
222 | bRunning = false; | ||
223 | bEscape = true; | ||
224 | } | ||
225 | |||
218 | Variable GameState::popDeref() | 226 | Variable GameState::popDeref() |
219 | { | 227 | { |
220 | Variable v = lStack.peekPop(); | 228 | Variable v = lStack.peekPop(); |
@@ -420,6 +428,17 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
420 | } | 428 | } |
421 | break; | 429 | break; |
422 | 430 | ||
431 | case AstNode::tReturn: | ||
432 | return; | ||
433 | break; | ||
434 | |||
435 | case AstNode::tAppend: | ||
436 | { | ||
437 | Variable v = popDeref(); | ||
438 | lStack.peek() += v; | ||
439 | } | ||
440 | break; | ||
441 | |||
423 | // tLeafLiteral | 442 | // tLeafLiteral |
424 | case AstNode::tVarName: | 443 | case AstNode::tVarName: |
425 | case AstNode::tLiteral: | 444 | case AstNode::tLiteral: |
@@ -435,6 +454,9 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
435 | 454 | ||
436 | // tBranch | 455 | // tBranch |
437 | case AstNode::tScope: | 456 | case AstNode::tScope: |
457 | throw Bu::ExceptionBase("Scope? that shouldn't be here..."); | ||
458 | break; | ||
459 | |||
438 | case AstNode::tIf: | 460 | case AstNode::tIf: |
439 | { | 461 | { |
440 | AstBranch::NodeList lIf = | 462 | AstBranch::NodeList lIf = |
@@ -488,7 +510,7 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
488 | break; | 510 | break; |
489 | } | 511 | } |
490 | 512 | ||
491 | if( bEscape ) | 513 | if( bEscape || !bRunning ) |
492 | { | 514 | { |
493 | return; | 515 | return; |
494 | } | 516 | } |
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: | |||
35 | Variable deref( const Variable &src ); | 35 | Variable deref( const Variable &src ); |
36 | Bu::StringList tokenize( const Bu::String &sSrc ); | 36 | Bu::StringList tokenize( const Bu::String &sSrc ); |
37 | 37 | ||
38 | void exit(); | ||
39 | bool isRunning() const { return bRunning; } | ||
40 | Bu::String getPrompt() const { return sPrompt; } | ||
41 | |||
38 | private: | 42 | private: |
39 | void parse( const AstBranch::NodeList &lCode ); | 43 | void parse( const AstBranch::NodeList &lCode ); |
40 | 44 | ||
@@ -49,6 +53,8 @@ private: | |||
49 | Bu::String sCurSituation; | 53 | Bu::String sCurSituation; |
50 | 54 | ||
51 | bool bEscape; | 55 | bool bEscape; |
56 | bool bRunning; | ||
57 | Bu::String sPrompt; | ||
52 | 58 | ||
53 | VariableList lStack; | 59 | VariableList lStack; |
54 | }; | 60 | }; |
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[] ) | |||
33 | GameState gs( pGame ); | 33 | GameState gs( pGame ); |
34 | gs.init(); | 34 | gs.init(); |
35 | 35 | ||
36 | char buf[1024]; | 36 | while( gs.isRunning() ) |
37 | sio << ">>" << sio.flush; | 37 | { |
38 | fgets( buf, 1024, stdin ); | 38 | char buf[1024]; |
39 | 39 | sio << "command> " << sio.flush; | |
40 | sio << "Read: >" << buf << "<" << sio.nl; | 40 | fgets( buf, 1024, stdin ); |
41 | gs.execCommand( buf ); | 41 | |
42 | gs.execCommand( buf ); | ||
43 | } | ||
42 | 44 | ||
43 | return 0; | 45 | return 0; |
44 | } | 46 | } |
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; } | |||
48 | enter { return tokEnter; } | 48 | enter { return tokEnter; } |
49 | and { return tokAnd; } | 49 | and { return tokAnd; } |
50 | or { return tokOr; } | 50 | or { return tokOr; } |
51 | return { return tokReturn; } | ||
51 | 52 | ||
52 | true { yylval->bValue = true; return tokBool; } | 53 | true { yylval->bValue = true; return tokBool; } |
53 | false { yylval->bValue = false; return tokBool; } | 54 | 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 | |||
62 | %token tokLtEq | 62 | %token tokLtEq |
63 | %token tokGtEq | 63 | %token tokGtEq |
64 | %token tokCmp | 64 | %token tokCmp |
65 | %token tokReturn | ||
65 | %token tokPlusAssign | 66 | %token tokPlusAssign |
66 | %token tokMinusAssign | 67 | %token tokMinusAssign |
67 | %token tokTimesAssign | 68 | %token tokTimesAssign |
@@ -150,6 +151,8 @@ cmpltExprList: | |||
150 | ; | 151 | ; |
151 | 152 | ||
152 | cmpltExpr: expr ';' | 153 | cmpltExpr: expr ';' |
154 | | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); } | ||
155 | | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); } | ||
153 | | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); } | 156 | | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); } |
154 | | ifbase | 157 | | ifbase |
155 | | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' | 158 | | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' |
@@ -224,8 +227,8 @@ expr: literal | |||
224 | | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); } | 227 | | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); } |
225 | | '(' expr ')' | 228 | | '(' expr ')' |
226 | | expr '[' expr ']' | 229 | | expr '[' expr ']' |
227 | | '[' ']' | 230 | | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } |
228 | | '[' listValues ']' | 231 | | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' |
229 | | '{' '}' | 232 | | '{' '}' |
230 | | '{' dictValues '}' | 233 | | '{' dictValues '}' |
231 | | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } | 234 | | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } |
@@ -240,8 +243,8 @@ funcCallParamsEx: | |||
240 | | funcCallParamsEx ',' expr | 243 | | funcCallParamsEx ',' expr |
241 | ; | 244 | ; |
242 | 245 | ||
243 | listValues: expr | 246 | listValues: expr { bld.addNode( AstNode::tAppend ); } |
244 | | listValues ',' expr | 247 | | listValues ',' expr { bld.addNode( AstNode::tAppend ); } |
245 | ; | 248 | ; |
246 | 249 | ||
247 | dictValues: expr ':' expr | 250 | 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 ) | |||
309 | case tVariable: | 309 | case tVariable: |
310 | throw VariableException("You cannot add variable names."); | 310 | throw VariableException("You cannot add variable names."); |
311 | break; | 311 | break; |
312 | |||
313 | case tList: | ||
314 | (*lValue).append( rhs ); | ||
315 | break; | ||
312 | } | 316 | } |
313 | 317 | ||
314 | return *this; | 318 | return *this; |