#include "../include/bytecode.h" #include #include const char* tsl_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_INDEX: return "index"; 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_CALLC: return "callc"; } return ""; } void tsl_bytecode_init(TslBytecode *self) { tsl_buffer_init(&self->buffer); } void tsl_bytecode_deinit(TslBytecode *self) { tsl_buffer_deinit(&self->buffer); } int tsl_bytecode_add_ins1(TslBytecode *self, TslOpcode opcode, int value) { TslInstructionType1 instruction; instruction.opcode = opcode; instruction.value = value; fprintf(stderr, "%s %d\n", tsl_opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } int tsl_bytecode_add_ins2(TslBytecode *self, TslOpcode opcode, double value) { TslInstructionType2 instruction; instruction.opcode = opcode; instruction.value = value; fprintf(stderr, "%s %g\n", tsl_opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } int tsl_bytecode_add_ins3(TslBytecode *self, TslOpcode opcode, TslBool value) { TslInstructionType3 instruction; instruction.opcode = opcode; instruction.value = value; fprintf(stderr, "%s %s\n", tsl_opcode_to_string(opcode), value ? "true" : "false"); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } int tsl_bytecode_add_ins4(TslBytecode *self, TslOpcode opcode, TslStringView *value) { TslInstructionType4 instruction; instruction.opcode = opcode; instruction.value = *value; fprintf(stderr, "%s \"%.*s\"\n", tsl_opcode_to_string(opcode), (int)value->size, value->data); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } int tsl_bytecode_add_ins5(TslBytecode *self, TslOpcode opcode) { TslInstructionType5 instruction; instruction.opcode = opcode; fprintf(stderr, "%s\n", tsl_opcode_to_string(opcode)); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); }