diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-27 00:04:28 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-27 00:04:28 -0700 |
commit | f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816 (patch) | |
tree | 3e62207265b4efbbe1565ea885a615e5a910671c /src | |
parent | 32c32bc48c8fd7cffca301b8919fb26726f6467e (diff) | |
download | stage-f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816.tar.gz stage-f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816.tar.bz2 stage-f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816.tar.xz stage-f33fdd93ef93fdbb0e6b3a8e2ecb80b78f1b2816.zip |
Branch building has started.
Diffstat (limited to 'src')
-rw-r--r-- | src/astbranch.cpp | 28 | ||||
-rw-r--r-- | src/astbranch.h | 6 | ||||
-rw-r--r-- | src/astleaf.cpp | 5 | ||||
-rw-r--r-- | src/astleaf.h | 5 | ||||
-rw-r--r-- | src/astleafliteral.cpp | 19 | ||||
-rw-r--r-- | src/astleafliteral.h | 22 | ||||
-rw-r--r-- | src/astnode.cpp | 55 | ||||
-rw-r--r-- | src/astnode.h | 46 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 29 | ||||
-rw-r--r-- | src/gamebuilder.h | 5 | ||||
-rw-r--r-- | src/main.cpp | 7 |
11 files changed, 200 insertions, 27 deletions
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 @@ | |||
1 | #include "astbranch.h" | 1 | #include "astbranch.h" |
2 | 2 | ||
3 | #include <bu/formatter.h> | ||
4 | |||
3 | AstBranch::AstBranch( AstNode::Type eType ) : | 5 | AstBranch::AstBranch( AstNode::Type eType ) : |
4 | AstNode( eType ) | 6 | AstNode( eType ), |
7 | pParent( NULL ) | ||
5 | { | 8 | { |
6 | } | 9 | } |
7 | 10 | ||
@@ -13,4 +16,27 @@ AstBranch::~AstBranch() | |||
13 | } | 16 | } |
14 | } | 17 | } |
15 | 18 | ||
19 | AstNode *AstBranch::addNode( AstNode *pNode ) | ||
20 | { | ||
21 | AstBranch *pBranch = dynamic_cast<AstBranch *>(pNode); | ||
22 | if( pBranch ) | ||
23 | { | ||
24 | pBranch->pParent = this; | ||
25 | } | ||
26 | lNodes.append( pNode ); | ||
27 | return pNode; | ||
28 | } | ||
29 | |||
30 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ) | ||
31 | { | ||
32 | f << "(Branch " << b.getType() << ": "; | ||
33 | f.incIndent(); | ||
34 | for( typename AstBranch::NodeList::const_iterator i = b.lNodes.begin(); | ||
35 | i; i++ ) | ||
36 | { | ||
37 | f << f.nl << *(*i); | ||
38 | } | ||
39 | f.decIndent(); | ||
40 | return f << f.nl << ")"; | ||
41 | } | ||
16 | 42 | ||
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 @@ | |||
7 | 7 | ||
8 | class AstBranch : public AstNode | 8 | class AstBranch : public AstNode |
9 | { | 9 | { |
10 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ); | ||
10 | public: | 11 | public: |
11 | AstBranch( Type eType ); | 12 | AstBranch( Type eType ); |
12 | virtual ~AstBranch(); | 13 | virtual ~AstBranch(); |
13 | 14 | ||
15 | AstNode *addNode( AstNode *pNode ); | ||
16 | |||
14 | typedef Bu::List<AstNode *> NodeList; | 17 | typedef Bu::List<AstNode *> NodeList; |
15 | 18 | ||
16 | private: | 19 | private: |
20 | AstBranch *pParent; | ||
17 | NodeList lNodes; | 21 | NodeList lNodes; |
18 | }; | 22 | }; |
19 | 23 | ||
24 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstBranch &b ); | ||
25 | |||
20 | #endif | 26 | #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() | |||
9 | { | 9 | { |
10 | } | 10 | } |
11 | 11 | ||
12 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ) | ||
13 | { | ||
14 | return f << l.getType(); | ||
15 | } | ||
16 | |||
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 @@ | |||
2 | #define AST_LEAF_H | 2 | #define AST_LEAF_H |
3 | 3 | ||
4 | #include "astnode.h" | 4 | #include "astnode.h" |
5 | #include "variable.h" | ||
6 | 5 | ||
7 | class AstLeaf : public AstNode | 6 | class AstLeaf : public AstNode |
8 | { | 7 | { |
8 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ); | ||
9 | public: | 9 | public: |
10 | AstLeaf( Type eType ); | 10 | AstLeaf( Type eType ); |
11 | virtual ~AstLeaf(); | 11 | virtual ~AstLeaf(); |
12 | 12 | ||
13 | private: | 13 | private: |
14 | Variable vValue; | ||
15 | }; | 14 | }; |
16 | 15 | ||
16 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeaf &l ); | ||
17 | |||
17 | #endif | 18 | #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 @@ | |||
1 | #include "astleafliteral.h" | ||
2 | |||
3 | #include <bu/formatter.h> | ||
4 | |||
5 | AstLeafLiteral::AstLeafLiteral( const Variable &v ) : | ||
6 | AstNode( AstNode::tLiteral ), | ||
7 | vValue( v ) | ||
8 | { | ||
9 | } | ||
10 | |||
11 | AstLeafLiteral::~AstLeafLiteral() | ||
12 | { | ||
13 | } | ||
14 | |||
15 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ) | ||
16 | { | ||
17 | return f << l.getType() << "::" << l.vValue; | ||
18 | } | ||
19 | |||
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 @@ | |||
1 | #ifndef AST_LEAF_LITERAL_H | ||
2 | #define AST_LEAF_LITERAL_H | ||
3 | |||
4 | #include "astnode.h" | ||
5 | #include "variable.h" | ||
6 | |||
7 | class AstLeafLiteral : public AstNode | ||
8 | { | ||
9 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); | ||
10 | public: | ||
11 | AstLeafLiteral( const Variable &v ); | ||
12 | virtual ~AstLeafLiteral(); | ||
13 | |||
14 | const Variable &getValue() const { return vValue; } | ||
15 | |||
16 | private: | ||
17 | Variable vValue; | ||
18 | }; | ||
19 | |||
20 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); | ||
21 | |||
22 | #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 @@ | |||
1 | #include "astnode.h" | 1 | #include "astnode.h" |
2 | 2 | ||
3 | #include <bu/formatter.h> | ||
4 | |||
5 | #include "astleaf.h" | ||
6 | #include "astleafliteral.h" | ||
7 | #include "astbranch.h" | ||
8 | |||
3 | AstNode::AstNode( AstNode::Type eType ) : | 9 | AstNode::AstNode( AstNode::Type eType ) : |
4 | eType( eType ) | 10 | eType( eType ) |
5 | { | 11 | { |
@@ -9,3 +15,52 @@ AstNode::~AstNode() | |||
9 | { | 15 | { |
10 | } | 16 | } |
11 | 17 | ||
18 | Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ) | ||
19 | { | ||
20 | switch( t ) | ||
21 | { | ||
22 | case AstNode::tLeaf: return f << "!tLeaf!"; | ||
23 | case AstNode::tNot: return f << "tNot"; | ||
24 | case AstNode::tComp: return f << "tComp"; | ||
25 | case AstNode::tCompGt: return f << "tCompGt"; | ||
26 | case AstNode::tCompLt: return f << "tCompLt"; | ||
27 | case AstNode::tCompGtEq: return f << "tCompGtEq"; | ||
28 | case AstNode::tCompLtEq: return f << "tCompLtEq"; | ||
29 | case AstNode::tStore: return f << "tStore"; | ||
30 | case AstNode::tAnd: return f << "tAnd"; | ||
31 | case AstNode::tOr: return f << "tOr"; | ||
32 | |||
33 | case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; | ||
34 | case AstNode::tVarName: return f << "tVarName"; | ||
35 | case AstNode::tLiteral: return f << "tLiteral"; | ||
36 | case AstNode::tFuncName: return f << "tFuncName"; | ||
37 | |||
38 | case AstNode::tBranch: return f << "!tBranch!"; | ||
39 | case AstNode::tScope: return f << "tScope"; | ||
40 | case AstNode::tIf: return f << "tIf"; | ||
41 | case AstNode::tForEach: return f << "tForEach"; | ||
42 | case AstNode::tWhile: return f << "tWhile"; | ||
43 | |||
44 | case AstNode::tTypeMask: return f << "!tTypeMask!"; | ||
45 | } | ||
46 | |||
47 | return f << "???"; | ||
48 | } | ||
49 | |||
50 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ) | ||
51 | { | ||
52 | switch( n.eType & AstNode::tTypeMask ) | ||
53 | { | ||
54 | case AstNode::tLeaf: | ||
55 | return f << dynamic_cast<const AstLeaf &>(n); | ||
56 | |||
57 | case AstNode::tLeafLiteral: | ||
58 | return f << dynamic_cast<const AstLeafLiteral &>(n); | ||
59 | |||
60 | case AstNode::tBranch: | ||
61 | return f << dynamic_cast<const AstBranch &>(n); | ||
62 | } | ||
63 | |||
64 | return f << "!!!ERROR!!!"; | ||
65 | } | ||
66 | |||
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 @@ | |||
5 | 5 | ||
6 | class AstNode | 6 | class AstNode |
7 | { | 7 | { |
8 | friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ); | ||
8 | public: | 9 | public: |
9 | enum Type | 10 | enum Type |
10 | { | 11 | { |
11 | tLeaf = 0x01000000, | 12 | tLeaf = 0x01000000, |
12 | tVarName = 0x01000001, | 13 | tNot = 0x01000001, |
13 | tLiteral = 0x01000002, | 14 | tComp = 0x01000002, |
14 | tFuncName = 0x01000003, | 15 | tCompGt = 0x01000003, |
15 | tNot = 0x01000004, | 16 | tCompLt = 0x01000004, |
16 | tComp = 0x01000005, | 17 | tCompGtEq = 0x01000005, |
17 | tCompGt = 0x01000006, | 18 | tCompLtEq = 0x01000006, |
18 | tCompLt = 0x01000007, | 19 | tStore = 0x01000007, |
19 | tCompGtEq = 0x01000008, | 20 | tAnd = 0x01000008, |
20 | tCompLtEq = 0x01000009, | 21 | tOr = 0x01000009, |
21 | tStore = 0x0100000A, | 22 | |
22 | tAnd = 0x0100000B, | 23 | tLeafLiteral = 0x02000000, |
23 | tOr = 0x0100000C, | 24 | tVarName = 0x02000001, |
24 | 25 | tLiteral = 0x02000002, | |
25 | tBranch = 0x02000000, | 26 | tFuncName = 0x02000003, |
26 | tScope = 0x02000001, | 27 | |
27 | tIf = 0x02000002, | 28 | tBranch = 0x04000000, |
28 | tForEach = 0x02000003, | 29 | tScope = 0x04000001, |
29 | tWhile = 0x02000004, | 30 | tIf = 0x04000002, |
30 | 31 | tForEach = 0x04000003, | |
31 | tTypeMask = 0x03000000, | 32 | tWhile = 0x04000004, |
33 | |||
34 | tTypeMask = 0x07000000, | ||
32 | }; | 35 | }; |
33 | 36 | ||
34 | AstNode( Type eType ); | 37 | AstNode( Type eType ); |
@@ -40,4 +43,7 @@ private: | |||
40 | Type eType; | 43 | Type eType; |
41 | }; | 44 | }; |
42 | 45 | ||
46 | Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t ); | ||
47 | Bu::Formatter &operator<<( Bu::Formatter &f, const AstNode &n ); | ||
48 | |||
43 | #endif | 49 | #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 @@ | |||
1 | #include "gamebuilder.h" | 1 | #include "gamebuilder.h" |
2 | #include <bu/sio.h> | 2 | #include <bu/sio.h> |
3 | 3 | ||
4 | #include "astbranch.h" | ||
5 | #include "astleaf.h" | ||
6 | #include "astleafliteral.h" | ||
7 | |||
4 | using namespace Bu; | 8 | using namespace Bu; |
5 | 9 | ||
6 | GameBuilder::GameBuilder() | 10 | GameBuilder::GameBuilder() : |
11 | pCurNode( NULL ), | ||
12 | pCurRoot( NULL ) | ||
7 | { | 13 | { |
8 | } | 14 | } |
9 | 15 | ||
@@ -24,12 +30,14 @@ void GameBuilder::setGameParam( const Bu::String &sName ) | |||
24 | 30 | ||
25 | void GameBuilder::beginFunction( const Bu::String &sName ) | 31 | void GameBuilder::beginFunction( const Bu::String &sName ) |
26 | { | 32 | { |
33 | pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); | ||
34 | |||
27 | sio << "New function: " << sName << sio.nl; | 35 | sio << "New function: " << sName << sio.nl; |
28 | } | 36 | } |
29 | 37 | ||
30 | void GameBuilder::endFunction() | 38 | void GameBuilder::endFunction() |
31 | { | 39 | { |
32 | sio << "Function ended." << sio.nl; | 40 | sio << "Function ended: " << *pCurRoot << sio.nl; |
33 | } | 41 | } |
34 | 42 | ||
35 | void GameBuilder::beginSituation( const Bu::String &sName ) | 43 | void GameBuilder::beginSituation( const Bu::String &sName ) |
@@ -47,14 +55,29 @@ void GameBuilder::addParam( const Bu::String &sName ) | |||
47 | sio << " - Param added '" << sName << "'" << sio.nl; | 55 | sio << " - Param added '" << sName << "'" << sio.nl; |
48 | } | 56 | } |
49 | 57 | ||
50 | void GameBuilder::addNode( int iType ) | 58 | void GameBuilder::addNode( AstNode::Type iType ) |
51 | { | 59 | { |
52 | sio << " - Added type " << Fmt::hex() << iType << sio.nl; | 60 | sio << " - Added type " << Fmt::hex() << iType << sio.nl; |
61 | |||
62 | switch( iType&AstNode::tTypeMask ) | ||
63 | { | ||
64 | case AstNode::tBranch: | ||
65 | pCurNode = (AstBranch *)pCurNode->addNode( new AstBranch( iType ) ); | ||
66 | break; | ||
67 | |||
68 | case AstNode::tLeaf: | ||
69 | pCurNode->addNode( new AstLeaf( iType ) ); | ||
70 | break; | ||
71 | } | ||
53 | } | 72 | } |
54 | 73 | ||
55 | void GameBuilder::addLiteral( const Variable &v ) | 74 | void GameBuilder::addLiteral( const Variable &v ) |
56 | { | 75 | { |
57 | setLiteral( v ); | 76 | setLiteral( v ); |
77 | if( pCurNode ) | ||
78 | { | ||
79 | pCurNode->addNode( new AstLeafLiteral( v ) ); | ||
80 | } | ||
58 | sio << " - Added literal " << v << sio.nl; | 81 | sio << " - Added literal " << v << sio.nl; |
59 | } | 82 | } |
60 | 83 | ||
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 @@ | |||
4 | #include <bu/string.h> | 4 | #include <bu/string.h> |
5 | 5 | ||
6 | #include "variable.h" | 6 | #include "variable.h" |
7 | #include "astnode.h" | ||
7 | 8 | ||
8 | class GameBuilder | 9 | class GameBuilder |
9 | { | 10 | { |
@@ -21,12 +22,14 @@ public: | |||
21 | void endSituation(); | 22 | void endSituation(); |
22 | 23 | ||
23 | void addParam( const Bu::String &sName ); | 24 | void addParam( const Bu::String &sName ); |
24 | void addNode( int iType ); | 25 | void addNode( AstNode::Type iType ); |
25 | void addLiteral( const Variable &v ); | 26 | void addLiteral( const Variable &v ); |
26 | void addVarRef( const Bu::String &sName ); | 27 | void addVarRef( const Bu::String &sName ); |
27 | 28 | ||
28 | private: | 29 | private: |
29 | Variable vLiteral; | 30 | Variable vLiteral; |
31 | class AstBranch *pCurNode; | ||
32 | class AstBranch *pCurRoot; | ||
30 | 33 | ||
31 | VariableHash hGameParams; | 34 | VariableHash hGameParams; |
32 | }; | 35 | }; |
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; | |||
5 | void yylex_init( yyscan_t * ); | 5 | void yylex_init( yyscan_t * ); |
6 | void yylex_destroy( yyscan_t ); | 6 | void yylex_destroy( yyscan_t ); |
7 | void yyparse( yyscan_t, GameBuilder &bld ); | 7 | void yyparse( yyscan_t, GameBuilder &bld ); |
8 | void yyset_in( FILE *, yyscan_t ); | ||
8 | 9 | ||
9 | int main( int argc, char *argv[] ) | 10 | int main( int argc, char *argv[] ) |
10 | { | 11 | { |
@@ -13,9 +14,15 @@ int main( int argc, char *argv[] ) | |||
13 | GameBuilder bld; | 14 | GameBuilder bld; |
14 | 15 | ||
15 | yylex_init( &scanner ); | 16 | yylex_init( &scanner ); |
17 | |||
18 | FILE *in = fopen( argv[1], "rb" ); | ||
19 | yyset_in( in, scanner ); | ||
20 | |||
16 | yyparse( scanner, bld ); | 21 | yyparse( scanner, bld ); |
17 | yylex_destroy( scanner ); | 22 | yylex_destroy( scanner ); |
18 | 23 | ||
24 | fclose( in ); | ||
25 | |||
19 | return 0; | 26 | return 0; |
20 | } | 27 | } |
21 | 28 | ||