Keep track of variable types
This commit is contained in:
parent
ca7c0d777b
commit
004710fcea
@ -958,8 +958,8 @@ int LuaScriptInterface::updateVM(UPDATE_FUNC_ARGS)
|
||||
machine->CSPush(i);
|
||||
machine->CSPush(x);
|
||||
machine->CSPush(y);
|
||||
//machine->Call(0);
|
||||
machine->CallCompiled(0);
|
||||
machine->Call(0);
|
||||
//machine->CallCompiled(0);
|
||||
|
||||
|
||||
/*vm::VirtualMachine * vMachine = updateVirtualMachines[parts[i].type];
|
||||
|
@ -274,11 +274,30 @@ namespace pim
|
||||
variableType = type;
|
||||
}
|
||||
|
||||
void Generator::PushType(int type)
|
||||
{
|
||||
typeStack.push(type);
|
||||
}
|
||||
|
||||
void Generator::pushType(int type)
|
||||
{
|
||||
typeStack.push(type);
|
||||
}
|
||||
|
||||
void Generator::popType(int count)
|
||||
{
|
||||
while(count--)
|
||||
{
|
||||
typeStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void Generator::ScopeVariable(std::string label)
|
||||
{
|
||||
currentScope->Definitions.push_back(Definition(label, variableType, currentScope->FrameSize));
|
||||
currentScope->Definitions.push_back(Definition(label, typeStack.top(), currentScope->FrameSize));
|
||||
currentScope->FrameSize += 4;
|
||||
currentScope->LocalFrameSize += 4;
|
||||
typeStack.pop();
|
||||
|
||||
output << "#declare " << label << " " << currentScope->FrameSize-4 << std::endl;
|
||||
}
|
||||
@ -290,22 +309,42 @@ namespace pim
|
||||
|
||||
void Generator::LoadVariable(std::string label)
|
||||
{
|
||||
Definition d = currentScope->GetDefinition(label);
|
||||
|
||||
pushType(d.Type);
|
||||
writeOpcode(Opcode::Load);
|
||||
writeConstant(currentScope->GetDefinition(label).StackPosition);
|
||||
writeConstant(d.StackPosition);
|
||||
|
||||
output << "load " << label << std::endl;
|
||||
}
|
||||
|
||||
void Generator::StoreVariable(std::string label)
|
||||
{
|
||||
Definition d = currentScope->GetDefinition(label);
|
||||
int otherType = typeStack.top();
|
||||
|
||||
if(otherType != d.Type)
|
||||
{
|
||||
if(otherType == DataType::Integer && d.Type == DataType::Float)
|
||||
{
|
||||
ToFloat();
|
||||
}
|
||||
else if(otherType == DataType::Float && d.Type == DataType::Integer)
|
||||
{
|
||||
ToInteger();
|
||||
}
|
||||
}
|
||||
popType(2);
|
||||
|
||||
writeOpcode(Opcode::Store);
|
||||
writeConstant(currentScope->GetDefinition(label).StackPosition);
|
||||
writeConstant(d.StackPosition);
|
||||
|
||||
output << "store " << label << std::endl;
|
||||
}
|
||||
|
||||
void Generator::RTConstant(std::string name)
|
||||
{
|
||||
pushType(Type::Integer);
|
||||
writeOpcode(Opcode::Constant);
|
||||
writeConstantMacroPlaceholder(name);
|
||||
|
||||
@ -314,6 +353,7 @@ namespace pim
|
||||
|
||||
void Generator::Constant(std::string constant)
|
||||
{
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Constant);
|
||||
writeConstant(constant);
|
||||
|
||||
@ -331,6 +371,7 @@ namespace pim
|
||||
|
||||
void Generator::Discard()
|
||||
{
|
||||
popType(1);
|
||||
writeOpcode(Opcode::Discard);
|
||||
|
||||
output << "discard" << std::endl;
|
||||
@ -338,6 +379,7 @@ namespace pim
|
||||
|
||||
void Generator::Duplicate()
|
||||
{
|
||||
pushType(typeStack.top());
|
||||
writeOpcode(Opcode::Duplicate);
|
||||
|
||||
output << "duplicate" << std::endl;
|
||||
@ -345,6 +387,8 @@ namespace pim
|
||||
|
||||
void Generator::Add()
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Add);
|
||||
|
||||
output << "add" << std::endl;
|
||||
@ -352,6 +396,8 @@ namespace pim
|
||||
|
||||
void Generator::Subtract()
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Subtract);
|
||||
|
||||
output << "sub" << std::endl;
|
||||
@ -359,13 +405,32 @@ namespace pim
|
||||
|
||||
void Generator::Multiply()
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Multiply);
|
||||
|
||||
|
||||
output << "mul" << std::endl;
|
||||
}
|
||||
|
||||
void Generator::ToInteger()
|
||||
{
|
||||
popType(1);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(OpCode::ToInteger);
|
||||
}
|
||||
|
||||
void Generator::ToFloat()
|
||||
{
|
||||
popType(1);
|
||||
pushType(DataType::Float);
|
||||
writeOpcode(OpCode::ToFloat);
|
||||
}
|
||||
|
||||
void Generator::Divide()
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Divide);
|
||||
|
||||
output << "div" << std::endl;
|
||||
@ -374,6 +439,8 @@ namespace pim
|
||||
void Generator::Modulus()
|
||||
{
|
||||
writeOpcode(Opcode::Modulus);
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
|
||||
output << "add" << std::endl;
|
||||
}
|
||||
@ -401,6 +468,8 @@ namespace pim
|
||||
|
||||
void Generator::GetParticle()
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Get);
|
||||
|
||||
output << "getpart" << std::endl;
|
||||
@ -408,6 +477,9 @@ namespace pim
|
||||
|
||||
void Generator::GetPosition()
|
||||
{
|
||||
popType(1);
|
||||
pushType(DataType::Integer);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::Position);
|
||||
|
||||
output << "getpos" << std::endl;
|
||||
@ -415,6 +487,7 @@ namespace pim
|
||||
|
||||
void Generator::KillParticle()
|
||||
{
|
||||
popType(1);
|
||||
writeOpcode(Opcode::Kill);
|
||||
|
||||
output << "kill" << std::endl;
|
||||
@ -422,6 +495,8 @@ namespace pim
|
||||
|
||||
void Generator::LoadProperty(std::string property)
|
||||
{
|
||||
popType(2);
|
||||
pushType(DataType::Integer);
|
||||
writeOpcode(Opcode::LoadProperty);
|
||||
writeConstantPropertyPlaceholder(property);
|
||||
|
||||
@ -430,6 +505,7 @@ namespace pim
|
||||
|
||||
void Generator::StoreProperty(std::string property)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::StoreProperty);
|
||||
writeConstantPropertyPlaceholder(property);
|
||||
|
||||
@ -449,6 +525,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpEqual(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpEqual);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -457,6 +534,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpNotEqual(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpNotEqual);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -465,6 +543,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpGreater(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpGreater);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -473,6 +552,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpGreaterEqual(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpGreaterEqual);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -481,6 +561,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpLess(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpLess);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -489,6 +570,7 @@ namespace pim
|
||||
|
||||
void Generator::JumpLessEqual(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::JumpLessEqual);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
@ -497,6 +579,7 @@ namespace pim
|
||||
|
||||
void Generator::Jump(std::string label)
|
||||
{
|
||||
popType(2);
|
||||
writeOpcode(Opcode::Jump);
|
||||
writeConstantPlaceholder(label);
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <stack>
|
||||
#include <iostream>
|
||||
#include "Token.h"
|
||||
#include "Types.h"
|
||||
namespace pim
|
||||
{
|
||||
namespace compiler
|
||||
@ -36,10 +37,6 @@ namespace pim
|
||||
}
|
||||
~SymbolNotFoundException() throw() {};
|
||||
};
|
||||
class Type
|
||||
{
|
||||
enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol };
|
||||
};
|
||||
class Definition
|
||||
{
|
||||
public:
|
||||
@ -88,6 +85,7 @@ namespace pim
|
||||
class Generator
|
||||
{
|
||||
int variableType;
|
||||
std::stack<int> typeStack;
|
||||
std::stack<Scope*> scopes;
|
||||
Scope * currentScope;
|
||||
std::ostream & output;
|
||||
@ -110,6 +108,9 @@ namespace pim
|
||||
|
||||
std::vector<unsigned char> program;
|
||||
|
||||
void pushType(int type);
|
||||
void popType(int count);
|
||||
|
||||
void defineLabel(std::string label);
|
||||
void writeOpcode(int opcode);
|
||||
void writeConstant(std::string constant);
|
||||
@ -135,6 +136,7 @@ namespace pim
|
||||
void ScopeVariableType(int type);
|
||||
void ScopeVariable(std::string label);
|
||||
|
||||
void PushType(int type);
|
||||
void PushVariableAddress(std::string label);
|
||||
// void Store();
|
||||
void LoadVariable(std::string label);
|
||||
@ -151,6 +153,8 @@ namespace pim
|
||||
void Divide();
|
||||
void Modulus();
|
||||
void Negate();
|
||||
void ToInteger();
|
||||
void ToFloat();
|
||||
|
||||
void TransformParticle();
|
||||
void CreateParticle();
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <fstream>
|
||||
#include "Machine.h"
|
||||
#include "Opcodes.h"
|
||||
#include "Types.h"
|
||||
#include "simulation/Simulation.h"
|
||||
#include "X86Native.h"
|
||||
namespace pim
|
||||
|
@ -2,6 +2,7 @@ namespace pim
|
||||
{
|
||||
struct Opcode
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
#define OPDEF(name) name,
|
||||
|
@ -1,4 +1,6 @@
|
||||
OPDEF(Load)
|
||||
OPDEF(ToFloat)
|
||||
OPDEF(ToInteger)
|
||||
OPDEF(Store)
|
||||
OPDEF(Constant)
|
||||
OPDEF(Increment)
|
||||
|
@ -1,6 +1,7 @@
|
||||
//Syntax analyser
|
||||
#include "Parser.h"
|
||||
#include "Format.h"
|
||||
#include "Types.h"
|
||||
namespace pim
|
||||
{
|
||||
namespace compiler
|
||||
@ -106,7 +107,18 @@ namespace pim
|
||||
*/
|
||||
void Parser::argument()
|
||||
{
|
||||
generator->ScopeVariableType(token.Symbol);
|
||||
int type;
|
||||
switch(token.Symbol)
|
||||
{
|
||||
case Token::DecimalConstant:
|
||||
type = DataType::Float;
|
||||
break;
|
||||
case Token::IntegerConstant:
|
||||
case Token::ParticleConstant:
|
||||
type = DataType::Integer;
|
||||
break;
|
||||
}
|
||||
generator->ScopeVariableType(type);
|
||||
if(!accept(Token::IntegerSymbol))
|
||||
if(!accept(Token::DecimalSymbol))
|
||||
if(!accept(Token::ParticleSymbol))
|
||||
@ -130,7 +142,18 @@ namespace pim
|
||||
*/
|
||||
void Parser::declaration()
|
||||
{
|
||||
generator->ScopeVariableType(token.Symbol);
|
||||
int type;
|
||||
switch(token.Symbol)
|
||||
{
|
||||
case Token::DecimalConstant:
|
||||
type = DataType::Float;
|
||||
break;
|
||||
case Token::IntegerConstant:
|
||||
case Token::ParticleConstant:
|
||||
type = DataType::Integer;
|
||||
break;
|
||||
}
|
||||
generator->ScopeVariableType(type);
|
||||
if(!accept(Token::IntegerSymbol))
|
||||
if(!accept(Token::DecimalSymbol))
|
||||
if(!accept(Token::ParticleSymbol))
|
||||
@ -296,7 +319,7 @@ namespace pim
|
||||
generator->PushLocalScope(loopLabel+"Start");
|
||||
neighbourVariable = token.Source;
|
||||
expect(Token::Identifier);
|
||||
generator->ScopeVariableType(Token::IntegerConstant);
|
||||
generator->ScopeVariableType(DataType::Integer);
|
||||
generator->ScopeVariable(neighbourVariable);
|
||||
generator->ScopeVariable(xVar);
|
||||
generator->ScopeVariable(yVar);
|
||||
|
13
src/pim/Types.h
Normal file
13
src/pim/Types.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
namespace pim
|
||||
{
|
||||
struct DataType
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
Integer,
|
||||
Float
|
||||
};
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user