-
Notifications
You must be signed in to change notification settings - Fork 10
/
bf.cfg
98 lines (87 loc) · 4.51 KB
/
bf.cfg
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
// DP console is TURING COMPLETE!
alias _bf_vcall "${$1} ${2- ?}"
// number system
// Xon RPN: set $_bf_zero 0
// Xon RPN: alias _bf_inc "rpn /$1 $2 1 add def"
// Xon RPN: alias _bf_dec "rpn /$1 $2 1 sub def"
// unary
// decimal | unary
// 0 | 0
// 1 | +
// 2 | ++
// -1 | -
// -2 | --
alias _bf_zero "set $1 0"
alias _bf_one "set $1 +"
alias _bf_minus_one "set $1 -"
alias _bf_return_3 "set $1 $3"
alias _bf_return_4 "set $1 $4"
set _bf_zero "0"
alias _bf_inc_loop1 "set _bf_tmp_$3 _bf_inc_loop2; set _bf_tmp_+$2 _bf_return_3; _bf_vcall _bf_tmp_$3 $*"
alias _bf_inc_loop2 "set _bf_tmp_$2 _bf_inc_loop3; set _bf_tmp_-$4 _bf_return_4; _bf_vcall _bf_tmp_$2 $*"
alias _bf_inc_loop3 "_bf_inc_loop1 $1 $2 +$3 -$4"
alias _bf_inc "set _bf_tmp_$2 _bf_inc_loop1; set _bf_tmp_0 _bf_one; set _bf_tmp_- _bf_zero; _bf_vcall _bf_tmp_$2 $1 $2 + -"
alias _bf_dec_loop1 "set _bf_tmp_$3 _bf_dec_loop2; set _bf_tmp_-$2 _bf_return_3; _bf_vcall _bf_tmp_$3 $*"
alias _bf_dec_loop2 "set _bf_tmp_$2 _bf_dec_loop3; set _bf_tmp_+$4 _bf_return_4; _bf_vcall _bf_tmp_$2 $*"
alias _bf_dec_loop3 "_bf_dec_loop1 $1 $2 -$3 +$4"
alias _bf_dec "set _bf_tmp_$2 _bf_dec_loop1; set _bf_tmp_0 _bf_minus_one; set _bf_tmp_+ _bf_zero; _bf_vcall _bf_tmp_$2 $1 $2 - +"
// end of unary
// interpreter state
set bf_input ""
alias _bf_clearstate "set _bf_left \"\"; set _bf_right \"\"; set _bf_register $_bf_zero; set _bf_execstack \"\""
alias _bf_dumpstate "echo rev($_bf_left) < $_bf_register > $_bf_right"
// a STACK!
alias _bf_pop_get "set $2 \"${3 ?}\"; set $1 \"${4- ?}\""
alias _bf_pop_dispatch "_bf_pop_get $1 $2 $_bf_popstack_"
// usage: _bf_pop stackvar outvar defaultvalue
alias _bf_pop "set _bf_popstack_ \"${$1 ?}\"; set \"_bf_popstack_${$1 ?}\" \"${3 ?}\"; _bf_pop_dispatch $*"
// usage: _bf_push stackvar value
alias _bf_push "set $1 \"$2 ${$1 ?}\""
// skip mode: skip until matching ] (1 on _bf_execstack), then continue executing
alias _bf_skip_ "echo PROGRAMM FELL OFF THE EDGE"
alias _bf_skip_+ "_bf_skip_${* ?}"
alias _bf_skip_- "_bf_skip_${* ?}"
alias _bf_skip_< "_bf_skip_${* ?}"
alias _bf_skip_> "_bf_skip_${* ?}"
alias _bf_skip_. "_bf_skip_${* ?}"
alias _bf_skip_, "_bf_skip_${* ?}"
alias _bf_skip_endloop_0 "_bf_skip_${* ?}" // continue skipping
alias _bf_skip_endloop_1 "bf_${* ?}" // back to execution
alias _bf_skip_endloop_dispatch "_bf_skip_endloop_$_bf_stackval ${* ?}"
alias _bf_skip_] "_bf_pop _bf_execstack _bf_stackval; _bf_skip_endloop_dispatch ${* ?}"
alias _bf_skip_[ "_bf_push _bf_execstack 0; _bf_skip_${* ?}"
// enter
alias _bf_skiploop "_bf_push _bf_execstack 1; _bf_skip_${* ?}"
// run mode: execute until matching ] (1 on _bf_execstack), then exit
alias bf_ "echo PROGRAMM FELL OFF THE EDGE"
alias bf_+ "_bf_inc _bf_register $_bf_register; bf_${* ?}"
alias bf_- "_bf_dec _bf_register $_bf_register; bf_${* ?}"
alias bf_< "_bf_push _bf_left $_bf_register; _bf_pop _bf_right _bf_register $_bf_zero; bf_${* ?}"
alias bf_> "_bf_push _bf_right $_bf_register; _bf_pop _bf_left _bf_register $_bf_zero; bf_${* ?}"
alias bf_. "echo $_bf_register; bf_${* ?}"
// note: on EOF, we don't change the register value!
alias _bf_input_get "set _bf_register $_bf_inputval; bf_${* ?}" // read input
alias _bf_input_eof "bf_${* ?}" // at EOF, just continue
alias _bf_input_dispatch "set _bf_inputval_ _bf_input_get; set _bf_inputval_$_bf_inputval _bf_input_eof; _bf_vcall _bf_inputval_ ${* ?}"
alias bf_, "_bf_pop bf_input _bf_inputval; _bf_input_dispatch ${* ?}"
alias _bf_endloop_0 "echo IN SKIP MODE, EXCEPT NOT"
alias _bf_endloop_1 "" // back to caller
alias _bf_endloop_ "echo PROGRAMM FELL OFF THE EDGE"
alias _bf_endloop_dispatch "_bf_endloop_$_bf_stackval ${* ?}"
alias bf_] "_bf_pop _bf_execstack _bf_stackval; _bf_endloop_dispatch ${* ?}"
// enter
alias _bf_runloop "_bf_push _bf_execstack 1; bf_$*; bf_[ ${* ?}"
// loop dispatcher
alias bf_[ "set _bf_runloop_$_bf_zero _bf_runloop; set _bf_runloop_$_bf_register _bf_skiploop; _bf_vcall _bf_runloop_$_bf_zero ${* ?}"
// start it
alias bf_exec "_bf_clearstate; _bf_push _bf_execstack 1; bf_$1 ]"
// "cat"
// Xon RPN: bf_input "1 2 3 4 5"
bf_input "+ ++ +++ ++++ +++++"
bf_exec "[ - ] - , + [ - . + [ - ] - , + ]"
// output 42
// Xon RPN: bf_input "12 6 9"
bf_input "++++++++++++ ++++++ +++++++++"
bf_exec ", > , > , < [ > [ - > + > + < < ] > > [ - < < + > > ] < < < - ] < [ - > > > - < < < ] > > > ."
// hello world
bf_exec "+ + + + + + + + + + [ > + + + + + + + > + + + + + + + + + + > + + + > + < < < < - ] > + + . > + . + + + + + + + . . + + + . > + + . < < + + + + + + + + + + + + + + + . > . + + + . - - - - - - . - - - - - - - - . > + . > ."