This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
136 lines (123 loc) · 2.57 KB
/
state.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
package fidgetlang
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func NewState() *State {
return &State{
registers: [256]int32{},
position: 0,
instruction: 0,
instructionMemory: []instruction{},
buffReader: bufio.NewReader(os.Stdin),
}
}
type State struct {
registers [256]int32
position byte
instruction uint32
instructionMemory []instruction
buffReader *bufio.Reader
DebugOut bool
SuperDebug bool
}
func (s *State) CompileDebug(program string) {
s.instructionMemory = compileDebug(program)
}
func (s *State) Compile(program string) {
s.instructionMemory = compileProgram(program)
}
func (s *State) GetDebugProgram() string {
return makeDebug(s.instructionMemory)
}
func (s *State) bug(text string, args ...interface{}) {
if !s.SuperDebug {
return
}
var sArgs = make([]string, len(args))
for k := range args {
sArgs[k] = fmt.Sprint(args[k])
}
println(strings.Join(append([]string{text}, sArgs...), " "))
}
// Step returns false if the end of memory is reached
func (s *State) Step() bool {
if s.overEnd() {
return false
}
s.bug("X at I", s.instruction)
switch s.curInstr() {
case iIncrPtr:
s.bug("I of P")
s.position++
case iDecrPtr:
s.bug("D of P")
s.position--
case iIncrVal:
s.bug("I of V")
s.setData(s.curData() + 1)
case iDecrVal:
s.bug("D of V")
s.setData(s.curData() - 1)
case iPutVal:
s.bug("P of V")
if s.DebugOut {
fmt.Printf("0x%04X\n", s.curData())
} else {
fmt.Printf("%s", string(rune(s.curData())))
}
case iGetVal:
s.bug("R of V")
rrune, _, err := s.buffReader.ReadRune()
if err == io.EOF {
return false
}
if err != nil {
panic(err)
}
s.setData(rrune)
case iJumpBehind:
if s.curData() != 0 {
for s.curInstr() != iJumpAhead {
s.instruction--
}
s.bug("JB to I", s.instruction)
return true
}
case iJumpAhead:
if s.atEnd() {
s.bug("JA abort at", s.instruction)
return false
}
if s.curData() == 0 {
for s.curInstr() != iJumpBehind {
s.instruction++
}
s.bug("JA to I", s.instruction)
return true
}
}
if s.atEnd() {
return false
}
s.instruction++
return true
}
func (s *State) atEnd() bool {
return int(s.instruction) == len(s.instructionMemory)-1
}
func (s *State) overEnd() bool {
return int(s.instruction) >= len(s.instructionMemory)
}
func (s *State) curInstr() instruction {
return instruction(s.instructionMemory[s.instruction])
}
func (s *State) curData() int32 {
return s.registers[s.position]
}
func (s *State) setData(d int32) {
s.registers[s.position] = d
}