Execute Brain****/Forth: Difference between revisions

m
(Copied from User talk:IanOsgood and documented)
 
 
(10 intermediate revisions by 4 users not shown)
Line 1:
{{implementation|Brainf***}}A Brainf*** compiler written by [[User:IanOsgood{{collection|Ian Osgood]] in [[Forth]].[[Category:Forth]]RCBF}}
A Brainf*** compiler written by [[User:IanOsgood|Ian Osgood]] in [[Forth]].
 
==Usage==
 
Load the code your preferred Forth environment. The code will define the wordwords ''':bf''' and ''':bf-file''',
===Compiling Brainf*** code===
Let's say you have this sequence that prints a capital A:
++++++[>+++++++++++<-]>-.
You can compile it by using the ''':bf''' word:
:bf" printA " ++++++[>+++++++++++<-]>-."
Your Brainf*** code is now compiled in your Forth environment as the word '''printA'''. If you want to use a different wordname, replace printA with your preferred wordname.
 
Larger BF programs may be loaded from a file instead of parsed from the interpreter:
:bf-file bottles bottles.bf
 
===Running the compiled Brainf*** code===
Assuming you used the word '''printA''' from the above example, you can execute your code by running the word:
Line 34 ⟶ 39:
[char] > of postpone right endof
[char] < of postpone left endof
[char] , of postpone key endofdrop
postpone key endof
[char] . of postpone dup
postpone emit endof
Line 40 ⟶ 46:
endcase ;
 
: :compile-bf"-string ( nameaddr bfcode"len -- )
:
[char] " parse \ "
postpone init
bounds do i c@ compile-bf-char loop
Line 48 ⟶ 52:
postpone c!
postpone ;
;
;</pre>
 
: :bf ( name " bfcode" -- )
:
[char] " parse \ "get string delimiter
compile-bf-string ;
 
: :bf-file ( name file -- )
:
bl parse slurp-file
compile-bf-string ;</pre>
Anonymous user