Execute Brain****/Forth: Difference between revisions
Content added Content deleted
(→Source: bug) |
m (moved RCBF/Forth to Execute Brain****/Forth) |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{implementation|Brainf***}}{{collection|RCBF}} |
{{implementation|Brainf***}}{{collection|RCBF}} |
||
A Brainf*** compiler written by [[User:IanOsgood|Ian Osgood]] in [[Forth]]. |
|||
==Usage== |
==Usage== |
||
Load the code your preferred Forth environment. The code will define the |
Load the code your preferred Forth environment. The code will define the words ''':bf''' and ''':bf-file''', |
||
===Compiling Brainf*** code=== |
===Compiling Brainf*** code=== |
||
Let's say you have this sequence that prints a capital A: |
Let's say you have this sequence that prints a capital A: |
||
++++++[>+++++++++++<-]>-. |
++++++[>+++++++++++<-]>-. |
||
You can compile it by using the '''bf''' word: |
You can compile it by using the ''':bf''' word: |
||
:bf |
:bf printA " ++++++[>+++++++++++<-]>-." |
||
Your Brainf*** code is now compiled in your Forth environment as the word '''printA'''. If you want to use a different |
Your Brainf*** code is now compiled in your Forth environment as the word '''printA'''. If you want to use a different name, replace printA with your preferred name. |
||
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=== |
===Running the compiled Brainf*** code=== |
||
Assuming you used the word '''printA''' from the above example, you can execute your code by running the word: |
Assuming you used the word '''printA''' from the above example, you can execute your code by running the word: |
||
Line 41: | Line 46: | ||
endcase ; |
endcase ; |
||
: |
: compile-bf-string ( addr len -- ) |
||
⚫ | |||
⚫ | |||
postpone init |
postpone init |
||
bounds do i c@ compile-bf-char loop |
bounds do i c@ compile-bf-char loop |
||
Line 49: | Line 52: | ||
postpone c! |
postpone c! |
||
postpone ; |
postpone ; |
||
; |
|||
;</pre> |
|||
: :bf ( name " bfcode" -- ) |
|||
⚫ | |||
⚫ | |||
compile-bf-string ; |
|||
: :bf-file ( name file -- ) |
|||
: |
|||
bl parse slurp-file |
|||
compile-bf-string ;</pre> |
Latest revision as of 14:20, 6 February 2010
Execute Brain****/Forth is an implementation of Brainf***.
Other implementations of Brainf***.
Execute Brain****/Forth is part of RCBF. You may find other members of RCBF at Category:RCBF.
A Brainf*** compiler written by Ian Osgood in Forth.
Usage
Load the code your preferred Forth environment. The code will define the words :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 name, replace printA with your preferred name.
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:
printA
Source
Tested in: GNU Forth
\ brainfuck compiler 1024 constant size : init ( -- p *p ) here size erase here 0 ; : right ( p *p -- p+1 *p ) over c! 1+ dup c@ ; : left ( p *p -- p-1 *p ) over c! 1- dup c@ ; \ range check? : compile-bf-char ( c -- ) case [char] [ of postpone begin postpone dup postpone while endof [char] ] of postpone repeat endof [char] + of postpone 1+ endof [char] - of postpone 1- endof [char] > of postpone right endof [char] < of postpone left endof [char] , of postpone drop postpone key endof [char] . of postpone dup postpone emit endof \ ignore all other characters endcase ; : compile-bf-string ( addr len -- ) postpone init bounds do i c@ compile-bf-char loop postpone swap postpone c! postpone ; ; : :bf ( name " bfcode" -- ) : char parse \ get string delimiter compile-bf-string ; : :bf-file ( name file -- ) : bl parse slurp-file compile-bf-string ;