-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.sh
executable file
·145 lines (125 loc) · 1.99 KB
/
compiler.sh
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
#!/usr/bin/env bash
# Name : BS Compiler
# Purpose : Compile BS language to brainfuck and execute it.
# Author : Jore
#
# COMMANDS
#
# increment/decrement memory block
# by arg
m() {
if [ "${1}" -gt "0" ]; then
for ((i = ${1}; i > 0; i--)); do
echo -n '+'
done
elif [ "${1}" -lt "0" ]; then
for ((i = ${1}; i < 0; i++)); do
echo -n '-'
done
fi
}
# move right and increment/decrement
# memory block block by arg
rm() {
r
m "${@}"
}
# move left and increment/decrement
# memory block block by arg
lm() {
l
m "${@}"
}
# move pointer right arg blocks
r() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n '>'
done
}
# move pointer left arg blocks
l() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n '<'
done
}
# input character arg times
i() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n ','
done
}
# move right and input character arg times
ri() {
r
i "${@}"
}
# move right and input character arg times
li() {
l
i "${@}"
}
# print character arg times
o() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n '.'
done
}
# move right and print character arg
# times
ro() {
r
o "${@}"
}
# move right and print character arg
# times
lo() {
l
o "${@}"
}
# Start loop n times
l:() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n '['
done
}
# End loop n times
:l() {
[ -z "${1}" ] && set -- 1
for ((i = ${1}; i > 0; i--)); do
echo -n ']'
done
}
#
# COMPILER
#
compile() {
wrap=80
if [ -z "${1}" ]; then
_usage
else
local file="${1%.bs}.b"
echo "#!/usr/bin/env bf" >"${file}"
source "${1}" | sed -e "s/\(.\{${wrap}\}\)/&\n/g" >>"${file}"
echo "@" >>"${file}"
chmod u+x "${file}"
echo "${file}"
fi
}
usage() {
echo "BS Compiler"
echo "Usage: $(basename "${0}") FILE"
}
#
# ENTRY
#
if [ -x "$(command -v bf)" ]; then
bf "$(compile "${1}")"
else
echo "No brainfuck interpreter (command 'bf') found! Will only compile."
compile "${1}"
fi