-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.go
188 lines (174 loc) · 4.08 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
package main
import (
"fmt"
"io"
"math"
"strconv"
"github.com/pkg/errors"
)
type VM struct {
stack []int
heap map[int]int
callStack []int
index int
labels map[int]int
cs []Command
}
func NewVM(cs []Command) *VM {
vm := &VM{
stack: []int{},
heap: map[int]int{},
callStack: []int{},
index: 0,
labels: map[int]int{},
cs: cs,
}
return vm
}
func (v *VM) Eval(r io.Reader, w io.Writer) error {
v.prepareLabels()
analysisCh, wg := StartAnalysis(v.cs)
defer func() {
if analysisCh != nil {
close(analysisCh)
wg.Wait()
}
}()
for {
c := v.cs[v.index]
if analysisCh != nil {
analysisCh <- c
}
// fmt.Println(v.stack)
// fmt.Println(v.heap)
// fmt.Printf("index: %d\n", v.index)
// fmt.Println("--------------------")
// fmt.Printf("%+v\n", c)
switch c.Type {
case C_STACK_PUSH:
v.stack = append(v.stack, c.Operand)
case C_STACK_DUP:
v.stack = append(v.stack, v.stack[len(v.stack)-1])
case C_STACK_SWAP:
a := v.stack[len(v.stack)-1]
b := v.stack[len(v.stack)-2]
v.stack[len(v.stack)-1] = b
v.stack[len(v.stack)-2] = a
case C_STACK_POP:
v.stack = v.stack[:len(v.stack)-1]
case C_CALC_ADD:
v.stack[len(v.stack)-2] = v.stack[len(v.stack)-2] + v.stack[len(v.stack)-1]
v.stack = v.stack[:len(v.stack)-1]
case C_CALC_SUB:
v.stack[len(v.stack)-2] = v.stack[len(v.stack)-2] - v.stack[len(v.stack)-1]
v.stack = v.stack[:len(v.stack)-1]
case C_CALC_MULTI:
v.stack[len(v.stack)-2] = v.stack[len(v.stack)-2] * v.stack[len(v.stack)-1]
v.stack = v.stack[:len(v.stack)-1]
case C_CALC_DIV:
v.stack[len(v.stack)-2] = div(v.stack[len(v.stack)-2], v.stack[len(v.stack)-1])
v.stack = v.stack[:len(v.stack)-1]
case C_CALC_MOD:
v.stack[len(v.stack)-2] = mod(v.stack[len(v.stack)-2], v.stack[len(v.stack)-1])
v.stack = v.stack[:len(v.stack)-1]
case C_HEAP_SAVE:
val := v.stack[len(v.stack)-1]
addr := v.stack[len(v.stack)-2]
v.heap[addr] = val
v.stack = v.stack[:len(v.stack)-2]
case C_HEAP_LOAD:
addr := v.stack[len(v.stack)-1]
v.stack[len(v.stack)-1] = v.heap[addr]
case C_FLOW_DEF:
// skip
case C_FLOW_CALL:
v.callStack = append(v.callStack, v.index)
v.index = v.labels[c.Operand]
case C_FLOW_JUMP:
idx, ok := v.labels[c.Operand]
if !ok {
return fmt.Errorf("%d is not valid label", c.Operand)
}
v.index = idx
case C_FLOW_JUMP_IF_ZERO:
if v.stack[len(v.stack)-1] == 0 {
idx, ok := v.labels[c.Operand]
if !ok {
return fmt.Errorf("%d is not valid label", c.Operand)
}
v.index = idx
}
v.stack = v.stack[:len(v.stack)-1]
case C_FLOW_JUMP_IF_NEG:
if v.stack[len(v.stack)-1] < 0 {
idx, ok := v.labels[c.Operand]
if !ok {
return fmt.Errorf("%d is not valid label", c.Operand)
}
v.index = idx
}
v.stack = v.stack[:len(v.stack)-1]
case C_FLOW_END:
v.index = v.callStack[len(v.callStack)-1]
v.callStack = v.callStack[:len(v.callStack)-1]
case C_FLOW_EXIT:
return nil
case C_IO_WRITE_CH:
b := v.stack[len(v.stack)-1]
fmt.Fprintf(w, "%c", b)
v.stack = v.stack[:len(v.stack)-1]
case C_IO_WRITE_NUM:
i := v.stack[len(v.stack)-1]
fmt.Fprint(w, i)
v.stack = v.stack[:len(v.stack)-1]
case C_IO_READ_CH:
addr := v.stack[len(v.stack)-1]
buf := []byte{0}
r.Read(buf)
v.heap[addr] = int(buf[0])
v.stack = v.stack[:len(v.stack)-1]
case C_IO_READ_NUM:
addr := v.stack[len(v.stack)-1]
i, err := readint(r)
if err != nil {
return err
}
v.heap[addr] = i
v.stack = v.stack[:len(v.stack)-1]
default:
return errors.New("Unreachable")
}
v.index += 1
}
}
func (v *VM) prepareLabels() {
for idx, c := range v.cs {
if c.Type == C_FLOW_DEF {
v.labels[c.Operand] = idx
}
}
}
func div(i, j int) int {
return int(math.Floor(float64(i) / float64(j)))
}
func mod(i, j int) int {
r := i % j
if r < 0 {
return j + r
}
return r
}
func readint(r io.Reader) (int, error) {
buf := []byte{0}
s := ""
for {
_, err := r.Read(buf)
if err != nil {
return 0, err
}
if buf[0] == '\n' {
return strconv.Atoi(s)
}
s += string(buf)
}
}