aboutsummaryrefslogtreecommitdiff
path: root/include/bytecode.h
blob: c0930ad3ea2ce9b8d287d1680f7f051870b0d265 (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
#ifndef TSL_BYTECODE_H
#define TSL_BYTECODE_H

#include "std/buffer.h"
#include "string_view.h"
#include "value.h"
#include <stdint.h>

typedef uint8_t TslOpcodeType;

typedef enum {
    TSL_OPCODE_LOADN,           /* load number */
    TSL_OPCODE_LOADB,           /* load bool */
    TSL_OPCODE_LOADS,           /* load string */
    TSL_OPCODE_LOADF,           /* load function */
    TSL_OPCODE_LOADV,           /* load variable */
    TSL_OPCODE_LOADNULL,        /* load null */
    TSL_OPCODE_SETV,            /* set variable to the value at the top of the stack */
    TSL_OPCODE_LIST,            /* create a list using values from the stack */
    TSL_OPCODE_MAP,             /* create a map using values from the stack */
    TSL_OPCODE_MINDEX,          /* map index. pop two values from stack, where the first value will be a map and the second a key */
    TSL_OPCODE_CALLF,           /* call the function at the top of the stack using the next N values at the top of the stack as arguments */
    TSL_OPCODE_ADD,
    TSL_OPCODE_SUB,
    TSL_OPCODE_MUL,
    TSL_OPCODE_DIV,
    TSL_OPCODE_CALLC            /* run a program using N arguments from the stack, where the bottom value is the name of the program */
} TslOpcode;

typedef struct {
    TslBuffer /*TslInstruction*/ buffer;
} TslBytecode;

typedef struct {
    TslOpcode opcode;
    int value;
} TslInstructionType1;

typedef struct {
    TslOpcode opcode;
    double value;
} TslInstructionType2;

typedef struct {
    TslOpcode opcode;
    TslBool value;
} TslInstructionType3;

typedef struct {
    TslOpcode opcode;
    TslStringView value;
} TslInstructionType4;

typedef struct {
    TslOpcode opcode;
} TslInstructionType5;

const char* tsl_opcode_to_string(TslOpcode opcode);

void tsl_bytecode_init(TslBytecode *self);
void tsl_bytecode_deinit(TslBytecode *self);

int tsl_bytecode_add_ins1(TslBytecode *self, TslOpcode opcode, int value);
int tsl_bytecode_add_ins2(TslBytecode *self, TslOpcode opcode, double value);
int tsl_bytecode_add_ins3(TslBytecode *self, TslOpcode opcode, TslBool value);
int tsl_bytecode_add_ins4(TslBytecode *self, TslOpcode opcode, TslStringView *value);
int tsl_bytecode_add_ins5(TslBytecode *self, TslOpcode opcode);

#endif /* TSL_BYTECODE_H */