From f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Dec 2011 00:04:28 -0700 Subject: Branch building has started. --- src/astbranch.cpp | 28 ++++++++++++++++++++++++- src/astbranch.h | 6 ++++++ src/astleaf.cpp | 5 +++++ src/astleaf.h | 5 +++-- src/astleafliteral.cpp | 19 +++++++++++++++++ src/astleafliteral.h | 22 ++++++++++++++++++++ src/astnode.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/astnode.h | 46 +++++++++++++++++++++++------------------ src/gamebuilder.cpp | 29 +++++++++++++++++++++++--- src/gamebuilder.h | 5 ++++- src/main.cpp | 7 +++++++ 11 files changed, 200 insertions(+), 27 deletions(-) create mode 100644 src/astleafliteral.cpp create mode 100644 src/astleafliteral.h (limited to 'src') diff --git a/src/astbranch.cpp b/src/astbranch.cpp index 7b31426..a422a62 100644 --- a/src/astbranch.cpp +++ b/src/astbranch.cpp @@ -1,7 +1,10 @@ #include "astbranch.h" +#include + AstBranch::AstBranch( AstNode::Type eType ) : - AstNode( eType ) + AstNode( eType ), + pParent( NULL ) { } @@ -13,4 +16,27 @@ AstBranch::~AstBranch() } } +AstNode *AstBranch::addNode( AstNode *pNode ) +{ + AstBranch *pBranch = dynamic_cast(pNode); + if( pBranch ) + { + pBranch->pParent = this; + } + lNodes.append( pNode ); + return pNode; +} + +Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ) +{ + f << "(Branch " << b.getType() << ": "; + f.incIndent(); + for( typename AstBranch::NodeList::const_iterator i = b.lNodes.begin(); + i; i++ ) + { + f << f.nl << *(*i); + } + f.decIndent(); + return f << f.nl << ")"; +} diff --git a/src/astbranch.h b/src/astbranch.h index 72f8014..26aac80 100644 --- a/src/astbranch.h +++ b/src/astbranch.h @@ -7,14 +7,20 @@ class AstBranch : public AstNode { +friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ); public: AstBranch( Type eType ); virtual ~AstBranch(); + AstNode *addNode( AstNode *pNode ); + typedef Bu::List NodeList; private: + AstBranch *pParent; NodeList lNodes; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ); + #endif diff --git a/src/astleaf.cpp b/src/astleaf.cpp index 6e29d7e..59a4e34 100644 --- a/src/astleaf.cpp +++ b/src/astleaf.cpp @@ -9,3 +9,8 @@ AstLeaf::~AstLeaf() { } +Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ) +{ + return f << l.getType(); +} + diff --git a/src/astleaf.h b/src/astleaf.h index 7cc9dda..757a267 100644 --- a/src/astleaf.h +++ b/src/astleaf.h @@ -2,16 +2,17 @@ #define AST_LEAF_H #include "astnode.h" -#include "variable.h" class AstLeaf : public AstNode { +friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ); public: AstLeaf( Type eType ); virtual ~AstLeaf(); private: - Variable vValue; }; +Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ); + #endif diff --git a/src/astleafliteral.cpp b/src/astleafliteral.cpp new file mode 100644 index 0000000..a2a0808 --- /dev/null +++ b/src/astleafliteral.cpp @@ -0,0 +1,19 @@ +#include "astleafliteral.h" + +#include + +AstLeafLiteral::AstLeafLiteral( const Variable &v ) : + AstNode( AstNode::tLiteral ), + vValue( v ) +{ +} + +AstLeafLiteral::~AstLeafLiteral() +{ +} + +Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ) +{ + return f << l.getType() << "::" << l.vValue; +} + diff --git a/src/astleafliteral.h b/src/astleafliteral.h new file mode 100644 index 0000000..c8867e1 --- /dev/null +++ b/src/astleafliteral.h @@ -0,0 +1,22 @@ +#ifndef AST_LEAF_LITERAL_H +#define AST_LEAF_LITERAL_H + +#include "astnode.h" +#include "variable.h" + +class AstLeafLiteral : public AstNode +{ +friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); +public: + AstLeafLiteral( const Variable &v ); + virtual ~AstLeafLiteral(); + + const Variable &getValue() const { return vValue; } + +private: + Variable vValue; +}; + +Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); + +#endif diff --git a/src/astnode.cpp b/src/astnode.cpp index 4e0f30e..2c94ca9 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp @@ -1,5 +1,11 @@ #include "astnode.h" +#include + +#include "astleaf.h" +#include "astleafliteral.h" +#include "astbranch.h" + AstNode::AstNode( AstNode::Type eType ) : eType( eType ) { @@ -9,3 +15,52 @@ AstNode::~AstNode() { } +Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ) +{ + switch( t ) + { + case AstNode::tLeaf: return f << "!tLeaf!"; + case AstNode::tNot: return f << "tNot"; + case AstNode::tComp: return f << "tComp"; + case AstNode::tCompGt: return f << "tCompGt"; + case AstNode::tCompLt: return f << "tCompLt"; + case AstNode::tCompGtEq: return f << "tCompGtEq"; + case AstNode::tCompLtEq: return f << "tCompLtEq"; + case AstNode::tStore: return f << "tStore"; + case AstNode::tAnd: return f << "tAnd"; + case AstNode::tOr: return f << "tOr"; + + case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; + case AstNode::tVarName: return f << "tVarName"; + case AstNode::tLiteral: return f << "tLiteral"; + case AstNode::tFuncName: return f << "tFuncName"; + + case AstNode::tBranch: return f << "!tBranch!"; + case AstNode::tScope: return f << "tScope"; + case AstNode::tIf: return f << "tIf"; + case AstNode::tForEach: return f << "tForEach"; + case AstNode::tWhile: return f << "tWhile"; + + case AstNode::tTypeMask: return f << "!tTypeMask!"; + } + + return f << "???"; +} + +Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ) +{ + switch( n.eType & AstNode::tTypeMask ) + { + case AstNode::tLeaf: + return f << dynamic_cast(n); + + case AstNode::tLeafLiteral: + return f << dynamic_cast(n); + + case AstNode::tBranch: + return f << dynamic_cast(n); + } + + return f << "!!!ERROR!!!"; +} + diff --git a/src/astnode.h b/src/astnode.h index 9584788..1054a2b 100644 --- a/src/astnode.h +++ b/src/astnode.h @@ -5,30 +5,33 @@ class AstNode { +friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ); public: enum Type { tLeaf = 0x01000000, - tVarName = 0x01000001, - tLiteral = 0x01000002, - tFuncName = 0x01000003, - tNot = 0x01000004, - tComp = 0x01000005, - tCompGt = 0x01000006, - tCompLt = 0x01000007, - tCompGtEq = 0x01000008, - tCompLtEq = 0x01000009, - tStore = 0x0100000A, - tAnd = 0x0100000B, - tOr = 0x0100000C, - - tBranch = 0x02000000, - tScope = 0x02000001, - tIf = 0x02000002, - tForEach = 0x02000003, - tWhile = 0x02000004, - - tTypeMask = 0x03000000, + tNot = 0x01000001, + tComp = 0x01000002, + tCompGt = 0x01000003, + tCompLt = 0x01000004, + tCompGtEq = 0x01000005, + tCompLtEq = 0x01000006, + tStore = 0x01000007, + tAnd = 0x01000008, + tOr = 0x01000009, + + tLeafLiteral = 0x02000000, + tVarName = 0x02000001, + tLiteral = 0x02000002, + tFuncName = 0x02000003, + + tBranch = 0x04000000, + tScope = 0x04000001, + tIf = 0x04000002, + tForEach = 0x04000003, + tWhile = 0x04000004, + + tTypeMask = 0x07000000, }; AstNode( Type eType ); @@ -40,4 +43,7 @@ private: Type eType; }; +Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ); +Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ); + #endif diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 3696495..44fd6c1 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp @@ -1,9 +1,15 @@ #include "gamebuilder.h" #include +#include "astbranch.h" +#include "astleaf.h" +#include "astleafliteral.h" + using namespace Bu; -GameBuilder::GameBuilder() +GameBuilder::GameBuilder() : + pCurNode( NULL ), + pCurRoot( NULL ) { } @@ -24,12 +30,14 @@ void GameBuilder::setGameParam( const Bu::String &sName ) void GameBuilder::beginFunction( const Bu::String &sName ) { + pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); + sio << "New function: " << sName << sio.nl; } void GameBuilder::endFunction() { - sio << "Function ended." << sio.nl; + sio << "Function ended: " << *pCurRoot << sio.nl; } void GameBuilder::beginSituation( const Bu::String &sName ) @@ -47,14 +55,29 @@ void GameBuilder::addParam( const Bu::String &sName ) sio << " - Param added '" << sName << "'" << sio.nl; } -void GameBuilder::addNode( int iType ) +void GameBuilder::addNode( AstNode::Type iType ) { sio << " - Added type " << Fmt::hex() << iType << sio.nl; + + switch( iType&AstNode::tTypeMask ) + { + case AstNode::tBranch: + pCurNode = (AstBranch *)pCurNode->addNode( new AstBranch( iType ) ); + break; + + case AstNode::tLeaf: + pCurNode->addNode( new AstLeaf( iType ) ); + break; + } } void GameBuilder::addLiteral( const Variable &v ) { setLiteral( v ); + if( pCurNode ) + { + pCurNode->addNode( new AstLeafLiteral( v ) ); + } sio << " - Added literal " << v << sio.nl; } diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 26d6781..51ff27a 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h @@ -4,6 +4,7 @@ #include #include "variable.h" +#include "astnode.h" class GameBuilder { @@ -21,12 +22,14 @@ public: void endSituation(); void addParam( const Bu::String &sName ); - void addNode( int iType ); + void addNode( AstNode::Type iType ); void addLiteral( const Variable &v ); void addVarRef( const Bu::String &sName ); private: Variable vLiteral; + class AstBranch *pCurNode; + class AstBranch *pCurRoot; VariableHash hGameParams; }; diff --git a/src/main.cpp b/src/main.cpp index 1adb55f..609802c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ typedef void *yyscan_t; void yylex_init( yyscan_t * ); void yylex_destroy( yyscan_t ); void yyparse( yyscan_t, GameBuilder &bld ); +void yyset_in( FILE *, yyscan_t ); int main( int argc, char *argv[] ) { @@ -13,9 +14,15 @@ int main( int argc, char *argv[] ) GameBuilder bld; yylex_init( &scanner ); + + FILE *in = fopen( argv[1], "rb" ); + yyset_in( in, scanner ); + yyparse( scanner, bld ); yylex_destroy( scanner ); + fclose( in ); + return 0; } -- cgit v1.2.3