<style> .reveal { font-size: 30px; } </style> <!-- .slide: data-background="https://hackmd.io/_uploads/rkLKVu7VR.jpg" --> # `Brainfxck` `HyperSoWeak` --- ## 蛤? ``` -[------->+<]>-.-[->+++++<]>++.+++++++..+++. [--->+<]>-----.--[->++++<]>+.----------.++++ ++.---.[-->+++++<]>+++.+[----->+<]>.++.--.+[ ----->++<]>++. ``` ---- ![8rv725](https://hackmd.io/_uploads/ryzpMumVR.jpg) ---- {%youtube hdHjjBS4cs8 %} ---- ![image](https://hackmd.io/_uploads/HJlyquQVR.png) ---- ### 圖靈機 ![image](https://hackmd.io/_uploads/ryodu_QVC.png) ---- ### 指令表 | 字元 | 含義 | | --- | ------ | | `>` | 指標加一 | | `<` | 指標減一 | | `+` | 指標所指位元組的值加一 | | `-` | 指標所指位元組的值減一 | | `.` | 輸出指標所指位元組內容(ASCII碼) | | `,` | 向指標所指的位元組輸入內容(ASCII碼) | | `[` | 若指標所指位元組的值為零,則向後跳轉,跳轉到其對應的`]`的下一個指令處 | | `]` | 若指標所指位元組的值不為零,則向前跳轉,跳轉到其對應的`[`的下一個指令處 | ---- ### 翻譯成 C 語言 | Brainfuck | C | | --------- | - | | `>` | `++ptr;` | | `<` | `--ptr;` | | `+` | `++*ptr;` | | `-` | `--*ptr;` | | `.` | `putchar(*ptr);` | | `,` | `*ptr = getchar();` | | `[` | `while(*ptr) {` | | `]` | `}` | ---- ### 一個簡單的例子 [Brainfuck Visualizer](https://ashupk.github.io/Brainfuck/brainfuck-visualizer-master/index.html#) [Brainfuck Interpreter in C](https://github.com/kgabis/brainfuck-c) ``` |0|1|2| | |a|b| ^ ``` ``` >[>+<-] ``` ``` |0|1| 2 | | | |a+b| ^ ``` <!-- .element: class="fragment" data-fragment-index="1" --> --- ## Desperate Syntax Algorithm :) ---- ### Convention ``` |0|1|2|3|4| | |a| | |b| ^ ``` ``` >[>>>+<<<-] ``` 等價於 ``` a[b+a-] ``` ---- ### Exercise 1 swap(x, y) ---- ### Solution 1 swap(x, y) ``` tmp[-] x[tmp+x-] y[x+y-] tmp[y+tmp-] ``` ```cpp int tmp = x; // x = 0; x = y; // y = 0; y = tmp; // tmp = 0; ``` ---- ### x = 0 ``` [-] ``` ---- ### x = x + y ``` x[y+x-] ``` ---- ### Exercise 2 ``` From | a | b | To | a | a+b | ``` ---- ### Solution 2 ``` tmp[-] a[b+tmp+a-] tmp[a+tmp-] ``` ---- ### x = x - y ``` x[y-x-] ``` ---- ### x = x * y ``` x[ y[z+tmp+y-] tmp[y+tmp-] x-] ``` <!-- .element: class="fragment" data-fragment-index="1" --> ---- ### x / y, x % y ``` From | >n | 0 | d | | | To | >0 | n | d-n%d | n%d | n/d | [->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<] ``` ---- ### x ^ 2 Uses the fact that<!-- .element: class="fragment" data-fragment-index="1" --> $$x^2 = \sum_{k=0}^{x-1} (2k+1)$$<!-- .element: class="fragment" data-fragment-index="1" --> ``` x[tmp1+x-] tmp1[ -[tmp2+x++tmp1-]x +tmp2[tmp1+tmp2-] tmp1] ``` <!-- .element: class="fragment" data-fragment-index="2" --> ---- ### x ^ y ``` temp0[-] x[temp0+x-] x+ y[ temp1[-] temp2[-] x[temp2+x-] temp2[ temp0[x+temp1+temp0-] temp1[temp0+temp1-] temp2-] y-] ``` --- ## 輸入輸出 ---- ## Read Integers ``` >,---------- n = 0 / d = getchar() / d sub 10 [ while(d) ++++++++++ d add 10 >++++[-<-------->]+< d sub 32 [ if(d) # d is not space >+++[-<---->]< d sub 16 <[->++++++++++<] d add 10 * n >[-<+>] n = 1 ]>[- else # d is space > move to next ] end if <, d = getchar() ----------] d sub 10 ``` ---- ### Print Integer ``` | >x | 0 | 0 | 0 | 0 | 0 | 0 | >>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[- <+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++ <]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]< ``` --- ## 來點有趣的ㄅ ---- ### 費波那契數列 by `HyperSoWeak` ``` >++++++++++>+>+[ [ +++++[>++++++++<-]>. <++++++[>--------<-]+<<< ]>.>>[ [-] <[>+<-] >>[<<+>+>-]< [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[ >[-]>+>+<<<-[>+<-] ]]]]]]]]]] +>>>] <<<] ``` ---- ### 謝爾賓斯基三角形 by `HyperSoWeak` ![image](https://hackmd.io/_uploads/BJ7cnRz40.png) ``` >++++[<++++++++>-] >++++++++[>++++<-] >>++>>>+>>>+<<<<<<<<<<[ -[->+<]>[-<+>>>.<<]>>>[ ->++++++++[>++++<-]>.<<[->+<] +>[->++++++++++<<+>]> .[-]>] +<<<[ -[->+<]+>[ -<+>>>-[->+<]++>[-<->] <<<] <<<<] ++++++++++.+++.[-]<]+++++ ``` ---- ### 遊戲 how [numguess.bf](https://github.com/DarthJDG/NumGuess/blob/master/numguess.bf) by DarthJDG [15puzzle.bf](https://github.com/arkark/15puzzle-brainfuck/blob/master/src/15puzzle.bf) by arkark ---- ### Brainfuck Interpreter in Brainfuck WTF [Brainfuck Interpreter](https://github.com/DillonDepeel/A-Brainfuck-Interpreter-Written-In-Brainfuck-) by DillonDepeel ---- ### 曼德博集合 ![image](https://hackmd.io/_uploads/BJlKzIzV0.png) [mandelbrot.bf](https://github.com/erikdubbelboer/brainfuck-jit/blob/master/mandelbrot.bf) by erikdubbelboer ---- ### [Game of Life](https://www.linusakesson.net/programming/brainfuck/) ![Gospers_glider_gun](https://hackmd.io/_uploads/Hyp9aUzVA.gif) ---- ### BTW [JavaScript is FUN](https://jsfuck.com/) --- ## 期末加油! ψ(`∇´)ψ ## 謝謝大家參加這學期的讀書會 ![123](https://hackmd.io/_uploads/HJSnwFXEC.png =20%x) ---- <!-- .slide: style="font-size: 16px;" --> ![image](https://hackmd.io/_uploads/ryFN4K7NR.png) Software is like sex: it's better when it's free.
{"title":"Brainfuck - slide","slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"023479a5-db5b-4251-8691-69550cf1f264\",\"add\":6976,\"del\":1545}]","description":"HyperSoWeak"}
    84 views