Basics VM are working.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "constants.hpp"
|
||||
#include "godot_cpp/classes/ref_counted.hpp"
|
||||
|
||||
namespace CoborVM
|
||||
@@ -11,10 +12,28 @@ namespace CoborVM
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
std::vector<ExecutableStep> program;
|
||||
std::vector<int32_t> register_memory;
|
||||
|
||||
public:
|
||||
CoborVirtualMachine() = default;
|
||||
~CoborVirtualMachine() override = default;
|
||||
|
||||
godot::PackedStringArray parse_source_code(godot::String source_code);
|
||||
godot::String set_registers(int size, godot::PackedInt32Array preloaded_values);
|
||||
godot::PackedInt32Array get_registers();
|
||||
int get_program_counter();
|
||||
godot::String run_step();
|
||||
godot::String run_all();
|
||||
|
||||
private:
|
||||
// Execution
|
||||
godot::String run_executable_step(ExecutableStep step);
|
||||
// Parsing
|
||||
bool string_to_instruction(godot::String command_string, Instruction &outInstruction);
|
||||
bool instruction_to_executable_step(Instruction instruction, godot::PackedStringArray line, ExecutableStep &outExecutableStep);
|
||||
bool validate_and_parse_pointer(const godot::String &arg, int &outValue);
|
||||
bool validate_and_parse_literal(const godot::String &arg, int &outValue);
|
||||
};
|
||||
}
|
||||
96
cobor_vm_extension/cobor_vm/include/constants.hpp
Normal file
96
cobor_vm_extension/cobor_vm/include/constants.hpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace CoborVM
|
||||
{
|
||||
enum Instruction
|
||||
{
|
||||
EMPTY,
|
||||
|
||||
SET,
|
||||
CLR,
|
||||
CPY,
|
||||
SWP,
|
||||
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
|
||||
SKIP,
|
||||
JMP,
|
||||
JMPTO,
|
||||
|
||||
EQL,
|
||||
BIGR,
|
||||
SMLR,
|
||||
ZERO,
|
||||
};
|
||||
|
||||
struct ExecutableStep
|
||||
{
|
||||
Instruction instruction = Instruction::EMPTY;
|
||||
int arg1;
|
||||
int arg2;
|
||||
int arg3;
|
||||
|
||||
int lineNumber;
|
||||
};
|
||||
|
||||
// Map from string to instruction
|
||||
const std::unordered_map<std::string, Instruction> stringToInstructionMap = {
|
||||
{"SET", SET},
|
||||
{"CLR", CLR},
|
||||
{"CPY", CPY},
|
||||
{"SWP", SWP},
|
||||
{"ADD", ADD},
|
||||
{"SUB", SUB},
|
||||
{"MUL", MUL},
|
||||
{"DIV", DIV},
|
||||
{"SKIP", SKIP},
|
||||
{"JMP", JMP},
|
||||
{"JMPTO", JMPTO},
|
||||
{"EQL", EQL},
|
||||
{"BIGR", BIGR},
|
||||
{"SMLR", SMLR},
|
||||
{"ZERO", ZERO}};
|
||||
|
||||
// Map from instruction to string
|
||||
const std::unordered_map<Instruction, std::string> InstructionToStringMap = {
|
||||
{SET, "SET"},
|
||||
{CLR, "CLR"},
|
||||
{CPY, "CPY"},
|
||||
{SWP, "SWP"},
|
||||
{ADD, "ADD"},
|
||||
{SUB, "SUB"},
|
||||
{MUL, "MUL"},
|
||||
{DIV, "DIV"},
|
||||
{SKIP, "SKIP"},
|
||||
{JMP, "JMP"},
|
||||
{JMPTO, "JMPTO"},
|
||||
{EQL, "EQL"},
|
||||
{BIGR, "BIGR"},
|
||||
{SMLR, "SMLR"},
|
||||
{ZERO, "ZERO"}};
|
||||
|
||||
// Define the expected argument types for each instruction (true = pointer, false = literal)
|
||||
const std::unordered_map<Instruction, std::vector<bool>> instructionPatterns = {
|
||||
{Instruction::SKIP, {}}, // No arguments
|
||||
{Instruction::EMPTY, {}},
|
||||
{Instruction::JMP, {false}}, // One literal argument
|
||||
{Instruction::JMPTO, {false}},
|
||||
{Instruction::CLR, {true}}, // One pointer argument
|
||||
{Instruction::ZERO, {true}},
|
||||
{Instruction::SET, {false, true}}, // One literal and one pointer argument
|
||||
{Instruction::CPY, {true, true}}, // Two pointer arguments
|
||||
{Instruction::SWP, {true, true}},
|
||||
{Instruction::EQL, {true, true}},
|
||||
{Instruction::BIGR, {true, true}},
|
||||
{Instruction::SMLR, {true, true}},
|
||||
{Instruction::ADD, {true, true, true}}, // Three pointer arguments
|
||||
{Instruction::SUB, {true, true, true}},
|
||||
{Instruction::MUL, {true, true, true}},
|
||||
{Instruction::DIV, {true, true, true}},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user