-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.c
98 lines (84 loc) · 2.2 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
#include <stdio.h>
#include <stdint.h>
#include "chunk.h"
#include "common.h"
#include "compiler.h"
#include "value.h"
#include "debug.h"
#include "vm.h"
VM vm;
static void resetStack() {
vm.stacktop = vm.stack;
}
void initVM(){
resetStack();
}
void freeVM(){
}
void push(Value value){
*vm.stacktop = value;
vm.stacktop++ ;
}
Value pop(){
vm.stacktop--;
return *vm.stacktop;
}
static IntrepretResult run(){
#define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
#define BINARY_OP(op) \
do { \
double b = pop(); \
double a = pop(); \
push(a op b); \
} while (false)
for(;;){
#ifdef DEBUG_TRACE_EXECUTION
printf(" ");
for(Value* slot = vm.stack; slot < vm.stacktop;slot++){
printf("[");
printValue(*slot);
printf("]");
}
printf("\n");
disassembleInstruction(vm.chunk, (int)(vm.ip - vm.chunk->code));
#endif
uint8_t instuction;
switch (instuction = READ_BYTE()) {
case OP_RETURN:{
printValue(pop());
printf("\n");
return INTERPRET_OK;
}
case OP_CONSTANT:{
Value constant = READ_CONSTANT();
push(constant);
break;
}
case OP_NEGATE: {
push(-(pop()));
break;
}
case OP_ADD: BINARY_OP(+); break;
case OP_SUBTRACT: BINARY_OP(-); break;
case OP_MULTIPLY: BINARY_OP(*); break;
case OP_DIVIDE: BINARY_OP(/); break;
}
}
#undef READ_BYTE
#undef READ_CONSTANT
#undef BINARY_OP
}
IntrepretResult interpret(const char* source){
Chunk chunk;
initChunk(&chunk);
if (!compile(source, &chunk)){
freeChunk(&chunk);
return INTERPRET_COMPILE_ERROR;
}
vm.chunk = &chunk;
vm.ip = vm.chunk->code;
IntrepretResult result = run();
freeChunk(&chunk);
return result;
}