Parser_parseOperation

This commit is contained in:
2025-02-03 21:14:02 +05:00
parent b443367f46
commit 2faff91981
9 changed files with 179 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
#include "instructions.h"
#include "../collections/HashMap.h"
i32 NOP_impl(VM* vm);
i32 PUSH_impl(VM* vm);
@@ -13,8 +14,8 @@ i32 EXIT_impl(VM* vm);
i32 JMP_impl(VM* vm);
i32 CALL_impl(VM* vm);
const Instruction instructions[] = {
Array_declare(Instruction);
static const Array_Instruction instructions_array = ARRAY(Instruction, {
Instruction_construct(NOP),
Instruction_construct(PUSH),
Instruction_construct(MOV),
@@ -27,11 +28,28 @@ const Instruction instructions[] = {
Instruction_construct(EXIT),
// Instruction_construct(JMP),
// Instruction_construct(CALL),
};
});
const Instruction* Instruction_getByOpcode(Opcode opcode){
if(opcode >= ARRAY_SIZE(instructions))
if(opcode >= instructions_array.len)
return NULL;
return instructions + opcode;
return instructions_array.data + opcode;
}
HashMap_declare(Instruction);
HashMap_define(Instruction, HashMap_DESTROY_VALUE_FUNC_NULL);
static HashMap_Instruction* instructions_map = NULL;
const Instruction* Instruction_getByName(str name){
if(instructions_map == NULL){
instructions_map = malloc(sizeof(HashMap_Instruction));
HashMap_Instruction_alloc(instructions_map);
for(u32 i = 0; i < instructions_array.len; i++){
HashMap_Instruction_tryPush(instructions_map, instructions_array.data[i].name, instructions_array.data[i]);
}
}
return HashMap_Instruction_tryGetPtr(instructions_map, name);
}

View File

@@ -34,3 +34,4 @@ typedef struct Instruction {
/// @param opcode any byte
/// @return ptr to struct or NULL
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
const Instruction* NULLABLE(Instruction_getByName)(str name);