diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-24 01:08:21 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-24 01:08:21 -0700 |
commit | 32c32bc48c8fd7cffca301b8919fb26726f6467e (patch) | |
tree | fbe58058531f253e4e351312f87e8276ce087c31 /src | |
parent | d7c0a793787eaf52c4302d6d1ea6cc43f7ff9ca7 (diff) | |
download | stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.gz stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.bz2 stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.xz stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.zip |
Ast nearly done, builder coming along.
Diffstat (limited to 'src')
-rw-r--r-- | src/astbranch.cpp | 16 | ||||
-rw-r--r-- | src/astbranch.h | 20 | ||||
-rw-r--r-- | src/astleaf.cpp | 11 | ||||
-rw-r--r-- | src/astleaf.h | 17 | ||||
-rw-r--r-- | src/astnode.cpp | 10 | ||||
-rw-r--r-- | src/astnode.h | 35 | ||||
-rw-r--r-- | src/game.h | 3 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 21 | ||||
-rw-r--r-- | src/gamebuilder.h | 6 | ||||
-rw-r--r-- | src/parser.l | 3 | ||||
-rw-r--r-- | src/parser.y | 51 |
11 files changed, 159 insertions, 34 deletions
diff --git a/src/astbranch.cpp b/src/astbranch.cpp new file mode 100644 index 0000000..7b31426 --- /dev/null +++ b/src/astbranch.cpp | |||
@@ -0,0 +1,16 @@ | |||
1 | #include "astbranch.h" | ||
2 | |||
3 | AstBranch::AstBranch( AstNode::Type eType ) : | ||
4 | AstNode( eType ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | AstBranch::~AstBranch() | ||
9 | { | ||
10 | for( NodeList::iterator i = lNodes.begin(); i; i++ ) | ||
11 | { | ||
12 | delete *i; | ||
13 | } | ||
14 | } | ||
15 | |||
16 | |||
diff --git a/src/astbranch.h b/src/astbranch.h new file mode 100644 index 0000000..72f8014 --- /dev/null +++ b/src/astbranch.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef AST_BRANCH_H | ||
2 | #define AST_BRANCH_H | ||
3 | |||
4 | #include "astnode.h" | ||
5 | |||
6 | #include <bu/list.h> | ||
7 | |||
8 | class AstBranch : public AstNode | ||
9 | { | ||
10 | public: | ||
11 | AstBranch( Type eType ); | ||
12 | virtual ~AstBranch(); | ||
13 | |||
14 | typedef Bu::List<AstNode *> NodeList; | ||
15 | |||
16 | private: | ||
17 | NodeList lNodes; | ||
18 | }; | ||
19 | |||
20 | #endif | ||
diff --git a/src/astleaf.cpp b/src/astleaf.cpp new file mode 100644 index 0000000..6e29d7e --- /dev/null +++ b/src/astleaf.cpp | |||
@@ -0,0 +1,11 @@ | |||
1 | #include "astleaf.h" | ||
2 | |||
3 | AstLeaf::AstLeaf( AstNode::Type eType ) : | ||
4 | AstNode( eType ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | AstLeaf::~AstLeaf() | ||
9 | { | ||
10 | } | ||
11 | |||
diff --git a/src/astleaf.h b/src/astleaf.h new file mode 100644 index 0000000..7cc9dda --- /dev/null +++ b/src/astleaf.h | |||
@@ -0,0 +1,17 @@ | |||
1 | #ifndef AST_LEAF_H | ||
2 | #define AST_LEAF_H | ||
3 | |||
4 | #include "astnode.h" | ||
5 | #include "variable.h" | ||
6 | |||
7 | class AstLeaf : public AstNode | ||
8 | { | ||
9 | public: | ||
10 | AstLeaf( Type eType ); | ||
11 | virtual ~AstLeaf(); | ||
12 | |||
13 | private: | ||
14 | Variable vValue; | ||
15 | }; | ||
16 | |||
17 | #endif | ||
diff --git a/src/astnode.cpp b/src/astnode.cpp index ff73346..4e0f30e 100644 --- a/src/astnode.cpp +++ b/src/astnode.cpp | |||
@@ -1 +1,11 @@ | |||
1 | #include "astnode.h" | 1 | #include "astnode.h" |
2 | |||
3 | AstNode::AstNode( AstNode::Type eType ) : | ||
4 | eType( eType ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | AstNode::~AstNode() | ||
9 | { | ||
10 | } | ||
11 | |||
diff --git a/src/astnode.h b/src/astnode.h index 780e07e..9584788 100644 --- a/src/astnode.h +++ b/src/astnode.h | |||
@@ -6,31 +6,38 @@ | |||
6 | class AstNode | 6 | class AstNode |
7 | { | 7 | { |
8 | public: | 8 | public: |
9 | AstNode(); | ||
10 | virtual ~AstNode(); | ||
11 | |||
12 | enum Type | 9 | enum Type |
13 | { | 10 | { |
14 | tValue = 0x01000000, | 11 | tLeaf = 0x01000000, |
15 | tVarName = 0x01000001, | 12 | tVarName = 0x01000001, |
16 | tLiteral = 0x01000002, | 13 | tLiteral = 0x01000002, |
17 | tFuncName = 0x01000003, | 14 | tFuncName = 0x01000003, |
15 | tNot = 0x01000004, | ||
16 | tComp = 0x01000005, | ||
17 | tCompGt = 0x01000006, | ||
18 | tCompLt = 0x01000007, | ||
19 | tCompGtEq = 0x01000008, | ||
20 | tCompLtEq = 0x01000009, | ||
21 | tStore = 0x0100000A, | ||
22 | tAnd = 0x0100000B, | ||
23 | tOr = 0x0100000C, | ||
24 | |||
18 | tBranch = 0x02000000, | 25 | tBranch = 0x02000000, |
19 | tScope = 0x02000001, | 26 | tScope = 0x02000001, |
20 | tIf = 0x02000002, | 27 | tIf = 0x02000002, |
21 | tElse = 0x02000003, | 28 | tForEach = 0x02000003, |
22 | tNot = 0x00000004, | 29 | tWhile = 0x02000004, |
23 | tComp = 0x02000005, | 30 | |
24 | tCompGt = 0x02000006, | 31 | tTypeMask = 0x03000000, |
25 | tCompLt = 0x02000007, | ||
26 | tCompGtEq = 0x02000008, | ||
27 | tCompLtEq = 0x02000009, | ||
28 | tStore = 0x0200000A, | ||
29 | tAnd = 0x0200000B, | ||
30 | tOr = 0x0200000C, | ||
31 | }; | 32 | }; |
33 | |||
34 | AstNode( Type eType ); | ||
35 | virtual ~AstNode(); | ||
36 | |||
37 | Type getType() const { return eType; } | ||
32 | 38 | ||
33 | private: | 39 | private: |
40 | Type eType; | ||
34 | }; | 41 | }; |
35 | 42 | ||
36 | #endif | 43 | #endif |
@@ -19,9 +19,6 @@ private: | |||
19 | typedef Bu::Hash<Bu::String, Scope *> ScopeHash; | 19 | typedef Bu::Hash<Bu::String, Scope *> ScopeHash; |
20 | FunctionHash hFunction; | 20 | FunctionHash hFunction; |
21 | SituationHash hSituation; | 21 | SituationHash hSituation; |
22 | ScopeHash hSituationScope; | ||
23 | Scope sGlobal; | ||
24 | Scope sPlayer; | ||
25 | Bu::String sCurSituation; | 22 | Bu::String sCurSituation; |
26 | }; | 23 | }; |
27 | 24 | ||
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp index 912a595..3696495 100644 --- a/src/gamebuilder.cpp +++ b/src/gamebuilder.cpp | |||
@@ -42,3 +42,24 @@ void GameBuilder::endSituation() | |||
42 | sio << "Situation ended." << sio.nl; | 42 | sio << "Situation ended." << sio.nl; |
43 | } | 43 | } |
44 | 44 | ||
45 | void GameBuilder::addParam( const Bu::String &sName ) | ||
46 | { | ||
47 | sio << " - Param added '" << sName << "'" << sio.nl; | ||
48 | } | ||
49 | |||
50 | void GameBuilder::addNode( int iType ) | ||
51 | { | ||
52 | sio << " - Added type " << Fmt::hex() << iType << sio.nl; | ||
53 | } | ||
54 | |||
55 | void GameBuilder::addLiteral( const Variable &v ) | ||
56 | { | ||
57 | setLiteral( v ); | ||
58 | sio << " - Added literal " << v << sio.nl; | ||
59 | } | ||
60 | |||
61 | void GameBuilder::addVarRef( const Bu::String &sName ) | ||
62 | { | ||
63 | sio << " - Added varref '" << sName << "'" << sio.nl; | ||
64 | } | ||
65 | |||
diff --git a/src/gamebuilder.h b/src/gamebuilder.h index 59b4b57..26d6781 100644 --- a/src/gamebuilder.h +++ b/src/gamebuilder.h | |||
@@ -16,11 +16,15 @@ public: | |||
16 | 16 | ||
17 | void beginFunction( const Bu::String &sName ); | 17 | void beginFunction( const Bu::String &sName ); |
18 | void endFunction(); | 18 | void endFunction(); |
19 | |||
20 | 19 | ||
21 | void beginSituation( const Bu::String &sName ); | 20 | void beginSituation( const Bu::String &sName ); |
22 | void endSituation(); | 21 | void endSituation(); |
23 | 22 | ||
23 | void addParam( const Bu::String &sName ); | ||
24 | void addNode( int iType ); | ||
25 | void addLiteral( const Variable &v ); | ||
26 | void addVarRef( const Bu::String &sName ); | ||
27 | |||
24 | private: | 28 | private: |
25 | Variable vLiteral; | 29 | Variable vLiteral; |
26 | 30 | ||
diff --git a/src/parser.l b/src/parser.l index 9e031eb..3491d27 100644 --- a/src/parser.l +++ b/src/parser.l | |||
@@ -36,6 +36,7 @@ player { return tokPlayer; } | |||
36 | while { return tokWhile; } | 36 | while { return tokWhile; } |
37 | for { return tokFor; } | 37 | for { return tokFor; } |
38 | each { return tokEach; } | 38 | each { return tokEach; } |
39 | do { return tokDo; } | ||
39 | in { return tokIn; } | 40 | in { return tokIn; } |
40 | if { return tokIf; } | 41 | if { return tokIf; } |
41 | then { return tokThen; } | 42 | then { return tokThen; } |
@@ -45,6 +46,8 @@ goto { return tokGoto; } | |||
45 | not { return tokNot; } | 46 | not { return tokNot; } |
46 | setup { return tokSetup; } | 47 | setup { return tokSetup; } |
47 | enter { return tokEnter; } | 48 | enter { return tokEnter; } |
49 | and { return tokAnd; } | ||
50 | or { return tokOr; } | ||
48 | 51 | ||
49 | true { yylval->bValue = true; return tokBool; } | 52 | true { yylval->bValue = true; return tokBool; } |
50 | false { yylval->bValue = false; return tokBool; } | 53 | false { yylval->bValue = false; return tokBool; } |
diff --git a/src/parser.y b/src/parser.y index 0b1d41b..7993927 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <bu/string.h> | 4 | #include <bu/string.h> |
5 | 5 | ||
6 | #include "gamebuilder.h" | 6 | #include "gamebuilder.h" |
7 | #include "astnode.h" | ||
7 | 8 | ||
8 | typedef void *yyscan_t; | 9 | typedef void *yyscan_t; |
9 | 10 | ||
@@ -44,9 +45,12 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err | |||
44 | %token tokSituation | 45 | %token tokSituation |
45 | %token tokSetup | 46 | %token tokSetup |
46 | %token tokEnter | 47 | %token tokEnter |
48 | %token tokAnd | ||
49 | %token tokOr | ||
47 | %token tokWhile | 50 | %token tokWhile |
48 | %token tokFor | 51 | %token tokFor |
49 | %token tokEach | 52 | %token tokEach |
53 | %token tokDo | ||
50 | %token tokIn | 54 | %token tokIn |
51 | %token tokIf | 55 | %token tokIf |
52 | %token tokThen | 56 | %token tokThen |
@@ -77,10 +81,10 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err | |||
77 | %right tokNot | 81 | %right tokNot |
78 | %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign | 82 | %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign |
79 | %right tokLtEq tokGtEq tokCmp | 83 | %right tokLtEq tokGtEq tokCmp |
80 | %left tokIn | ||
81 | %left '(' ')' '[' ']' | ||
82 | %left '*' '/' | ||
83 | %left '-' '+' | 84 | %left '-' '+' |
85 | %left '*' '/' | ||
86 | %left tokIn tokAnd tokOr | ||
87 | %left '(' ')' '[' ']' | ||
84 | 88 | ||
85 | %% | 89 | %% |
86 | input: gameDecls globalDecl bodyDecl | 90 | input: gameDecls globalDecl bodyDecl |
@@ -125,9 +129,17 @@ situationMode: tokSetup | |||
125 | | tokEnter | 129 | | tokEnter |
126 | ; | 130 | ; |
127 | 131 | ||
128 | function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' ')' '{' '}' { bld.endFunction(); } | 132 | function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); } |
129 | ; | 133 | ; |
130 | 134 | ||
135 | funcParamList: | ||
136 | | tokIdent { bld.addParam( *($1) ); } funcParamListEx | ||
137 | ; | ||
138 | |||
139 | funcParamListEx: | ||
140 | | funcParamListEx ',' tokIdent { bld.addParam( *($3) ); } | ||
141 | ; | ||
142 | |||
131 | cmpltExprList: | 143 | cmpltExprList: |
132 | | cmpltExprList cmpltExpr | 144 | | cmpltExprList cmpltExpr |
133 | ; | 145 | ; |
@@ -135,39 +147,46 @@ cmpltExprList: | |||
135 | cmpltExpr: expr ';' | 147 | cmpltExpr: expr ';' |
136 | | tokGoto '(' expr ')' ';' | 148 | | tokGoto '(' expr ')' ';' |
137 | | tokIf expr tokThen '{' cmpltExprList '}' ifnext | 149 | | tokIf expr tokThen '{' cmpltExprList '}' ifnext |
150 | | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' | ||
151 | | tokWhile expr tokDo '{' cmpltExprList '}' | ||
138 | ; | 152 | ; |
139 | 153 | ||
154 | forIterator: tokIdent | ||
155 | | tokIdent ':' tokIdent | ||
156 | |||
140 | ifnext: | 157 | ifnext: |
141 | | tokElse '{' cmpltExprList '}' | 158 | | tokElse '{' cmpltExprList '}' |
142 | | tokElse tokIf '{' cmpltExprList '}' ifnext | 159 | | tokElse tokIf '{' cmpltExprList '}' ifnext |
143 | ; | 160 | ; |
144 | 161 | ||
145 | varRef: tokIdent | 162 | varRef: tokIdent { bld.addVarRef( *($1) ); } |
146 | | tokPlayer '.' tokIdent | 163 | | tokPlayer '.' tokIdent |
147 | | tokGlobal '.' tokIdent | 164 | | tokGlobal '.' tokIdent |
148 | | tokSituation '.' tokIdent | 165 | | tokSituation '.' tokIdent |
149 | ; | 166 | ; |
150 | 167 | ||
151 | literal: tokInt { bld.setLiteral( Variable( $1 ) ); } | 168 | literal: tokInt { bld.addLiteral( Variable( $1 ) ); } |
152 | | tokFloat { bld.setLiteral( Variable( $1 ) ); } | 169 | | tokFloat { bld.addLiteral( Variable( $1 ) ); } |
153 | | tokString { bld.setLiteral( Variable( *($1) ) ); } | 170 | | tokString { bld.addLiteral( Variable( *($1) ) ); } |
154 | | tokBool { bld.setLiteral( Variable( $1 ) ); } | 171 | | tokBool { bld.addLiteral( Variable( $1 ) ); } |
155 | | tokNull { bld.setLiteral( Variable( Variable::tNull ) ); } | 172 | | tokNull { bld.addLiteral( Variable( Variable::tNull ) ); } |
156 | | tokSituationName { bld.setLiteral( Variable::newSituationName( *($1) ) ); } | 173 | | tokSituationName { bld.addLiteral( Variable::newSituationName( *($1) ) ); } |
157 | ; | 174 | ; |
158 | 175 | ||
159 | expr: literal | 176 | expr: literal |
160 | | tokIdent '(' listValues ')' | 177 | | tokIdent '(' listValues ')' |
161 | | varRef | 178 | | varRef |
162 | | varRef '=' expr | 179 | | varRef '=' expr { bld.addNode( AstNode::tStore ); } |
163 | | varRef tokPlusAssign expr | 180 | | varRef tokPlusAssign expr |
164 | | varRef tokMinusAssign expr | 181 | | varRef tokMinusAssign expr |
165 | | varRef tokTimesAssign expr | 182 | | varRef tokTimesAssign expr |
166 | | varRef tokDivideAssign expr | 183 | | varRef tokDivideAssign expr |
167 | | expr '+' expr | 184 | | expr '+' expr { printf(" + plus\n"); } |
168 | | expr '-' expr | 185 | | expr '-' expr { printf(" + minus\n"); } |
169 | | expr '/' expr | 186 | | expr '/' expr { printf(" + divide\n"); } |
170 | | expr '*' expr | 187 | | expr '*' expr { printf(" + times\n"); } |
188 | | expr tokAnd expr | ||
189 | | expr tokOr expr | ||
171 | | expr tokIn expr | 190 | | expr tokIn expr |
172 | | '(' expr ')' | 191 | | '(' expr ')' |
173 | | expr '[' expr ']' | 192 | | expr '[' expr ']' |