-
Notifications
You must be signed in to change notification settings - Fork 43
/
vm.c
39 lines (32 loc) · 904 Bytes
/
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
#include <stdio.h>
#include "vm.h"
#include "stack.h"
#include "sym.h"
#include "codes.h"
byte *cur_byte;
byte next_byte()
{
return *cur_byte++;
}
void run(byte *code)
{
int arg1, arg2;
cur_byte = code;
next_op:
switch (next_byte()) {
case PUSH: push(next_byte()); goto next_op;
case READ: push(get_sym(next_byte())->val); goto next_op;
case WRITE: set_sym(next_byte(), pop()); goto next_op;
case ADD: POP_BOTH; push(arg1 + arg2); goto next_op;
case SUB: POP_BOTH; push(arg1 - arg2); goto next_op;
case MUL: POP_BOTH; push(arg1 * arg2); goto next_op;
case DIV: POP_BOTH; push(arg1 / arg2); goto next_op;
case RET: {
int i;
for (i = 0; i < get_table_size(); i++) {
printf("%s = %i\n", get_sym(i)->name, get_sym(i)->val);
}
}
return;
}
}