summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-29 10:27:38 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-29 10:27:38 -0700
commitbae6192c54533e8da95d8ae1ed4d4eccee28c39a (patch)
treef27b03b518894420992ad1a3ed3ee1a07935ca4b
parent550d4f13ace49e3d442e43d46cd066f174709400 (diff)
downloadstage-bae6192c54533e8da95d8ae1ed4d4eccee28c39a.tar.gz
stage-bae6192c54533e8da95d8ae1ed4d4eccee28c39a.tar.bz2
stage-bae6192c54533e8da95d8ae1ed4d4eccee28c39a.tar.xz
stage-bae6192c54533e8da95d8ae1ed4d4eccee28c39a.zip
Getting close to realy running.
Diffstat (limited to '')
-rw-r--r--src/astfunction.cpp28
-rw-r--r--src/astfunction.h24
-rw-r--r--src/astnode.cpp2
-rw-r--r--src/astnode.h2
-rw-r--r--src/command.cpp46
-rw-r--r--src/command.h38
-rw-r--r--src/commandset.cpp21
-rw-r--r--src/commandset.h20
-rw-r--r--src/functiondisplay.cpp14
-rw-r--r--src/functiondisplay.h16
-rw-r--r--src/game.cpp8
-rw-r--r--src/game.h3
-rw-r--r--src/gamebuilder.cpp73
-rw-r--r--src/gamebuilder.h17
-rw-r--r--src/parser.y27
-rw-r--r--test.stage4
16 files changed, 320 insertions, 23 deletions
diff --git a/src/astfunction.cpp b/src/astfunction.cpp
new file mode 100644
index 0000000..df183d3
--- /dev/null
+++ b/src/astfunction.cpp
@@ -0,0 +1,28 @@
1#include "astfunction.h"
2#include "astbranch.h"
3
4AstFunction::AstFunction( const Bu::String &sName ) :
5 sName( sName ),
6 pAst( NULL )
7{
8}
9
10AstFunction::~AstFunction()
11{
12 delete pAst;
13}
14
15Variable AstFunction::call( const VariableList &lParams )
16{
17}
18
19void AstFunction::addParam( const Bu::String &sName )
20{
21 lParam.append( sName );
22}
23
24void AstFunction::setAst( class AstBranch *pAst )
25{
26 this->pAst = pAst;
27}
28
diff --git a/src/astfunction.h b/src/astfunction.h
new file mode 100644
index 0000000..e6470e4
--- /dev/null
+++ b/src/astfunction.h
@@ -0,0 +1,24 @@
1#ifndef AST_FUNCTION_H
2#define AST_FUNCTION_H
3
4#include "function.h"
5
6class AstFunction : public Function
7{
8public:
9 AstFunction( const Bu::String &sName );
10 virtual ~AstFunction();
11
12 virtual Bu::String getName() const { return sName; }
13 virtual Variable call( const VariableList &lParams );
14
15 void addParam( const Bu::String &sName );
16 void setAst( class AstBranch *pAst );
17
18private:
19 Bu::String sName;
20 Bu::StringList lParam;
21 class AstBranch *pAst;
22};
23
24#endif
diff --git a/src/astnode.cpp b/src/astnode.cpp
index 6e59ab6..7191292 100644
--- a/src/astnode.cpp
+++ b/src/astnode.cpp
@@ -43,7 +43,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t )
43 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; 43 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!";
44 case AstNode::tVarName: return f << "tVarName"; 44 case AstNode::tVarName: return f << "tVarName";
45 case AstNode::tLiteral: return f << "tLiteral"; 45 case AstNode::tLiteral: return f << "tLiteral";
46 case AstNode::tFuncName: return f << "tFuncName"; 46 case AstNode::tFuncCall: return f << "tFuncCall";
47 47
48 case AstNode::tBranch: return f << "!tBranch!"; 48 case AstNode::tBranch: return f << "!tBranch!";
49 case AstNode::tScope: return f << "tScope"; 49 case AstNode::tScope: return f << "tScope";
diff --git a/src/astnode.h b/src/astnode.h
index b38b895..28e5336 100644
--- a/src/astnode.h
+++ b/src/astnode.h
@@ -33,7 +33,7 @@ public:
33 tLeafLiteral = 0x02000000, 33 tLeafLiteral = 0x02000000,
34 tVarName = 0x02000001, 34 tVarName = 0x02000001,
35 tLiteral = 0x02000002, 35 tLiteral = 0x02000002,
36 tFuncName = 0x02000003, 36 tFuncCall = 0x02000003,
37 37
38 tBranch = 0x04000000, 38 tBranch = 0x04000000,
39 tScope = 0x04000001, 39 tScope = 0x04000001,
diff --git a/src/command.cpp b/src/command.cpp
new file mode 100644
index 0000000..5b859f7
--- /dev/null
+++ b/src/command.cpp
@@ -0,0 +1,46 @@
1#include "command.h"
2
3#include "astbranch.h"
4
5#include <bu/sio.h>
6using namespace Bu;
7
8Command::Command() :
9 pAst( NULL )
10{
11}
12
13Command::~Command()
14{
15 delete pAst;
16}
17
18void Command::addLiteral( const Bu::String &sValue )
19{
20 lChunks.append( Chunk( true, sValue ) );
21}
22
23void Command::addParam( const Bu::String &sValue )
24{
25 lChunks.append( Chunk( false, sValue ) );
26}
27
28void Command::setAst( class AstBranch *pAst )
29{
30 this->pAst = pAst;
31}
32
33void Command::print()
34{
35 sio << "command:";
36 for( ChunkList::iterator i = lChunks.begin(); i; i++ )
37 {
38 if( (*i).bLiteral )
39 sio << " \"" << (*i).sValue << "\"";
40 else
41 sio << " " << (*i).sValue;
42 }
43
44 sio << sio.nl;
45}
46
diff --git a/src/command.h b/src/command.h
new file mode 100644
index 0000000..f36fc14
--- /dev/null
+++ b/src/command.h
@@ -0,0 +1,38 @@
1#ifndef COMMAND_H
2#define COMMAND_H
3
4#include <bu/string.h>
5
6class Command
7{
8public:
9 Command();
10 virtual ~Command();
11
12 void addLiteral( const Bu::String &sValue );
13 void addParam( const Bu::String &sValue );
14
15 void setAst( class AstBranch *pAst );
16
17 void print();
18
19private:
20 class Chunk
21 {
22 public:
23 Chunk( bool bLiteral, const Bu::String &sValue ) :
24 bLiteral( bLiteral ), sValue( sValue )
25 {
26 }
27
28 bool bLiteral;
29 Bu::String sValue;
30 };
31
32 typedef Bu::List<Chunk> ChunkList;
33 ChunkList lChunks;
34
35 class AstBranch *pAst;
36};
37
38#endif
diff --git a/src/commandset.cpp b/src/commandset.cpp
new file mode 100644
index 0000000..ffd58a5
--- /dev/null
+++ b/src/commandset.cpp
@@ -0,0 +1,21 @@
1#include "commandset.h"
2
3#include "command.h"
4
5CommandSet::CommandSet()
6{
7}
8
9CommandSet::~CommandSet()
10{
11 for( CommandList::iterator i = lCommand.begin(); i; i++ )
12 {
13 delete (*i);
14 }
15}
16
17void CommandSet::addCommand( class Command *pCmd )
18{
19 lCommand.append( pCmd );
20}
21
diff --git a/src/commandset.h b/src/commandset.h
new file mode 100644
index 0000000..593d529
--- /dev/null
+++ b/src/commandset.h
@@ -0,0 +1,20 @@
1#ifndef COMMAND_SET_H
2#define COMMAND_SET_H
3
4#include <bu/list.h>
5#include <bu/string.h>
6
7class CommandSet
8{
9public:
10 CommandSet();
11 virtual ~CommandSet();
12
13 void addCommand( class Command *pCmd );
14
15private:
16 typedef Bu::List<class Command *> CommandList;
17 CommandList lCommand;
18};
19
20#endif
diff --git a/src/functiondisplay.cpp b/src/functiondisplay.cpp
new file mode 100644
index 0000000..920eefd
--- /dev/null
+++ b/src/functiondisplay.cpp
@@ -0,0 +1,14 @@
1#include "functiondisplay.h"
2
3FunctionDisplay::FunctionDisplay()
4{
5}
6
7FunctionDisplay::~FunctionDisplay()
8{
9}
10
11Variable FunctionDisplay::call( const VariableList &lParams )
12{
13}
14
diff --git a/src/functiondisplay.h b/src/functiondisplay.h
new file mode 100644
index 0000000..2f13360
--- /dev/null
+++ b/src/functiondisplay.h
@@ -0,0 +1,16 @@
1#ifndef FUNCTION_DISPLAY_H
2#define FUNCTION_DISPLAY_H
3
4#include "function.h"
5
6class FunctionDisplay
7{
8public:
9 FunctionDisplay();
10 virtual ~FunctionDisplay();
11
12 virtual Bu::String getName() const { return "display"; }
13 virtual Variable call( const VariableList &lParams );
14};
15
16#endif
diff --git a/src/game.cpp b/src/game.cpp
index 61fa255..f3b5828 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -6,5 +6,13 @@ Game::Game()
6 6
7Game::~Game() 7Game::~Game()
8{ 8{
9 for( FunctionHash::iterator i = hFunction.begin(); i; i++ )
10 {
11 delete (*i);
12 }
13 for( SituationHash::iterator i = hSituation.begin(); i; i++ )
14 {
15 delete (*i);
16 }
9} 17}
10 18
diff --git a/src/game.h b/src/game.h
index 1daa148..81247ca 100644
--- a/src/game.h
+++ b/src/game.h
@@ -9,6 +9,7 @@
9 9
10class Game 10class Game
11{ 11{
12friend class GameBuilder;
12public: 13public:
13 Game(); 14 Game();
14 virtual ~Game(); 15 virtual ~Game();
@@ -16,7 +17,7 @@ public:
16private: 17private:
17 typedef Bu::Hash<Bu::String, Function *> FunctionHash; 18 typedef Bu::Hash<Bu::String, Function *> FunctionHash;
18 typedef Bu::Hash<Bu::String, Situation *> SituationHash; 19 typedef Bu::Hash<Bu::String, Situation *> SituationHash;
19 typedef Bu::Hash<Bu::String, Scope *> ScopeHash; 20 VariableHash hGlobalParam;
20 FunctionHash hFunction; 21 FunctionHash hFunction;
21 SituationHash hSituation; 22 SituationHash hSituation;
22 Bu::String sCurSituation; 23 Bu::String sCurSituation;
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp
index 47de17d..e6c1f38 100644
--- a/src/gamebuilder.cpp
+++ b/src/gamebuilder.cpp
@@ -1,16 +1,24 @@
1#include "gamebuilder.h" 1#include "gamebuilder.h"
2#include <bu/sio.h> 2#include <bu/sio.h>
3 3
4#include "game.h"
4#include "astbranch.h" 5#include "astbranch.h"
5#include "astleaf.h" 6#include "astleaf.h"
6#include "astleafliteral.h" 7#include "astleafliteral.h"
8#include "astfunction.h"
9#include "command.h"
7 10
8using namespace Bu; 11using namespace Bu;
9 12
10GameBuilder::GameBuilder() : 13GameBuilder::GameBuilder() :
14 pGame( NULL ),
15 bGlobal( false ),
11 pCurNode( NULL ), 16 pCurNode( NULL ),
12 pCurRoot( NULL ) 17 pCurRoot( NULL ),
18 pCurCmd( NULL ),
19 pCurFnc( NULL )
13{ 20{
21 pGame = new Game();
14} 22}
15 23
16GameBuilder::~GameBuilder() 24GameBuilder::~GameBuilder()
@@ -24,20 +32,28 @@ void GameBuilder::setLiteral( const Variable &v )
24 32
25void GameBuilder::setGameParam( const Bu::String &sName ) 33void GameBuilder::setGameParam( const Bu::String &sName )
26{ 34{
27 sio << "Set game param '" << sName << "' to " << vLiteral << sio.nl; 35 pGame->hGlobalParam.insert( sName, vLiteral );
28 hGameParams.insert( sName, vLiteral );
29} 36}
30 37
31void GameBuilder::beginFunction( const Bu::String &sName ) 38void GameBuilder::beginFunction( const Bu::String &sName )
32{ 39{
33 pCurNode = pCurRoot = new AstBranch( AstNode::tScope ); 40 pCurNode = pCurRoot = new AstBranch( AstNode::tScope );
34 41 pCurFnc = new AstFunction( sName );
35 sio << "New function: " << sName << sio.nl; 42 sio << "New function: " << sName << sio.nl;
36} 43}
37 44
45void GameBuilder::addFunctionParam( const Bu::String &sName )
46{
47 pCurFnc->addParam( sName );
48 sio << " - Param added '" << sName << "'" << sio.nl;
49}
50
38void GameBuilder::endFunction() 51void GameBuilder::endFunction()
39{ 52{
40 sio << "Function ended: " << *pCurRoot << sio.nl; 53 sio << "Function ended: " << *pCurRoot << sio.nl;
54 pCurFnc->setAst( pCurRoot );
55 pCurRoot = pCurNode = NULL;
56 pGame->hFunction.insert( pCurFnc->getName(), pCurFnc );
41} 57}
42 58
43void GameBuilder::beginSituation( const Bu::String &sName ) 59void GameBuilder::beginSituation( const Bu::String &sName )
@@ -50,11 +66,6 @@ void GameBuilder::endSituation()
50 sio << "Situation ended." << sio.nl; 66 sio << "Situation ended." << sio.nl;
51} 67}
52 68
53void GameBuilder::addParam( const Bu::String &sName )
54{
55 sio << " - Param added '" << sName << "'" << sio.nl;
56}
57
58void GameBuilder::addNode( AstNode::Type iType ) 69void GameBuilder::addNode( AstNode::Type iType )
59{ 70{
60 switch( iType&AstNode::tTypeMask ) 71 switch( iType&AstNode::tTypeMask )
@@ -91,3 +102,47 @@ void GameBuilder::addVarRef( const Bu::String &sName )
91 } 102 }
92} 103}
93 104
105void GameBuilder::addFuncCall( const Bu::String &sName )
106{
107 if( pCurNode )
108 {
109 pCurNode->addNode( new AstLeafLiteral( AstNode::tFuncCall, sName ) );
110 }
111}
112
113void GameBuilder::beginGlobal()
114{
115 bGlobal = true;
116}
117
118void GameBuilder::closeGlobal()
119{
120 bGlobal = false;
121}
122
123void GameBuilder::beginCommand( const Bu::String &sValue )
124{
125 pCurNode = pCurRoot = new AstBranch( AstNode::tScope );
126 pCurCmd = new Command();
127 pCurCmd->addLiteral( sValue );
128}
129
130void GameBuilder::addCommandLiteral( const Bu::String &sValue )
131{
132 pCurCmd->addLiteral( sValue );
133}
134
135void GameBuilder::addCommandParam( const Bu::String &sValue )
136{
137 pCurCmd->addParam( sValue );
138}
139
140void GameBuilder::closeCommand()
141{
142 pCurCmd->print();
143 sio << *pCurRoot << sio.nl;
144 pCurCmd->setAst( pCurRoot );
145 delete pCurCmd;
146 pCurCmd = NULL;
147}
148
diff --git a/src/gamebuilder.h b/src/gamebuilder.h
index 2637c37..bc32f55 100644
--- a/src/gamebuilder.h
+++ b/src/gamebuilder.h
@@ -16,23 +16,34 @@ public:
16 void setGameParam( const Bu::String &sName ); 16 void setGameParam( const Bu::String &sName );
17 17
18 void beginFunction( const Bu::String &sName ); 18 void beginFunction( const Bu::String &sName );
19 void addFunctionParam( const Bu::String &sName );
19 void endFunction(); 20 void endFunction();
20 21
21 void beginSituation( const Bu::String &sName ); 22 void beginSituation( const Bu::String &sName );
22 void endSituation(); 23 void endSituation();
23 24
24 void addParam( const Bu::String &sName );
25 void addNode( AstNode::Type iType ); 25 void addNode( AstNode::Type iType );
26 void closeNode(); 26 void closeNode();
27 void addLiteral( const Variable &v ); 27 void addLiteral( const Variable &v );
28 void addVarRef( const Bu::String &sName ); 28 void addVarRef( const Bu::String &sName );
29 void addFuncCall( const Bu::String &sName );
30
31 void beginGlobal();
32 void closeGlobal();
33
34 void beginCommand( const Bu::String &sValue );
35 void addCommandLiteral( const Bu::String &sValue );
36 void addCommandParam( const Bu::String &sValue );
37 void closeCommand();
29 38
30private: 39private:
40 class Game *pGame;
41 bool bGlobal;
31 Variable vLiteral; 42 Variable vLiteral;
32 class AstBranch *pCurNode; 43 class AstBranch *pCurNode;
33 class AstBranch *pCurRoot; 44 class AstBranch *pCurRoot;
34 45 class Command *pCurCmd;
35 VariableHash hGameParams; 46 class AstFunction *pCurFnc;
36}; 47};
37 48
38#endif 49#endif
diff --git a/src/parser.y b/src/parser.y
index 90c771d..e6210d1 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -96,7 +96,8 @@ gameDecls:
96 ; 96 ;
97 97
98globalDecl: 98globalDecl:
99 | tokGlobal '{' globalExprList '}' 99 | tokGlobal '{' { bld.beginGlobal(); } globalExprList '}'
100 { bld.closeGlobal(); }
100 ; 101 ;
101 102
102globalExprList: 103globalExprList:
@@ -134,11 +135,11 @@ function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList
134 ; 135 ;
135 136
136funcParamList: 137funcParamList:
137 | tokIdent { bld.addParam( *($1) ); } funcParamListEx 138 | tokIdent { bld.addFunctionParam( *($1) ); } funcParamListEx
138 ; 139 ;
139 140
140funcParamListEx: 141funcParamListEx:
141 | funcParamListEx ',' tokIdent { bld.addParam( *($3) ); } 142 | funcParamListEx ',' tokIdent { bld.addFunctionParam( *($3) ); }
142 ; 143 ;
143 144
144cmpltExprList: 145cmpltExprList:
@@ -156,7 +157,8 @@ cmpltExpr: expr ';'
156 bld.closeNode(); 157 bld.closeNode();
157 bld.addNode( AstNode::tScope ); 158 bld.addNode( AstNode::tScope );
158 } tokDo '{' cmpltExprList '}' { 159 } tokDo '{' cmpltExprList '}' {
159 bld.closeNode(); bld.closeNode(); 160 bld.closeNode();
161 bld.closeNode();
160 } 162 }
161 ; 163 ;
162 164
@@ -198,7 +200,7 @@ literal: tokInt { bld.addLiteral( Variable( $1 ) ); }
198 ; 200 ;
199 201
200expr: literal 202expr: literal
201 | tokIdent '(' listValues ')' 203 | tokIdent '(' funcCallParams ')' { bld.addFuncCall( *($1) ); }
202 | varRef 204 | varRef
203 | varRef '=' expr { bld.addNode( AstNode::tStore ); } 205 | varRef '=' expr { bld.addNode( AstNode::tStore ); }
204 | varRef tokPlusAssign expr { bld.addNode( AstNode::tPlusStore ); } 206 | varRef tokPlusAssign expr { bld.addNode( AstNode::tPlusStore ); }
@@ -227,6 +229,14 @@ expr: literal
227 | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } 229 | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); }
228 ; 230 ;
229 231
232funcCallParams:
233 | expr funcCallParamsEx
234 ;
235
236funcCallParamsEx:
237 | funcCallParamsEx ',' expr
238 ;
239
230listValues: expr 240listValues: expr
231 | listValues ',' expr 241 | listValues ',' expr
232 ; 242 ;
@@ -235,12 +245,13 @@ dictValues: expr ':' expr
235 | dictValues ',' expr ':' expr 245 | dictValues ',' expr ':' expr
236 ; 246 ;
237 247
238commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}' 248commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); }
249 commandParamList '{' cmpltExprList '}' { bld.closeCommand(); }
239 ; 250 ;
240 251
241commandParamList: 252commandParamList:
242 | commandParamList tokString 253 | commandParamList tokString { bld.addCommandLiteral( *$2 ); }
243 | commandParamList tokIdent 254 | commandParamList tokIdent { bld.addCommandParam( *$2 ); }
244 ; 255 ;
245%% 256%%
246/* 257/*
diff --git a/test.stage b/test.stage
index a705c93..13155d3 100644
--- a/test.stage
+++ b/test.stage
@@ -7,6 +7,10 @@ game.start = <<start>>;
7 7
8global 8global
9{ 9{
10 command: "eat" object
11 {
12 pow(5, x*2);
13 }
10} 14}
11 15
12function hello( a, b, c ) 16function hello( a, b, c )