-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
203 lines (178 loc) · 4.47 KB
/
vm.go
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
package main
import (
"fmt"
"os"
)
const DEBUG_TRACE_EXECUTION = true // N.B. this does not use conditional compilation; it's handled at runtime.
const DEBUG_PRINT_CODE = true
var vm VM
const STACK_MAX = 256
type VM struct {
// N.B. uses slice indices instead 'real C-pointers', to avoid the unsafe package.
chunk *Chunk
ip int
stack [STACK_MAX]Value
stackTop int
}
type InterpretResult byte
const (
INTERPRET_OK InterpretResult = iota
INTERPRET_COMPILE_ERROR
INTERPRET_RUNTIME_ERROR
)
func interpret(source string) InterpretResult {
var chunk Chunk
if !compile(source, &chunk) {
return INTERPRET_COMPILE_ERROR
}
vm.chunk = &chunk
vm.ip = 0
result := run()
return result
}
func Interpret(chunk *Chunk) InterpretResult {
vm.chunk = chunk
vm.ip = 0
return run()
}
var ADD = func(a, b float64) Value { return NumberVal(a + b) }
var SUB = func(a, b float64) Value { return NumberVal(a - b) }
var MUL = func(a, b float64) Value { return NumberVal(a * b) }
var DIV = func(a, b float64) Value { return NumberVal(a / b) }
var GT = func(a, b float64) Value { return BoolVal(a > b) }
var LT = func(a, b float64) Value { return BoolVal(a < b) }
func run() InterpretResult {
for {
if DEBUG_TRACE_EXECUTION {
fmt.Printf(" ")
for i := 0; i < vm.stackTop; i++ {
fmt.Printf("[ ")
vm.stack[i].Print()
fmt.Printf(" ]")
}
println()
disassembleInstruction(vm.chunk, vm.ip)
}
instruction := vm.readByte()
switch instruction {
case OP_CONSTANT:
constant := vm.readConstant()
vm.push(constant)
case OP_NEGATE:
if !isNumber(vm.peek(0)) {
runtimeError("Operand must be a number.")
return INTERPRET_RUNTIME_ERROR
}
vm.push(NumberVal(-vm.pop().AsNumber()))
case OP_EQUAL:
b, a := vm.pop(), vm.pop()
vm.push(BoolVal(valuesEqual(a, b)))
case OP_GREATER:
if !vm.binaryOp(GT) {
return INTERPRET_COMPILE_ERROR
}
case OP_LESS:
if !vm.binaryOp(LT) {
return INTERPRET_COMPILE_ERROR
}
case OP_ADD:
if isString(vm.peek(0)) && isString(vm.peek(1)) {
concatenate()
} else if isNumber(vm.peek(0)) && isNumber(vm.peek(1)) {
if !vm.binaryOp(ADD) {
return INTERPRET_RUNTIME_ERROR
}
} else {
runtimeError("Operands must be two numbers or two strings.")
return INTERPRET_RUNTIME_ERROR
}
case OP_SUBTRACT:
if !vm.binaryOp(SUB) {
return INTERPRET_RUNTIME_ERROR
}
case OP_MULTIPLY:
if !vm.binaryOp(MUL) {
return INTERPRET_RUNTIME_ERROR
}
case OP_DIVIDE:
if !vm.binaryOp(DIV) {
return INTERPRET_RUNTIME_ERROR
}
case OP_NOT:
vm.push(BoolVal(isFalsey(vm.pop())))
case OP_NIL:
vm.push(NilVal(struct{}{}))
case OP_TRUE:
vm.push(BoolVal(true))
case OP_FALSE:
vm.push(BoolVal(false))
case OP_RETURN:
vm.pop().Print()
println()
return INTERPRET_OK
}
}
}
func valuesEqual(a, b Value) bool {
if a.Type() != b.Type() {
return false
}
switch a.Type() {
case VAL_BOOL:
return a.AsBoolean() == b.AsBoolean()
case VAL_NIL:
return true
case VAL_NUMBER:
return a.AsNumber() == b.AsNumber()
case VAL_OBJ:
a, b := asString(a), asString(b)
return a.value == b.value
}
return false
}
func isFalsey(value Value) bool {
return isNil(value) || (isBool(value) && !value.AsBoolean())
}
func concatenate() {
b, a := asString(vm.pop()), asString(vm.pop())
vm.push(NewObjString(a.value + b.value))
}
func (v *VM) binaryOp(op func(float64, float64) Value) bool {
if !isNumber(v.peek(0)) || !isNumber(v.peek(1)) {
runtimeError("Operands must be numbers.")
return false
}
b, a := v.pop().AsNumber(), v.pop().AsNumber()
v.push(op(a, b))
return true
}
func (v *VM) readByte() byte {
result := v.chunk.Code[v.ip]
v.ip++
return result
}
func (v *VM) readConstant() Value {
result := v.chunk.constants.Values[v.readByte()]
return result
}
func (v *VM) resetStack() {
vm.stackTop = 0
}
func (v VM) peek(distance int) Value {
return vm.stack[vm.stackTop-distance-1]
}
func (v *VM) push(value Value) {
v.stack[v.stackTop] = value
v.stackTop++
}
func (v *VM) pop() Value {
v.stackTop--
return v.stack[v.stackTop]
}
func runtimeError(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
fmt.Fprintln(os.Stderr)
instruction := vm.ip - 1
line := vm.chunk.lines[instruction]
fmt.Fprintf(os.Stderr, "[line %d] in script\n", line)
}