Set properties with integer.property

This commit is contained in:
Simon Robertshaw 2012-09-21 21:21:03 +01:00
parent 939a04d3c7
commit 644c130712
9 changed files with 65 additions and 55 deletions

View File

@ -257,6 +257,17 @@ namespace pim
writeOpcode(Opcode::Kill); 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() void Generator::IntegerToDecimal()
{ {

View File

@ -36,10 +36,13 @@ namespace pim
} }
~SymbolNotFoundException() throw() {}; ~SymbolNotFoundException() throw() {};
}; };
class Type
{
enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol };
};
class Definition class Definition
{ {
public: public:
enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol };
std::string Name; std::string Name;
int Type; int Type;
int StackPosition; int StackPosition;
@ -145,6 +148,8 @@ namespace pim
void GetParticle(); void GetParticle();
void GetPosition(); void GetPosition();
void KillParticle(); void KillParticle();
void LoadProperty(std::string property);
void StoreProperty(std::string property);
void IntegerToDecimal(); void IntegerToDecimal();
void DecimalToInteger(); void DecimalToInteger();

View File

@ -105,6 +105,8 @@ namespace pim
case Opcode::Jump: case Opcode::Jump:
case Opcode::Return: case Opcode::Return:
case Opcode::LocalEnter: case Opcode::LocalEnter:
case Opcode::LoadProperty:
case Opcode::StoreProperty:
return 4; return 4;
case Opcode::Discard: case Opcode::Discard:
case Opcode::Duplicate: case Opcode::Duplicate:
@ -214,6 +216,13 @@ namespace pim
sim->kill_part(PSPop().Integer); sim->kill_part(PSPop().Integer);
PSPush((Word)0); PSPush((Word)0);
break; 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: case Opcode::JumpEqual:
if(PSPop().Integer == PSPop().Integer) if(PSPop().Integer == PSPop().Integer)
programCounter = argument.Integer-1; programCounter = argument.Integer-1;

View File

@ -42,6 +42,7 @@ namespace pim
#define CSA(argument) (*((Word*)&ram[framePointer-argument])) #define CSA(argument) (*((Word*)&ram[framePointer-argument]))
#define CS() (*((Word*)&ram[callStack])) #define CS() (*((Word*)&ram[callStack]))
#define PS() (*((Word*)&ram[programStack])) #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 programStack; //Points to the item on top of the Program Stack
int callStack; //Points to the item on top of the call stack int callStack; //Points to the item on top of the call stack

View File

@ -15,6 +15,8 @@ OPDEF(Transform)
OPDEF(Get) OPDEF(Get)
OPDEF(Position) OPDEF(Position)
OPDEF(Kill) OPDEF(Kill)
OPDEF(LoadProperty)
OPDEF(StoreProperty)
OPDEF(JumpEqual) OPDEF(JumpEqual)
OPDEF(JumpNotEqual) OPDEF(JumpNotEqual)
OPDEF(JumpGreater) OPDEF(JumpGreater)

View File

@ -438,17 +438,26 @@ namespace pim
} }
/* /*
<assigment statement> ::= identifier = <expression> <assigment statement> ::= identifier = <expression> | identifier.property = <expression>
*/ */
void Parser::assigmentStatement() void Parser::assigmentStatement()
{ {
std::string variable = token.Source; std::string variable = token.Source;
//generator->PushVariableAddress(token.Source);
expect(Token::Identifier); expect(Token::Identifier);
expect(Token::AssignSymbol); if(accept(Token::AssignSymbol))
expression(); {
//generator->Store(); expression();
generator->StoreVariable(variable); 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) if(doNegate)
generator->Negate(); 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();
}*/
} }
/* /*
<variable value> ::= <function call> | identifier | <particle action> <variable value> ::= <function call> | identifier | identifier.property | <particle action>
*/ */
void Parser::variableValue() void Parser::variableValue()
{ {
@ -596,7 +559,17 @@ namespace pim
} }
else 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 else
@ -618,8 +591,10 @@ namespace pim
} }
else else
token = scanner->NextToken(); token = scanner->NextToken();
std::cout << "Symbol " << Token::SymbolNames[symbol] << " " << lastToken.Source << std::endl;
return true; return true;
} }
std::cout << "Bad Symbol " << Token::SymbolNames[symbol] << " " << token.Source << " (" << token.GetName() << ")" << std::endl;
return false; return false;
} }

View File

@ -160,6 +160,11 @@ namespace pim
nextCharacter(); nextCharacter();
return Token(Token::CommaSymbol, ",", cLine); return Token(Token::CommaSymbol, ",", cLine);
} }
else if(cChar == '.')
{
nextCharacter();
return Token(Token::DotSymbol, ".", cLine);
}
else else
{ {
nextCharacter(); nextCharacter();

View File

@ -41,6 +41,7 @@ namespace pim
"get", "get",
"IDENTIFIER", "IDENTIFIER",
",", ",",
".",
"INVALID SYMBOL" "INVALID SYMBOL"
}; };
} }

View File

@ -56,6 +56,7 @@ namespace pim
Identifier, Identifier,
CommaSymbol, CommaSymbol,
DotSymbol,
InvalidSymbol, InvalidSymbol,