-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (152 loc) · 3.62 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
package main
import (
"flag"
"fmt"
"io"
"math"
"os"
"strings"
)
const (
add10 = ">%s[-<++++++++++>]<"
sub10 = ">%s[-<---------->]<"
errMsg = "couldn't %s `%s` (%s)"
)
type Flags struct {
width uint
output string
from string
doWrap bool
}
func parseFlags() Flags {
var f Flags
flag.UintVar(
&f.width, "w", 0, "split output into lines of specified width",
)
flag.StringVar(
&f.output, "o", "", "write the output to a specified file",
)
flag.StringVar(
&f.from, "f", "", "get the input from a specified file",
)
flag.BoolVar(
&f.doWrap, "do-wrap", false, "consider cell wrapping while creating output",
)
flag.Parse()
return f
}
func formatError(err error, verb string) error {
if err, ok := err.(*os.PathError); ok {
return fmt.Errorf(errMsg, verb, err.Path, err.Err)
}
return fmt.Errorf("unknown error encountered")
}
func getOutputFile(f Flags) (*os.File, error) {
if f.output == "" {
return os.Stdout, nil
}
outFile, err := os.Create(f.output)
if err != nil {
return nil, formatError(err, "create")
}
return outFile, nil
}
func printOutput(output string, f Flags) error {
outFile, err := getOutputFile(f)
if err != nil {
return err
}
if f.width == 0 {
f.width = uint(len(output))
}
for 0 < len(output) {
a := float64(len(output))
b := float64(f.width)
width := int(math.Min(a, b))
fmt.Fprintln(outFile, output[:width])
output = output[width:]
}
if outFile != os.Stdout {
outFile.Close()
}
return nil
}
// TODO: implement a better conversion algorithm
func toBrainfuck(input string, f Flags) string {
var builder strings.Builder
var last byte
for i := 0; i < len(input); i++ {
char := input[i]
delta := int(char) - int(last)
loops := delta / 10
extra := delta % 10
symbol := "+"
loop := add10
if delta < 0 {
loops = -delta / 10
extra = -delta % 10
symbol = "-"
loop = sub10
}
if 0 < loops {
loop = fmt.Sprintf(loop, strings.Repeat("+", loops))
builder.WriteString(loop)
}
builder.WriteString(strings.Repeat(symbol, extra))
builder.WriteByte('.')
last = char
}
return builder.String()
}
func readFile(r io.Reader) string {
var builder strings.Builder
buffer := make([]byte, 1024)
for {
n, err := r.Read(buffer)
if n == 0 && err == io.EOF {
break
}
if err != nil {
panic(err)
}
builder.Write(buffer[:n])
}
return builder.String()
}
func getInput(f Flags) (string, error) {
stat, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
return readFile(os.Stdin), nil
}
if f.from != "" {
inFile, err := os.Open(f.from)
if err != nil {
return "", formatError(err, "open")
}
defer inFile.Close()
input := readFile(inFile)
return input, nil
}
if flag.NArg() == 0 {
return "", fmt.Errorf("no input provided")
}
return flag.Arg(0), nil
}
func main() {
f := parseFlags()
input, err := getInput(f)
if err != nil {
fmt.Fprintln(os.Stderr, err)
flag.Usage()
os.Exit(1)
}
output := toBrainfuck(input, f)
err = printOutput(output, f)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}