Jump to content

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
https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/
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.

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.

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

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 ?

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

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? :-)

  • Like 1

A few years ago I did a whole IDE for brainfuck on Windows, for serious brainfuck programmers!

Brainfuck.zip

 

okay. ... I thought I was a geek but I'm clearly an amateur compared to you ;-)

I also like the Ook programming language.

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...