#include "../include/bytecode.h" #include #include 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); } void tsl_bytecode_writer_deinit(TslBytecodeWriter *self) { tsl_buffer_deinit(&self->buffer); } int tsl_bytecode_writer_add_ins1(TslBytecodeWriter *self, TslOpcode opcode, int value) { TslInstructionType1 instruction; 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_add_ins2(TslBytecodeWriter *self, TslOpcode opcode, double value) { TslInstructionType2 instruction; instruction.opcode = opcode; instruction.value = value; fprintf(stderr, "%s %f\n", opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } int tsl_bytecode_writer_add_ins3(TslBytecodeWriter *self, TslOpcode opcode, TslBool value) { TslInstructionType3 instruction; 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)); }