-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
291 lines (281 loc) · 5.29 KB
/
main.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
package main
import (
"befunge/stack"
"bufio"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"strconv"
)
type BefungeMatrix struct {
code [][]int
s *stack.Stack
pos [2]int
vector [2]int
}
func getLines(s string) []string {
main := []string{}
aggr := ""
max := 0
for _, c := range s {
if c == '\n' {
if max < len(aggr) {
max = len(aggr)
}
main = append(main, aggr)
aggr = ""
continue
}
aggr += string(c)
}
if max < len(aggr) {
max = len(aggr)
}
main = append(main, aggr)
for i, v := range main {
if len(v) < max {
for j := 0; j <= max-len(v); j++ {
main[i] += " "
}
}
}
return main
}
func createMatrix(s []string) [][]int {
main := [][]int{}
for _, line := range s {
temp := []int{}
for _, c := range line {
temp = append(temp, int(c))
}
main = append(main, temp)
}
return main
}
func (Bm *BefungeMatrix) setRight() {
Bm.vector = [2]int{0, 1}
}
func (Bm *BefungeMatrix) setLeft() {
Bm.vector = [2]int{0, -1}
}
func (Bm *BefungeMatrix) setDown() {
Bm.vector = [2]int{1, 0}
}
func (Bm *BefungeMatrix) setUp() {
Bm.vector = [2]int{-1, 0}
}
func (Bm *BefungeMatrix) Next() {
Bm.pos[0] += Bm.vector[0]
Bm.pos[1] += Bm.vector[1]
tmp := [2]int{Bm.pos[0], Bm.pos[1]}
if Bm.pos[0] == -1 {
// going up
tmp[0] = len(Bm.code) - 1
} else if Bm.pos[1] == -1 {
// going left
tmp[1] = len(Bm.code[0]) - 1
} else if Bm.pos[0] == len(Bm.code) {
// going down
tmp[0] = 0
} else if Bm.pos[1] == len(Bm.code[Bm.pos[0]]) {
// bottom right going right
tmp[1] = 0
}
Bm.pos = tmp
// fmt.Println(Bm.s)
}
func runFromFile(fpath string) error {
if path.Ext(fpath) == ".bf" {
f, err := ioutil.ReadFile(fpath)
if err != nil {
return errors.New("an error occured while reading the file: " + err.Error())
}
s := stack.Stack{Items: []int{}}
bm := BefungeMatrix{code: createMatrix(getLines(string(f))), s: &s, vector: [2]int{0, 1}, pos: [2]int{0, 0}}
bm.RunCode()
}
return nil
}
func (Bm *BefungeMatrix) RunCode() {
stringMode := false
for Bm.code[Bm.pos[0]][Bm.pos[1]] != '@' {
curr := Bm.code[Bm.pos[0]][Bm.pos[1]]
if stringMode && curr == '"' {
stringMode = !stringMode
Bm.Next()
continue
}
if stringMode {
Bm.s.Push(curr)
Bm.Next()
continue
}
switch curr {
case '+':
// Addition +
a := Bm.s.Pop()
b := Bm.s.Pop()
Bm.s.Push(a + b)
case '-':
// Subtraction -
a := Bm.s.Pop()
b := Bm.s.Pop()
Bm.s.Push(b - a)
case '*':
// Multiplication *
a := Bm.s.Pop()
b := Bm.s.Pop()
Bm.s.Push(a * b)
case '/':
// Division /
a := Bm.s.Pop()
b := Bm.s.Pop()
Bm.s.Push(int(b / a))
case '%':
// Modulo %
a := Bm.s.Pop()
b := Bm.s.Pop()
Bm.s.Push(b % a)
case '!':
// Logical Not !
a := Bm.s.Pop()
if a == 0 {
Bm.s.Push(1)
} else {
Bm.s.Push(0)
}
case '`':
// Greate Than `
a := Bm.s.Pop()
b := Bm.s.Pop()
if b > a {
Bm.s.Push(1)
} else {
Bm.s.Push(0)
}
case '>':
// right >
Bm.setRight()
case '<':
// left <
Bm.setLeft()
case '^':
// up ^
Bm.setUp()
case 'v':
// down v
Bm.setDown()
case '?':
// random direction ?
randNum := rand.Intn(4)
switch randNum {
case 0:
// right >
Bm.setRight()
case 1:
// left <
Bm.setLeft()
case 2:
// up ^
Bm.setUp()
case 3:
// down v
Bm.setDown()
}
case '_':
// Horizontal IF _
n := Bm.s.Pop()
if n == 0 {
Bm.setRight()
} else {
Bm.setLeft()
}
case '|':
// Vertical IF |
n := Bm.s.Pop()
if n == 0 {
Bm.setDown()
} else {
Bm.setUp()
}
case '"':
// StringMode "
stringMode = !stringMode
case ':':
// Duplicate Push :
Bm.s.Push(Bm.s.Peek())
case '\\':
// Swap Stack \
m := Bm.s.Pop()
n := Bm.s.Pop()
Bm.s.Push(m)
Bm.s.Push(n)
case '$':
// Discard Stack Top $
Bm.s.Pop()
case '.':
// Output as Int .
fmt.Printf("%d", Bm.s.Pop())
case ',':
// Output as ASCII Char ,
fmt.Printf("%s", string(rune(Bm.s.Pop())))
case '#':
// Bridge Jump Cmd #
Bm.Next()
Bm.Next()
continue
case 'g':
// Get call g
y := Bm.s.Pop()
x := Bm.s.Pop()
if y >= 0 && x >= 0 && y < len(Bm.code) && x < len(Bm.code[y]) {
Bm.s.Push(Bm.code[y][x])
} else {
Bm.s.Push(0)
}
case 'p':
// Put call p
y := Bm.s.Pop()
x := Bm.s.Pop()
v := Bm.s.Pop()
if y >= 0 && x >= 0 && y < len(Bm.code) && x < len(Bm.code[y]) {
Bm.code[y][x] = v
}
case '&':
// Get Int Input &
n := 0
fmt.Scanf("%d", &n)
Bm.s.Push(n)
case '~':
// Get Char Input ~
reader := bufio.NewReader(os.Stdin)
r, _, _ := reader.ReadRune()
Bm.s.Push(int(r))
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// Push to Stack
num, _ := strconv.Atoi(string(rune(curr)))
Bm.s.Push(num)
}
Bm.Next()
// if Bm.pos[0] == -1 || Bm.pos[1] == -1 {
// fmt.Println("Stack", Bm.s)
// fmt.Println("Pos", Bm.pos)
// fmt.Println("Vector", Bm.vector)
// fmt.Println("Cmd", Bm.code)
// }
}
}
func main() {
// Befunge-97/98 have issues
if os.Args[1] != "" {
err := runFromFile(os.Args[1])
if err != nil {
panic(errors.New("an error occured while opening the given file path: " + err.Error()))
}
} else {
panic(errors.New("an error occured while opening the file path: " + os.Args[1]))
}
}