diff options
-rw-r--r-- | src/command.h | 2 | ||||
-rw-r--r-- | src/commandset.h | 4 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 20 | ||||
-rw-r--r-- | src/gamebuilder.h | 5 | ||||
-rw-r--r-- | src/gamestate.cpp | 10 | ||||
-rw-r--r-- | src/gamestate.h | 3 | ||||
-rw-r--r-- | src/interfaceconsole.cpp | 45 | ||||
-rw-r--r-- | src/parser.y | 9 | ||||
-rw-r--r-- | src/situation.cpp | 7 | ||||
-rw-r--r-- | src/situation.h | 12 | ||||
-rw-r--r-- | support/vim/syntax/stage.vim | 2 |
11 files changed, 105 insertions, 14 deletions
diff --git a/src/command.h b/src/command.h index f82e87e..cc49996 100644 --- a/src/command.h +++ b/src/command.h | |||
@@ -9,6 +9,8 @@ public: | |||
9 | Command(); | 9 | Command(); |
10 | virtual ~Command(); | 10 | virtual ~Command(); |
11 | 11 | ||
12 | Bu::String getRoot() const { return lChunks.first().sValue; } | ||
13 | |||
12 | void addLiteral( const Bu::String &sValue ); | 14 | void addLiteral( const Bu::String &sValue ); |
13 | void addParam( const Bu::String &sValue ); | 15 | void addParam( const Bu::String &sValue ); |
14 | 16 | ||
diff --git a/src/commandset.h b/src/commandset.h index 579e1d8..aff2e4c 100644 --- a/src/commandset.h +++ b/src/commandset.h | |||
@@ -14,8 +14,10 @@ public: | |||
14 | 14 | ||
15 | bool dispatch( class GameState &gState, const Bu::StringList &lCmd ); | 15 | bool dispatch( class GameState &gState, const Bu::StringList &lCmd ); |
16 | 16 | ||
17 | private: | ||
18 | typedef Bu::List<class Command *> CommandList; | 17 | typedef Bu::List<class Command *> CommandList; |
18 | const CommandList &getCommandList() const { return lCommand; } | ||
19 | |||
20 | private: | ||
19 | CommandList lCommand; | 21 | CommandList lCommand; |
20 | }; | 22 | }; |
21 | 23 | ||
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 83cbe79..353e254 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp | |||
@@ -96,9 +96,9 @@ void GameBuilder::endFunction() | |||
96 | pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); | 96 | pGame->hFunction.insert( pCurFnc->getName(), pCurFnc ); |
97 | } | 97 | } |
98 | 98 | ||
99 | void GameBuilder::beginSituation( const Bu::String &sName ) | 99 | void GameBuilder::beginSituation( const Bu::String &sName, Situation::InputType tInput ) |
100 | { | 100 | { |
101 | pCurSit = new Situation( sName ); | 101 | pCurSit = new Situation( sName, tInput ); |
102 | //sio << "New situation: " << sName << sio.nl; | 102 | //sio << "New situation: " << sName << sio.nl; |
103 | } | 103 | } |
104 | 104 | ||
@@ -219,3 +219,19 @@ void GameBuilder::closeCommand() | |||
219 | pCurCmd = NULL; | 219 | pCurCmd = NULL; |
220 | } | 220 | } |
221 | 221 | ||
222 | void GameBuilder::beginOption( const Bu::String &sValue ) | ||
223 | { | ||
224 | pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); | ||
225 | pCurCmd = new Command(); | ||
226 | pCurCmd->addLiteral( sValue ); | ||
227 | } | ||
228 | |||
229 | void GameBuilder::closeOption() | ||
230 | { | ||
231 | pCurCmd->setAst( pCurRoot ); | ||
232 | pCurRoot = pCurNode = NULL; | ||
233 | //pCurCmd->print(); | ||
234 | pCurSit->csLocal.addCommand( pCurCmd ); | ||
235 | pCurCmd = NULL; | ||
236 | } | ||
237 | |||
diff --git a/src/gamebuilder.h b/src/gamebuilder.h index f3e3a1f..80df1bc 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h | |||
@@ -25,7 +25,7 @@ public: | |||
25 | void endFunctionParams(); | 25 | void endFunctionParams(); |
26 | void endFunction(); | 26 | void endFunction(); |
27 | 27 | ||
28 | void beginSituation( const Bu::String &sName ); | 28 | void beginSituation( const Bu::String &sName, Situation::InputType tInput ); |
29 | void beginSituationMode( Situation::Mode m ); | 29 | void beginSituationMode( Situation::Mode m ); |
30 | void closeSituationMode(); | 30 | void closeSituationMode(); |
31 | void endSituation(); | 31 | void endSituation(); |
@@ -44,6 +44,9 @@ public: | |||
44 | void addCommandParam( const Bu::String &sValue ); | 44 | void addCommandParam( const Bu::String &sValue ); |
45 | void endCommandParams(); | 45 | void endCommandParams(); |
46 | void closeCommand(); | 46 | void closeCommand(); |
47 | |||
48 | void beginOption( const Bu::String &sValue ); | ||
49 | void closeOption(); | ||
47 | 50 | ||
48 | private: | 51 | private: |
49 | class Game *pGame; | 52 | class Game *pGame; |
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index ec6bf72..0a82b99 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
@@ -233,6 +233,11 @@ Variable GameState::gatsToVariable( Gats::List *pLst ) const | |||
233 | throw Bu::ExceptionBase("Type unknown: %d", dynamic_cast<Gats::Integer *>(*i)->getValue() ); | 233 | throw Bu::ExceptionBase("Type unknown: %d", dynamic_cast<Gats::Integer *>(*i)->getValue() ); |
234 | } | 234 | } |
235 | 235 | ||
236 | class Situation *GameState::getCurSituation() | ||
237 | { | ||
238 | return pGame->getSituation( sCurSituation ); | ||
239 | } | ||
240 | |||
236 | void GameState::parse( class AstBranch *pAst ) | 241 | void GameState::parse( class AstBranch *pAst ) |
237 | { | 242 | { |
238 | if( pAst->getType() != AstNode::tScope ) | 243 | if( pAst->getType() != AstNode::tScope ) |
@@ -297,6 +302,11 @@ void GameState::execCommand( const Bu::String &sCmd ) | |||
297 | } | 302 | } |
298 | } | 303 | } |
299 | 304 | ||
305 | void GameState::execOption( int idx ) | ||
306 | { | ||
307 | pGame->getSituation( sCurSituation )->execOption( *this, idx ); | ||
308 | } | ||
309 | |||
300 | bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) | 310 | bool GameState::hasVariable( const Bu::String &sName, ScopeId id ) |
301 | { | 311 | { |
302 | switch( id ) | 312 | switch( id ) |
diff --git a/src/gamestate.h b/src/gamestate.h index 8ff92f5..4209ebf 100644 --- a/src/gamestate.h +++ b/src/gamestate.h | |||
@@ -39,6 +39,7 @@ public: | |||
39 | 39 | ||
40 | void callFunction( const Bu::String &sName ); | 40 | void callFunction( const Bu::String &sName ); |
41 | void execCommand( const Bu::String &sCmd ); | 41 | void execCommand( const Bu::String &sCmd ); |
42 | void execOption( int idx ); | ||
42 | 43 | ||
43 | bool hasVariable( const Bu::String &sName, ScopeId id ); | 44 | bool hasVariable( const Bu::String &sName, ScopeId id ); |
44 | void delVariable( const Bu::String &sName, ScopeId id ); | 45 | void delVariable( const Bu::String &sName, ScopeId id ); |
@@ -52,6 +53,8 @@ public: | |||
52 | bool isRunning() const { return bRunning; } | 53 | bool isRunning() const { return bRunning; } |
53 | Bu::String getPrompt() const { return sPrompt; } | 54 | Bu::String getPrompt() const { return sPrompt; } |
54 | 55 | ||
56 | class Situation *getCurSituation(); | ||
57 | |||
55 | private: | 58 | private: |
56 | void parse( const AstBranch::NodeList &lCode ); | 59 | void parse( const AstBranch::NodeList &lCode ); |
57 | 60 | ||
diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp index 49be5c1..e652da8 100644 --- a/src/interfaceconsole.cpp +++ b/src/interfaceconsole.cpp | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | #include "smlnode.h" | 3 | #include "smlnode.h" |
4 | #include "gamestate.h" | 4 | #include "gamestate.h" |
5 | #include "situation.h" | ||
6 | #include "command.h" | ||
5 | 7 | ||
6 | #include <bu/sio.h> | 8 | #include <bu/sio.h> |
7 | 9 | ||
@@ -12,6 +14,8 @@ | |||
12 | #include <gats/gatsstream.h> | 14 | #include <gats/gatsstream.h> |
13 | #include <bu/file.h> | 15 | #include <bu/file.h> |
14 | 16 | ||
17 | #include <stdlib.h> | ||
18 | |||
15 | PluginInterface3( plugin_interface_console, console, InterfaceConsole, | 19 | PluginInterface3( plugin_interface_console, console, InterfaceConsole, |
16 | Interface, "Mike Buland", 1, 0 ); | 20 | Interface, "Mike Buland", 1, 0 ); |
17 | 21 | ||
@@ -34,10 +38,45 @@ void InterfaceConsole::run( Game *pGame ) | |||
34 | while( gs.isRunning() ) | 38 | while( gs.isRunning() ) |
35 | { | 39 | { |
36 | char buf[1024]; | 40 | char buf[1024]; |
37 | sio << sio.nl << "command> " << sio.flush; | 41 | switch( gs.getCurSituation()->getInputType() ) |
38 | fgets( buf, 1024, stdin ); | 42 | { |
43 | case Situation::inputCommand: | ||
44 | sio << sio.nl << "command> " << sio.flush; | ||
45 | fgets( buf, 1024, stdin ); | ||
46 | |||
47 | gs.execCommand( buf ); | ||
48 | break; | ||
49 | |||
50 | case Situation::inputOption: | ||
51 | sio << sio.nl; | ||
52 | { | ||
53 | const CommandSet::CommandList &cl = | ||
54 | gs.getCurSituation()->getCommandSet().getCommandList(); | ||
39 | 55 | ||
40 | gs.execCommand( buf ); | 56 | int j = 1; |
57 | for( CommandSet::CommandList::const_iterator i = cl.begin(); | ||
58 | i; i++ ) | ||
59 | { | ||
60 | sio << " " << j << ") " << (*i)->getRoot() << sio.nl; | ||
61 | j++; | ||
62 | } | ||
63 | sio << "Enter your selection: " << sio.flush; | ||
64 | fgets( buf, 1024, stdin ); | ||
65 | if( buf[0] < '1' || buf[0] > '9' ) | ||
66 | j = -1; | ||
67 | else | ||
68 | j = strtol( buf, NULL, 10 ); | ||
69 | if( j < 1 || j > cl.getSize() ) | ||
70 | { | ||
71 | sio << "Invalid selection, try again." << sio.nl; | ||
72 | } | ||
73 | else | ||
74 | { | ||
75 | gs.execOption( j-1 ); | ||
76 | } | ||
77 | } | ||
78 | break; | ||
79 | } | ||
41 | } | 80 | } |
42 | } | 81 | } |
43 | 82 | ||
diff --git a/src/parser.y b/src/parser.y index e127627..b50b897 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -121,9 +121,9 @@ bodyDecl: | |||
121 | | bodyDecl function | 121 | | bodyDecl function |
122 | ; | 122 | ; |
123 | 123 | ||
124 | situationMode: tokSituation tokSituationName { bld.beginSituation( *($2) ); } | 124 | situationMode: tokSituation tokSituationName { bld.beginSituation( *($2), Situation::inputCommand ); } |
125 | | tokCommand tokSituation tokSituationName { bld.beginSituation( *($2) ); } | 125 | | tokCommand tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputCommand ); } |
126 | | tokOption tokSituation tokSituationName { bld.beginSituation( *($2) ); } | 126 | | tokOption tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputOption ); } |
127 | ; | 127 | ; |
128 | 128 | ||
129 | situation: situationMode '{' situationMembers '}' { bld.endSituation(); } | 129 | situation: situationMode '{' situationMembers '}' { bld.endSituation(); } |
@@ -284,7 +284,8 @@ dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); } | |||
284 | | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } | 284 | | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } |
285 | ; | 285 | ; |
286 | 286 | ||
287 | optionDecl: tokOption ':' tokString '{' cmpltExprList '}' | 287 | optionDecl: tokOption ':' tokString { bld.beginOption( *$3 ); } '{' |
288 | cmpltExprList '}' { bld.closeOption(); } | ||
288 | ; | 289 | ; |
289 | 290 | ||
290 | commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } | 291 | commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } |
diff --git a/src/situation.cpp b/src/situation.cpp index 86dd5c5..54096cc 100644 --- a/src/situation.cpp +++ b/src/situation.cpp | |||
@@ -5,8 +5,9 @@ | |||
5 | 5 | ||
6 | #include <bu/formatter.h> | 6 | #include <bu/formatter.h> |
7 | 7 | ||
8 | Situation::Situation( const Bu::String &sName ) : | 8 | Situation::Situation( const Bu::String &sName, InputType tInput ) : |
9 | sName( sName ), | 9 | sName( sName ), |
10 | tInput( tInput ), | ||
10 | pAstSetup( NULL ), | 11 | pAstSetup( NULL ), |
11 | pAstEnter( NULL ) | 12 | pAstEnter( NULL ) |
12 | { | 13 | { |
@@ -59,6 +60,10 @@ bool Situation::execCommand( class GameState &gState, | |||
59 | return csLocal.dispatch( gState, lCmd ); | 60 | return csLocal.dispatch( gState, lCmd ); |
60 | } | 61 | } |
61 | 62 | ||
63 | void Situation::execOption( class GameState &gState, int idx ) | ||
64 | { | ||
65 | } | ||
66 | |||
62 | Bu::Formatter &operator<<( Bu::Formatter &f, Situation::Mode m ) | 67 | Bu::Formatter &operator<<( Bu::Formatter &f, Situation::Mode m ) |
63 | { | 68 | { |
64 | switch( m ) | 69 | switch( m ) |
diff --git a/src/situation.h b/src/situation.h index 365533a..794b308 100644 --- a/src/situation.h +++ b/src/situation.h | |||
@@ -9,10 +9,17 @@ class Situation | |||
9 | { | 9 | { |
10 | friend class GameBuilder; | 10 | friend class GameBuilder; |
11 | public: | 11 | public: |
12 | Situation( const Bu::String &sName ); | 12 | enum InputType |
13 | { | ||
14 | inputCommand, | ||
15 | inputOption, | ||
16 | }; | ||
17 | |||
18 | Situation( const Bu::String &sName, InputType tInput ); | ||
13 | virtual ~Situation(); | 19 | virtual ~Situation(); |
14 | 20 | ||
15 | Bu::String getName() const { return sName; } | 21 | Bu::String getName() const { return sName; } |
22 | InputType getInputType() const { return tInput; } | ||
16 | 23 | ||
17 | enum Mode | 24 | enum Mode |
18 | { | 25 | { |
@@ -25,9 +32,12 @@ public: | |||
25 | 32 | ||
26 | void addCommand( Command *pCmd ); | 33 | void addCommand( Command *pCmd ); |
27 | bool execCommand( class GameState &gState, const Bu::StringList &lCmd ); | 34 | bool execCommand( class GameState &gState, const Bu::StringList &lCmd ); |
35 | void execOption( class GameState &gState, int idx ); | ||
36 | const CommandSet &getCommandSet() const { return csLocal; } | ||
28 | 37 | ||
29 | private: | 38 | private: |
30 | Bu::String sName; | 39 | Bu::String sName; |
40 | InputType tInput; | ||
31 | CommandSet csLocal; | 41 | CommandSet csLocal; |
32 | class AstBranch *pAstSetup; | 42 | class AstBranch *pAstSetup; |
33 | class AstBranch *pAstEnter; | 43 | class AstBranch *pAstEnter; |
diff --git a/support/vim/syntax/stage.vim b/support/vim/syntax/stage.vim index 13d86b4..6009d80 100644 --- a/support/vim/syntax/stage.vim +++ b/support/vim/syntax/stage.vim | |||
@@ -17,7 +17,7 @@ syn keyword Loop for each do in while | |||
17 | syn keyword Logic not and or | 17 | syn keyword Logic not and or |
18 | syn keyword Statement setup enter | 18 | syn keyword Statement setup enter |
19 | syn keyword Todo TODO FIXME XXX | 19 | syn keyword Todo TODO FIXME XXX |
20 | syn keyword Type function command situation game global player | 20 | syn keyword Type function command situation game global player option |
21 | syn keyword Constant null true false | 21 | syn keyword Constant null true false |
22 | syn keyword Builtins display goto exists delete exit return random integer float string debugString keys join | 22 | syn keyword Builtins display goto exists delete exit return random integer float string debugString keys join |
23 | 23 | ||