-
Notifications
You must be signed in to change notification settings - Fork 0
/
big.go
118 lines (101 loc) · 1.72 KB
/
big.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
package BiG
import (
"io"
"os"
"time"
"bytes"
"fmt"
)
const (
PAGEWIDTH = 80
PAGEHEIGHT = 25
)
type FungeSpace [PAGEHEIGHT][PAGEWIDTH]int8
func (fs FungeSpace) String() string {
buff := new(bytes.Buffer)
for i := 0; i < PAGEHEIGHT; i++ {
for j := 0; j < PAGEWIDTH; j++ {
fmt.Fprint(buff, string(fs[i][j]))
}
fmt.Fprintln(buff)
}
return buff.String()
}
type InstructionPointer struct {
WE, NS int8
}
func (ip *InstructionPointer) Add(d Delta) {
ip.NS += d[1]
ip.WE += d[0]
if ip.NS < 0 {
ip.NS = PAGEHEIGHT + ip.NS
}
if ip.NS > 24 {
ip.NS = ip.NS % PAGEHEIGHT
}
if ip.WE < 0 {
ip.WE = PAGEWIDTH + ip.WE
}
if ip.WE > 24 {
ip.WE = ip.WE % PAGEWIDTH
}
}
type Delta [2]int8
var (
LEFT = Delta{-1, 0}
UP = Delta{0, -1}
RIGHT = Delta{1, 0}
DOWN = Delta{0, 1}
)
type InstructionSet map[int8]func(*VM)
func (old InstructionSet) Clone() (newIS InstructionSet) {
newIS = make(InstructionSet)
for k, v := range old {
newIS[k] = v
}
return
}
type VM struct {
FS *FungeSpace
IP *InstructionPointer
IS InstructionSet
Delta *Delta
SP Stack
quitting bool
Stdin io.Reader
Stdout io.Writer
}
func NewVM(fs *FungeSpace) (vm *VM) {
vm = new(VM)
vm.Delta = &RIGHT
vm.quitting = false
vm.SP = NewStack()
vm.IS = StdIS
vm.IP = &InstructionPointer{0, 0}
vm.FS = fs
vm.Stdin = os.Stdin
vm.Stdout = os.Stdout
return
}
func (vm *VM) Tick() {
f := vm.IS[vm.FS[vm.IP.NS][vm.IP.WE]]
if f != nil {
f(vm)
}
vm.IP.Add(*vm.Delta)
//fmt.Println("Tick end!")
}
func (vm *VM) Run(ticker *time.Ticker) {
for _ = range ticker.C {
if vm.quitting {
break
}
vm.Tick()
}
}
func (vm VM) Done() bool {
return vm.quitting
}
func (vm *VM) Exit() {
vm.quitting = true
}