/
frame.go
315 lines (285 loc) · 6.9 KB
/
frame.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"fmt"
"os"
)
// Frame is a code object in execution context.
// It has a data stack and a block stack.
type Frame struct {
stack []Object
blocks []block
code *Code
names map[string]Object
}
type block struct {
start, end int
}
func hasArg(opcode byte) bool {
return opcode > 89
}
// Push pushes an item to the stack associated
// with the frame.
func (f *Frame) Push(item Object) {
f.stack = append(f.stack, item)
}
// Pop pops an item to the stack associated
// with the frame.
func (f *Frame) Pop() Object {
item := f.stack[len(f.stack)-1]
f.stack = f.stack[:len(f.stack)-1]
return item
}
// Peek gives you what's on top of the stack,
// but does not remove the item.
func (f *Frame) Peek() Object {
return f.stack[len(f.stack)-1]
}
// Execute brings the frame to execution. The attached
// code will be executed starting at the first instruction
// and interpreted.
func (f *Frame) Execute() Object {
if *debug {
fmt.Println()
}
f.blocks = make([]block, 0)
pc := 0
var op, first, second byte
for pc < len(f.code.Instructions) {
op = f.code.Instructions[pc]
if *debug {
fmt.Printf("\x1b[0;32m%4d %-17s", pc, opcode[op])
}
if hasArg(op) {
first, second = f.code.Instructions[pc+1], f.code.Instructions[pc+2]
if *debug {
fmt.Printf(" %3d %3d", first, second)
}
pc += 2
} else {
first, second = 0, 0
if *debug {
fmt.Printf(" ")
}
}
if *debug {
fmt.Printf(" %s\x1b[0m\n", f.stack)
}
pc++
switch op {
case POP_TOP:
f.Pop()
case LOAD_CONST:
f.Push(f.code.Consts[first])
case STORE_NAME:
f.code.Names[first] = f.Pop()
case LOAD_NAME:
f.Push(f.code.Names[first])
case LOAD_FAST:
name := f.code.Varnames[int(first)].(String).string
f.Push(f.names[name])
case MAKE_FUNCTION:
name := f.Pop().(String)
code := f.Pop().(Code)
f.Push(NewExternalFunction(name.string, &code))
case CALL_FUNCTION:
if second > 0 {
panic("Keyword parameters are not implemented.")
}
pos := make([]Object, int(first))
for j := int(first) - 1; j > -1; j-- {
pos[j] = f.Pop()
}
args := args{Positional: pos}
o := f.Pop()
if function, ok := o.(Function); ok {
f.Push(function.Call(&args))
} else if fname, ok := o.(String); ok {
value, isBuiltin := builtin[fname.string]
if isBuiltin {
f.Push(value)
} else {
panic("can only resolve named functions to builtins")
}
function := value.(Function)
f.Push(function.Call(&args))
} else {
fmt.Fprintf(os.Stderr, "%+v", o)
panic("unknown function call")
}
case BINARY_MULTIPLY: // TODO(flowlo): implement this for floats
if a, ok := f.Pop().(Int); ok {
if b, ok := f.Pop().(Int); ok {
f.Push(Int{a.int32 * b.int32})
}
}
case BINARY_ADD: // TODO(flowlo): implement this for floats
if a, ok := f.Pop().(Int); ok {
if b, ok := f.Pop().(Int); ok {
f.Push(Int{a.int32 + b.int32})
}
}
case RETURN_VALUE:
return f.Pop()
case LOAD_GLOBAL:
name := f.code.Names[first].(String).string
value, isBuiltin := builtin[name]
if isBuiltin {
f.Push(value)
} else {
panic("lookup of globals other than builtins not implemented")
}
case COMPARE_OP:
rightx := f.Pop()
leftx := f.Pop()
if right, ok := rightx.(Int); ok {
if left, ok := leftx.(Int); ok {
switch first {
case OP_LT:
if left.int32 < right.int32 {
f.Push(True{})
} else {
f.Push(False{})
}
case OP_LEQ:
if left.int32 <= right.int32 {
f.Push(True{})
} else {
f.Push(False{})
}
case OP_EQ:
if left.int32 == right.int32 {
f.Push(True{})
} else {
f.Push(False{})
}
case OP_GT:
if left.int32 > right.int32 {
f.Push(True{})
} else {
f.Push(False{})
}
case OP_GE:
if left.int32 >= right.int32 {
f.Push(True{})
} else {
f.Push(False{})
}
default:
panic("comparison operator not implemented.")
}
}
} else if right, ok := rightx.(String); ok {
if left, ok := leftx.(String); ok {
if first == OP_EQ || first == OP_IS {
if left.string == right.string {
f.Push(True{})
} else {
f.Push(False{})
}
} else if first == OP_ISNT {
if left.string != right.string {
f.Push(True{})
} else {
f.Push(False{})
}
} else {
panic(fmt.Sprintf("unimplemented operator: %d", int(first)))
}
}
} else {
panic(fmt.Sprintf("cannot compare %T and %T", rightx, leftx))
}
case POP_JUMP_IF_FALSE:
if _, ok := f.Pop().(False); ok {
pc = int(first) + int(second)*256
}
case BINARY_SUBTRACT: // TODO(flowlo): Implement this for floats
if right, ok := f.Pop().(*Int); ok {
if left, ok := f.Pop().(*Int); ok {
f.Push(&Int{left.int32 - right.int32})
}
}
case NOP:
case ROT_TWO:
a := f.Pop()
b := f.Pop()
f.Push(a)
f.Push(b)
case ROT_THREE:
a := f.Pop()
b := f.Pop()
c := f.Pop()
f.Push(b)
f.Push(a)
f.Push(c)
case UNARY_POSITIVE: // TODO(flowlo): Implement for floats
o := f.Pop()
if a, ok := o.(*Int); ok {
a.int32 = +a.int32
}
case UNARY_NEGATIVE: // TODO(flowlo): Implement for floats
o := f.Pop()
if a, ok := o.(*Int); ok {
a.int32 = -a.int32
}
case UNARY_NOT: // TODO(flowlo): Implement for floats
o := f.Pop()
if a, ok := o.(*Int); ok {
a.int32 = ^a.int32
}
case UNARY_INVERT: // TODO(flowlo): Implement for floats
o := f.Pop()
if a, ok := o.(*Int); ok {
a.int32 = ^a.int32
}
case IMPORT_NAME:
name := f.code.Names[first]
fromlist := f.Pop()
level := f.Pop()
fmt.Printf("import %s with %s and %s\n", name, fromlist, level)
imp := builtin["__import__"].(Function)
arg := &args{
Positional: []Object{name},
}
f.Push(imp.Call(arg))
case LOAD_ATTR:
name := f.code.Names[first]
o := f.Pop()
ao, ok := o.(*Code)
if !ok {
panic(fmt.Sprintf("TypeError: %v of type %T has no attributes!", o, o))
}
a, err := ao.GetAttribute(name, nil)
if err != nil {
panic(err.Error())
}
f.Push(a)
case JUMP_ABSOLUTE:
pc = int(first)
case SETUP_LOOP:
block := &block{pc, pc + int(first)}
f.blocks = append(f.blocks, *block)
case POP_BLOCK:
f.blocks = f.blocks[:len(f.blocks)-1]
case STORE_FAST:
name := f.code.Varnames[int(first)].(String).string
f.names[name] = f.Pop()
default:
fmt.Sprintf("\x1b[31;1mSkipped\x1b[0m unknown opcode: %d\n", op)
panic(fmt.Sprintf("Unknown opcode: %d", op))
}
}
return &Null{}
}
// NewFrame constructs a new Frame to execute the passed code. This
// allocates memory for the execution stack and sets everything
// up.
func NewFrame(code *Code) *Frame {
f := new(Frame)
f.stack = make([]Object, 0)
// TODO(flowlo): What's a good capacity here?
f.blocks = make([]block, 0)
f.code = code
f.names = make(map[string]Object, 0)
return f
}