summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/environment.cpp10
-rw-r--r--src/game.cpp10
-rw-r--r--src/game.h (renamed from src/environment.h)10
-rw-r--r--src/gamebuilder.cpp44
-rw-r--r--src/gamebuilder.h30
-rw-r--r--src/main.cpp21
-rw-r--r--src/parser.y24
-rw-r--r--src/variable.cpp40
-rw-r--r--src/variable.h5
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
3Environment::Environment()
4{
5}
6
7Environment::~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
3Game::Game()
4{
5}
6
7Game::~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
10class Environment 10class Game
11{ 11{
12public: 12public:
13 Environment(); 13 Game();
14 virtual ~Environment(); 14 virtual ~Game();
15 15
16private: 16private:
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
4using namespace Bu;
5
6GameBuilder::GameBuilder()
7{
8}
9
10GameBuilder::~GameBuilder()
11{
12}
13
14void GameBuilder::setLiteral( const Variable &v )
15{
16 vLiteral = v;
17}
18
19void GameBuilder::setGameParam( const Bu::String &sName )
20{
21 sio << "Set game param '" << sName << "' to " << vLiteral << sio.nl;
22 hGameParams.insert( sName, vLiteral );
23}
24
25void GameBuilder::beginFunction( const Bu::String &sName )
26{
27 sio << "New function: " << sName << sio.nl;
28}
29
30void GameBuilder::endFunction()
31{
32 sio << "Function ended." << sio.nl;
33}
34
35void GameBuilder::beginSituation( const Bu::String &sName )
36{
37 sio << "New situation: " << sName << sio.nl;
38}
39
40void 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
8class GameBuilder
9{
10public:
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
24private:
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
4typedef void *yyscan_t;
5void yylex_init( yyscan_t * );
6void yylex_destroy( yyscan_t );
7void yyparse( yyscan_t, GameBuilder &bld );
8
9int 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
6typedef void *yyscan_t; 8typedef 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%{
27int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); 31int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner );
28void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error ) 32void 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
85gameDecls: 89gameDecls:
86 | gameDecls tokGame '.' tokIdent '=' literal ';' 90 | gameDecls tokGame '.' tokIdent '=' literal ';' { bld.setGameParam( *($4) ); }
87 ; 91 ;
88 92
89globalDecl: tokGlobal '{' globalExprList '}' 93globalDecl: tokGlobal '{' globalExprList '}'
@@ -143,12 +147,12 @@ varRef: tokIdent
143 | tokSituation '.' tokIdent 147 | tokSituation '.' tokIdent
144 ; 148 ;
145 149
146literal: tokInt 150literal: 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
154expr: literal 158expr: literal
@@ -189,7 +193,7 @@ commandParamList:
189 | commandParamList tokIdent 193 | commandParamList tokIdent
190 ; 194 ;
191%% 195%%
192 196/*
193void yylex_init( yyscan_t * ); 197void yylex_init( yyscan_t * );
194void yylex_destroy( yyscan_t ); 198void 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
5typedef Bu::ExceptionBase VariableException; 6typedef Bu::ExceptionBase VariableException;
@@ -54,6 +55,13 @@ Variable::~Variable()
54 deinitType(); 55 deinitType();
55} 56}
56 57
58Variable Variable::newSituationName( const Bu::String &s )
59{
60 Variable v( tSituation );
61 (*v.sValue) = s;
62 return v;
63}
64
57Variable Variable::to( Type e ) const 65Variable 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
435Bu::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 @@
9class Variable 9class Variable
10{ 10{
11friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k ); 11friend uint32_t Bu::__calcHashCode<Variable>( const Variable &k );
12friend Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v );
12public: 13public:
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
85Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v );
86
82typedef Bu::List<Variable> VariableList; 87typedef Bu::List<Variable> VariableList;
83typedef Bu::Hash<Bu::String, Variable> VariableHash; 88typedef Bu::Hash<Bu::String, Variable> VariableHash;
84 89