-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
256 lines (228 loc) · 7.08 KB
/
vm.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// Created by Fabian Simon on 29.09.23.
//
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "vm.h"
#include "debug.h"
#include "compiler.h"
#include "memory.h"
#include "object.h"
VM vm;
void free_objects();
static void reset_stack() {
vm.stack_top = vm.stack;
vm.frame_count = 0;
}
static void runtime_error(const char* format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputs("\n", stderr);
CallFrame* frame = &vm.frames[vm.frame_count-1];
size_t instruction = frame->ip - frame->function->chunk.code-1;
int line = frame->function->chunk.lines[instruction];
fprintf(stderr, "[line %d] in script\n", line);
reset_stack();
}
void init_VM() {
reset_stack();
vm.objects = NULL;
init_table(&vm.globals);
init_table(&vm.strings);
}
void free_VM() {
free_table(&vm.globals);
free_table(&vm.strings);
free_objects();
}
void push(Value val) {
*vm.stack_top = val;
vm.stack_top++;
}
Value pop() {
vm.stack_top--;
return *vm.stack_top;
}
static Value peek(int distance) {
return vm.stack_top[-1 - distance];
}
static bool is_falsey(Value val) {
return IS_NIL(val) || (IS_BOOL(val) && !AS_BOOL(val));
}
static void concatenate() {
ObjString* b = AS_STRING(pop());
ObjString* a = AS_STRING(pop());
int length = a->length+b->length;
char* chars = ALLOCATE(char, length+1);
memcpy(chars, a->chars, a->length);
memcpy(chars + a->length, b->chars, b->length);
chars[length] = '\0';
ObjString* result = take_string(chars, length);
push(OBJ_VAL(result));
}
static InterpretResult run() {
CallFrame* frame = &vm.frames[vm.frame_count-1];
#define READ_BYTE() (*frame->ip++)
#define READ_CONSTANT() \
(frame->function->chunk.constants.values[READ_BYTE()])
#define READ_STRING() AS_STRING(READ_CONSTANT())
#define READ_SHORT() \
(frame->ip += 2, \
(uint16_t)((frame->ip[-2] << 8) | frame->ip[-1]))
#define BINARY_OP(value_type, op) \
do { \
if (!IS_NUMBER(peek(0)) || !IS_NUMBER(peek(1))) { \
runtime_error("Operands must be numbers"); \
return INTERPRET_RUNTIME_ERROR; \
} \
double b = AS_NUMBER(pop()); \
double a = AS_NUMBER(pop()); \
push(value_type(a op b)); \
} while (false)
for (;;) {
#ifdef DEBUG_TRACE_EXECUTION
printf(" ");
for (Value* slot = vm.stack; slot < vm.stack_top; slot++) {
printf("[ ");
print_value(*slot);
printf(" ]");
}
printf("\n");
disassemble_instruction(&frame->function->chunk,
(int)(frame->ip - frame->function->chunk.code));
#endif
uint8_t instruction;
switch (instruction = READ_BYTE()) {
case OP_ADD: {
if (IS_STRING(peek(0)) && IS_STRING(peek(1))) {
concatenate();
} else if (IS_NUMBER(peek(0)) && IS_NUMBER(peek(1))) {
double b = AS_NUMBER(pop());
double a = AS_NUMBER(pop());
push(NUMBER_VAL(a + b));
} else {
runtime_error("Operands must be two numbers or two strings.");
return INTERPRET_RUNTIME_ERROR;
}
break;
}
case OP_SUBTRACT: {
BINARY_OP(NUMBER_VAL, -);
break;
}
case OP_MULTIPLY: {
BINARY_OP(NUMBER_VAL, *);
break;
}
case OP_DIVIDE: {
BINARY_OP(NUMBER_VAL, /);
break;
}
case OP_NOT: {
push(BOOL_VAL(is_falsey(pop())));
break;
}
case OP_NEGATE: {
if (!IS_NUMBER(peek(0))) {
runtime_error("Operand must be a number");
return INTERPRET_RUNTIME_ERROR;
}
push(NUMBER_VAL(-AS_NUMBER(pop())));
break;
}
case OP_PRINT:
print_value(pop());
printf("\n");
break;
case OP_JUMP: {
uint16_t offset = READ_SHORT();
frame->ip += offset;
break;
}
case OP_JUMP_IF_FALSE: {
uint16_t offset = READ_SHORT();
if (is_falsey(peek(0))) frame->ip += offset;
break;
}
case OP_LOOP: {
uint16_t offset = READ_SHORT();
frame->ip -= offset;
break;
}
case OP_RETURN: {
// Exit interpreter.
return INTERPRET_OK;
}
case OP_CONSTANT: {
Value constant = READ_CONSTANT();
push(constant);
break;
}
case OP_NIL: push(NIL_VAL); break;
case OP_TRUE: push(BOOL_VAL(true)); break;
case OP_FALSE: push(BOOL_VAL(false)); break;
case OP_POP: pop(); break;
case OP_GET_LOCAL: {
uint8_t slot = READ_BYTE();
push(frame->slots[slot]);
break;
}
case OP_SET_LOCAL: {
uint8_t slot = READ_BYTE();
frame->slots[slot] = peek(0);
break;
}
case OP_GET_GLOBAL: {
ObjString* name = READ_STRING();
Value val;
if (!table_get(&vm.globals, name, &val)) {
runtime_error("Undefined variable '%s'.", name->chars);
return INTERPRET_RUNTIME_ERROR;
}
push(val);
break;
}
case OP_DEFINE_GLOBAL: {
ObjString* name = READ_STRING();
table_set(&vm.globals, name, peek(0));
pop();
break;
}
case OP_SET_GLOBAL: {
ObjString *name = READ_STRING();
if (table_set(&vm.globals, name, peek(0))) {
table_delete(&vm.globals, name);
runtime_error("Undefined variable '%s'.", name->chars);
return INTERPRET_RUNTIME_ERROR;
}
break;
}
case OP_EQUAL: {
Value b = pop();
Value a = pop();
push(BOOL_VAL(values_equal(a, b)));
break;
}
case OP_GREATER: BINARY_OP(BOOL_VAL, >); break;
case OP_LESS: BINARY_OP(BOOL_VAL, <); break;
}
}
#undef READ_BYTE
#undef READ_CONSTANT
#undef READ_STRING
#undef READ_SHORT
#undef BINARY_OP
}
InterpretResult interpret(const char* src) {
ObjFunction* func = compile(src);
if (func == NULL) return INTERPRET_COMPILE_ERROR;
push(OBJ_VAL(func));
CallFrame* frame = &vm.frames[vm.frame_count++];
frame->function = func;
frame->ip = func->chunk.code;
frame->slots = vm.stack;
return run();
}