-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf2js.go
134 lines (107 loc) · 2.75 KB
/
bf2js.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
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func cleanup(prog string) string {
control := []int32(".,[]+-><")
var cleanProg string
for _,c := range prog {
for _,b := range control {
if c == b {
cleanProg += string(c)
}
}
}
return cleanProg
}
func deleteComments(filename string) string {
/*
Lines with // (2 slashes) could contain special symbols such as .,[] which we are using for transpiling BF to JS
It's the reason we deleting them before running transpiling
*/
r, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(-1)
}
defer r.Close()
scanner := bufio.NewScanner(r)
SLASH := "/"
slashCode := []byte(SLASH)[0]
commentCodeSlashes := slashCode*slashCode+slashCode+slashCode
scanner.Split(bufio.ScanLines)
var prog string
var line string
for scanner.Scan() {
line = scanner.Text()
if len(line) < 2 {
prog += line
continue
}
code := line[0] * line[1] + line[0] + line[1]
if code == commentCodeSlashes {
continue
}
prog += line
}
return prog
}
func transpile(prog string, w io.Writer) error {
var (
fpos uint = 0
size uint = 30000
plen = uint(len(prog))
)
fmt.Fprint(w, "//The script is generated. Do not edit manually\n\n")
fmt.Fprint(w, "\nlet fs = require('fs');")
fmt.Fprint(w, "\nfunction getChar(){let buf = Buffer.alloc(1);fs.readSync(0, buf, 0, 1); return buf.toString('ascii').charCodeAt(0)}")
fmt.Fprint(w, "\nfunction checkedDecrement(d){ if (d === 0) {return 255} return --d}")
fmt.Fprint(w, "\nfunction checkedIncrement(d){ if (d === 255) {return 0} return ++d}")
fmt.Fprintf(w, "\nlet d=new Array(%d);", size)
fmt.Fprint(w, "\nlet i=0; let output=\"\";")
fmt.Fprintf(w, "\nfor(a=0; a<%d; a++){d[a]=0}\n", size-1)
for fpos < plen {
switch prog[fpos] {
case '+':
fmt.Fprint(w, "d[i]=checkedIncrement(d[i]);")
case '-':
fmt.Fprint(w, "d[i]=checkedDecrement(d[i]);")
case '>':
fmt.Fprint(w, "i++;")
case '<':
fmt.Fprint(w, "i--;")
case '.':
fmt.Fprint(w, "output+=String.fromCharCode(d[i]);")
case ',':
fmt.Fprint(w,"d[i]=getChar();")
case '[':
fmt.Fprint(w, "\nwhile(d[i]){")
case ']':
fmt.Fprint(w, "\n}\n")
}
fpos++
}
fmt.Fprint(w, "\n\nconsole.log(output);")
return nil
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%v\n", "Error: programm accepts only 1 argument: filename of bf program")
os.Exit(-1)
}
filename := os.Args[1]
program := deleteComments(filename)
cleanProgram := cleanup(program)
if len(cleanProgram) == 0 {
fmt.Fprintf(os.Stderr, "The file %s doesn't contain valid bf program\n", filename)
os.Exit(-1)
}
err := transpile(cleanProgram, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "%b\n", err)
os.Exit(-1)
}
}