-
Notifications
You must be signed in to change notification settings - Fork 5
/
brainbool
executable file
·66 lines (54 loc) · 1.06 KB
/
brainbool
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
#!/usr/bin/env bash
eval set -- "$(getopt --shell=bash bB "$@")"
input=input
output=output
while (($# > 1)); do
[[ "$1" == -b ]] && input=byte_input
[[ "$1" == -B ]] && output=byte_output
shift
done
cat > "$1.c" << EOF
#include <stdint.h>
#include <stdio.h>
uint8_t input()
{
return getchar() == 49;
}
uint8_t byte_input()
{
static uint8_t byte = 0, mask = 0;
mask /= 2;
if (mask == 0)
byte = getchar(), mask = 128;
return (byte & mask) != 0;
}
void output(uint8_t bit)
{
putchar(48 | bit);
}
uint8_t byte_output(uint8_t bit)
{
static uint8_t byte = 0, shift = 7;
byte |= bit << shift--;
if (shift == 255)
putchar(byte), byte = 0, shift = 7;
}
int main()
{
uint8_t t[65536] = {0};
uint16_t p = 0;
int i;
EOF
while read -n1 char; do
case "$char" in
\>) echo '++p;' ;;
\<) echo '--p;' ;;
\+) echo 't[p]=!t[p];' ;;
\,) echo 't[p]=INPUT();' ;;
\.) echo 'OUTPUT(t[p]);' ;;
\[) echo 'while(t[p]){' ;;
\]) echo '}' ;;
esac
done < "$1" >> "$1.c"
echo 'return 0;}' >> "$1.c"
cc -O3 -D INPUT=$input -D OUTPUT=$output -o "$1.out" "$1.c" && "./$1.out"