-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_stacking.sh
More file actions
executable file
·325 lines (308 loc) · 5.2 KB
/
shell_stacking.sh
File metadata and controls
executable file
·325 lines (308 loc) · 5.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
set -eu
usage() {
cat <<EOF >&2
An esolang interpreter inplemented as a shell script.
Usage:
$0 [options] [-f] <source file>
Options:
-h, --help Print this message then exit
-e <code> Run <code> instead of source file
EOF
exit $1
}
readonly LF=$'\n'
ord() {
printf "%d" \'"$1"
}
chr() {
local hex=$(printf "%x" "$1")
printf "\U${hex}"
}
run() {
local pc=0
local -a stack
local stack_size=0
local -a heap
local ibuf=""
local popped
local tmp
local tmp2
local depth
push() {
stack[$((stack_size++))]=$1
}
pop() {
if [ $stack_size -eq 0 ]; then
popped=0
else
stack_size=$((--stack_size))
popped=${stack[$stack_size]}
fi
}
while [ "$pc" -lt "${#1}" ]; do
local cmd="${1:$pc:1}"
case "$cmd" in
#push charcode
[A-Za-z] )
push $(ord "$cmd")
;;
#push the number
[0-9] )
push $cmd
;;
#add
"+" )
pop
tmp=$popped
pop
push $((popped+tmp))
;;
#subtract
"-" )
pop
tmp=$popped
pop
push $((popped-tmp))
;;
#multiply
"*" )
pop
tmp=$popped
pop
push $((popped*tmp))
;;
#devide
"/" )
pop
tmp=$popped
pop
push $((popped/tmp))
;;
#modulo
"%" )
pop
tmp=$popped
pop
push $((popped%tmp))
;;
#less than
"<" )
pop
tmp=$popped
pop
if [ $popped -lt $tmp ]; then
push 1
else
push 0
fi
;;
#greater than
">" )
pop
tmp=$popped
pop
if [ $popped -gt $tmp ]; then
push 1
else
push 0
fi
;;
#is positive
"?" )
pop
if [ $popped -gt 0 ]; then
push 1
else
push 0
fi
;;
#not
"!" )
pop
if [ $popped -eq 0 ]; then
push 1
else
push 0
fi
;;
#dup
":" )
pop
tmp=$popped
push $tmp
push $tmp
;;
#swap
"\\" )
pop
tmp=$popped
pop
tmp2=$popped
push $tmp
push $tmp2
;;
#push size
"#" )
push $stack_size
;;
#trash
"$" )
pop
tmp=$popped
;;
#store to heap
"^" )
pop
tmp=$popped
if [ $tmp -lt 0 ]; then
echo >&2
echo "Error: Invalid index of heap. (index must be positive)" >&2
return 1
fi
pop
heap[$tmp]=$popped
;;
#restore from heap
"~" )
pop
tmp=$popped
push ${heap[$tmp]:=0}
;;
#input(char)
"," )
while [ -z "$ibuf" ]; do
IFS="" read ibuf
done
push $(ord "${ibuf:0:1}")
ibuf="${ibuf:1}"
;;
#input(number)
"|" )
tmp=""
local pattern="^(-?[0-9]+).*"
while [[ ! "$tmp" =~ $pattern ]]; do
read tmp
done
push ${BASH_REMATCH[1]}
;;
#output(char)
"." )
pop
chr $popped
;;
#output(number)
"_" )
pop
printf "%d" $popped
;;
#loop start
"[" )
pop
if [ $popped -eq 0 ]; then
depth=1
while [ $depth -gt 0 ]; do
pc=$((pc+1))
if [ $pc -ge ${#1} ];then
echo >&2
echo "Syntax Error: Too many '['"
return 1
fi
if [ "${1:$pc:1}" = "[" ]; then
depth=$((depth+1))
elif [ "${1:$pc:1}" = "]" ]; then
depth=$((depth-1))
fi
done
fi
;;
#loop end
"]" )
depth=1
while [ $depth -gt 0 ]; do
pc=$((pc-1))
if [ $pc -lt 0 ];then
echo >&2
echo "Syntax Error: Too many ']'"
return 1
fi
if [ "${1:$pc:1}" = "[" ]; then
depth=$((depth-1))
elif [ "${1:$pc:1}" = "]" ]; then
depth=$((depth+1))
fi
done
pc=$((pc-1))
;;
#if start
"(" )
pop
if [ $popped -eq 0 ]; then
depth=1
while [ $depth -gt 0 ]; do
pc=$((pc+1))
if [ $pc -ge ${#1} ];then
echo >&2
echo "Syntax Error: Too many '('"
return 1
fi
if [ "${1:$pc:1}" = "(" ]; then
depth=$((depth+1))
elif [ "${1:$pc:1}" = ")" ]; then
depth=$((depth-1))
fi
done
fi
;;
#if end
")" )
# nothing to do
;;
#exit
"@" )
return
;;
* )
;;
esac
pc=$((pc+1))
done
}
if [ "$#" -eq 0 ]; then
usage 0
fi
code=""
for opt; do
case "$opt" in
"-h" | "--help" )
usage 0
;;
"-e" )
if [ "$#" -lt 2 ]; then
echo "Error: Pass a code after -e option." >&2
usage 1
fi
code="$2"
break
;;
"-f" )
if [ "$#" -lt 2 ]; then
echo "Error: Pass source file's path after -f option." >&2
usage 1
fi
break
;;
esac
done
if [ -z "$code" ]; then
if [ "$#" -eq 1 -a -f "$1" ]; then
code="$(cat "$1")"
elif [ "$#" -eq 0 ]; then
echo "Error: Pass source file or code." >&2
usage 1
else
echo "Error: Cannot find ${1} as a file." >&2
usage 1
fi
fi
run "$code" || exit $?