From ba7897ebadbc03d99200fda03a574a57b650e429 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 30 Dec 2011 15:44:06 -0700 Subject: Dictionaries can be created, in operator works. --- src/gamestate.cpp | 22 ++++++++++++++++++++++ src/parser.y | 10 +++++----- src/variable.cpp | 17 +++++++++++++++++ src/variable.h | 5 +++++ test.stage | 3 ++- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index d45d53c..e8df070 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -408,6 +408,20 @@ void GameState::parse( const AstBranch::NodeList &lCode ) break; case AstNode::tIn: + { + Variable v = popDeref(); + Variable x = popDeref(); + if( v.getType() == Variable::tDictionary ) + { + push( Variable( v.has( x ) ) ); + } + else + { + throw Bu::ExceptionBase("Invalid type for operator in"); + } + } + break; + case AstNode::tGoto: { Variable x = popDeref(); @@ -439,6 +453,14 @@ void GameState::parse( const AstBranch::NodeList &lCode ) } break; + case AstNode::tInsert: + { + Variable v = popDeref(); + Variable k = popDeref(); + lStack.peek().insert( k, v ); + } + break; + // tLeafLiteral case AstNode::tVarName: case AstNode::tLiteral: diff --git a/src/parser.y b/src/parser.y index f01e0e4..707d85d 100644 --- a/src/parser.y +++ b/src/parser.y @@ -82,9 +82,9 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err %left NOT %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign +%left tokIn tokAnd tokOr %left '-' '+' %left '*' '/' -%left tokIn tokAnd tokOr %right '<' '>' tokLtEq tokGtEq tokCmp %left '(' ')' '[' ']' %left NEG @@ -229,8 +229,8 @@ expr: literal | expr '[' expr ']' | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' - | '{' '}' - | '{' dictValues '}' + | '{' '}' { bld.addLiteral( Variable( Variable::tDictionary ) ); } + | '{' { bld.addLiteral( Variable( Variable::tDictionary ) ); } dictValues '}' | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } ; @@ -247,8 +247,8 @@ listValues: expr { bld.addNode( AstNode::tAppend ); } | listValues ',' expr { bld.addNode( AstNode::tAppend ); } ; -dictValues: expr ':' expr - | dictValues ',' expr ':' expr +dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); } + | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } ; commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } diff --git a/src/variable.cpp b/src/variable.cpp index db7726c..1e5cc9b 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -210,6 +210,23 @@ Variable Variable::to( Type e ) const eType, e ); } +void Variable::insert( const Variable &vKey, const Variable &vValue ) +{ + if( eType != tDictionary ) + throw Bu::ExceptionBase("Insert on non-dictionary."); + hValue->insert( vKey, vValue ); +} + +bool Variable::has( const Variable &vKey ) +{ + if( eType == tDictionary ) + return hValue->has( vKey ); +// else if( eType == tList ) +// return lValue->contains( vKey ); + else + throw Bu::ExceptionBase("Insert on non-dictionary."); +} + Variable &Variable::operator=( const Variable &rhs ) { deinitType(); diff --git a/src/variable.h b/src/variable.h index 7f482a3..97308c7 100644 --- a/src/variable.h +++ b/src/variable.h @@ -31,6 +31,7 @@ public: tString, tSituation, tVariable, + tVarPtr, tList, tDictionary }; @@ -58,6 +59,9 @@ public: Variable to( Type e ) const; + void insert( const Variable &vKey, const Variable &vValue ); + bool has( const Variable &vKey ); + Variable &operator=( const Variable &rhs ); Variable &operator+=( const Variable &rhs ); Variable &operator-=( const Variable &rhs ); @@ -94,6 +98,7 @@ private: VList *lValue; VHash *hValue; VariableRef *rValue; + Variable *pValue; }; }; diff --git a/test.stage b/test.stage index 1c58991..ff159ff 100644 --- a/test.stage +++ b/test.stage @@ -61,8 +61,9 @@ situation <> } setup { - stuff = [5, 10, "bob"]; + stuff = {"things": 55, 112: "Bob"}; display( stuff ); + display( 113 - 1 in stuff ); exit(); } -- cgit v1.2.3