diff options
-rw-r--r-- | src/environment.cpp | 10 | ||||
-rw-r--r-- | src/game.cpp | 10 | ||||
-rw-r--r-- | src/game.h (renamed from src/environment.h) | 10 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 44 | ||||
-rw-r--r-- | src/gamebuilder.h | 30 | ||||
-rw-r--r-- | src/main.cpp | 21 | ||||
-rw-r--r-- | src/parser.y | 24 | ||||
-rw-r--r-- | src/variable.cpp | 40 | ||||
-rw-r--r-- | src/variable.h | 5 |
9 files changed, 169 insertions, 25 deletions
diff --git a/src/environment.cpp b/src/environment.cpp deleted file mode 100644 index 513649f..0000000 --- a/src/environment.cpp +++ /dev/null | |||
@@ -1,10 +0,0 @@ | |||
1 | #include "environment.h" | ||
2 | |||
3 | Environment::Environment() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Environment::~Environment() | ||
8 | { | ||
9 | } | ||
10 | |||
diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..61fa255 --- /dev/null +++ b/src/game.cpp | |||
@@ -0,0 +1,10 @@ | |||
1 | #include "game.h" | ||
2 | |||
3 | Game::Game() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Game::~Game() | ||
8 | { | ||
9 | } | ||
10 | |||
diff --git a/src/environment.h b/src/game.h index e728c50..a5b1163 100644 --- a/src/environment.h +++ b/src/game.h | |||
@@ -1,5 +1,5 @@ | |||
1 | #ifndef ENVIRONMENT_H | 1 | #ifndef GAME_H |
2 | #define ENVIRONMENT_H | 2 | #define GAME_H |
3 | 3 | ||
4 | #include <bu/string.h> | 4 | #include <bu/string.h> |
5 | #include <bu/hash.h> | 5 | #include <bu/hash.h> |
@@ -7,11 +7,11 @@ | |||
7 | #include "situation.h" | 7 | #include "situation.h" |
8 | #include "scope.h" | 8 | #include "scope.h" |
9 | 9 | ||
10 | class Environment | 10 | class Game |
11 | { | 11 | { |
12 | public: | 12 | public: |
13 | Environment(); | 13 | Game(); |
14 | virtual ~Environment(); | 14 | virtual ~Game(); |
15 | 15 | ||
16 | private: | 16 | private: |
17 | typedef Bu::Hash<Bu::String, Function *> FunctionHash; | 17 | typedef Bu::Hash<Bu::String, Function *> FunctionHash; |
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp new file mode 100644 index 0000000..912a595 --- /dev/null +++ b/src/gamebuilder.cpp | |||
@@ -0,0 +1,44 @@ | |||
1 | #include "gamebuilder.h" | ||
2 | #include <bu/sio.h> | ||
3 | |||
4 | using namespace Bu; | ||
5 | |||
6 | GameBuilder::GameBuilder() | ||
7 | { | ||
8 | } | ||
9 | |||
10 | GameBuilder::~GameBuilder() | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void GameBuilder::setLiteral( const Variable &v ) | ||
15 | { | ||
16 | vLiteral = v; | ||
17 | } | ||
18 | |||
19 | void GameBuilder::setGameParam( const Bu::String &sName ) | ||
20 | { | ||
21 | sio << "Set game param '" << sName << "' to " << vLiteral << sio.nl; | ||
22 | hGameParams.insert( sName, vLiteral ); | ||
23 | } | ||
24 | |||
25 | void GameBuilder::beginFunction( const Bu::String &sName ) | ||
26 | { | ||
27 | sio << "New function: " << sName << sio.nl; | ||
28 | } | ||
29 | |||
30 | void GameBuilder::endFunction() | ||
31 | { | ||
32 | sio << "Function ended." << sio.nl; | ||
33 | } | ||
34 | |||
35 | void GameBuilder::beginSituation( const Bu::String &sName ) | ||
36 | { | ||
37 | sio << "New situation: " << sName << sio.nl; | ||
38 | } | ||
39 | |||
40 | void GameBuilder::endSituation() | ||
41 | { | ||
42 | sio << "Situation ended." << sio.nl; | ||
43 | } | ||
44 | |||
diff --git a/src/gamebuilder.h b/src/gamebuilder.h new file mode 100644 index 0000000..59b4b57 --- /dev/null +++ b/src/gamebuilder.h | |||
@@ -0,0 +1,30 @@ | |||
1 | #ifndef GAME_BUILDER_H | ||
2 | #define GAME_BUILDER_H | ||
3 | |||
4 | #include <bu/string.h> | ||
5 | |||
6 | #include "variable.h" | ||
7 | |||
8 | class GameBuilder | ||
9 | { | ||
10 | public: | ||
11 | GameBuilder(); | ||
12 | virtual ~GameBuilder(); | ||
13 | |||
14 | void setLiteral( const Variable &v ); | ||
15 | void setGameParam( const Bu::String &sName ); | ||
16 | |||
17 | void beginFunction( const Bu::String &sName ); | ||
18 | void endFunction(); | ||
19 | |||
20 | |||
21 | void beginSituation( const Bu::String &sName ); | ||
22 | void endSituation(); | ||
23 | |||
24 | private: | ||
25 | Variable vLiteral; | ||
26 | |||
27 | VariableHash hGameParams; | ||
28 | }; | ||
29 | |||
30 | #endif | ||
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1adb55f --- /dev/null +++ b/src/main.cpp | |||
@@ -0,0 +1,21 @@ | |||
1 | #include "gamebuilder.h" | ||
2 | #include "parser.tab.h" | ||
3 | |||
4 | typedef void *yyscan_t; | ||
5 | void yylex_init( yyscan_t * ); | ||
6 | void yylex_destroy( yyscan_t ); | ||
7 | void yyparse( yyscan_t, GameBuilder &bld ); | ||
8 | |||
9 | int main( int argc, char *argv[] ) | ||
10 | { | ||
11 | yyscan_t scanner; | ||
12 | |||
13 | GameBuilder bld; | ||
14 | |||
15 | yylex_init( &scanner ); | ||
16 | yyparse( scanner, bld ); | ||
17 | yylex_destroy( scanner ); | ||
18 | |||
19 | return 0; | ||
20 | } | ||
21 | |||
diff --git a/src/parser.y b/src/parser.y index b1c8ec9..8e198c8 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -3,6 +3,8 @@ | |||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <bu/string.h> | 4 | #include <bu/string.h> |
5 | 5 | ||
6 | #include "gamebuilder.h" | ||
7 | |||
6 | typedef void *yyscan_t; | 8 | typedef void *yyscan_t; |
7 | 9 | ||
8 | %} | 10 | %} |
@@ -16,6 +18,8 @@ typedef void *yyscan_t; | |||
16 | %parse-param { yyscan_t yyscanner } | 18 | %parse-param { yyscan_t yyscanner } |
17 | %lex-param { yysacn_t yyscanner } | 19 | %lex-param { yysacn_t yyscanner } |
18 | 20 | ||
21 | %parse-param { GameBuilder &bld } | ||
22 | |||
19 | %union { | 23 | %union { |
20 | int64_t iValue; | 24 | int64_t iValue; |
21 | double dValue; | 25 | double dValue; |
@@ -25,7 +29,7 @@ typedef void *yyscan_t; | |||
25 | 29 | ||
26 | %{ | 30 | %{ |
27 | int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); | 31 | int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); |
28 | void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error ) | 32 | void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *error ) |
29 | { | 33 | { |
30 | printf("%d:%d-%d:%d: %s\n", | 34 | printf("%d:%d-%d:%d: %s\n", |
31 | llocp->first_line, llocp->first_column, | 35 | llocp->first_line, llocp->first_column, |
@@ -83,7 +87,7 @@ input: gameDecls globalDecl bodyDecl | |||
83 | ; | 87 | ; |
84 | 88 | ||
85 | gameDecls: | 89 | gameDecls: |
86 | | gameDecls tokGame '.' tokIdent '=' literal ';' | 90 | | gameDecls tokGame '.' tokIdent '=' literal ';' { bld.setGameParam( *($4) ); } |
87 | ; | 91 | ; |
88 | 92 | ||
89 | globalDecl: tokGlobal '{' globalExprList '}' | 93 | globalDecl: tokGlobal '{' globalExprList '}' |
@@ -143,12 +147,12 @@ varRef: tokIdent | |||
143 | | tokSituation '.' tokIdent | 147 | | tokSituation '.' tokIdent |
144 | ; | 148 | ; |
145 | 149 | ||
146 | literal: tokInt | 150 | literal: tokInt { bld.setLiteral( Variable( $1 ) ); } |
147 | | tokFloat | 151 | | tokFloat { bld.setLiteral( Variable( $1 ) ); } |
148 | | tokString | 152 | | tokString { bld.setLiteral( Variable( *($1) ) ); } |
149 | | tokBool | 153 | | tokBool { bld.setLiteral( Variable( $1 ) ); } |
150 | | tokNull | 154 | | tokNull { bld.setLiteral( Variable( Variable::tNull ) ); } |
151 | | tokSituationName | 155 | | tokSituationName { bld.setLiteral( Variable::newSituationName( *($1) ) ); } |
152 | ; | 156 | ; |
153 | 157 | ||
154 | expr: literal | 158 | expr: literal |
@@ -189,7 +193,7 @@ commandParamList: | |||
189 | | commandParamList tokIdent | 193 | | commandParamList tokIdent |
190 | ; | 194 | ; |
191 | %% | 195 | %% |
192 | 196 | /* | |
193 | void yylex_init( yyscan_t * ); | 197 | void yylex_init( yyscan_t * ); |
194 | void yylex_destroy( yyscan_t ); | 198 | void yylex_destroy( yyscan_t ); |
195 | 199 | ||
@@ -202,4 +206,4 @@ int main() | |||
202 | yylex_destroy ( scanner ); | 206 | yylex_destroy ( scanner ); |
203 | return 0; | 207 | return 0; |
204 | } | 208 | } |
205 | 209 | */ | |
diff --git a/src/variable.cpp b/src/variable.cpp index ef6760d..adf1511 100644 --- a/src/variable.cpp +++ b/src/variable.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | #include "variable.h" | 1 | #include "variable.h" |
2 | 2 | ||
3 | #include <bu/formatter.h> | ||
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
4 | 5 | ||
5 | typedef Bu::ExceptionBase VariableException; | 6 | typedef Bu::ExceptionBase VariableException; |
@@ -54,6 +55,13 @@ Variable::~Variable() | |||
54 | deinitType(); | 55 | deinitType(); |
55 | } | 56 | } |
56 | 57 | ||
58 | Variable Variable::newSituationName( const Bu::String &s ) | ||
59 | { | ||
60 | Variable v( tSituation ); | ||
61 | (*v.sValue) = s; | ||
62 | return v; | ||
63 | } | ||
64 | |||
57 | Variable Variable::to( Type e ) const | 65 | Variable Variable::to( Type e ) const |
58 | { | 66 | { |
59 | if( e == eType ) | 67 | if( e == eType ) |
@@ -424,3 +432,35 @@ template<> bool Bu::__cmpHashKeys<Variable>( const Variable &a, const Variable & | |||
424 | return a == b; | 432 | return a == b; |
425 | } | 433 | } |
426 | 434 | ||
435 | Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) | ||
436 | { | ||
437 | switch( v.eType ) | ||
438 | { | ||
439 | case Variable::tNull: | ||
440 | return f << "(null)"; | ||
441 | |||
442 | case Variable::tBool: | ||
443 | return f << v.bValue; | ||
444 | |||
445 | case Variable::tInt: | ||
446 | return f << v.iValue; | ||
447 | |||
448 | case Variable::tFloat: | ||
449 | return f << v.fValue; | ||
450 | |||
451 | case Variable::tString: | ||
452 | return f << '"' << *v.sValue << '"'; | ||
453 | |||
454 | case Variable::tSituation: | ||
455 | return f << "<<" << *v.sValue << ">>"; | ||
456 | |||
457 | case Variable::tList: | ||
458 | return f << *v.lValue; | ||
459 | |||
460 | case Variable::tDictionary: | ||
461 | return f << *v.hValue; | ||
462 | } | ||
463 | |||
464 | return f << "ERROR"; | ||
465 | } | ||
466 | |||
diff --git a/src/variable.h b/src/variable.h index 8bc12a6..9a35d48 100644 --- a/src/variable.h +++ b/src/variable.h | |||
@@ -9,6 +9,7 @@ | |||
9 | class Variable | 9 | class Variable |
10 | { | 10 | { |
11 | friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k ); | 11 | friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k ); |
12 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ); | ||
12 | public: | 13 | public: |
13 | enum Type | 14 | enum Type |
14 | { | 15 | { |
@@ -32,6 +33,8 @@ public: | |||
32 | Variable( const Bu::String &sValue ); | 33 | Variable( const Bu::String &sValue ); |
33 | virtual ~Variable(); | 34 | virtual ~Variable(); |
34 | 35 | ||
36 | static Variable newSituationName( const Bu::String &s ); | ||
37 | |||
35 | Type getType() const { return eType; } | 38 | Type getType() const { return eType; } |
36 | 39 | ||
37 | Variable to( Type e ) const; | 40 | Variable to( Type e ) const; |
@@ -79,6 +82,8 @@ namespace Bu | |||
79 | template<> bool __cmpHashKeys<Variable>( const Variable &a, const Variable &b ); | 82 | template<> bool __cmpHashKeys<Variable>( const Variable &a, const Variable &b ); |
80 | }; | 83 | }; |
81 | 84 | ||
85 | Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ); | ||
86 | |||
82 | typedef Bu::List<Variable> VariableList; | 87 | typedef Bu::List<Variable> VariableList; |
83 | typedef Bu::Hash<Bu::String, Variable> VariableHash; | 88 | typedef Bu::Hash<Bu::String, Variable> VariableHash; |
84 | 89 | ||