Willsy Posted May 2, 2014 Share Posted May 2, 2014 (edited) 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. To prove it's compiled Forth, use SEE (in TurboForth, it's block 27 on the accompanying disk): SEE HW And you get: 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 . Edited May 2, 2014 by Willsy 4 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/ Share on other sites More sharing options...
Willsy Posted May 2, 2014 Author Share Posted May 2, 2014 Here's the BF "rules" for implementation. Perhaps someone can produce an assembler version > 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. Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2981705 Share on other sites More sharing options...
+OLD CS1 Posted May 2, 2014 Share Posted May 2, 2014 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. Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2981827 Share on other sites More sharing options...
Willsy Posted May 2, 2014 Author Share Posted May 2, 2014 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 1 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2981887 Share on other sites More sharing options...
Retrospect Posted May 2, 2014 Share Posted May 2, 2014 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 ? Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2981912 Share on other sites More sharing options...
Willsy Posted May 2, 2014 Author Share Posted May 2, 2014 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. 1 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2982006 Share on other sites More sharing options...
Willsy Posted May 10, 2014 Author Share Posted May 10, 2014 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? :-) 1 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2987399 Share on other sites More sharing options...
+Ksarul Posted May 10, 2014 Share Posted May 10, 2014 You've gone insane, Willsy! I mean that in a nice way. . .LOLOL Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2987417 Share on other sites More sharing options...
+OLD CS1 Posted May 10, 2014 Share Posted May 10, 2014 The men in white coats are coming to deliver your special jacket. Oh, and this. Captain Willsy is writing a compiler; Why is he writing a compiler? 1 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2987510 Share on other sites More sharing options...
lucien2 Posted May 10, 2014 Share Posted May 10, 2014 A few years ago I did a whole IDE for brainfuck on Windows, for serious brainfuck programmers! Brainfuck.zip I also like the Ook programming language. 1 Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2987549 Share on other sites More sharing options...
Willsy Posted May 10, 2014 Author Share Posted May 10, 2014 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. Quote Link to comment https://forums.atariage.com/topic/224987-brainfuck-in-forth-on-the-4a/#findComment-2987617 Share on other sites More sharing options...
Recommended Posts
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.