summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-30 13:11:35 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-30 13:11:35 -0700
commitf2ee67558acbe3c418a7558587b56158d593d88d (patch)
treed0b306fc26679fee672df740acdbdc0ede3c50dd
parente112d781ea5ffc4186e7d70dba595b5a233335a8 (diff)
downloadstage-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...
-rw-r--r--src/astnode.cpp3
-rw-r--r--src/astnode.h3
-rw-r--r--src/functionexit.cpp17
-rw-r--r--src/functionexit.h16
-rw-r--r--src/game.cpp2
-rw-r--r--src/gamebuilder.cpp14
-rw-r--r--src/gamestate.cpp28
-rw-r--r--src/gamestate.h6
-rw-r--r--src/main.cpp14
-rw-r--r--src/parser.l1
-rw-r--r--src/parser.y11
-rw-r--r--src/variable.cpp4
-rw-r--r--support/vim/syntax/stage.vim14
-rw-r--r--test.stage37
14 files changed, 128 insertions, 42 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
5FunctionExit::FunctionExit()
6{
7}
8
9FunctionExit::~FunctionExit()
10{
11}
12
13void 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
6class FunctionExit : public Function
7{
8public:
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
7Game::Game() 8Game::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
14Game::~Game() 16Game::~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
47void GameBuilder::addFunctionParam( const Bu::String &sName ) 47void 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
53void GameBuilder::endFunctionParams() 53void GameBuilder::endFunctionParams()
@@ -68,7 +68,7 @@ void GameBuilder::endFunctionParams()
68 68
69void GameBuilder::endFunction() 69void 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()
77void GameBuilder::beginSituation( const Bu::String &sName ) 77void 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
83void GameBuilder::beginSituationMode( Situation::Mode m ) 83void GameBuilder::beginSituationMode( Situation::Mode m )
@@ -88,7 +88,7 @@ void GameBuilder::beginSituationMode( Situation::Mode m )
88 88
89void GameBuilder::closeSituationMode() 89void 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()
96void GameBuilder::endSituation() 96void 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
102void GameBuilder::addNode( AstNode::Type iType ) 102void 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 @@
9using namespace Bu; 9using namespace Bu;
10 10
11GameState::GameState( Game *pGame ) : 11GameState::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
220void GameState::exit()
221{
222 bRunning = false;
223 bEscape = true;
224}
225
218Variable GameState::popDeref() 226Variable 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
38private: 42private:
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; }
48enter { return tokEnter; } 48enter { return tokEnter; }
49and { return tokAnd; } 49and { return tokAnd; }
50or { return tokOr; } 50or { return tokOr; }
51return { return tokReturn; }
51 52
52true { yylval->bValue = true; return tokBool; } 53true { yylval->bValue = true; return tokBool; }
53false { yylval->bValue = false; return tokBool; } 54false { 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
152cmpltExpr: expr ';' 153cmpltExpr: 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
243listValues: expr 246listValues: expr { bld.addNode( AstNode::tAppend ); }
244 | listValues ',' expr 247 | listValues ',' expr { bld.addNode( AstNode::tAppend ); }
245 ; 248 ;
246 249
247dictValues: expr ':' expr 250dictValues: 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;
diff --git a/support/vim/syntax/stage.vim b/support/vim/syntax/stage.vim
index b5dab28..b18ec0d 100644
--- a/support/vim/syntax/stage.vim
+++ b/support/vim/syntax/stage.vim
@@ -19,7 +19,7 @@ syn keyword Statement setup enter
19syn keyword Todo TODO FIXME XXX 19syn keyword Todo TODO FIXME XXX
20syn keyword Type function command situation game global player 20syn keyword Type function command situation game global player
21syn keyword Constant null true false 21syn keyword Constant null true false
22syn keyword Builtins display goto exists delete 22syn keyword Builtins display goto exists delete exit return
23 23
24syn match TargetProcess /[a-zA-Z_][a-zA-Z0-9_]*:/he=e-1 24syn match TargetProcess /[a-zA-Z_][a-zA-Z0-9_]*:/he=e-1
25 25
@@ -29,13 +29,11 @@ syn match Special display contained "\\\(x\x\+\|\o\{1,3}\|.\|$\)"
29syn match Special display contained "\\\(u\x\{4}\|U\x\{8}\)" 29syn match Special display contained "\\\(u\x\{4}\|U\x\{8}\)"
30 30
31" TODO: do we want to end at end of line too? 31" TODO: do we want to end at end of line too?
32syn region tripSingleString start=+'''+ skip=+\\\\\|\\'+ end=+'''+ contains=Special,CmdEx 32syn region singleString start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=Special
33syn region tripDoubleString start=+"""+ skip=+\\\\\|\\"+ end=+"""+ contains=Special,CmdEx 33syn region doubleString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=Special
34syn region singleString start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=Special,CmdEx 34syn region tripSingleString start=+'''+ end=+'''+ contains=Special
35syn region doubleString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=Special,CmdEx 35syn region tripDoubleString start=+"""+ end=+"""+ contains=Special
36syn region sitName start=+<<+ end=+>>+ contains=Special,CmdEx 36syn region sitName start=+<<+ end=+>>+ contains=Special
37
38syn region CmdEx start=+$(+ skip=+"\|\\)\|\\\\+ end=+)+
39 37
40syn case ignore 38syn case ignore
41syn match Numbers display transparent "\<\d\|\.\d" contains=Number,Float,OctalError,Octal 39syn match Numbers display transparent "\<\d\|\.\d" contains=Number,Float,OctalError,Octal
diff --git a/test.stage b/test.stage
index 0963143..1c58991 100644
--- a/test.stage
+++ b/test.stage
@@ -11,6 +11,17 @@ global
11 { 11 {
12 display(object); 12 display(object);
13 } 13 }
14 // You should always have a global exit, quit, escape, something for
15 // dev-testing at least.
16 command: "exit"
17 {
18 exit();
19 }
20}
21
22function square( x )
23{
24 return( x*x );
14} 25}
15 26
16function sillyDisplay( txt, extra ) 27function sillyDisplay( txt, extra )
@@ -39,22 +50,20 @@ function getThing()
39 50
40situation <<start>> 51situation <<start>>
41{ 52{
42 setup 53 command: "go" "home"
43 { 54 {
44 situation.thing = 55; 55 display("Home would be nice...");
45 player.name = "Bob"; 56 }
46 name = player.name + "o";
47 name += " The Man";
48 display("This is the setup phase for start, " + name);
49 sillyDisplay( "Hello", name == player.name );
50
51 display( exists(name) );
52 delete( name );
53 display( exists(name) );
54 57
55 getThing(); 58 command: "go" elsewhere
56 myGoto( <<stuff>> ); 59 {
57 display("You shouldn't see this."); 60 display("You don't know how to go " + elsewhere );
61 }
62 setup
63 {
64 stuff = [5, 10, "bob"];
65 display( stuff );
66 exit();
58 } 67 }
59 68
60 enter 69 enter