From 7a53f0e95a5ffe13a21fc8d2076e34ee96693ac0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 23 Dec 2011 00:52:32 -0700 Subject: The game is actually being built now. --- src/environment.cpp | 10 ---------- src/environment.h | 28 ---------------------------- src/game.cpp | 10 ++++++++++ src/game.h | 28 ++++++++++++++++++++++++++++ src/gamebuilder.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/gamebuilder.h | 30 ++++++++++++++++++++++++++++++ src/main.cpp | 21 +++++++++++++++++++++ src/parser.y | 24 ++++++++++++++---------- src/variable.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/variable.h | 5 +++++ 10 files changed, 192 insertions(+), 48 deletions(-) delete mode 100644 src/environment.cpp delete mode 100644 src/environment.h create mode 100644 src/game.cpp create mode 100644 src/game.h create mode 100644 src/gamebuilder.cpp create mode 100644 src/gamebuilder.h create mode 100644 src/main.cpp 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 @@ -#include "environment.h" - -Environment::Environment() -{ -} - -Environment::~Environment() -{ -} - diff --git a/src/environment.h b/src/environment.h deleted file mode 100644 index e728c50..0000000 --- a/src/environment.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef ENVIRONMENT_H -#define ENVIRONMENT_H - -#include -#include -#include "function.h" -#include "situation.h" -#include "scope.h" - -class Environment -{ -public: - Environment(); - virtual ~Environment(); - -private: - typedef Bu::Hash FunctionHash; - typedef Bu::Hash SituationHash; - typedef Bu::Hash ScopeHash; - FunctionHash hFunction; - SituationHash hSituation; - ScopeHash hSituationScope; - Scope sGlobal; - Scope sPlayer; - Bu::String sCurSituation; -}; - -#endif 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 @@ +#include "game.h" + +Game::Game() +{ +} + +Game::~Game() +{ +} + diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..a5b1163 --- /dev/null +++ b/src/game.h @@ -0,0 +1,28 @@ +#ifndef GAME_H +#define GAME_H + +#include +#include +#include "function.h" +#include "situation.h" +#include "scope.h" + +class Game +{ +public: + Game(); + virtual ~Game(); + +private: + typedef Bu::Hash FunctionHash; + typedef Bu::Hash SituationHash; + typedef Bu::Hash ScopeHash; + FunctionHash hFunction; + SituationHash hSituation; + ScopeHash hSituationScope; + Scope sGlobal; + Scope sPlayer; + Bu::String sCurSituation; +}; + +#endif 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 @@ +#include "gamebuilder.h" +#include + +using namespace Bu; + +GameBuilder::GameBuilder() +{ +} + +GameBuilder::~GameBuilder() +{ +} + +void GameBuilder::setLiteral( const Variable &v ) +{ + vLiteral = v; +} + +void GameBuilder::setGameParam( const Bu::String &sName ) +{ + sio << "Set game param '" << sName << "' to " << vLiteral << sio.nl; + hGameParams.insert( sName, vLiteral ); +} + +void GameBuilder::beginFunction( const Bu::String &sName ) +{ + sio << "New function: " << sName << sio.nl; +} + +void GameBuilder::endFunction() +{ + sio << "Function ended." << sio.nl; +} + +void GameBuilder::beginSituation( const Bu::String &sName ) +{ + sio << "New situation: " << sName << sio.nl; +} + +void GameBuilder::endSituation() +{ + sio << "Situation ended." << sio.nl; +} + 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 @@ +#ifndef GAME_BUILDER_H +#define GAME_BUILDER_H + +#include + +#include "variable.h" + +class GameBuilder +{ +public: + GameBuilder(); + virtual ~GameBuilder(); + + void setLiteral( const Variable &v ); + void setGameParam( const Bu::String &sName ); + + void beginFunction( const Bu::String &sName ); + void endFunction(); + + + void beginSituation( const Bu::String &sName ); + void endSituation(); + +private: + Variable vLiteral; + + VariableHash hGameParams; +}; + +#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 @@ +#include "gamebuilder.h" +#include "parser.tab.h" + +typedef void *yyscan_t; +void yylex_init( yyscan_t * ); +void yylex_destroy( yyscan_t ); +void yyparse( yyscan_t, GameBuilder &bld ); + +int main( int argc, char *argv[] ) +{ + yyscan_t scanner; + + GameBuilder bld; + + yylex_init( &scanner ); + yyparse( scanner, bld ); + yylex_destroy( scanner ); + + return 0; +} + 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 @@ #include #include +#include "gamebuilder.h" + typedef void *yyscan_t; %} @@ -16,6 +18,8 @@ typedef void *yyscan_t; %parse-param { yyscan_t yyscanner } %lex-param { yysacn_t yyscanner } +%parse-param { GameBuilder &bld } + %union { int64_t iValue; double dValue; @@ -25,7 +29,7 @@ typedef void *yyscan_t; %{ int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); -void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error ) +void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *error ) { printf("%d:%d-%d:%d: %s\n", llocp->first_line, llocp->first_column, @@ -83,7 +87,7 @@ input: gameDecls globalDecl bodyDecl ; gameDecls: - | gameDecls tokGame '.' tokIdent '=' literal ';' + | gameDecls tokGame '.' tokIdent '=' literal ';' { bld.setGameParam( *($4) ); } ; globalDecl: tokGlobal '{' globalExprList '}' @@ -143,12 +147,12 @@ varRef: tokIdent | tokSituation '.' tokIdent ; -literal: tokInt - | tokFloat - | tokString - | tokBool - | tokNull - | tokSituationName +literal: tokInt { bld.setLiteral( Variable( $1 ) ); } + | tokFloat { bld.setLiteral( Variable( $1 ) ); } + | tokString { bld.setLiteral( Variable( *($1) ) ); } + | tokBool { bld.setLiteral( Variable( $1 ) ); } + | tokNull { bld.setLiteral( Variable( Variable::tNull ) ); } + | tokSituationName { bld.setLiteral( Variable::newSituationName( *($1) ) ); } ; expr: literal @@ -189,7 +193,7 @@ commandParamList: | commandParamList tokIdent ; %% - +/* void yylex_init( yyscan_t * ); void yylex_destroy( yyscan_t ); @@ -202,4 +206,4 @@ int main() yylex_destroy ( scanner ); return 0; } - +*/ 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 @@ #include "variable.h" +#include #include typedef Bu::ExceptionBase VariableException; @@ -54,6 +55,13 @@ Variable::~Variable() deinitType(); } +Variable Variable::newSituationName( const Bu::String &s ) +{ + Variable v( tSituation ); + (*v.sValue) = s; + return v; +} + Variable Variable::to( Type e ) const { if( e == eType ) @@ -424,3 +432,35 @@ template<> bool Bu::__cmpHashKeys( const Variable &a, const Variable & return a == b; } +Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ) +{ + switch( v.eType ) + { + case Variable::tNull: + return f << "(null)"; + + case Variable::tBool: + return f << v.bValue; + + case Variable::tInt: + return f << v.iValue; + + case Variable::tFloat: + return f << v.fValue; + + case Variable::tString: + return f << '"' << *v.sValue << '"'; + + case Variable::tSituation: + return f << "<<" << *v.sValue << ">>"; + + case Variable::tList: + return f << *v.lValue; + + case Variable::tDictionary: + return f << *v.hValue; + } + + return f << "ERROR"; +} + 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 @@ class Variable { friend uint32_t Bu::__calcHashCode( const Variable &k ); +friend Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ); public: enum Type { @@ -32,6 +33,8 @@ public: Variable( const Bu::String &sValue ); virtual ~Variable(); + static Variable newSituationName( const Bu::String &s ); + Type getType() const { return eType; } Variable to( Type e ) const; @@ -79,6 +82,8 @@ namespace Bu template<> bool __cmpHashKeys( const Variable &a, const Variable &b ); }; +Bu::Formatter &operator<<( Bu::Formatter &f, const Variable &v ); + typedef Bu::List VariableList; typedef Bu::Hash VariableHash; -- cgit v1.2.3