? bf interpreter (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
This is the development website, where we test out new features. This isn't the place you're looking for! Go here

bf interpreter

  • #1 ✎ 237 Lumage Head Admin Gardening I like to garden! Hobbies Reading I like to read books! Hobbies Drawing I like to draw! Hobbies 'Note: a lone . is the same as 0.0 'logical NOT (!) on this yields 1 'All the numbers in here are constructed 'by getting a "1" this way, and then 'addition and left bit shift ___$=""+"$" 'BF string ___ =. 'code pointer __$ ="........"'tape __% =. 'tape head index __ =(!.<<!.+!.+!.)'tape len (8) _ =. 'accumulator 'main loop @_ 'read 8-bit int from boolean tape into accumulator 'the tape represents 0 as . and 1 as _ 'so reading a value just involves 'c=="_" + bit shift 'advance _=__$[__%]=="_"__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. _=(_<<!.)+(__$[__%]=="_")__%=__%+!. 'realign tape head __%=__%-(!.+!.<<!.+!.) 'execute instruction label _$=___$[___] 'select instruction by multiplying 'boolean result from REM _$=="whatever" 'with the correct offset (1-7) + 8 'done in one line with addition 'since results are independent GOTO"@________"+"_"*((_$==">")+(_$=="<")*(!.+!.)+(_$=="[")*(!.+!.+!.)+(_$=="]")*(!.+!.+!.+!.)+(_$=="+")*(!.+!.+!.+!.+!.)+(_$=="-")*(!.+!.+!.<<!.)+(_$==".")*(!.+!.+!.+!.+!.+!.+!.)) @________ 'return anchor for writing 'write 8-bit value to boolean tape 'shift bit of interest into sign bit 'then check if < 0 'advance tape head __$[__%]="._"[(_<<(!.+!.+!.<<!.+!.+!.))<.] __%=__%+!. __$[__%]="._"[(_<<(!.+(!.+!.+!.<<!.+!.+!.)))<.] __%=__%+!. __$[__%]="._"[(_<<((!.<<!.)+(!.+!.+!.<<!.+!.+!.)))<.] __%=__%+!. __$[__%]="._"[(_<<(!.+!.+!.+(!.+!.+!.<<!.+!.+!.)))<.] __%=__%+!. __$[__%]="._"[(_<<(!.+!.+!.+!.+!.+!.+!.<<!.+!.))<.] __%=__%+!. __$[__%]="._"[(_<<(!.+(!.+!.+!.+!.+!.+!.+!.<<!.+!.)))<.]__%=__%+!. __$[__%]="._"[(_<<((!.<<!.+!.+!.+!.)-!.<<!.))<.] __%=__%+!. __$[__%]="._"[(_<<((!.<<!.+!.+!.+!.+!.)-!.))<.] 'realign tape head __%=__%-!.-!.-!.-!.-!.-!.-!. @________________________________ 'skip write anchor ___=___+!. 'increment code pointer 'if instruction is not terminating symbol, repeat GOTO"@_"+"_"*(___$[___]=="$") @_________ '> __%=__%+(!.+!.<<!.+!.) 'add 8 to tape ptr __$=__$+"........"*(__%==__) 'if ptr > len, extend __=__+(!.+!.<<!.+!.)*(__%==__) 'if ptr > len, add 8 to tape len 'return, do not write [email protected]________________________________ @__________ '< __%=__%-(!.+!.<<!.+!.) 'sub 8 from tape ptr __$="........"*(__%<.)+__$ 'if ptr<0, extend tape __=__+(!.+!.<<!.+!.)*(__%<.) 'if ptr<0, tape len+=8 __%=__%+(!.+!.<<!.+!.)*(__%<.) 'if ptr<0, ptr+=8 'return, do not write [email protected]________________________________ @___________ '[ ____=!. 'depth = 1 GOTO"@____"+"_"*!!_'if cell != 0, enter the loop @____ 'search for matching bracket ___=___+!. 'inc code pointer ____=____+(___$[___]=="[") 'inc depth ____=____-(___$[___]=="]") 'dec depth GOTO"@____"+"_"*!____ 'break on depth==0 @_____ 'return, do not write [email protected]________________________________ @____________ '] ____=. 'depth = 0 @______ ____=____+(___$[___]=="]") 'inc depth ____=____-(___$[___]=="[") 'dec depth ___=___-!. 'dec code pointer (searching backwards) GOTO"@______"+"_"*!____ 'break on depth==0 @_______ 'return, do not write [email protected]________________________________ @_____________ '+ _=_+!. 'inc accumulator 'return, write [email protected]________ @______________ '- _=_-!. 'dec accumulator 'return, write [email protected]________ @_______________ '. ?_ 'print cell value 'return, do not write [email protected]________________________________ 'END @__? Posted Edited by Lumage