Hovori vam nieco slovo ezoterika?
Nie, nie, nebudeme vestit z kariet a ani skumat aki sme podla polohy hviezd v den narodenia.
Kedysi som na webe natrafil na pojem ‚Ezotericke programovacie jazyky‘. Priznam sa, ihned mi niektore ucarovali a nie len svojim nazvom, ale napriklad aj svojou jednoduchostou ci naopak prehnanou zlozitostou. A tak som sa zoznamil s jazykom Brainfu*k (selfcensored ;]), Befunge, Malbolge alebo Piet. (vid. http://en.wikipedia.org/wiki/Esoteric_programming_language , https://esolangs.org/wiki/Main_Page )
Nuz a tak mi napadlo, preco si nejaky taky Brainfu*k nenaprogramovat pre nasho milacika? Ako motivacia posluzil tiez tento „hello world“ program:
++++++++++[>+++++++>++++++++++>+++<<<-]>++.>+.+++++++ ..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.
( http://en.wikipedia.org/wiki/Esoteric_programming_language#brainfuck )
No nie je to nadhera? ;]
Popis jazyka a „rozpitvany“ hello world (jemne inu verziu ako uz spomenutu) je mozne najst na stranke http://en.wikipedia.org/wiki/Brainfuck .
Nuz, a implementacia (netvrdim ze bezchybna ;]) pre zx:
cpu z80undoc relaxed on ROM_KEY_SCAN: equ 0x028E org 25000 ;randomize usr 25000 => translate char(10)->char(13) ;randomize usr 25001 => do not translate db 62 ;ld a,"code of xor a" xor a ld (translate),a ld hl,dta ld de,dta+1 ld bc,32767 ld (hl),0 ldir ld a,2 call $1601 ld de,prg ld hl,dta call interpret ret interpret: ld a,d rlca ret c ; out off program memory ld a,(de) cp 255 ; artifical instruction end of program ret z cp '>' jr z,inc_ptr cp '<' jr z,dec_ptr cp '+' jr z,inc_dta cp '-' jr z,dec_dta cp '[' jr z,brnch_open cp ']' jr z,brnch_close cp '.' jr z,print cp ',' jr nz,next input: push de push hl ld a,1 out (254),a input_loop: call ROM_KEY_SCAN jr nz,input_loop ld a,e cp 0xff ;nokey jr z,input_loop cp 0x27 ;caps jr z,input_loop cp 0x18 ;symb jr z,input_loop ld hl,normal_keys ld a,d cp 0xff jr z,input_havetab ld hl,caps_keys cp 0x27 jr z,input_havetab ld hl,symb_keys cp 0x18 jr nz,input_loop ;some weird value input_havetab: ld d,0 add hl,de ld a,(hl) call translate_zx2pc pop hl pop de ld (hl),a input_wait_release: xor a in a,(254) cpl and 31 jr nz,input_wait_release out (254),a next: inc de jr interpret print: ld a,(hl) call translate_pc2zx rst 16 jr next inc_ptr: inc hl jr next dec_ptr: dec hl jr next inc_dta: inc (hl) jr next dec_dta: dec (hl) jr next brnch_open: ld a,(hl) or a jr nz,next ld c,0 branch_open_loop: ld a,(de) cp '[' jr nz,branch_open_1 inc c branch_open_1: cp ']' jr nz,branch_open_2 dec c branch_open_2: inc de ld a,c or a jr nz,branch_open_loop dec de jr next brnch_close: ld a,(hl) or a jr z,next ld c,0 branch_close_loop: ld a,(de) cp ']' jr nz,branch_close_1 dec c branch_close_1: cp '[' jr nz,branch_close_2 inc c branch_close_2: dec de ld a,c or a jr nz,branch_close_loop inc de jr next translate_zx2pc: cp 13 ret nz ld a,(translate) or a ld a,10 ret nz ld a,13 ret translate_pc2zx: cp 10 ret nz ld a,(translate) or a ld a,13 ret nz ld a,10 ret normal_keys: db "bhy65tgv" db "nju74rfc" db "mki83edx" db 0x18,"lo92wsz" db " ",13,"p01qa",0x27 caps_keys: db "BHY",10,8,"TGV" db "NJU",11,"4RFC" db "MKI",9,"3EDX" db 0x18,"LO92WSZ" db " ",13,"P",8,"1QA",0x27 symb_keys: db "*^[&%>}/" db ",-]'$<{?" db ".+",0x7f,"(#e\\`" db 0x18,"=;)@w|:" db " ",13,'"',"_!q~",0x27 translate: db 0 prg: binclude "fib.b" dta: equ 32768
a ukazkovy program (pouzity v predchadzajucom code pomocou binclude) na vypis fibonacciho postupnosti cisel (fib.b):
(zdroj: http://www.hevanet.com/cristofd/brainfuck/)
>++++++++++>+>+[ [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[ [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>> ]<<< ] This program doesn't terminate; you will have to kill it. Daniel B Cristofani (cristofdathevanetdotcom) http://www.hevanet.com/cristofd/brainfuck/
– ub
Hezke, ale Whitespace asi nedas 🙂