blob: 6011928b434378ebf847ef564b93d076710fd211 (
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
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
|
import std.stdio;
import std.string;
import chunk;
import parser;
//import value;
//import obj;
import dbg;
enum InterpretResult {
OK,
COMPILE_ERROR,
RUNTIME_ERROR,
}
class VM {
int topOffset = 0;
ubyte ip = 0;
Value[] stack;
//ObjFunction func;
Function func;
this(Function func) {
this.func = func;
}
Value peek(int offset) {
if (offset >= this.topOffset) {
writefln("offset of %d greater than stack size %d", offset, topOffset);
}
return this.stack[topOffset - offset - 1];
}
Value pop() {
if (this.topOffset > 0) {
this.topOffset--;
} else {
writeln("pop() on an empty stack!!");
}
return this.stack[this.topOffset];
}
//InterpretResult push(Value value) {
void push(Value value) {
if (this.stack.length > this.topOffset) {
this.stack[topOffset] = value;
} else {
this.stack ~= value;
}
this.topOffset++;
}
ubyte readByte() {
return this.func.chunk.code[this.ip++];
}
bool isNumber(Value value) {
return value.type == ValueType.NUMBER;
}
double asNumber(Value value) {
return value.as.number;
}
InterpretResult run() {
//int ip = 0; // TODO this is wrong
while (true) {
write(" ");
//foreach (Value val; this.stack) {
for (int i = 0; i < this.topOffset; i++) {
writef("[ %s ]", printableValue(this.stack[i]));
}
writeln("\n--");
disassemble(this.func.chunk, this.ip);
ubyte inst;
switch (inst = this.readByte()) {
case OpCode.OP_CONSTANT:
Value constant = this.func.chunk.constants[this.readByte()];
this.push(constant);
break;
case OpCode.OP_NEGATE:
if (!isNumber(peek(0))) {
writeln("negate operand must be a number");
return InterpretResult.RUNTIME_ERROR;
}
double val = asNumber(pop());
push(makeNumberValue(val * -1));
break;
case OpCode.OP_ADD:
Value b = this.pop();
if (!isNumber(b)) {
writeln("b is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
Value a = this.pop();
if (!isNumber(a)) {
writeln("a is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
double bnum = asNumber(b);
double anum = asNumber(a);
push(makeNumberValue(anum + bnum));
break;
case OpCode.OP_SUBTRACT:
Value b = this.pop();
if (!isNumber(b)) {
writeln("b is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
Value a = this.pop();
if (!isNumber(a)) {
writeln("a is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
double bnum = asNumber(b);
double anum = asNumber(a);
push(makeNumberValue(anum - bnum));
break;
case OpCode.OP_LESS:
Value b = this.pop();
if (!isNumber(b)) {
writeln("b is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
Value a = this.pop();
if (!isNumber(a)) {
writeln("a is not a number!");
return InterpretResult.RUNTIME_ERROR; // TODO error
}
double bnum = asNumber(b);
double anum = asNumber(a);
push(makeBooleanValue(anum < bnum));
break;
case OpCode.OP_RETURN:
Value ret = this.pop();
writefln("returned %s", printableValue(ret));
return InterpretResult.OK;
default:
writeln("unknown opcode to run");
break;
}
}
}
}
|