aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode.c
blob: d15df9e0d44b800098838035a75bf62a3dad39db (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
#include "../include/bytecode.h"
#include <assert.h>
#include <stdio.h>

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_loadn(TslBytecodeWriter *self, double number) {
    TslInstructionType1 instruction;
    instruction.opcode = TSL_OPCODE_LOAD_NUMBER;
    instruction.number = number;
    fprintf(stderr, "loadn %f\n", number);
    return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
}

int tsl_bytecode_writer_loadb(TslBytecodeWriter *self, TslBool value) {
    TslInstructionType2 instruction;
    instruction.opcode = TSL_OPCODE_LOAD_BOOL;
    instruction.value = value;
    fprintf(stderr, "loadb %s\n", value ? "true" : "false");
    return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
}

int tsl_bytecode_writer_setv(TslBytecodeWriter *self, TslStringView *key) {
    TslInstructionType3 instruction;
    instruction.opcode = TSL_OPCODE_SETV;
    instruction.key = *key;
    fprintf(stderr, "setv \"%.*s\"\n", (int)key->size, key->data);
    return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
}