-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbrainfuck.bash
More file actions
executable file
·40 lines (38 loc) · 1.22 KB
/
brainfuck.bash
File metadata and controls
executable file
·40 lines (38 loc) · 1.22 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
#!/bin/bash
code=$(cat $1)
#echo $code
len=${#code}
data[30000]=0
dptr=0
iptr=0
while [[ $iptr -lt $((len + 1)) ]]; do
count=1
case ${code:$iptr:1} in
'>') dptr=$((dptr+1));;
'<') dptr=$((dptr-1));;
'+') data[$dptr]=$((data[$dptr] + 1));;
'-') data[$dptr]=$((data[$dptr] - 1));;
'.') printf "\x` printf %x ${data[$dptr]}`";;
',') read -n1 s; data[$dptr]=`printf "%d" \'$s` ;;
'[')
if [[ data[$dptr] == 0 ]]; then
iptr=$((iptr+1))
while [[ $count -gt 0 ]]; do
if [[ ${code:$iptr:1} == '[' ]]; then count=$((count+1)); fi
if [[ ${code:$iptr:1} == ']' ]]; then count=$((count-1)); fi
iptr=$((iptr+1))
done
iptr=$((iptr-1))
fi;;
']')
if [[ data[$dptr] -ne 0 ]]; then
iptr=$((iptr-1))
while [[ $count -gt 0 ]]; do
if [[ ${code:$iptr:1} == ']' ]]; then count=$((count+1)); fi
if [[ ${code:$iptr:1} == '[' ]]; then count=$((count-1)); fi
iptr=$((iptr-1))
done
fi;;
esac
iptr=$((iptr+1))
done