summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/astbranch.h1
-rw-r--r--src/astleafliteral.cpp6
-rw-r--r--src/astleafliteral.h1
-rw-r--r--src/astnode.cpp4
-rw-r--r--src/astnode.h4
-rw-r--r--src/gamebuilder.cpp13
-rw-r--r--src/gamebuilder.h1
-rw-r--r--src/parser.y48
-rw-r--r--test.stage14
9 files changed, 77 insertions, 15 deletions
diff --git a/src/astbranch.h b/src/astbranch.h
index 26aac80..8e63af5 100644
--- a/src/astbranch.h
+++ b/src/astbranch.h
@@ -13,6 +13,7 @@ public:
13 virtual ~AstBranch(); 13 virtual ~AstBranch();
14 14
15 AstNode *addNode( AstNode *pNode ); 15 AstNode *addNode( AstNode *pNode );
16 AstBranch *getParent() const { return pParent; }
16 17
17 typedef Bu::List<AstNode *> NodeList; 18 typedef Bu::List<AstNode *> NodeList;
18 19
diff --git a/src/astleafliteral.cpp b/src/astleafliteral.cpp
index a2a0808..116aeb6 100644
--- a/src/astleafliteral.cpp
+++ b/src/astleafliteral.cpp
@@ -8,6 +8,12 @@ AstLeafLiteral::AstLeafLiteral( const Variable &v ) :
8{ 8{
9} 9}
10 10
11AstLeafLiteral::AstLeafLiteral( AstNode eType, const Variable &v ) :
12 AstNode( eType ),
13 vValue( v )
14{
15}
16
11AstLeafLiteral::~AstLeafLiteral() 17AstLeafLiteral::~AstLeafLiteral()
12{ 18{
13} 19}
diff --git a/src/astleafliteral.h b/src/astleafliteral.h
index c8867e1..e2da3f6 100644
--- a/src/astleafliteral.h
+++ b/src/astleafliteral.h
@@ -9,6 +9,7 @@ class AstLeafLiteral : public AstNode
9friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l ); 9friend Bu::Formatter &operator<<( Bu::Formatter &f, const AstLeafLiteral &l );
10public: 10public:
11 AstLeafLiteral( const Variable &v ); 11 AstLeafLiteral( const Variable &v );
12 AstLeafLiteral( AstNode eType, const Variable &v );
12 virtual ~AstLeafLiteral(); 13 virtual ~AstLeafLiteral();
13 14
14 const Variable &getValue() const { return vValue; } 15 const Variable &getValue() const { return vValue; }
diff --git a/src/astnode.cpp b/src/astnode.cpp
index 2c94ca9..18ce122 100644
--- a/src/astnode.cpp
+++ b/src/astnode.cpp
@@ -29,6 +29,10 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t )
29 case AstNode::tStore: return f << "tStore"; 29 case AstNode::tStore: return f << "tStore";
30 case AstNode::tAnd: return f << "tAnd"; 30 case AstNode::tAnd: return f << "tAnd";
31 case AstNode::tOr: return f << "tOr"; 31 case AstNode::tOr: return f << "tOr";
32 case AstNode::tPlus: return f << "tPlus";
33 case AstNode::tMinus: return f << "tMinus";
34 case AstNode::tDivide: return f << "tDivide";
35 case AstNode::tMultiply: return f << "tMultiply";
32 36
33 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; 37 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!";
34 case AstNode::tVarName: return f << "tVarName"; 38 case AstNode::tVarName: return f << "tVarName";
diff --git a/src/astnode.h b/src/astnode.h
index 1054a2b..abbcaaf 100644
--- a/src/astnode.h
+++ b/src/astnode.h
@@ -19,6 +19,10 @@ public:
19 tStore = 0x01000007, 19 tStore = 0x01000007,
20 tAnd = 0x01000008, 20 tAnd = 0x01000008,
21 tOr = 0x01000009, 21 tOr = 0x01000009,
22 tPlus = 0x0100000A,
23 tMinus = 0x0100000B,
24 tDivide = 0x0100000C,
25 tMultiply = 0x0100000D,
22 26
23 tLeafLiteral = 0x02000000, 27 tLeafLiteral = 0x02000000,
24 tVarName = 0x02000001, 28 tVarName = 0x02000001,
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp
index 44fd6c1..47de17d 100644
--- a/src/gamebuilder.cpp
+++ b/src/gamebuilder.cpp
@@ -57,8 +57,6 @@ void GameBuilder::addParam( const Bu::String &sName )
57 57
58void GameBuilder::addNode( AstNode::Type iType ) 58void GameBuilder::addNode( AstNode::Type iType )
59{ 59{
60 sio << " - Added type " << Fmt::hex() << iType << sio.nl;
61
62 switch( iType&AstNode::tTypeMask ) 60 switch( iType&AstNode::tTypeMask )
63 { 61 {
64 case AstNode::tBranch: 62 case AstNode::tBranch:
@@ -71,6 +69,11 @@ void GameBuilder::addNode( AstNode::Type iType )
71 } 69 }
72} 70}
73 71
72void GameBuilder::closeNode()
73{
74 pCurNode = pCurNode->getParent();
75}
76
74void GameBuilder::addLiteral( const Variable &v ) 77void GameBuilder::addLiteral( const Variable &v )
75{ 78{
76 setLiteral( v ); 79 setLiteral( v );
@@ -78,11 +81,13 @@ void GameBuilder::addLiteral( const Variable &v )
78 { 81 {
79 pCurNode->addNode( new AstLeafLiteral( v ) ); 82 pCurNode->addNode( new AstLeafLiteral( v ) );
80 } 83 }
81 sio << " - Added literal " << v << sio.nl;
82} 84}
83 85
84void GameBuilder::addVarRef( const Bu::String &sName ) 86void GameBuilder::addVarRef( const Bu::String &sName )
85{ 87{
86 sio << " - Added varref '" << sName << "'" << sio.nl; 88 if( pCurNode )
89 {
90 pCurNode->addNode( new AstLeafLiteral( AstNode::tVarName, sName ) );
91 }
87} 92}
88 93
diff --git a/src/gamebuilder.h b/src/gamebuilder.h
index 51ff27a..2637c37 100644
--- a/src/gamebuilder.h
+++ b/src/gamebuilder.h
@@ -23,6 +23,7 @@ public:
23 23
24 void addParam( const Bu::String &sName ); 24 void addParam( const Bu::String &sName );
25 void addNode( AstNode::Type iType ); 25 void addNode( AstNode::Type iType );
26 void closeNode();
26 void addLiteral( const Variable &v ); 27 void addLiteral( const Variable &v );
27 void addVarRef( const Bu::String &sName ); 28 void addVarRef( const Bu::String &sName );
28 29
diff --git a/src/parser.y b/src/parser.y
index 7993927..fbada5a 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -80,10 +80,10 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err
80 80
81%right tokNot 81%right tokNot
82%right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign 82%right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign
83%right tokLtEq tokGtEq tokCmp
84%left '-' '+' 83%left '-' '+'
85%left '*' '/' 84%left '*' '/'
86%left tokIn tokAnd tokOr 85%left tokIn tokAnd tokOr
86%right '<' '>' tokLtEq tokGtEq tokCmp
87%left '(' ')' '[' ']' 87%left '(' ')' '[' ']'
88 88
89%% 89%%
@@ -146,17 +146,40 @@ cmpltExprList:
146 146
147cmpltExpr: expr ';' 147cmpltExpr: expr ';'
148 | tokGoto '(' expr ')' ';' 148 | tokGoto '(' expr ')' ';'
149 | tokIf expr tokThen '{' cmpltExprList '}' ifnext 149 | ifbase
150 | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}' 150 | tokFor tokEach forIterator tokIn expr tokDo '{' cmpltExprList '}'
151 | tokWhile expr tokDo '{' cmpltExprList '}' 151 | tokWhile {
152 bld.addNode( AstNode::tWhile );
153 bld.addNode( AstNode::tScope );
154 } expr {
155 bld.closeNode();
156 bld.addNode( AstNode::tScope );
157 } tokDo '{' cmpltExprList '}' {
158 bld.closeNode(); bld.closeNode();
159 }
152 ; 160 ;
153 161
154forIterator: tokIdent 162forIterator: tokIdent
155 | tokIdent ':' tokIdent 163 | tokIdent ':' tokIdent
156 164
165ifbase: tokIf {
166 bld.addNode( AstNode::tIf );
167 bld.addNode( AstNode::tScope );
168 } expr {
169 bld.closeNode();
170 bld.addNode( AstNode::tScope );
171 } tokThen '{' cmpltExprList '}' {
172 bld.closeNode();
173 } ifnext {
174 bld.closeNode();
175 }
176 ;
177
157ifnext: 178ifnext:
158 | tokElse '{' cmpltExprList '}' 179 | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' {
159 | tokElse tokIf '{' cmpltExprList '}' ifnext 180 bld.closeNode();
181 }
182 | tokElse { bld.addNode( AstNode::tScope ); } ifbase
160 ; 183 ;
161 184
162varRef: tokIdent { bld.addVarRef( *($1) ); } 185varRef: tokIdent { bld.addVarRef( *($1) ); }
@@ -177,17 +200,22 @@ expr: literal
177 | tokIdent '(' listValues ')' 200 | tokIdent '(' listValues ')'
178 | varRef 201 | varRef
179 | varRef '=' expr { bld.addNode( AstNode::tStore ); } 202 | varRef '=' expr { bld.addNode( AstNode::tStore ); }
180 | varRef tokPlusAssign expr 203 | varRef tokPlusAssign expr
181 | varRef tokMinusAssign expr 204 | varRef tokMinusAssign expr
182 | varRef tokTimesAssign expr 205 | varRef tokTimesAssign expr
183 | varRef tokDivideAssign expr 206 | varRef tokDivideAssign expr
184 | expr '+' expr { printf(" + plus\n"); } 207 | expr '+' expr { bld.addNode( AstNode::tPlus ); }
185 | expr '-' expr { printf(" + minus\n"); } 208 | expr '-' expr { bld.addNode( AstNode::tMinus ); }
186 | expr '/' expr { printf(" + divide\n"); } 209 | expr '/' expr { bld.addNode( AstNode::tDivide ); }
187 | expr '*' expr { printf(" + times\n"); } 210 | expr '*' expr { bld.addNode( AstNode::tMultiply ); }
188 | expr tokAnd expr 211 | expr tokAnd expr
189 | expr tokOr expr 212 | expr tokOr expr
190 | expr tokIn expr 213 | expr tokIn expr
214 | expr tokCmp expr { bld.addNode( AstNode::tComp ); }
215 | expr '<' expr { bld.addNode( AstNode::tCompLt ); }
216 | expr '>' expr { bld.addNode( AstNode::tCompGt ); }
217 | expr tokLtEq expr { bld.addNode( AstNode::tCompLtEq ); }
218 | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); }
191 | '(' expr ')' 219 | '(' expr ')'
192 | expr '[' expr ']' 220 | expr '[' expr ']'
193 | '[' ']' 221 | '[' ']'
diff --git a/test.stage b/test.stage
index c242a44..a1c6ab3 100644
--- a/test.stage
+++ b/test.stage
@@ -11,6 +11,18 @@ global
11 11
12function hello( a, b, c ) 12function hello( a, b, c )
13{ 13{
14 bob = 5 + 55 * 2 + 1; 14 joe = a;
15 if b < c then
16 {
17 sam = b * c + 1;
18 }
19 else if something <= c then
20 {
21 x * 43;
22 }
23 while true do
24 {
25 bob = 5 + 55 * 2 + 1;
26 }
15} 27}
16 28