-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.go
441 lines (361 loc) · 8.04 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
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
package gmh
import (
"errors"
"fmt"
"strconv"
"strings"
)
// Create a new instance of the VM
func New() *VM {
return &VM{
stack: newStack(),
heap: make([]int, 0),
buf: "",
}
}
// The interrupter function will be called in the main loop
// If the function returns true, the execution will be terminated immediately.
func (this *VM) SetInterrupter(f func() bool) *VM {
this.interrupter = f
return this
}
// The input function will be called when input buffer runs out
// The function should return a string for input buffer.
func (this *VM) SetInput(f func(string) string) *VM {
this.input = f
return this
}
func (this *VM) readInput(res *string) (r byte) {
if this.input == nil {
r = ' '
return
}
if len(this.buf) == 0 {
this.buf = this.input(*res)
(*res) = ""
}
if len(this.buf) > 0 {
r = this.buf[0]
this.buf = this.buf[1:]
} else {
r = ' '
}
return
}
// Execute commands
func (this *VM) Exec(cmds []string) (res string, err error) {
labels := make(map[int]int)
calls := newStack()
// Scan for labels
for i := 0; i < len(cmds); i++ {
cmd := cmds[i]
if strings.HasPrefix(cmd, FLOW) {
c := cmd[len(FLOW):]
if strings.HasPrefix(c, MARK) {
var num int
num, err = parseNum(c[len(MARK):], false)
if err == nil {
labels[num] = i
} else {
return
}
}
}
}
for i := 0; i < len(cmds); i++ {
if (this.interrupter != nil) && this.interrupter() {
err = errors.New("Interrupted.")
return
}
cmd := cmds[i]
switch {
case strings.HasPrefix(cmd, STACK):
err = this.opStack(cmd[len(STACK):])
case strings.HasPrefix(cmd, MATH):
err = this.opMath(cmd[len(MATH):])
case strings.HasPrefix(cmd, HEAP):
err = this.opHeap(cmd[len(HEAP):])
case strings.HasPrefix(cmd, IO):
// I/O control, do it here
c := cmd[len(IO):]
switch {
case strings.HasPrefix(c, PRINT_CHAR):
m := this.stack.pop()
if m != nil {
res += string(m.(int))
}
case strings.HasPrefix(c, PRINT_NUM):
m := this.stack.pop()
if m != nil {
res += fmt.Sprintf("%d", m.(int))
}
case strings.HasPrefix(c, SCAN_CHAR):
m := this.stack.pop()
if m != nil {
r := this.readInput(&res)
if this.checkHeap(m.(int), true) {
this.heap[m.(int)] = int(r)
}
}
case strings.HasPrefix(c, SCAN_NUM):
m := this.stack.pop()
if m != nil {
r := this.readInput(&res)
var n int64
n, err = strconv.ParseInt(string(r), 10, 32)
if this.checkHeap(m.(int), true) {
this.heap[m.(int)] = int(n)
}
}
}
case strings.HasPrefix(cmd, FLOW):
// Flow control, do it here
c := cmd[len(FLOW):]
switch {
case strings.HasPrefix(c, END1):
return
//case strings.HasPrefix(c, MARK):
// Mark a point
// var num int
// num, err = parseNum(c[len(MARK):], false)
// if err == nil {
// labels[num] = i
// }
case strings.HasPrefix(c, CALL):
// Call a label as a function
var num int
num, err = parseNum(c[len(CALL):], false)
if err == nil {
calls.push(i)
if val, ok := labels[num]; ok {
i = val
} else {
err = errors.New(fmt.Sprintf("Label %d not found.", num))
}
}
case strings.HasPrefix(c, RETURN):
m := calls.pop()
if m == nil {
err = errors.New("Cannot return to caller.")
return
}
i = m.(int)
case strings.HasPrefix(c, JUMP):
// Jump to a label
var num int
num, err = parseNum(c[len(JUMP):], false)
if err == nil {
if val, ok := labels[num]; ok {
i = val
}
}
case strings.HasPrefix(c, JUMP_ZERO):
// Jump to a label if top of stack is 0
m := this.stack.pop()
if m == nil {
err = errors.New("Stack has no elements.")
return
}
if m.(int) == 0 {
var num int
num, err = parseNum(c[len(JUMP_ZERO):], false)
if err == nil {
if val, ok := labels[num]; ok {
i = val
}
}
}
case strings.HasPrefix(c, JUMP_SUBZERO):
// Jump to a label if top of stack is negative
m := this.stack.pop()
if m == nil {
err = errors.New("Stack has no elements.")
return
}
if m.(int) < 0 {
var num int
num, err = parseNum(c[len(JUMP_SUBZERO):], false)
if err == nil {
if val, ok := labels[num]; ok {
i = val
}
}
}
}
case cmd == END2:
return
}
//fmt.Println(i)
if err != nil {
break
}
}
return
}
// Stack operation
func (this *VM) opStack(cmd string) (err error) {
switch {
case strings.HasPrefix(cmd, PUSH):
// Push into stack
var num int
num, err = parseNum(cmd[len(PUSH):], true)
if err == nil {
this.stack.push(num)
}
case strings.HasPrefix(cmd, DUPLICATE):
// Duplicate the top item
m := this.stack.pop()
// Double-push
this.stack.push(m).push(m)
case strings.HasPrefix(cmd, COPY):
// Copy an item onto the top
var num int
num, err = parseNum(cmd[len(COPY):], true)
if err == nil {
tmp := newStack()
for i := 0; i < num-1; i++ {
tmp.push(this.stack.pop())
}
m := this.stack.pop()
this.stack.push(m)
for {
t := tmp.pop()
if t == nil {
break
}
this.stack.push(t)
}
this.stack.push(m)
}
case strings.HasPrefix(cmd, SWAP):
// Swap the top two items
m := this.stack.pop()
n := this.stack.pop()
this.stack.push(m)
this.stack.push(n)
case strings.HasPrefix(cmd, DISCARD):
// Discard the top
this.stack.pop() // Throw it to GC
case strings.HasPrefix(cmd, SLIDE):
// Slide n items from the top but keep the top
var num int
num, err = parseNum(cmd[len(SLIDE):], true)
if err == nil {
m := this.stack.pop()
for i := 0; i < num; i++ {
this.stack.pop()
}
this.stack.push(m)
}
}
return
}
// Math operation
func (this *VM) opMath(cmd string) (err error) {
m := this.stack.pop()
n := this.stack.pop()
if (m == nil) || (n == nil) {
err = errors.New("Can't evaluate when arguments are null")
return
}
x := m.(int)
y := n.(int)
switch {
case strings.HasPrefix(cmd, ADD):
this.stack.push(y + x)
return
case strings.HasPrefix(cmd, SUB):
this.stack.push(y - x)
return
case strings.HasPrefix(cmd, DIV):
if x == 0 {
err = errors.New("Cannot devide 0")
return
}
this.stack.push(y / x)
return
case strings.HasPrefix(cmd, MUL):
this.stack.push(y * x)
return
case strings.HasPrefix(cmd, MOD):
this.stack.push(y % x)
return
}
// If arrived here, we've got an invalid operation
err = errors.New("Invalid operation" + cmd)
return
}
// Heap
func (this *VM) opHeap(cmd string) (err error) {
switch {
case strings.HasPrefix(cmd, STORE):
m := this.stack.pop()
n := this.stack.pop()
if (m == nil) || (n == nil) {
err = errors.New("Missing argument for heap operation")
return
}
x := m.(int)
y := n.(int)
if !this.checkHeap(y, true) {
err = errors.New(fmt.Sprintf("Heap address %d inaccessible", y))
}
this.heap[y] = x
return
case strings.HasPrefix(cmd, RETRIEVE):
m := this.stack.pop()
if m == nil {
err = errors.New("Missing argument for heap operation")
return
}
x := m.(int)
if !this.checkHeap(x, false) {
err = errors.New(fmt.Sprintf("Invalid heap address %d", x))
return
}
this.stack.push(this.heap[x])
return
}
// Errored
err = errors.New("Illegal operation " + cmd)
return
}
func (this *VM) checkHeap(i int, alloc bool) bool {
if (i >= 65535) || (i < 0) {
// Overflown
return false
}
if alloc && (i >= len(this.heap)) {
for {
this.heap = append(this.heap, 0)
if i < len(this.heap) {
break
}
}
}
return true
}
func parseNum(str string, signed bool) (res int, err error) {
str = strings.Trim(str, " ")
if len(str) == 0 {
err = errors.New("Can't parse empty number.")
return
}
i := strings.LastIndex(str, NUM_END)
if i > 0 {
str = str[:i]
}
if signed {
if strings.HasPrefix(str, ZERO) {
str = str[len(ZERO):]
} else if strings.HasPrefix(str, ONE) {
str = "-" + str[len(ONE):]
}
}
str = strings.Replace(str, ZERO, "0", -1)
str = strings.Replace(str, ONE, "1", -1)
r, e := strconv.ParseInt(str, 2, 32)
res = int(r)
err = e
return
}