diff options
Add homework 3 - brainfsck interpreter (#55)
* Add brainfsck interpreter
* Remove unused imports in Main
* Add .gitignore
* Fix example commands
7 files changed, 199 insertions, 0 deletions
| -rw-r--r-- | homework/81333/brainfsck/examples/fact.bf | 42 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/hello.bf | 3 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/input.bf | 13 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/pretty.bf | 31 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/sierpinski.bf | 5 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/squares.bf | 5 | ||||
| -rw-r--r-- | homework/81333/brainfsck/examples/sum.bf | 100 |
diff --git a/homework/81333/brainfsck/examples/fact.bf b/homework/81333/brainfsck/examples/fact.bf new file mode 100644 index 0000000..197f331 --- /dev/null +++ b/homework/81333/brainfsck/examples/fact.bf @@ -0,0 +1,42 @@ ++++++++++++++++++++++++++++++++++ c1v33 : ASCII code of ! +>++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++ c2v61 : ASCII code of = +>++++++++++ c3v10 : ASCII code of EOL +>+++++++ c4v7 : quantity of numbers to be calculated +> c5v0 : current number (one digit) +>+ c6v1 : current value of factorial (up to three digits) +<< c4 : loop counter +[ block : loop to print one line and calculate next +>++++++++++++++++++++++++++++++++++++++++++++++++. c5 : print current number +------------------------------------------------ c5 : back from ASCII to number +<<<<.-.>.<.+ c1 : print !_=_ + +>>>>> block : print c6 (preserve it) +> c7v0 : service zero +>++++++++++ c8v10 : divizor +<< c6 : back to dividend +[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<] c6v0 : divmod algo borrowed from esolangs; results in 0 n d_n%d n%d n/d +>[<+>-] c6 : move dividend back to c6 and clear c7 +>[-] c8v0 : clear c8 + +>> block : c10 can have two digits; divide it by ten again +>++++++++++ c11v10: divizor +< c10 : back to dividend +[->-[>+>>]>[+[-<+>]>+>>]<<<<<] c10v0 : another divmod algo borrowed from esolangs; results in 0 d_n%d n%d n/d +>[-] c11v0 : clear c11 +>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]c13v0 : print nonzero n/d (first digit) and clear c13 +<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]] c12v0 : print nonzero n%d (second digit) and clear c12 +<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-] c9v0 : print any n%d (last digit) and clear c9 + +<<<<<<. c3 : EOL +>>+ c5 : increment current number + block : multiply c6 by c5 (don't preserve c6) +>[>>+<<-] c6v0 : move c6 to c8 +>> c8v0 : repeat c8 times +[ +<<<[>+>+<<-] c5v0 : move c5 to c6 and c7 +>>[<<+>>-] c7v0 : move c7 back to c5 +>- +] +<<<<- c4 : decrement loop counter +] diff --git a/homework/81333/brainfsck/examples/hello.bf b/homework/81333/brainfsck/examples/hello.bf new file mode 100644 index 0000000..273b209 --- /dev/null +++ b/homework/81333/brainfsck/examples/hello.bf @@ -0,0 +1,3 @@ +>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-] +>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++ +.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++. diff --git a/homework/81333/brainfsck/examples/input.bf b/homework/81333/brainfsck/examples/input.bf new file mode 100644 index 0000000..12597fc --- /dev/null +++ b/homework/81333/brainfsck/examples/input.bf @@ -0,0 +1,13 @@ +> + +[ - >,>+< + ----- ----- ; minus 10 + [ ; if enters means it is not a \n + +++++ +++++ ; restore prev value + < + ] >> ; moving forward +] + ; numbers are 0 0 49 0 50 0 51 + ; for input 123 +<<<<[<<] ; moving to the beginning +>> ; reaching first char +[.>>] ; just printing till end diff --git a/homework/81333/brainfsck/examples/pretty.bf b/homework/81333/brainfsck/examples/pretty.bf new file mode 100644 index 0000000..069aa4d --- /dev/null +++ b/homework/81333/brainfsck/examples/pretty.bf @@ -0,0 +1,31 @@ +>>>>+>+++>+++>>>>>+++[ + >,+>++++[>++++<-]>[<<[-[->]]>[<]>-]<<[ + >+>+>>+>+[<<<<]<+>>[+<]<[>]>+[[>>>]>>+[<<<<]>-]+<+>>>-[ + <<+[>]>>+<<<+<+<--------[ + <<-<<+[>]>+<<-<<-[ + <<<+<-[>>]<-<-<<<-<----[ + <<<->>>>+<-[ + <<<+[>]>+<<+<-<-[ + <<+<-<+[>>]<+<<<<+<-[ + <<-[>]>>-<<<-<-<-[ + <<<+<-[>>]<+<<<+<+<-[ + <<<<+[>]<-<<-[ + <<+[>]>>-<<<<-<-[ + >>>>>+<-<<<+<-[ + >>+<<-[ + <<-<-[>]>+<<-<-<-[ + <<+<+[>]<+<+<-[ + >>-<-<-[ + <<-[>]<+<++++[<-------->-]++<[ + <<+[>]>>-<-<<<<-[ + <<-<<->>>>-[ + <<<<+[>]>+<<<<-[ + <<+<<-[>>]<+<<<<<-[ + >>>>-<<<-<- + ]]]]]]]]]]]]]]]]]]]]]]>[>[[[<<<<]>+>>[>>>>>]<-]<]>>>+>>>>>>>+>]< +]<[-]<<<<<<<++<+++<+++[ + [>]>>>>>>++++++++[<<++++>++++++>-]<-<<[-[<+>>.<-]]<<<<[ + -[-[>+<-]>]>>>>>[.[>]]<<[<+>-]>>>[<<++[<+>--]>>-] + <<[->+<[<++>-]]<<<[<+>-]<<<< + ]>>+>>>--[<+>---]<.>>[[-]<<]< +] diff --git a/homework/81333/brainfsck/examples/sierpinski.bf b/homework/81333/brainfsck/examples/sierpinski.bf new file mode 100644 index 0000000..4c66685 --- /dev/null +++ b/homework/81333/brainfsck/examples/sierpinski.bf @@ -0,0 +1,5 @@ +++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[ + -<<<[ + ->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<< + ]>.>+[>>]>+ +] diff --git a/homework/81333/brainfsck/examples/squares.bf b/homework/81333/brainfsck/examples/squares.bf new file mode 100644 index 0000000..fa0a6f8 --- /dev/null +++ b/homework/81333/brainfsck/examples/squares.bf @@ -0,0 +1,5 @@ +++++[>+++++<-]>[<+++++>-]+<+[ + >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ + >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] + <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<- +] diff --git a/homework/81333/brainfsck/examples/sum.bf b/homework/81333/brainfsck/examples/sum.bf new file mode 100644 index 0000000..df694ec --- /dev/null +++ b/homework/81333/brainfsck/examples/sum.bf @@ -0,0 +1,100 @@ +[ + A brainfuck program for doing a single digit addition. + + Ex. input: '13' -> output: '4' + input: '99' -> output: '18' + + Author: Florian Baierl +] + +initialize #0 with 48 (ASCII char for '0') +>++++ ++++ +[ +<++++ ++ +>- +] + +save input to #1 and #2 +,>, + +substract value from #0 from #1 and #2 +<< +[ +>- +>- +<<- +] + +move to #1 +> + +substract from #1 and add to #2; now the answer is in #2 +[ + ->+< +] + +since we need to modify the answer afterwards write it to #3 and #6 +as well +> +[>+>>>+<<<<-] + + +Is the answer bigger than 9? +to continue the memory tape should look like this: +0 1 0 (a) b 0 +with the pointer pointing to a + +<+ +>>>+++++ +++++ b is 10 +< point to a + ++>+< necessary in case a and b are zero + +loop to determine whether a or b reach 0 first +[->-[>]<<] + +<[- + a was bigger or equals b + the answer is still stored in #6 + + subtract 10 from #6 (the answer) + >>>>> ----- ----- + + write 48 to #4 + <++++ ++++ + [ + <++++ ++ + >- + ] + add 48 to #5 and #6 + < + [->+>+<<] + + print out the results + >+.>. + + leave loop (back to #2) + <<<< + ] + <[- + a was samller so we can simply print the answer out after adding 48 to it + the answer is still stored in #6 + + >> + ++++ ++++ + [ + <++++ ++ + >- + ] + + < + [ + ->>>>+<<<< + ] + + print #2 + >>>>. + + we want to leave the loop so go somewhere with the value '0' + > +] |
