-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytecode.go
549 lines (439 loc) · 13.2 KB
/
bytecode.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package bytecode
import (
"fmt"
)
type CompiledProgram struct {
Version int
Imports []*CompiledProgramImport
Code map[string][]Bytecode
}
type CompiledProgramImport struct {
Path string
Name string
}
func NewCompiledProgram() *CompiledProgram {
return &CompiledProgram{
Version: 1,
Code: map[string][]Bytecode{
"main": []Bytecode{},
// TODO(2021-08-26): Think of a better way to do this.
"Optional__Some": []Bytecode{
&PushEnum{"Some", 1},
},
},
}
}
type Bytecode interface {
fmt.Stringer
bytecode()
}
// Pop x, y from the stack and push x + y.
type BinaryAdd struct{}
// Pop x, y from the stack and push x ++ y where x and y are strings.
type BinaryConcat struct{}
// Pop x, y from the stack and push x == y.
type BinaryEq struct{}
// Pop x, y from the stack and push x > y.
type BinaryGt struct{}
// Pop x, y from the stack and push x >= y.
type BinaryGtEq struct{}
// Pop x, y from the stack and push x in y.
type BinaryIn struct{}
// Pop x, y from the stack and push x[y] where x is a list.
type BinaryListIndex struct{}
// Pop x, y from the stack and push x < y.
type BinaryLt struct{}
// Pop x, y from the stack and push x <= y.
type BinaryLtEq struct{}
// Pop x, y from the stack and push x[y] where x is a map.
type BinaryMapIndex struct{}
// Pop x, y from the stack and push x % y.
type BinaryModulo struct{}
// Pop x, y from the stack and push x * y.
type BinaryMul struct{}
// Pop x, y from the stack and push x != y.
type BinaryNotEq struct{}
// Same as BinaryAdd, BinaryMul, etc. except the operands are real numbers instead of
// integers.
type BinaryRealAdd struct{}
type BinaryRealDiv struct{}
type BinaryRealMul struct{}
type BinaryRealSub struct{}
// Pop x, y from the stack and push x[y] where x is a string.
type BinaryStringIndex struct{}
// Pop x, y from the stack and push x - y.
type BinarySub struct{}
// Pop N values from the stack, bundle them into a class object, and push it onto the stack.
type BuildClass struct {
Name string
N int
}
// Pop N values from the stack, bundle them into a list, and push it onto the stack.
type BuildList struct {
N int
}
// Pop N*2 values from the stack where each key is before its corresponding value
// (e.g., [K, V, K, V]), bundle them into a map, and push it onto the stack.
type BuildMap struct {
N int
}
// Pop N values from the stack, bundle them into a tuple, and push it onto the stack.
type BuildTuple struct {
N int
}
// Pop a function and N arguments from the top of stack (e.g., [arg3, arg2, arg1, f]),
// call the function with the arguments, and push the return value onto the stack if it
// is not void.
//
// Note that in case the top of the stack is a bound method object created by
// LOOKUP_METHOD, N-1 arguments will be popped from the stack as the first argument will
// be supplied by the bound method object.
type CallFunction struct {
N int
}
// Check the top of the stack, which must be an enum object, and push true if it has the
// given label, or false otherwise.
//
// Used to implement pattern-matching.
type CheckLabel struct {
Name string
}
// Duplicate the value at the top of the stack. This only copies the reference, not the
// value itself.
type DupTop struct{}
// Call Next() on the top of the stack, which must be an iterator. If it returns nil, jump
// N instructions forward in the program. Otherwise, push the return value onto the stack.
type ForIter struct {
N int
}
// Pop the top of the stack, convert it into an iterator, and push the iterator onto the
// stack.
type GetIter struct{}
// Pop the top of the stack, lookup the method called `Name`, and push a function object
// onto the stack.
type LookupMethod struct {
Name string
}
// Placeholder instruction used internally by the compiler.
type Placeholder struct {
Name string
}
// Push a constant boolean value onto the stack.
type PushConstBool struct {
Value bool
}
// Push a constant function object onto the stack.
type PushConstFunction struct {
Name string
IsBuiltin bool
}
// Push a constant integer value onto the stack.
type PushConstInt struct {
Value int
}
// Push a constant real number value onto the stack.
type PushConstRealNumber struct {
Value float64
}
// Push a constant string value onto the stack.
type PushConstStr struct {
Value string
}
// Push a constant enum object onto the stack.
type PushEnum struct {
Name string
N int
}
// Assuming the top of the stack is an enum object, push the value of the enum at `Index`.
//
// Note that unlike PushField and PushTupleField, this instruction leaves the enum on the
// stack.
type PushEnumIndex struct {
Index int
}
// Assuming the top of the stack is a class object, pop it and push its field at `Index`.
//
// Note that unlike PushEnumIndex, this instruction pops the class object from the stack.
type PushField struct {
Index int
}
// Push the value of the given symbol onto the stack.
type PushName struct {
Name string
}
// Assuming the top of the stack is a tuple, pop it and push its field at `Index`.
//
// Note that unlike PushEnumIndex, this instruction pops the tuple from the stack.
type PushTupleField struct {
Index int
}
// Jump N instructions forward in the program.
//
// If N = 0, it loops forever. If N = 1, it is a no-op. If N > 1, it skips over 1 or more
// instructions.
type RelJump struct {
N int
}
// Pop the top of the stack, which must be a boolean value. If it is false, jump N
// instructions forward in the program. Otherwise, continue as normal.
type RelJumpIfFalse struct {
N int
}
// Pop the top of the stack, which must be a boolean value. If it is false, jump N
// instructions forward in the program. Otherwise, pop the top of the stack and
// continue as normal.
type RelJumpIfFalseOrPop struct {
N int
}
// Pop the top of the stack, which must be a boolean value. If it is true, jump N
// instructions forward in the program. Otherwise, pop the top of the stack and
// continue as normal.
type RelJumpIfTrueOrPop struct {
N int
}
// Return from the current function.
type Return struct{}
// Pop x, y, z from the stack and push z, x, y.
type RotThree struct{}
// Pop x, y from the stack and store y in x[Index].
type StoreField struct {
Index int
}
// Pop x, y, z from the stack and store z in x[y] where x is a list.
type StoreIndex struct{}
// Pop x, y, z from the stack and store z in x[y] where x is a map.
type StoreMapIndex struct{}
// Pop a value from the stack and store it under `Name` in the current environment.
type StoreName struct {
Name string
}
// Pop x from the stack and push -x.
type UnaryMinus struct{}
// Pop x from the stack and push not x.
type UnaryNot struct{}
// Assuming the top of the stack is a tuple, pop it and push its contents individually
// onto the stack, e.g. if the stack is [..., x] and x = (a, b, c), then the stack will be
// [..., c, b, a].
type UnpackTuple struct{}
func (b *BinaryAdd) String() string {
return "BINARY_ADD"
}
func (b *BinaryConcat) String() string {
return "BINARY_CONCAT"
}
func (b *BinaryEq) String() string {
return "BINARY_EQ"
}
func (b *BinaryGt) String() string {
return "BINARY_GT"
}
func (b *BinaryGtEq) String() string {
return "BINARY_GT_EQ"
}
func (b *BinaryIn) String() string {
return "BINARY_IN"
}
func (b *BinaryListIndex) String() string {
return "BINARY_LIST_INDEX"
}
func (b *BinaryLt) String() string {
return "BINARY_LT"
}
func (b *BinaryLtEq) String() string {
return "BINARY_LT_EQ"
}
func (b *BinaryMapIndex) String() string {
return "BINARY_MAP_INDEX"
}
func (b *BinaryModulo) String() string {
return "BINARY_MODULO"
}
func (b *BinaryMul) String() string {
return "BINARY_MUL"
}
func (b *BinaryNotEq) String() string {
return "BINARY_NOT_EQ"
}
func (b *BinaryRealAdd) String() string {
return "BINARY_REAL_ADD"
}
func (b *BinaryRealDiv) String() string {
return "BINARY_REAL_DIV"
}
func (b *BinaryRealMul) String() string {
return "BINARY_REAL_MUL"
}
func (b *BinaryRealSub) String() string {
return "BINARY_REAL_SUB"
}
func (b *BinaryStringIndex) String() string {
return "BINARY_STRING_INDEX"
}
func (b *BinarySub) String() string {
return "BINARY_SUB"
}
func (b *BuildClass) String() string {
return fmt.Sprintf("BUILD_CLASS %q %d", b.Name, b.N)
}
func (b *BuildList) String() string {
return fmt.Sprintf("BUILD_LIST %d", b.N)
}
func (b *BuildMap) String() string {
return fmt.Sprintf("BUILD_MAP %d", b.N)
}
func (b *BuildTuple) String() string {
return fmt.Sprintf("BUILD_TUPLE %d", b.N)
}
func (b *CallFunction) String() string {
return fmt.Sprintf("CALL_FUNCTION %d", b.N)
}
func (b *CheckLabel) String() string {
return fmt.Sprintf("CHECK_LABEL %q", b.Name)
}
func (b *DupTop) String() string {
return "DUP_TOP"
}
func (b *ForIter) String() string {
return fmt.Sprintf("FOR_ITER %d", b.N)
}
func (b *GetIter) String() string {
return "GET_ITER"
}
func (b *LookupMethod) String() string {
return fmt.Sprintf("LOOKUP_METHOD %q", b.Name)
}
func (b *Placeholder) String() string {
return fmt.Sprintf("PLACEHOLDER %q", b.Name)
}
func (b *PushConstBool) String() string {
if b.Value {
return "PUSH_CONST_BOOL 1"
} else {
return "PUSH_CONST_BOOL 0"
}
}
func (b *PushConstFunction) String() string {
var x string
if b.IsBuiltin {
x = "1"
} else {
x = "0"
}
return fmt.Sprintf("PUSH_CONST_FUNCTION %q %s", b.Name, x)
}
func (b *PushConstInt) String() string {
return fmt.Sprintf("PUSH_CONST_INT %d", b.Value)
}
func (b *PushConstRealNumber) String() string {
return fmt.Sprintf("PUSH_CONST_REAL_NUMBER %f", b.Value)
}
func (b *PushConstStr) String() string {
return fmt.Sprintf("PUSH_CONST_STR %q", b.Value)
}
func (b *PushEnum) String() string {
return fmt.Sprintf("PUSH_ENUM %q %d", b.Name, b.N)
}
func (b *PushEnumIndex) String() string {
return fmt.Sprintf("PUSH_ENUM_INDEX %d", b.Index)
}
func (b *PushField) String() string {
return fmt.Sprintf("PUSH_FIELD %d", b.Index)
}
func (b *PushName) String() string {
return fmt.Sprintf("PUSH_NAME %q", b.Name)
}
func (b *PushTupleField) String() string {
return fmt.Sprintf("PUSH_TUPLE_FIELD %d", b.Index)
}
func (b *RelJump) String() string {
return fmt.Sprintf("REL_JUMP %d", b.N)
}
func (b *RelJumpIfFalse) String() string {
return fmt.Sprintf("REL_JUMP_IF_FALSE %d", b.N)
}
func (b *RelJumpIfFalseOrPop) String() string {
return fmt.Sprintf("REL_JUMP_IF_FALSE_OR_POP %d", b.N)
}
func (b *RelJumpIfTrueOrPop) String() string {
return fmt.Sprintf("REL_JUMP_IF_TRUE_OR_POP %d", b.N)
}
func (b *Return) String() string {
return "RETURN"
}
func (b *RotThree) String() string {
return "ROT_THREE"
}
func (b *StoreField) String() string {
return fmt.Sprintf("STORE_FIELD %d", b.Index)
}
func (b *StoreIndex) String() string {
return "STORE_INDEX"
}
func (b *StoreMapIndex) String() string {
return "STORE_MAP_INDEX"
}
func (b *StoreName) String() string {
return fmt.Sprintf("STORE_NAME %q", b.Name)
}
func (b *UnaryMinus) String() string {
return "UNARY_MINUS"
}
func (b *UnaryNot) String() string {
return "UNARY_NOT"
}
func (b *UnpackTuple) String() string {
return "UNPACK_TUPLE"
}
func (b *BinaryAdd) bytecode() {}
func (b *BinaryConcat) bytecode() {}
func (b *BinaryEq) bytecode() {}
func (b *BinaryGt) bytecode() {}
func (b *BinaryGtEq) bytecode() {}
func (b *BinaryIn) bytecode() {}
func (b *BinaryListIndex) bytecode() {}
func (b *BinaryLt) bytecode() {}
func (b *BinaryLtEq) bytecode() {}
func (b *BinaryMapIndex) bytecode() {}
func (b *BinaryModulo) bytecode() {}
func (b *BinaryMul) bytecode() {}
func (b *BinaryNotEq) bytecode() {}
func (b *BinaryRealAdd) bytecode() {}
func (b *BinaryRealDiv) bytecode() {}
func (b *BinaryRealMul) bytecode() {}
func (b *BinaryRealSub) bytecode() {}
func (b *BinaryStringIndex) bytecode() {}
func (b *BinarySub) bytecode() {}
func (b *BuildClass) bytecode() {}
func (b *BuildList) bytecode() {}
func (b *BuildMap) bytecode() {}
func (b *BuildTuple) bytecode() {}
func (b *CallFunction) bytecode() {}
func (b *CheckLabel) bytecode() {}
func (b *DupTop) bytecode() {}
func (b *ForIter) bytecode() {}
func (b *GetIter) bytecode() {}
func (b *LookupMethod) bytecode() {}
func (b *Placeholder) bytecode() {}
func (b *PushConstBool) bytecode() {}
func (b *PushConstInt) bytecode() {}
func (b *PushConstFunction) bytecode() {}
func (b *PushConstRealNumber) bytecode() {}
func (b *PushConstStr) bytecode() {}
func (b *PushEnum) bytecode() {}
func (b *PushEnumIndex) bytecode() {}
func (b *PushField) bytecode() {}
func (b *PushName) bytecode() {}
func (b *PushTupleField) bytecode() {}
func (b *RelJump) bytecode() {}
func (b *RelJumpIfFalse) bytecode() {}
func (b *RelJumpIfFalseOrPop) bytecode() {}
func (b *RelJumpIfTrueOrPop) bytecode() {}
func (b *Return) bytecode() {}
func (b *RotThree) bytecode() {}
func (b *StoreField) bytecode() {}
func (b *StoreIndex) bytecode() {}
func (b *StoreMapIndex) bytecode() {}
func (b *StoreName) bytecode() {}
func (b *UnaryMinus) bytecode() {}
func (b *UnaryNot) bytecode() {}
func (b *UnpackTuple) bytecode() {}