Jump to content
IGNORED

BrainFuck in Forth on the 4A


Willsy

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.

 

post-24932-0-65425000-1399020377_thumb.png

 

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

 

SEE HW

 

And you get:

 

post-24932-0-86994100-1399020446_thumb.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
  • Like 4
Link to comment
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.

Link to comment
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.

Link to comment
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

  • Like 1
Link to comment
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.

  • Like 1
Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...