diff options
Diffstat (limited to 'src/bytecode.c')
-rw-r--r-- | src/bytecode.c | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index d15df9e..acdb2d9 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -2,6 +2,29 @@ #include <assert.h> #include <stdio.h> +static const char* opcode_to_string(TslOpcode opcode) { + switch(opcode) { + case TSL_OPCODE_LOADN: return "loadn"; + case TSL_OPCODE_LOADB: return "loadb"; + case TSL_OPCODE_LOADS: return "loads"; + case TSL_OPCODE_LOADF: return "loadf"; + case TSL_OPCODE_LOADV: return "loadv"; + case TSL_OPCODE_LOADNULL: return "loadnull"; + case TSL_OPCODE_SETV: return "setv"; + case TSL_OPCODE_LIST: return "list"; + case TSL_OPCODE_MAP: return "map"; + case TSL_OPCODE_MINDEX: return "mindex"; + case TSL_OPCODE_CALLF: return "callf"; + case TSL_OPCODE_ADD: return "add"; + case TSL_OPCODE_SUB: return "sub"; + case TSL_OPCODE_MUL: return "mul"; + case TSL_OPCODE_DIV: return "div"; + case TSL_OPCODE_LOADCA: return "loadca"; + case TSL_OPCODE_CALLC: return "callc"; + } + return ""; +} + void tsl_bytecode_writer_init(TslBytecodeWriter *self) { tsl_buffer_init(&self->buffer); } @@ -10,26 +33,41 @@ void tsl_bytecode_writer_deinit(TslBytecodeWriter *self) { tsl_buffer_deinit(&self->buffer); } -int tsl_bytecode_writer_loadn(TslBytecodeWriter *self, double number) { +int tsl_bytecode_writer_add_ins1(TslBytecodeWriter *self, TslOpcode opcode, int value) { TslInstructionType1 instruction; - instruction.opcode = TSL_OPCODE_LOAD_NUMBER; - instruction.number = number; - fprintf(stderr, "loadn %f\n", number); + instruction.opcode = opcode; + instruction.value = value; + fprintf(stderr, "%s %d\n", opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } -int tsl_bytecode_writer_loadb(TslBytecodeWriter *self, TslBool value) { +int tsl_bytecode_writer_add_ins2(TslBytecodeWriter *self, TslOpcode opcode, double value) { TslInstructionType2 instruction; - instruction.opcode = TSL_OPCODE_LOAD_BOOL; + instruction.opcode = opcode; instruction.value = value; - fprintf(stderr, "loadb %s\n", value ? "true" : "false"); + fprintf(stderr, "%s %f\n", opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } -int tsl_bytecode_writer_setv(TslBytecodeWriter *self, TslStringView *key) { +int tsl_bytecode_writer_add_ins3(TslBytecodeWriter *self, TslOpcode opcode, TslBool value) { TslInstructionType3 instruction; - instruction.opcode = TSL_OPCODE_SETV; - instruction.key = *key; - fprintf(stderr, "setv \"%.*s\"\n", (int)key->size, key->data); + instruction.opcode = opcode; + instruction.value = value; + fprintf(stderr, "%s %s\n", opcode_to_string(opcode), value ? "true" : "false"); + return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); +} + +int tsl_bytecode_writer_add_ins4(TslBytecodeWriter *self, TslOpcode opcode, TslStringView *value) { + TslInstructionType4 instruction; + instruction.opcode = opcode; + instruction.value = *value; + fprintf(stderr, "%s \"%.*s\"\n", opcode_to_string(opcode), (int)value->size, value->data); + return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); +} + +int tsl_bytecode_writer_add_ins5(TslBytecodeWriter *self, TslOpcode opcode) { + TslInstructionType5 instruction; + instruction.opcode = opcode; + fprintf(stderr, "%s\n", opcode_to_string(opcode)); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } |