-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecodes.c
68 lines (59 loc) · 1.18 KB
/
bytecodes.c
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
#include "bytecodes.h"
#include "debug.h"
// must match definition of Derp_code_type in 'vm.h'
char Derp_code_names[][16] = {
"NULL",
"PUSH",
"PUSH_INT",
"PUSH_FLOAT",
"PUSH_STR",
"PUSH_BOOL",
"PUSH_ARRAY",
"PUSH_FN",
// mathematical and logical operations
"ADD",
"SUB",
"MUL",
"DIV",
"CMP_EQ",
"CMP_NEQ",
"CMP_LT",
"CMP_LT_EQ",
"CMP_GT",
"CMP_GT_EQ",
"CALL",
"REGISTER",
"PUSH_LOOKUP",
"ASSIGN",
"RET",
"JUMP_IF_FALSE",
"JUMP",
"REPEAT"
};
const char* code_type_to_str(Derp_code_type code) {
return Derp_code_names[(int) code];
}
void bytecode_print(instr* code) {
printf("%s", code_type_to_str(code->code));
if (code->arg1 != NULL)
printf(" '%s'", code->arg1);
printf(" %d\n", code->arg2);
}
void bytecode_vec_print(instr* instrv, int instrc) {
for (int i = 0; i < instrc; i++) {
bytecode_print(&instrv[i]);
}
}
instr* bytecodes_compress(List* input) {
instr* output = malloc(sizeof(instr) * input->length);
check_mem(output);
int i = 0;
while (input->length > 0) {
instr* inst = List_dequeue(input);
output[i] = *inst;
i++;
}
return output;
error:
return NULL;
}