diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 22:27:59 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-29 22:27:59 -0700 |
commit | f66458278ce3663397fc985a1253c85b74f011e6 (patch) | |
tree | 785e2fb51a91fe277c698ea84009e0052ef6555e /src | |
parent | f404d991aa53ed81855e51b597bfe1d5c2288b42 (diff) | |
download | stage-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/astfunction.cpp | 3 | ||||
-rw-r--r-- | src/astfunction.h | 1 | ||||
-rw-r--r-- | src/astnode.cpp | 1 | ||||
-rw-r--r-- | src/astnode.h | 1 | ||||
-rw-r--r-- | src/gamebuilder.cpp | 17 | ||||
-rw-r--r-- | src/gamebuilder.h | 1 | ||||
-rw-r--r-- | src/gamestate.cpp | 72 | ||||
-rw-r--r-- | src/main.cpp | 3 | ||||
-rw-r--r-- | src/parser.y | 2 | ||||
-rw-r--r-- | src/variable.cpp | 134 |
10 files changed, 225 insertions, 10 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 | |||
4 | AstFunction::AstFunction( const Bu::String &sName ) : | 6 | AstFunction::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 | ||
15 | Variable AstFunction::call( class GameState &gState ) | 17 | Variable AstFunction::call( class GameState &gState ) |
16 | { | 18 | { |
19 | gState.parse( pAst ); | ||
17 | } | 20 | } |
18 | 21 | ||
19 | void AstFunction::addParam( const Bu::String &sName ) | 22 | void 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 | ||
18 | private: | 19 | private: |
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 | ||
53 | void 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 | |||
53 | void GameBuilder::endFunction() | 70 | void 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 | ||
52 | void GameState::callFunction( const Bu::String &sName ) | 52 | void 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 | ||
57 | Variable GameState::getVariable( const Bu::String &sName, ScopeId id ) | 59 | Variable 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 | ||
137 | function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' '{' cmpltExprList '}' { bld.endFunction(); } | 137 | function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' { bld.endFunctionParams(); } '{' cmpltExprList '}' { bld.endFunction(); } |
138 | ; | 138 | ; |
139 | 139 | ||
140 | funcParamList: | 140 | funcParamList: |
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 | /* | 546 | bool Variable::operator>( const Variable &rhs ) const |
547 | Variable &Variable::operator>( const Variable &rhs ); | 547 | { |
548 | Variable &Variable::operator<( const Variable &rhs ); | 548 | if( eType != rhs.eType ) |
549 | Variable &Variable::operator>=( const Variable &rhs ); | 549 | return false; |
550 | Variable &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 | |||
578 | bool 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 | |||
610 | bool 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 | |||
642 | bool 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 | |||
552 | void Variable::initType() | 674 | void Variable::initType() |
553 | { | 675 | { |
554 | iValue = 0; | 676 | iValue = 0; |