-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiledcode.go
398 lines (368 loc) · 7.43 KB
/
compiledcode.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package trompe
import (
"fmt"
"math/rand"
"strings"
)
type Closure interface {
Arity() int
Apply(*Interp, *Context, *Env) (Value, error)
}
type CompiledCode struct {
Id int
Syms []string
Lits []Value
Ops []Opcode
Labels map[int]int
}
func NewCompiledCode() *CompiledCode {
return &CompiledCode{
Id: int(rand.Int31()),
Syms: []string{},
Lits: []Value{},
Ops: []Opcode{},
Labels: make(map[int]int, 0),
}
}
func (code *CompiledCode) AddLit(value Value) {
code.Lits = append(code.Lits, value)
}
func (code *CompiledCode) LiteralDesc(i int) string {
return code.Lits[i].Desc()
}
func (code *CompiledCode) Inspect() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("id: %d\n", code.Id))
b.WriteString("symbols:\n")
for i, name := range code.Syms {
s := fmt.Sprintf(" %d: \"%s\"\n", i, name)
b.WriteString(s)
}
b.WriteString("literals:\n")
for i, value := range code.Lits {
s := fmt.Sprintf(" %d: %s\n", i, value.Desc())
b.WriteString(s)
}
b.WriteString("opcodes:\n")
pc := 0
for ; pc < len(code.Ops); pc++ {
s := fmt.Sprintf(" %d: ", pc)
switch code.Ops[pc] {
case OpNop:
s += "nop"
case OpLoadUnit:
s += "load ()"
case OpLoadTrue:
s += "load true"
case OpLoadFalse:
s += "load false"
case OpLoadZero:
s += "load 0"
case OpLoadOne:
s += "load 1"
case OpLoadNegOne:
s += "load -1"
case OpLoadInt:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load %d", i)
case OpLoadNone:
s += "load none"
case OpLoadRef:
s += "load ref"
case OpLoadLit:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load literal %s", code.LiteralDesc(i))
case OpLoadLocal:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load local \"%s\"", code.Syms[i])
case OpLoadAttr:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load attr \"%s\"", code.Syms[i])
case OpLoadArg:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load arg %d", i)
case OpLoadModule:
s += "load module"
case OpStoreLocal:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("store local \"%s\"", code.LiteralDesc(i))
case OpStoreRef:
s += fmt.Sprintf("store ref")
case OpStoreAttr:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("store attr \"%s\"", code.LiteralDesc(i))
case OpPop:
s += "pop"
case OpDup:
s += "dup"
case OpReturn:
s += "return"
case OpReturnUnit:
s += "return ()"
case OpLabel:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("label L%d", i)
case OpJump:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("jump L%d", i)
case OpBranchTrue:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("branch true L%d", i)
case OpBranchFalse:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("branch false L%d", i)
case OpBranchNext:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("load next; branch L%d", i)
case OpBegin:
s += "begin block"
case OpEnd:
s += "end block"
case OpCall:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("call with %d args", i)
case OpPanic:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("panic %d", i)
case OpEq:
s += "=="
case OpNe:
s += "=="
case OpLt:
s += "<"
case OpLe:
s += "<="
case OpGt:
s += ">"
case OpGe:
s += ">="
case OpMatch:
s += "match"
case OpAdd:
s += "+"
case OpSub:
s += "-"
case OpMul:
s += "*"
case OpDiv:
s += "/"
case OpMod:
s += "%"
case OpSome:
s += "create some"
case OpList:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("create list %d", i)
case OpTuple:
i := code.Ops[pc+1]
pc++
s += fmt.Sprintf("create tuple %d", i)
case OpClosedRange:
s += fmt.Sprintf("create closed range")
case OpHalfOpenRange:
s += fmt.Sprintf("create half-open range")
case OpIter:
s += fmt.Sprintf("create iterator")
default:
panic(fmt.Sprintf("unknown opcode %d", code.Ops[pc]))
}
b.WriteString(s + "\n")
}
return b.String()
}
func (code *CompiledCode) Type() int {
return ValueTypeClos
}
func (code *CompiledCode) Desc() string {
return fmt.Sprintf("CompiledCode %p", code)
}
func (code *CompiledCode) Arity() int {
// TODO
return 0
}
func (code *CompiledCode) Apply(ip *Interp, ctx *Context, env *Env) (Value, error) {
return ip.Eval(ctx, env, code)
}
/*
show "Hello, world!"
*/
func TestCompiledCodeHelloWorld() {
code := CompiledCode{
Syms: []string{
"show",
},
Lits: []Value{
NewString("show"),
NewString("Hello, world!"),
},
Ops: []int{
OpLoadLocal, 0, // "show"
OpLoadLit, 1, // "Hello, world!"
OpCall, 1,
OpReturnUnit,
},
}
fmt.Println(code.Inspect())
Run("__test__", &code)
}
/*
match (arg1 % 3, arg1 % 5) with
case (0, 0) then show("FizzBuzz")
case (0, _) then show("Fizz")
case (_, 0) then show("Buzz")
else show(arg1)
end
*/
func TestCompiledCodeFizzBuzzMatch() {
code := CompiledCode{
Syms: []string{
"show",
},
Lits: []Value{
NewString("Fizz"),
NewString("Buzz"),
NewString("FizzBuzz"),
newPattern(newPtnTuple(&ptnInt{0}, &ptnInt{0})),
newPattern(newPtnTuple(&ptnInt{0}, ptnWildcard)),
newPattern(newPtnTuple(ptnWildcard, &ptnInt{0})),
},
Ops: []int{
OpLoadArg, 0,
OpLoadInt, 3,
OpMod,
OpLoadArg, 0,
OpLoadInt, 5,
OpMod,
OpTuple, 2,
OpBegin,
OpLoadLit, 4, // pattern 1
OpMatch,
OpBranchFalse, 0, // label 0
OpLoadLocal, 0, // "show"
OpLoadLit, 3, // "FizzBuzz",
OpCall, 1,
OpEnd,
OpJump, 4, // label 4
OpLabel, 0,
OpBegin,
OpLoadLit, 5, // pattern 2
OpMatch,
OpBranchFalse, 1, // label 1
OpLoadLocal, 0, // "show"
OpLoadLit, 3, // "FizzBuzz",
OpCall, 1,
OpEnd,
OpJump, 4, // label 4
OpLabel, 1,
OpBegin,
OpLoadLit, 6, // pattern 3
OpMatch,
OpBranchFalse, 2, // label 2
OpLoadLocal, 0, // "show"
OpLoadLit, 3, // "FizzBuzz",
OpCall, 1,
OpEnd,
OpJump, 4, // label 4
OpLabel, 2,
OpBegin,
OpLoadLit, 6, // pattern 3
OpMatch,
OpBranchFalse, 2, // label 3
OpLoadLocal, 0, // "show"
OpLoadArg, 0, // arg 1
OpCall, 1,
OpEnd,
OpJump, 4, // label 4
OpLabel, 3,
OpLabel, 4, // match end
OpReturnUnit,
},
}
fmt.Println(code.Inspect())
/*
prog := Program{}
ctx := NewContext(nil, &code, nil, 0)
prog.Eval(&ctx)
*/
}
/*
if arg1 == 3 then
show("Fizz")
else if arg1 == 5 then
show("Buzz")
else if arg1 == 15 then
show("FizzBuzz")
else
show(arg1)
end
*/
func TestCompiledCodeFizzBuzzCompare() {
code := CompiledCode{
Syms: []string{
"show",
},
Lits: []Value{
NewString("show"),
NewString("Fizz"),
NewString("Buzz"),
NewString("FizzBuzz"),
},
Ops: []int{
OpLoadArg, 0, // arg 1
OpLoadInt, 3, // 3
OpEq, // ==
OpBranchFalse, 0, // label 0
OpLoadLocal, 0, // "show"
OpLoadLit, 1, // "Fizz"
OpCall, 1,
OpJump, 4, // label 4
OpLabel, 0,
OpLoadArg, 0, // arg 1
OpLoadInt, 5, // 5
OpEq, // ==
OpBranchFalse, 1, // label 1
OpLoadLocal, 0, // "show"
OpLoadLit, 2, // "Buzz"
OpCall, 1,
OpJump, 4, // label 4
OpLabel, 1,
OpLoadArg, 0, // arg 1
OpLoadInt, 15, // 15
OpEq, // ==
OpBranchFalse, 2, // label 2
OpLoadLocal, 0, // "show"
OpLoadLit, 3, // "FizzBuzz"
OpCall, 1,
OpJump, 4, // label 4
OpLabel, 2,
OpLoadLocal, 0, // "show"
OpLoadArg, 0, // arg 1
OpCall, 1,
OpJump, 4, // label 4
OpLabel, 3,
OpLabel, 4,
OpReturnUnit,
},
}
fmt.Println(code.Inspect())
/*
prog := Program{}
ctx := NewContext(nil, &code, nil, 0)
prog.Eval(&ctx)
*/
}