Jump to content
Sign in to follow this  
Willsy

BrainFuck in Forth on the 4A

Recommended Posts

I got to wondering how much code it would need to implement the BrainFuck language in Forth. Turns out not much:

create array
8000 chars allot
variable pointer
array pointer !
: reset ( -- )
    \ zero bf memory and reset pointer
    array 8000 0 fill
    array pointer !  ;
: peek ( -- )
    pointer @ c@ ;
   
: poke ( -- )
    pointer @ c! ;
   
: > ( -- )
    \ increment the pointer
    pointer @ 1+ pointer ! ;
   
: < ( -- )
    \ decrement the pointer
    pointer @ 1- pointer ! ;
: + ( -- )
    \ increment the byte at the pointer
    peek 1+ poke ;
   
: - ( -- )
    \ decrement the byte at the pointer
    peek 1- poke ;
: . ( -- )
    \ output the byte at the pointer
    peek emit ;
   
: , ( -- )
    \ input a byte and store it in the byte at the pointer
    pad 3 expect  pad 3 number  drop poke ;
   
: [ \ ( -- )
    \ jump forward past the matching ] if the byte at the pointer is zero
    [compile] begin  compile peek  [compile] while ; immediate
   
: ] ( -- )
    \ jump backward to the matching [ unless the byte at the pointer is zero
    [compile] repeat ; immediate

That's a complete BrainFuck *compiler*. Most implementations I have seen are interpreters. Here, you feed in BF code using normal colon definitions. Because it's a normal colon definition, what you get out is compiled Forth code. Here's Hello World! in BrainFuck:

: hw ( hello world in BrainFuck )
    reset
    + + + + + + + + [ > + + + + [ > + + > + + + > + + + > + < < < < - ] > +
    > + > - > > + [ < ] < - ] > > . > - - - . + + + + + + + . . + + + . > >
    . < - . < . + + + . - - - - - - . - - - - - - - - . > > + . > + + . ;

To run it, just type hw and press enter.

 

[attachment=339775:brainfuck.png]

 

To prove it's compiled Forth, use SEE (in TurboForth, it's block 27 on the accompanying disk):

 

SEE HW

 

And you get:

 

[attachment=339776:see_bf.png]

 

Forth, you gotta love it. Of course, some say Forth is a bit of a BrainFuck, and it is, until the penny drops. Even then, managing the stack is still a puzzle. But it's so powerful :grin: .

Edited by Willsy

Share this post


Link to post
Share on other sites

Here's the BF "rules" for implementation. Perhaps someone can produce an assembler version :twisted:

 

>  Increment the pointer.
<  Decrement the pointer.
+  Increment the byte at the pointer.
-  Decrement the byte at the pointer.
.  Output the byte at the pointer.
,  Input a byte and store it in the byte at the pointer.
[  Jump forward past the matching ] if the byte at the pointer is zero.
]  Jump backward to the matching [ unless the byte at the pointer is zero.
 

Share this post


Link to post
Share on other sites

Damn sweet, Willsy!  BF has been a curiosity of mine since it was first released for the Amiga.  We used to toy around with it in IRC way back in the day and I think I still have somewhere an ARexx interface between AmIRC and the BF interpreter.

Share this post


Link to post
Share on other sites

Cool huh? Forth is so cool for implementing other languages. There are actually working implementations (done years ago) of both a BASIC and PASCAL compiler, all done in Forth.

 

As a result of a comment on comp.lang.forth I'm now going to be spending this evening working on an optimizing BF compiler. A complete and utter total waste of time. But I don't care. I love hacking in Forth  :P

Share this post


Link to post
Share on other sites

That would make sense, since the TI BASIC was done in GPL which is another language itself that has to crunch down through to assembly .... so if Basic is running in Forth .... what would the speed be like, versus doing it in GPL ?

Share this post


Link to post
Share on other sites
It's more like BASIC running *as* Forth rather than "in" Forth, since they take BASIC in and output Forth add the result. I think it would be a lot faster than TI Basic assuming the compiler was clever enough. I don't think it would be as fast as compiled BASIC though.

Share this post


Link to post
Share on other sites

It just got a whole lot weirder. Not content with writing a BrainFuck interpreter/compiler, I knocked together an optimising Brainfuck compiler! It compiles to Forth code, but optimises as it goes.

 

I wrote a page on it here. Does it get any geekier than this, folks? :-)

Share this post


Link to post
Share on other sites

The men in white coats are coming to deliver your special jacket.

 

Oh, and this.

 

http://www.youtube.com/watch?v=HU2ftCitvyQ

 

Captain Willsy is writing a compiler;

Why is he writing a compiler?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...