X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=7dcb41e969c0de829f87ef56c67896e0c0dfc82a;hb=cc3d698b8c1ad09d7a3f9e3f28bc84e0ac1735ea;hp=c2ce0d4145abf20ee1b90c291ad7965d2e5446aa;hpb=77915e0186f4fc0788054eb34651c726b80d981c;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index c2ce0d4..7dcb41e 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -1,10 +1,3 @@ -/* - * Interpreter.cpp - * - * Created on: Aug 26, 2012 - * Author: holy - */ - #include "Interpreter.h" #include "ParsedSource.h" @@ -62,8 +55,16 @@ Interpreter::~Interpreter() { } -const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const { - return parsedDefinitions.at(identifier); +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) { + std::map::const_iterator i(parsedDefinitions.find(identifier)); + if (i != parsedDefinitions.end()) { + return i->second; + } else if (source.IsDefined(identifier)) { + ReadDefinition(source.GetDefinition(identifier)); + return parsedDefinitions.at(identifier); + } else { + throw Error("access to undefined object " + identifier); + } } @@ -114,13 +115,13 @@ void Interpreter::ReadLiteral(const Definition &dfn) { || dfn.GetLiteral()->GetType() == Literal::STRING) ? dfn.GetLiteral()->GetString().size() : td.Size()); char *object(alloc.Alloc(size)); + values[typeId].push_back(object); + parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); if (dfn.GetLiteral()->GetType() == Literal::OBJECT) { ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties()); } else { ReadLiteral(typeId, id, object, *dfn.GetLiteral()); } - values[typeId].push_back(object); - parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); } void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &literal) { @@ -221,8 +222,9 @@ void *Interpreter::GetObject(int typeId, const Value &v) { case Literal::SCRIPT: { typeId = TypeDescription::GetTypeId("Script"); + char *script(ReadScript(v.GetLiteral().GetScript())); id = values[typeId].size(); - values[typeId].push_back(ReadScript(v.GetLiteral().GetScript())); + values[typeId].push_back(script); } break; case Literal::STRING: @@ -271,8 +273,8 @@ void Interpreter::ReadObject(const Definition &dfn) { char *object(alloc.Alloc(td.Size())); td.Construct(object); values[typeId].push_back(object); - ReadObject(typeId, id, object, *dfn.GetProperties()); parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, typeId, id))); + ReadObject(typeId, id, object, *dfn.GetProperties()); } @@ -334,12 +336,19 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis void Interpreter::ReadScript(const std::vector &s, Script *script) { + std::map labels; int size(0); for (vector::const_iterator i(s.begin()), end(s.end()); i != end; ++i) { - if ((*i)->GetType() != ScriptToken::COMMAND) { + if ((*i)->GetType() == ScriptToken::LABEL) { + if (labels.count((*i)->Label())) { + throw Error("duplicate label " + (*i)->Label()); + } else { + labels[(*i)->Label()] = size; + } + } else if ((*i)->GetType() != ScriptToken::COMMAND) { throw Error("unexpected script token"); } - ++size; + size += sizeof(Script::Code); const string &cmd((*i)->CommandName()); if (cmd == "move") { ++i; @@ -347,62 +356,126 @@ void Interpreter::ReadScript(const std::vector &s, Script *script throw Error("unexpected script end after move"); } const string ®((*i)->RegisterName()); - switch (reg[0]) { - case 'a': - size += sizeof(void *); - break; - case 'i': - size += sizeof(int); - break; - case 'v': - size += sizeof(Vector); - break; - default: - throw Error("unknown register " + reg); + if (reg.size() != 2) { + throw Error("invalid register name " + reg); } ++i; if (i == end) { throw Error("unexpected script end after move"); } + if ((*i)->GetType() != ScriptToken::REGISTER) { + switch (reg[0]) { + case 'a': + size += sizeof(void *); + break; + case 'i': + size += sizeof(int); + break; + case 'v': + size += sizeof(Vector); + break; + default: + throw Error("unknown register " + reg); + } + } } else if (cmd == "add") { ++i; if (i == end) { throw Error("unexpected script end after add"); } const string ®((*i)->RegisterName()); - switch (reg[0]) { - case 'i': - size += sizeof(int); - break; - case 'v': - size += sizeof(Vector); - break; - default: - throw Error("expected register after add " + reg); + if (reg.size() != 2) { + throw Error("invalid register name " + reg); } ++i; if (i == end) { throw Error("unexpected script end after add"); } + if ((*i)->GetType() != ScriptToken::REGISTER) { + switch (reg[0]) { + case 'i': + size += sizeof(int); + break; + case 'v': + size += sizeof(Vector); + break; + default: + throw Error("expected register after add " + reg); + } + } } else if (cmd == "mod") { ++i; if (i == end) { throw Error("unexpected script end after mod"); } - size += sizeof(int); ++i; if (i == end) { throw Error("unexpected script end after mod"); } + if ((*i)->GetType() != ScriptToken::REGISTER) { + size += sizeof(int); + } } else if (cmd == "rand") { ++i; if (i == end) { throw Error("unexpected script end after rand"); } + } else if (cmd == "cmp") { + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + if ((*i)->GetType() != ScriptToken::REGISTER) { + size += sizeof(int); + } + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + if ((*i)->GetType() != ScriptToken::REGISTER) { + size += sizeof(int); + } + } else if (cmd == "jmp") { size += sizeof(int); ++i; if (i == end) { - throw Error("unexpected script end after rand"); + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jeq") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jne") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jl") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jle") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jg") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); + } + } else if (cmd == "jge") { + size += sizeof(int); + ++i; + if (i == end) { + throw Error("unexpected script end after cmp"); } } else if (cmd == "sysc") { @@ -411,109 +484,268 @@ void Interpreter::ReadScript(const std::vector &s, Script *script } } - unsigned char *text(reinterpret_cast(alloc.Alloc(size))); + char *text(alloc.Alloc(size)); int cursor(0); for (vector::const_iterator i(s.begin()), end(s.end()); i != end; ++i) { + if ((*i)->GetType() == ScriptToken::LABEL) { + continue; + } if ((*i)->GetType() != ScriptToken::COMMAND) { throw Error("unexpected script token"); } const string &cmd((*i)->CommandName()); if (cmd == "move") { + Script::Code &code(CreateScriptCode(Script::COMMAND_MOVE, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 2; ++i; const string ®((*i)->RegisterName()); + switch (reg[0]) { + case 'a': + code.type = Script::TYPE_ADDRESS; + break; + case 'i': + code.type = Script::TYPE_INTEGER; + break; + case 'v': + code.type = Script::TYPE_VECTOR; + break; + default: + throw Error("invalid register " + reg); + } + int regnum(reg[1] - '0'); + if (regnum < 0 || regnum > 6) { + throw Error("invalid register " + reg); + } + code.reg1 = regnum; ++i; - if (reg == "a0") { - text[cursor] = Script::CODE_MOVE_A0; - ++cursor; - ReadScriptAddress(**i, text + cursor); - cursor += sizeof(void *); - } else if (reg == "a1") { - text[cursor] = Script::CODE_MOVE_A1; - ++cursor; - ReadScriptAddress(**i, text + cursor); - cursor += sizeof(void *); - } else if (reg == "i0") { - text[cursor] = Script::CODE_MOVE_I0; - ++cursor; - ReadScriptInteger(**i, text + cursor); - cursor += sizeof(int); - } else if (reg == "i1") { - text[cursor] = Script::CODE_MOVE_I1; - ++cursor; - ReadScriptInteger(**i, text + cursor); - cursor += sizeof(int); - } else if (reg == "v0") { - text[cursor] = Script::CODE_MOVE_V0; - ++cursor; - ReadScriptVector(**i, text + cursor); - cursor += sizeof(Vector); - } else if (reg == "v1") { - text[cursor] = Script::CODE_MOVE_V1; - ++cursor; - ReadScriptVector(**i, text + cursor); - cursor += sizeof(Vector); + + if ((*i)->GetType() == ScriptToken::REGISTER) { + string reg2((*i)->RegisterName()); + if (reg[0] != reg2[0]) { + throw Error("mixed-type commands not allowed"); + } + int reg2num(reg[1] - '0'); + if (reg2num < 0 || reg2num > 6) { + throw Error("invalid register " + reg2); + } + code.reg2 = reg2num; } else { - throw Error("unknown register " + reg); + code.reg2 = 7; + switch (code.type) { + case Script::TYPE_ADDRESS: + ReadScriptAddress(**i, text + cursor); + cursor += sizeof(void *); + break; + case Script::TYPE_INTEGER: + ReadScriptInteger(**i, text + cursor); + cursor += sizeof(int); + break; + case Script::TYPE_VECTOR: + ReadScriptVector(**i, text + cursor); + cursor += sizeof(Vector); + break; + } } } else if (cmd == "add") { + Script::Code &code(CreateScriptCode(Script::COMMAND_ADD, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 2; ++i; const string ®((*i)->RegisterName()); + switch (reg[0]) { + case 'i': + code.type = Script::TYPE_INTEGER; + break; + case 'v': + code.type = Script::TYPE_VECTOR; + break; + default: + throw Error("invalid register " + reg); + } + int regnum(reg[1] - '0'); + if (regnum < 0 || regnum > 6) { + throw Error("invalid register " + reg); + } + code.reg1 = regnum; ++i; - if (reg == "i0") { - text[cursor] = Script::CODE_ADD_I0; - ++cursor; - ReadScriptInteger(**i, text + cursor); - cursor += sizeof(int); - } else if (reg == "i1") { - text[cursor] = Script::CODE_ADD_I1; - ++cursor; - ReadScriptInteger(**i, text + cursor); - cursor += sizeof(int); - } else if (reg == "v0") { - text[cursor] = Script::CODE_ADD_V0; - ++cursor; - ReadScriptVector(**i, text + cursor); - cursor += sizeof(Vector); - } else if (reg == "v1") { - text[cursor] = Script::CODE_ADD_V1; - ++cursor; - ReadScriptVector(**i, text + cursor); - cursor += sizeof(Vector); + + if ((*i)->GetType() == ScriptToken::REGISTER) { + string reg2((*i)->RegisterName()); + if (reg[0] != reg2[0]) { + throw Error("mixed-type commands not allowed"); + } + int reg2num(reg[1] - '0'); + if (reg2num < 0 || reg2num > 6) { + throw Error("invalid register name " + reg2); + } + code.reg2 = reg2num; } else { - throw Error("unexpected register " + reg); + code.reg2 = 7; + switch (code.type) { + case Script::TYPE_INTEGER: + ReadScriptInteger(**i, text + cursor); + cursor += sizeof(int); + break; + case Script::TYPE_VECTOR: + ReadScriptVector(**i, text + cursor); + cursor += sizeof(Vector); + break; + } } } else if (cmd == "mod") { + Script::Code &code(CreateScriptCode(Script::COMMAND_MOD, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 2; ++i; const string ®((*i)->RegisterName()); + if (reg[0] != 'i') { + throw Error("invalid register " + reg); + } + code.type = Script::TYPE_INTEGER; + int regnum(reg[1] - '0'); + if (regnum < 0 || regnum > 6) { + throw Error("invalid register " + reg); + } + code.reg1 = regnum; ++i; - if (reg == "i0") { - text[cursor] = Script::CODE_MOD_I0; - ++cursor; - ReadScriptInteger(**i, text + cursor); - cursor += sizeof(int); - } else if (reg == "i1") { - text[cursor] = Script::CODE_MOD_I1; - ++cursor; + + if ((*i)->GetType() == ScriptToken::REGISTER) { + string reg2((*i)->RegisterName()); + if (reg[0] != reg2[0]) { + throw Error("mixed-type commands not allowed"); + } + int reg2num(reg[1] - '0'); + if (reg2num < 0 || reg2num > 6) { + throw Error("invalid register name " + reg2); + } + code.reg2 = reg2num; + } else { + code.reg2 = 7; ReadScriptInteger(**i, text + cursor); cursor += sizeof(int); - } else { - throw Error("unexpected register " + reg); } } else if (cmd == "rand") { + Script::Code &code(CreateScriptCode(Script::COMMAND_RAND, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + const string ®((*i)->RegisterName()); + if (reg[0] != 'i') { + throw Error("invalid register " + reg); + } + code.type = Script::TYPE_INTEGER; + int regnum(reg[1] - '0'); + if (regnum < 0 || regnum > 6) { + throw Error("invalid register " + reg); + } + code.reg1 = regnum; + } else if (cmd == "cmp") { + Script::Code &code(CreateScriptCode(Script::COMMAND_CMP, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 2; ++i; const string ®((*i)->RegisterName()); - if (reg == "i0") { - text[cursor] = Script::CODE_RAND_I0; - ++cursor; - } else if (reg == "i1") { - text[cursor] = Script::CODE_RAND_I1; - ++cursor; + if (reg[0] != 'i') { + throw Error("invalid register " + reg); + } + code.type = Script::TYPE_INTEGER; + int regnum(reg[1] - '0'); + if (regnum < 0 || regnum > 6) { + throw Error("invalid register " + reg); + } + code.reg1 = regnum; + ++i; + + if ((*i)->GetType() == ScriptToken::REGISTER) { + string reg2((*i)->RegisterName()); + if (reg[0] != reg2[0]) { + throw Error("mixed-type commands not allowed"); + } + int reg2num(reg[1] - '0'); + if (reg2num < 0 || reg2num > 6) { + throw Error("invalid register name " + reg2); + } + code.reg2 = reg2num; } else { - throw Error("unexpected register " + reg); + code.reg2 = 7; + ReadScriptInteger(**i, text + cursor); + cursor += sizeof(int); } + } else if (cmd == "jmp") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JMP, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jeq") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JEQ, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jne") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JNE, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jl") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JL, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jle") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JLE, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jg") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JG, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); + } else if (cmd == "jge") { + Script::Code &code(CreateScriptCode(Script::COMMAND_JGE, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 1; + ++i; + if (!labels.count((*i)->Identifier())) { + throw Error("use of undefined label " + (*i)->Identifier()); + } + *reinterpret_cast(text + cursor) = labels[(*i)->Identifier()]; + cursor += sizeof(int); } else if (cmd == "sysc") { - text[cursor] = Script::CODE_SYSCALL; - ++cursor; + Script::Code &code(CreateScriptCode(Script::COMMAND_SYSC, text + cursor)); + cursor += sizeof(Script::Code); + code.numParams = 0; } else { throw Error("unknown command " + cmd); } @@ -531,23 +763,33 @@ char *Interpreter::ReadScript(const vector &s) { return mem; } -void Interpreter::ReadScriptAddress(const ScriptToken &t, unsigned char *dest) { +Script::Code &Interpreter::CreateScriptCode(Script::Command c, char *dest) { + Script::Code &code(*reinterpret_cast(dest)); + code.command = c; + code.numParams = 0; + code.type = Script::TYPE_NONE; + code.reg1 = 7; + code.reg2 = 7; + return code; +} + +void Interpreter::ReadScriptAddress(const ScriptToken &t, char *dest) { if (t.GetType() != ScriptToken::IDENTIFIER) { throw Error("expected identifier for address"); } - if (source.IsDefined(t.GetIdentifier())) { - const ParsedDefinition &def(GetDefinition(t.GetIdentifier())); - void *addr(GetObject(def.type, t.GetIdentifier())); + if (source.IsDefined(t.Identifier())) { + const ParsedDefinition &def(GetDefinition(t.Identifier())); + void *addr(GetObject(def.type, t.Identifier())); *reinterpret_cast(dest) = addr; } else { throw Error("postponing values in scripts not implemented"); } } -void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) { +void Interpreter::ReadScriptInteger(const ScriptToken &t, char *dest) { if (t.GetType() == ScriptToken::IDENTIFIER) { - if (source.IsDefined(t.GetIdentifier())) { - void *num(GetObject(TypeDescription::GetTypeId("Number"), t.GetIdentifier())); + if (source.IsDefined(t.Identifier())) { + void *num(GetObject(TypeDescription::GetTypeId("Number"), t.Identifier())); *reinterpret_cast(dest) = *reinterpret_cast(num); } else { throw Error("postponing values in scripts not implemented"); @@ -559,10 +801,10 @@ void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) { } } -void Interpreter::ReadScriptVector(const ScriptToken &t, unsigned char *dest) { +void Interpreter::ReadScriptVector(const ScriptToken &t, char *dest) { if (t.GetType() == ScriptToken::IDENTIFIER) { - if (source.IsDefined(t.GetIdentifier())) { - void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.GetIdentifier())); + if (source.IsDefined(t.Identifier())) { + void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.Identifier())); *reinterpret_cast *>(dest) = *reinterpret_cast *>(vec); } else { throw Error("postponing values in scripts not implemented"); @@ -601,12 +843,12 @@ void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::s void Interpreter::CreateTypeDescriptions() { { - TypeDescription &td(TypeDescription::CreateOrGet("Boolean")); + TypeDescription &td(TypeDescription::Create(BOOLEAN_ID, "Boolean")); td.SetDescription("Logical value which can be either true or false."); td.SetSize(sizeof(bool)); } { - TypeDescription &td(TypeDescription::CreateOrGet("Color")); + TypeDescription &td(TypeDescription::Create(COLOR_ID, "Color")); td.SetDescription( "A color in RGB format with an optional alpha channel.\n" "Components range from 0 to 255.\n" @@ -614,34 +856,33 @@ void Interpreter::CreateTypeDescriptions() { td.SetSize(sizeof(Color)); } { - TypeDescription &td(TypeDescription::CreateOrGet("Image")); + TypeDescription &td(TypeDescription::Create(IMAGE_ID, "Image")); td.SetDescription("Path to a PNG file with image data."); td.SetSize(sizeof(SDL_Surface)); } { - TypeDescription &td(TypeDescription::CreateOrGet("Number")); + TypeDescription &td(TypeDescription::Create(NUMBER_ID, "Number")); td.SetDescription("A signed integer."); td.SetSize(sizeof(int)); } - { - int stringId(TypeDescription::GetTypeId("String")); - TypeDescription &td(TypeDescription::CreateOrGet("Path")); + {; + TypeDescription &td(TypeDescription::Create(PATH_ID, "Path")); td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location."); td.SetSize(1); - td.AddSupertype(stringId, 0); + td.AddSupertype(STRING_ID, 0); } { - TypeDescription &td(TypeDescription::CreateOrGet("Script")); + TypeDescription &td(TypeDescription::Create(SCRIPT_ID, "Script")); td.SetDescription("Collection of commands that define a behaviour."); td.SetSize(sizeof(Script)); } { - TypeDescription &td(TypeDescription::CreateOrGet("String")); + TypeDescription &td(TypeDescription::Create(STRING_ID, "String")); td.SetDescription("Some characters."); td.SetSize(1); } { - TypeDescription &td(TypeDescription::CreateOrGet("Vector")); + TypeDescription &td(TypeDescription::Create(VECTOR_ID, "Vector")); td.SetDescription("A pair of numbers usually describing a 2D translation or offset."); td.SetSize(sizeof(Vector)); }