-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.go
64 lines (55 loc) · 893 Bytes
/
ops.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
package cpu
func NOP(c *CPU) {}
func DEC(c *CPU) {
c.tape[c.tp]--
}
func INC(c *CPU) {
c.tape[c.tp]++
}
func PREV(c *CPU) {
if c.tp > 0 {
c.tp--
return
}
newTape := make([]byte, len(c.tape), (len(c.tape)+1)*2)
c.tp = uint(len(c.tape) - 1)
c.tape = append(newTape, c.tape...)
}
func NEXT(c *CPU) {
c.tp++
if c.tp < uint(len(c.tape)) {
return
}
newTape := make([]byte, (len(c.tape)+1)*2)
copy(newTape, c.tape)
c.tape = newTape
}
func IN(c *CPU) {
char, err := c.in.ReadByte()
if err != nil {
panic(err)
}
c.tape[c.tp] = char
}
func OUT(c *CPU) {
if err := c.out.WriteByte(c.tape[c.tp]); err != nil {
panic(err)
}
if err := c.out.Flush(); err != nil {
panic(err)
}
}
func FWD(ip uint) Op {
return func(c *CPU) {
if c.tape[c.tp] == 0 {
c.ip = ip
}
}
}
func BACK(ip uint) Op {
return func(c *CPU) {
if c.tape[c.tp] != 0 {
c.ip = ip
}
}
}