Skip to content

Commit 7e2eeeb

Browse files
🥔
Golang build
1 parent bfa2d5f commit 7e2eeeb

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

golang/a.bf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+[,.+]

golang/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module joshdev.codes/go/bftolua
2+
3+
go 1.16

golang/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"strings"
9+
)
10+
11+
func main() {
12+
args := os.Args
13+
14+
if len(args) == 1 {
15+
log.Fatalln("Please provide a file.")
16+
}
17+
18+
f, err := ioutil.ReadFile(args[1])
19+
20+
if err != nil {
21+
log.Fatalln("Error: " + err.Error())
22+
}
23+
24+
code := string(f)
25+
26+
base := "local cells = {0}"
27+
28+
cur := 1
29+
var set = make(map[int]bool)
30+
set[1] = true
31+
32+
instructions := strings.Split(code, "")
33+
34+
for _, instruction := range instructions {
35+
switch instruction {
36+
case "+":
37+
base += fmt.Sprintf("cells[%v] = cells[%v] + 1;\n", cur, cur)
38+
case "-":
39+
base += fmt.Sprintf("cells[%v] = cells[%v] - 1;\n", cur, cur)
40+
case ">":
41+
cur++
42+
if !set[cur] {
43+
base += fmt.Sprintf("cells[%v] = 0;\n", cur)
44+
}
45+
case "<":
46+
cur--
47+
if cur < 1 {
48+
log.Fatalln("ERROR: Grid escape detected at instruction " + instruction)
49+
}
50+
case "[":
51+
base += fmt.Sprintf("while cells[%v] ~= 0 do \n", cur)
52+
case "]":
53+
base += "end;\n"
54+
case ".":
55+
base += fmt.Sprintf("print(string.char(cells[%v]));\n", cur)
56+
case ",":
57+
base += fmt.Sprintf("cells[%v] = io.read();\nif #cells[%v] > 1 then\n error('Expected string of length 1')\nend;\ncells[%v] = string.byte(cells[%v]);\n", cur, cur, cur, cur)
58+
}
59+
}
60+
ioutil.WriteFile("out.lua", []byte(base), 0644)
61+
62+
fmt.Println("Outputed to out.lua")
63+
}

0 commit comments

Comments
 (0)