From 644c1307124dffd13268445d0aebaed13a090c78 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 21 Sep 2012 21:21:03 +0100 Subject: [PATCH] Set properties with integer.property --- src/pim/Generator.cpp | 11 ++++++ src/pim/Generator.h | 7 +++- src/pim/Machine.cpp | 9 +++++ src/pim/Machine.h | 1 + src/pim/Opcodes.inl | 2 ++ src/pim/Parser.cpp | 83 +++++++++++++++---------------------------- src/pim/Scanner.cpp | 5 +++ src/pim/Token.cpp | 1 + src/pim/Token.h | 1 + 9 files changed, 65 insertions(+), 55 deletions(-) diff --git a/src/pim/Generator.cpp b/src/pim/Generator.cpp index b2ea84cb2..8228c1c2a 100644 --- a/src/pim/Generator.cpp +++ b/src/pim/Generator.cpp @@ -257,6 +257,17 @@ namespace pim writeOpcode(Opcode::Kill); } + void Generator::LoadProperty(std::string property) + { + writeOpcode(Opcode::LoadProperty); + writeConstant(0); + } + + void Generator::StoreProperty(std::string property) + { + writeOpcode(Opcode::StoreProperty); + writeConstant(0); + } void Generator::IntegerToDecimal() { diff --git a/src/pim/Generator.h b/src/pim/Generator.h index 0121c1369..028733921 100644 --- a/src/pim/Generator.h +++ b/src/pim/Generator.h @@ -36,10 +36,13 @@ namespace pim } ~SymbolNotFoundException() throw() {}; }; + class Type + { + enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol }; + }; class Definition { public: - enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol }; std::string Name; int Type; int StackPosition; @@ -145,6 +148,8 @@ namespace pim void GetParticle(); void GetPosition(); void KillParticle(); + void LoadProperty(std::string property); + void StoreProperty(std::string property); void IntegerToDecimal(); void DecimalToInteger(); diff --git a/src/pim/Machine.cpp b/src/pim/Machine.cpp index 4c07514b0..a112363de 100644 --- a/src/pim/Machine.cpp +++ b/src/pim/Machine.cpp @@ -105,6 +105,8 @@ namespace pim case Opcode::Jump: case Opcode::Return: case Opcode::LocalEnter: + case Opcode::LoadProperty: + case Opcode::StoreProperty: return 4; case Opcode::Discard: case Opcode::Duplicate: @@ -214,6 +216,13 @@ namespace pim sim->kill_part(PSPop().Integer); PSPush((Word)0); break; + case Opcode::LoadProperty: + PSPush(PPROP(PSPop().Integer, argument.Integer)); + break; + case Opcode::StoreProperty: + temp1 = PSPop(); + PPROP(temp1.Integer, argument.Integer) = PSPop(); + break; case Opcode::JumpEqual: if(PSPop().Integer == PSPop().Integer) programCounter = argument.Integer-1; diff --git a/src/pim/Machine.h b/src/pim/Machine.h index dcaef92e0..a40dd51dc 100644 --- a/src/pim/Machine.h +++ b/src/pim/Machine.h @@ -42,6 +42,7 @@ namespace pim #define CSA(argument) (*((Word*)&ram[framePointer-argument])) #define CS() (*((Word*)&ram[callStack])) #define PS() (*((Word*)&ram[programStack])) + #define PPROP(index, property) (*((Word*)(&sim->parts[(index)]+property))) int programStack; //Points to the item on top of the Program Stack int callStack; //Points to the item on top of the call stack diff --git a/src/pim/Opcodes.inl b/src/pim/Opcodes.inl index 3c18da50d..4b2829425 100644 --- a/src/pim/Opcodes.inl +++ b/src/pim/Opcodes.inl @@ -15,6 +15,8 @@ OPDEF(Transform) OPDEF(Get) OPDEF(Position) OPDEF(Kill) +OPDEF(LoadProperty) +OPDEF(StoreProperty) OPDEF(JumpEqual) OPDEF(JumpNotEqual) OPDEF(JumpGreater) diff --git a/src/pim/Parser.cpp b/src/pim/Parser.cpp index 75af21dcb..378a25246 100644 --- a/src/pim/Parser.cpp +++ b/src/pim/Parser.cpp @@ -438,17 +438,26 @@ namespace pim } /* - ::= identifier = + ::= identifier = | identifier.property = */ void Parser::assigmentStatement() { std::string variable = token.Source; - //generator->PushVariableAddress(token.Source); expect(Token::Identifier); - expect(Token::AssignSymbol); - expression(); - //generator->Store(); - generator->StoreVariable(variable); + if(accept(Token::AssignSymbol)) + { + expression(); + generator->StoreVariable(variable); + } + else if(accept(Token::DotSymbol)) + { + std::string property = token.Source; + expect(Token::Identifier); + expect(Token::AssignSymbol); + expression(); + generator->LoadVariable(variable); + generator->StoreProperty(property); + } } /* @@ -533,56 +542,10 @@ namespace pim } if(doNegate) generator->Negate(); - /*if(!accept(Token::Identifier)) - { - if(!accept(Token::IntegerConstant)) - { - if(!accept(Token::DecimalConstant)) - { - if(!accept(Token::LeftBracket)) - { - throw ParserExpectException(token, "identifier or constant"); - } - else - { - expression(); - expect(Token::RightBracket); - } - } - else - { - if(doNegate) - { - doNegate = false; - generator->Constant("-" + factor); - } - else - generator->Constant(factor); - } - } - else - { - if(doNegate) - { - doNegate = false; - generator->Constant("-" + factor); - } - else - generator->Constant(factor); - } - } - else - { - generator->LoadVariable(factor); - } - if(doNegate) - { - generator->Negate(); - }*/ } /* - ::= | identifier | + ::= | identifier | identifier.property | */ void Parser::variableValue() { @@ -596,7 +559,17 @@ namespace pim } else { - generator->LoadVariable(variable); + if(accept(Token::DotSymbol)) + { + std::string property = token.Source; + expect(Token::Identifier); + generator->LoadVariable(variable); + generator->LoadProperty(property); + } + else + { + generator->LoadVariable(variable); + } } } else @@ -618,8 +591,10 @@ namespace pim } else token = scanner->NextToken(); + std::cout << "Symbol " << Token::SymbolNames[symbol] << " " << lastToken.Source << std::endl; return true; } + std::cout << "Bad Symbol " << Token::SymbolNames[symbol] << " " << token.Source << " (" << token.GetName() << ")" << std::endl; return false; } diff --git a/src/pim/Scanner.cpp b/src/pim/Scanner.cpp index 32e64d0a0..7aba994e6 100644 --- a/src/pim/Scanner.cpp +++ b/src/pim/Scanner.cpp @@ -160,6 +160,11 @@ namespace pim nextCharacter(); return Token(Token::CommaSymbol, ",", cLine); } + else if(cChar == '.') + { + nextCharacter(); + return Token(Token::DotSymbol, ".", cLine); + } else { nextCharacter(); diff --git a/src/pim/Token.cpp b/src/pim/Token.cpp index 2db451ea9..1741af0ec 100644 --- a/src/pim/Token.cpp +++ b/src/pim/Token.cpp @@ -41,6 +41,7 @@ namespace pim "get", "IDENTIFIER", ",", + ".", "INVALID SYMBOL" }; } diff --git a/src/pim/Token.h b/src/pim/Token.h index 6a80f49f4..42e0bc277 100644 --- a/src/pim/Token.h +++ b/src/pim/Token.h @@ -56,6 +56,7 @@ namespace pim Identifier, CommaSymbol, + DotSymbol, InvalidSymbol,