summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-24 01:08:21 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-24 01:08:21 -0700
commit32c32bc48c8fd7cffca301b8919fb26726f6467e (patch)
treefbe58058531f253e4e351312f87e8276ce087c31
parentd7c0a793787eaf52c4302d6d1ea6cc43f7ff9ca7 (diff)
downloadstage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.gz
stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.bz2
stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.tar.xz
stage-32c32bc48c8fd7cffca301b8919fb26726f6467e.zip
Ast nearly done, builder coming along.
-rw-r--r--demo.stage1
-rw-r--r--src/astbranch.cpp16
-rw-r--r--src/astbranch.h20
-rw-r--r--src/astleaf.cpp11
-rw-r--r--src/astleaf.h17
-rw-r--r--src/astnode.cpp10
-rw-r--r--src/astnode.h35
-rw-r--r--src/game.h3
-rw-r--r--src/gamebuilder.cpp21
-rw-r--r--src/gamebuilder.h6
-rw-r--r--src/parser.l3
-rw-r--r--src/parser.y51
-rw-r--r--test.stage16
13 files changed, 176 insertions, 34 deletions
diff --git a/demo.stage b/demo.stage
index e51c160..67c6063 100644
--- a/demo.stage
+++ b/demo.stage
@@ -50,6 +50,7 @@ situation <<main>>
50 50
51function hello() 51function hello()
52{ 52{
53 bob = 55 * 2 + 1;
53} 54}
54 55
55situation <<start>> 56situation <<start>>
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
3AstBranch::AstBranch( AstNode::Type eType ) :
4 AstNode( eType )
5{
6}
7
8AstBranch::~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
8class AstBranch : public AstNode
9{
10public:
11 AstBranch( Type eType );
12 virtual ~AstBranch();
13
14 typedef Bu::List<AstNode *> NodeList;
15
16private:
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
3AstLeaf::AstLeaf( AstNode::Type eType ) :
4 AstNode( eType )
5{
6}
7
8AstLeaf::~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
7class AstLeaf : public AstNode
8{
9public:
10 AstLeaf( Type eType );
11 virtual ~AstLeaf();
12
13private:
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
3AstNode::AstNode( AstNode::Type eType ) :
4 eType( eType )
5{
6}
7
8AstNode::~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 @@
6class AstNode 6class AstNode
7{ 7{
8public: 8public:
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
33private: 39private:
40 Type eType;
34}; 41};
35 42
36#endif 43#endif
diff --git a/src/game.h b/src/game.h
index a5b1163..1daa148 100644
--- a/src/game.h
+++ b/src/game.h
@@ -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
45void GameBuilder::addParam( const Bu::String &sName )
46{
47 sio << " - Param added '" << sName << "'" << sio.nl;
48}
49
50void GameBuilder::addNode( int iType )
51{
52 sio << " - Added type " << Fmt::hex() << iType << sio.nl;
53}
54
55void GameBuilder::addLiteral( const Variable &v )
56{
57 setLiteral( v );
58 sio << " - Added literal " << v << sio.nl;
59}
60
61void 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
24private: 28private:
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; }
36while { return tokWhile; } 36while { return tokWhile; }
37for { return tokFor; } 37for { return tokFor; }
38each { return tokEach; } 38each { return tokEach; }
39do { return tokDo; }
39in { return tokIn; } 40in { return tokIn; }
40if { return tokIf; } 41if { return tokIf; }
41then { return tokThen; } 42then { return tokThen; }
@@ -45,6 +46,8 @@ goto { return tokGoto; }
45not { return tokNot; } 46not { return tokNot; }
46setup { return tokSetup; } 47setup { return tokSetup; }
47enter { return tokEnter; } 48enter { return tokEnter; }
49and { return tokAnd; }
50or { return tokOr; }
48 51
49true { yylval->bValue = true; return tokBool; } 52true { yylval->bValue = true; return tokBool; }
50false { yylval->bValue = false; return tokBool; } 53false { 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
8typedef void *yyscan_t; 9typedef 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%%
86input: gameDecls globalDecl bodyDecl 90input: gameDecls globalDecl bodyDecl
@@ -125,9 +129,17 @@ situationMode: tokSetup
125 | tokEnter 129 | tokEnter
126 ; 130 ;
127 131
128function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' ')' '{' '}' { bld.endFunction(); } 132function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); }
129 ; 133 ;
130 134
135funcParamList:
136 | tokIdent { bld.addParam( *($1) ); } funcParamListEx
137 ;
138
139funcParamListEx:
140 | funcParamListEx ',' tokIdent { bld.addParam( *($3) ); }
141 ;
142
131cmpltExprList: 143cmpltExprList:
132 | cmpltExprList cmpltExpr 144 | cmpltExprList cmpltExpr
133 ; 145 ;
@@ -135,39 +147,46 @@ cmpltExprList:
135cmpltExpr: expr ';' 147cmpltExpr: 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
154forIterator: tokIdent
155 | tokIdent ':' tokIdent
156
140ifnext: 157ifnext:
141 | tokElse '{' cmpltExprList '}' 158 | tokElse '{' cmpltExprList '}'
142 | tokElse tokIf '{' cmpltExprList '}' ifnext 159 | tokElse tokIf '{' cmpltExprList '}' ifnext
143 ; 160 ;
144 161
145varRef: tokIdent 162varRef: 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
151literal: tokInt { bld.setLiteral( Variable( $1 ) ); } 168literal: 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
159expr: literal 176expr: 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 ']'
diff --git a/test.stage b/test.stage
new file mode 100644
index 0000000..c242a44
--- /dev/null
+++ b/test.stage
@@ -0,0 +1,16 @@
1
2game.title = "Demo game";
3game.author = "Mike Buland";
4game.version = 1;
5game.revision = 0;
6game.start = <<start>>;
7
8global
9{
10}
11
12function hello( a, b, c )
13{
14 bob = 5 + 55 * 2 + 1;
15}
16