-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (117 loc) · 2.89 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
// Bfc interpreter written in go
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"unsafe"
"github.com/ultrabear/bfi/compiler"
"github.com/ultrabear/bfi/constants"
"github.com/ultrabear/bfi/render"
"github.com/ultrabear/bfi/runtime"
)
func max(x, y int) int {
if x > y {
return x
}
return y
}
// fatal will print to stderr and exit
func fatal(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
os.Exit(1)
}
var (
lStartByte = []byte{'['}
lEndByte = []byte{']'}
)
func ustring(s []byte) string {
return *(*string)(unsafe.Pointer(&s))
}
// RunCompile compiles a stream of bfc to compressed bfc and a intfuck stream, it also optimizes the stream
func RunCompile(indata []byte) ([]byte, []uint) {
// Check amount of loops
if bytes.Count(indata, lStartByte) != bytes.Count(indata, lEndByte) {
fatal(constants.SyntaxUnbalanced)
}
// Compress brainfuck and run static optimizations
brainfuck := compiler.CompressBFC(indata)
brainfuck = []byte(strings.NewReplacer("[-]", "0", "[+]", "0").Replace(ustring(brainfuck)))
// Get count of loop items
LoopCount := bytes.Count(brainfuck, lStartByte) * 2
// Convert to intfuck and optimize
intfuck := compiler.PMoptimize(compiler.ToIntfuck(brainfuck, LoopCount))
intfuck = compiler.GetJumpMap(intfuck, LoopCount)
return brainfuck, intfuck
}
// RunFull runs the given brainfuck from compiling to running to exiting error handling
func RunFull(indata []byte) {
brainfuck, intfuck := RunCompile(indata)
// Instantiate brainfuck execution environment
bfc := runtime.Initbfc(max(bytes.Count(brainfuck, []byte{'>'})+1, 30000))
// Run brainfuck
bfc.RunUnsafe(intfuck)
}
type conf struct {
n string
b bool
}
type confContainer map[string]*conf
func (C confContainer) newConf(n string) *conf {
c := &conf{n: n}
C[n] = c
return c
}
func (C confContainer) parse() {
if len(os.Args) > 1 {
for k, v := range C {
v.b = strings.Contains(os.Args[1], k)
}
}
}
func getbrainfuckstring(b bool) []byte {
var indata []byte
if b {
if len(os.Args) > 2 {
cont, readerr := os.ReadFile(os.Args[2])
if readerr != nil {
fatal(constants.Error+"Could not open file:", os.Args[2])
}
indata = cont
} else {
fatal(constants.Error + "Could not open file:")
}
} else {
indata = []byte(strings.Join(os.Args[1:], ""))
}
return indata
}
func renderbrainfuck(indata []byte, renderc bool) {
brainfuck, intfuck := RunCompile(indata)
if renderc {
cintf := render.CIntFuck{
Data: intfuck,
Len: max(bytes.Count(brainfuck, []byte{'>'})+1, 30000),
}
w := bufio.NewWriter(os.Stdout)
_, _ = cintf.WriteTo(w)
w.Flush()
return
}
fmt.Println(render.StrIntFuck(intfuck))
}
func main() {
conf := make(confContainer)
f := conf.newConf("f")
r := conf.newConf("r")
c := conf.newConf("c")
conf.parse()
indata := getbrainfuckstring(f.b)
if r.b {
renderbrainfuck(indata, c.b)
os.Exit(0)
}
RunFull(indata)
}