summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-29 22:27:59 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-29 22:27:59 -0700
commitf66458278ce3663397fc985a1253c85b74f011e6 (patch)
tree785e2fb51a91fe277c698ea84009e0052ef6555e
parentf404d991aa53ed81855e51b597bfe1d5c2288b42 (diff)
downloadstage-f66458278ce3663397fc985a1253c85b74f011e6.tar.gz
stage-f66458278ce3663397fc985a1253c85b74f011e6.tar.bz2
stage-f66458278ce3663397fc985a1253c85b74f011e6.tar.xz
stage-f66458278ce3663397fc985a1253c85b74f011e6.zip
Most AstNodes work now.
Next up: loops, proper variable references with scopes, and gotos.
-rw-r--r--src/astfunction.cpp3
-rw-r--r--src/astfunction.h1
-rw-r--r--src/astnode.cpp1
-rw-r--r--src/astnode.h1
-rw-r--r--src/gamebuilder.cpp17
-rw-r--r--src/gamebuilder.h1
-rw-r--r--src/gamestate.cpp72
-rw-r--r--src/main.cpp3
-rw-r--r--src/parser.y2
-rw-r--r--src/variable.cpp134
-rw-r--r--test.stage23
11 files changed, 242 insertions, 16 deletions
diff --git a/src/astfunction.cpp b/src/astfunction.cpp
index f091644..f5e25dc 100644
--- a/src/astfunction.cpp
+++ b/src/astfunction.cpp
@@ -1,6 +1,8 @@
1#include "astfunction.h" 1#include "astfunction.h"
2#include "astbranch.h" 2#include "astbranch.h"
3 3
4#include "gamestate.h"
5
4AstFunction::AstFunction( const Bu::String &sName ) : 6AstFunction::AstFunction( const Bu::String &sName ) :
5 sName( sName ), 7 sName( sName ),
6 pAst( NULL ) 8 pAst( NULL )
@@ -14,6 +16,7 @@ AstFunction::~AstFunction()
14 16
15Variable AstFunction::call( class GameState &gState ) 17Variable AstFunction::call( class GameState &gState )
16{ 18{
19 gState.parse( pAst );
17} 20}
18 21
19void AstFunction::addParam( const Bu::String &sName ) 22void AstFunction::addParam( const Bu::String &sName )
diff --git a/src/astfunction.h b/src/astfunction.h
index 4b2a049..a546b19 100644
--- a/src/astfunction.h
+++ b/src/astfunction.h
@@ -13,6 +13,7 @@ public:
13 virtual Variable call( class GameState &gState ); 13 virtual Variable call( class GameState &gState );
14 14
15 void addParam( const Bu::String &sName ); 15 void addParam( const Bu::String &sName );
16 const Bu::StringList &getParamList() { return lParam; }
16 void setAst( class AstBranch *pAst ); 17 void setAst( class AstBranch *pAst );
17 18
18private: 19private:
diff --git a/src/astnode.cpp b/src/astnode.cpp
index d7295b3..88363c9 100644
--- a/src/astnode.cpp
+++ b/src/astnode.cpp
@@ -40,6 +40,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, AstNode::Type t )
40 case AstNode::tNegate: return f << "tNegate"; 40 case AstNode::tNegate: return f << "tNegate";
41 case AstNode::tIn: return f << "tIn"; 41 case AstNode::tIn: return f << "tIn";
42 case AstNode::tGoto: return f << "tGoto"; 42 case AstNode::tGoto: return f << "tGoto";
43 case AstNode::tSwap: return f << "tSwap";
43 44
44 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!"; 45 case AstNode::tLeafLiteral: return f << "!tLeafLiteral!";
45 case AstNode::tVarName: return f << "tVarName"; 46 case AstNode::tVarName: return f << "tVarName";
diff --git a/src/astnode.h b/src/astnode.h
index c0a8eea..63bf64f 100644
--- a/src/astnode.h
+++ b/src/astnode.h
@@ -30,6 +30,7 @@ public:
30 tNegate = 0x01000012, 30 tNegate = 0x01000012,
31 tIn = 0x01000013, 31 tIn = 0x01000013,
32 tGoto = 0x01000014, 32 tGoto = 0x01000014,
33 tSwap = 0x01000015,
33 34
34 tLeafLiteral = 0x02000000, 35 tLeafLiteral = 0x02000000,
35 tVarName = 0x02000001, 36 tVarName = 0x02000001,
diff --git a/src/gamebuilder.cpp b/src/gamebuilder.cpp
index 6c42fb0..d1b4430 100644
--- a/src/gamebuilder.cpp
+++ b/src/gamebuilder.cpp
@@ -50,6 +50,23 @@ void GameBuilder::addFunctionParam( const Bu::String &sName )
50 sio << " - Param added '" << sName << "'" << sio.nl; 50 sio << " - Param added '" << sName << "'" << sio.nl;
51} 51}
52 52
53void GameBuilder::endFunctionParams()
54{
55 Bu::StringList lRev;
56 for( Bu::StringList::const_iterator i = pCurFnc->getParamList().begin();
57 i; i++ )
58 {
59 lRev.prepend( *i );
60 }
61
62 for( Bu::StringList::iterator i = lRev.begin(); i; i++ )
63 {
64 addVarRef( *i );
65 addNode( AstNode::tSwap );
66 addNode( AstNode::tStore );
67 }
68}
69
53void GameBuilder::endFunction() 70void GameBuilder::endFunction()
54{ 71{
55 sio << "Function ended: " << *pCurRoot << sio.nl; 72 sio << "Function ended: " << *pCurRoot << sio.nl;
diff --git a/src/gamebuilder.h b/src/gamebuilder.h
index 50dcbb7..e4ead71 100644
--- a/src/gamebuilder.h
+++ b/src/gamebuilder.h
@@ -20,6 +20,7 @@ public:
20 20
21 void beginFunction( const Bu::String &sName ); 21 void beginFunction( const Bu::String &sName );
22 void addFunctionParam( const Bu::String &sName ); 22 void addFunctionParam( const Bu::String &sName );
23 void endFunctionParams();
23 void endFunction(); 24 void endFunction();
24 25
25 void beginSituation( const Bu::String &sName ); 26 void beginSituation( const Bu::String &sName );
diff --git a/src/gamestate.cpp b/src/gamestate.cpp
index 5577c17..14a7b53 100644
--- a/src/gamestate.cpp
+++ b/src/gamestate.cpp
@@ -51,7 +51,9 @@ void GameState::gotoSituation( const Bu::String &sName )
51 51
52void GameState::callFunction( const Bu::String &sName ) 52void GameState::callFunction( const Bu::String &sName )
53{ 53{
54 lsLocal.push( new Scope() );
54 pGame->getFunction( sName )->call( *this ); 55 pGame->getFunction( sName )->call( *this );
56 delete lsLocal.peekPop();
55} 57}
56 58
57Variable GameState::getVariable( const Bu::String &sName, ScopeId id ) 59Variable GameState::getVariable( const Bu::String &sName, ScopeId id )
@@ -119,10 +121,19 @@ void GameState::parse( const AstBranch::NodeList &lCode )
119 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ ) 121 for( AstBranch::NodeList::const_iterator i = lCode.begin(); i; i++ )
120 { 122 {
121// sio << "Stack: " << lStack << sio.nl; 123// sio << "Stack: " << lStack << sio.nl;
124// sio << "exec: " << (*i)->getType() << sio.nl;
122 switch( (*i)->getType() ) 125 switch( (*i)->getType() )
123 { 126 {
124 // tLeaf 127 // tLeaf
125 case AstNode::tNot: 128 case AstNode::tNot:
129 {
130 Variable x = popDeref();
131 if( x.getType() != Variable::tBool )
132 throw Bu::ExceptionBase("Non-bool used with logical not operator.");
133 push( Variable( !x.getBool() ) );
134 }
135 break;
136
126 case AstNode::tComp: 137 case AstNode::tComp:
127 { 138 {
128 push( popDeref() == popDeref() ); 139 push( popDeref() == popDeref() );
@@ -130,9 +141,37 @@ void GameState::parse( const AstBranch::NodeList &lCode )
130 break; 141 break;
131 142
132 case AstNode::tCompGt: 143 case AstNode::tCompGt:
144 {
145 Variable y = popDeref();
146 Variable x = popDeref();
147 push( x > y );
148 }
149 break;
150
133 case AstNode::tCompLt: 151 case AstNode::tCompLt:
152 {
153 Variable y = popDeref();
154 Variable x = popDeref();
155 push( x < y );
156 }
157 break;
158
134 case AstNode::tCompGtEq: 159 case AstNode::tCompGtEq:
160 {
161 Variable y = popDeref();
162 Variable x = popDeref();
163 push( x >= y );
164 }
165 break;
166
135 case AstNode::tCompLtEq: 167 case AstNode::tCompLtEq:
168 {
169 Variable y = popDeref();
170 Variable x = popDeref();
171 push( x <= y );
172 }
173 break;
174
136 case AstNode::tStore: 175 case AstNode::tStore:
137 { 176 {
138 Variable y = popDeref(); 177 Variable y = popDeref();
@@ -142,7 +181,31 @@ void GameState::parse( const AstBranch::NodeList &lCode )
142 break; 181 break;
143 182
144 case AstNode::tAnd: 183 case AstNode::tAnd:
184 {
185 Variable y = popDeref();
186 Variable x = popDeref();
187 if( x.getType() != Variable::tBool ||
188 y.getType() != Variable::tBool )
189 {
190 throw Bu::ExceptionBase("Non-bool used in logical AND operator.");
191 }
192 lStack.push( Variable( x.getBool() && y.getBool() ) );
193 }
194 break;
195
145 case AstNode::tOr: 196 case AstNode::tOr:
197 {
198 Variable y = popDeref();
199 Variable x = popDeref();
200 if( x.getType() != Variable::tBool ||
201 y.getType() != Variable::tBool )
202 {
203 throw Bu::ExceptionBase("Non-bool used in logical OR operator.");
204 }
205 lStack.push( Variable( x.getBool() || y.getBool() ) );
206 }
207 break;
208
146 case AstNode::tPlus: 209 case AstNode::tPlus:
147 { 210 {
148 Variable y = popDeref(); 211 Variable y = popDeref();
@@ -212,6 +275,15 @@ void GameState::parse( const AstBranch::NodeList &lCode )
212 break; 275 break;
213 276
214 case AstNode::tIn: 277 case AstNode::tIn:
278 case AstNode::tGoto:
279 case AstNode::tSwap:
280 {
281 Variable y = pop();
282 Variable x = pop();
283 push( y );
284 push( x );
285 }
286 break;
215 287
216 // tLeafLiteral 288 // tLeafLiteral
217 case AstNode::tVarName: 289 case AstNode::tVarName:
diff --git a/src/main.cpp b/src/main.cpp
index 273158b..8fe5247 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -30,9 +30,6 @@ int main( int argc, char *argv[] )
30 GameState gs( pGame ); 30 GameState gs( pGame );
31 gs.init(); 31 gs.init();
32 32
33 gs.gotoSituation("stuff");
34 gs.gotoSituation("start");
35
36 return 0; 33 return 0;
37} 34}
38 35
diff --git a/src/parser.y b/src/parser.y
index ffb6da2..ccb0c97 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -134,7 +134,7 @@ situationMode: tokSetup { bld.beginSituationMode( Situation::modeSetup ); }
134 | tokEnter { bld.beginSituationMode( Situation::modeEnter ); } 134 | tokEnter { bld.beginSituationMode( Situation::modeEnter ); }
135 ; 135 ;
136 136
137function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); } 137function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' { bld.endFunctionParams(); } '{' cmpltExprList '}' { bld.endFunction(); }
138 ; 138 ;
139 139
140funcParamList: 140funcParamList:
diff --git a/src/variable.cpp b/src/variable.cpp
index d5bde2e..5e2462c 100644
--- a/src/variable.cpp
+++ b/src/variable.cpp
@@ -543,12 +543,134 @@ bool Variable::operator!=( const Variable &rhs ) const
543 return !(*this == rhs); 543 return !(*this == rhs);
544} 544}
545 545
546/* 546bool Variable::operator>( const Variable &rhs ) const
547Variable &Variable::operator>( const Variable &rhs ); 547{
548Variable &Variable::operator<( const Variable &rhs ); 548 if( eType != rhs.eType )
549Variable &Variable::operator>=( const Variable &rhs ); 549 return false;
550Variable &Variable::operator<=( const Variable &rhs ); 550
551*/ 551 switch( eType )
552 {
553 case tNull:
554 throw Bu::ExceptionBase("You cannot use > to compare nulls.");
555
556 case tBool:
557 throw Bu::ExceptionBase("You cannot use > to compare bools.");
558
559 case tInt:
560 return iValue > rhs.iValue;
561
562 case tFloat:
563 return fValue > rhs.fValue;
564
565 case tString:
566 case tSituation:
567 case tVariable:
568 throw Bu::ExceptionBase("You cannot use > to compare strings.");
569
570 case tList:
571 throw Bu::ExceptionBase("You cannot use > to compare lists.");
572
573 case tDictionary:
574 throw Bu::ExceptionBase("You cannot use > to compare dictionary.");
575 }
576}
577
578bool Variable::operator<( const Variable &rhs ) const
579{
580 if( eType != rhs.eType )
581 return false;
582
583 switch( eType )
584 {
585 case tNull:
586 throw Bu::ExceptionBase("You cannot use < to compare nulls.");
587
588 case tBool:
589 throw Bu::ExceptionBase("You cannot use < to compare bools.");
590
591 case tInt:
592 return iValue < rhs.iValue;
593
594 case tFloat:
595 return fValue < rhs.fValue;
596
597 case tString:
598 case tSituation:
599 case tVariable:
600 throw Bu::ExceptionBase("You cannot use < to compare strings.");
601
602 case tList:
603 throw Bu::ExceptionBase("You cannot use < to compare lists.");
604
605 case tDictionary:
606 throw Bu::ExceptionBase("You cannot use < to compare dictionary.");
607 }
608}
609
610bool Variable::operator>=( const Variable &rhs ) const
611{
612 if( eType != rhs.eType )
613 return false;
614
615 switch( eType )
616 {
617 case tNull:
618 throw Bu::ExceptionBase("You cannot use >= to compare nulls.");
619
620 case tBool:
621 throw Bu::ExceptionBase("You cannot use >= to compare bools.");
622
623 case tInt:
624 return iValue >= rhs.iValue;
625
626 case tFloat:
627 return fValue >= rhs.fValue;
628
629 case tString:
630 case tSituation:
631 case tVariable:
632 throw Bu::ExceptionBase("You cannot use >= to compare strings.");
633
634 case tList:
635 throw Bu::ExceptionBase("You cannot use >= to compare lists.");
636
637 case tDictionary:
638 throw Bu::ExceptionBase("You cannot use >= to compare dictionary.");
639 }
640}
641
642bool Variable::operator<=( const Variable &rhs ) const
643{
644 if( eType != rhs.eType )
645 return false;
646
647 switch( eType )
648 {
649 case tNull:
650 throw Bu::ExceptionBase("You cannot use <= to compare nulls.");
651
652 case tBool:
653 throw Bu::ExceptionBase("You cannot use <= to compare bools.");
654
655 case tInt:
656 return iValue <= rhs.iValue;
657
658 case tFloat:
659 return fValue <= rhs.fValue;
660
661 case tString:
662 case tSituation:
663 case tVariable:
664 throw Bu::ExceptionBase("You cannot use <= to compare strings.");
665
666 case tList:
667 throw Bu::ExceptionBase("You cannot use <= to compare lists.");
668
669 case tDictionary:
670 throw Bu::ExceptionBase("You cannot use <= to compare dictionary.");
671 }
672}
673
552void Variable::initType() 674void Variable::initType()
553{ 675{
554 iValue = 0; 676 iValue = 0;
diff --git a/test.stage b/test.stage
index 75bff27..f150baa 100644
--- a/test.stage
+++ b/test.stage
@@ -13,6 +13,19 @@ global
13 } 13 }
14} 14}
15 15
16function sillyDisplay( txt, extra )
17{
18 display("!~! " + txt + " !~!");
19 if extra then
20 {
21 display("And then some extra!");
22 }
23 else
24 {
25 display("...no extra for you");
26 }
27}
28
16situation <<start>> 29situation <<start>>
17{ 30{
18 setup 31 setup
@@ -20,17 +33,15 @@ situation <<start>>
20 name = "Bob"; 33 name = "Bob";
21 name += " The Man"; 34 name += " The Man";
22 display("This is the setup phase for start, " + name); 35 display("This is the setup phase for start, " + name);
23 display( 5 == 6 ); 36 display( 5 <= 1 );
24 display( 5 == 5 ); 37 display( 1 <= 1 );
38 display( 0 <= 1 );
39 sillyDisplay( "Hello", true );
25 40
26 if name == "Bob The Man" then 41 if name == "Bob The Man" then
27 { 42 {
28 display("You are bob"); 43 display("You are bob");
29 } 44 }
30 else
31 {
32 display("You are not bob");
33 }
34 } 45 }
35 46
36 enter 47 enter