aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode.c
blob: 9cf32a613b7dbd72930bc97200c8ef526a8001ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "../include/bytecode.h"
#include <assert.h>
#include <stdio.h>

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));
}