October 23, 2003

Language hacking for fun 'n profit

Well, fun at least.

You may or may not know, but Parrot's got a simple but functional Forth implementation as part of it. Nothing fancy, just the base line scanner and math functions, but the compiler works so you can do things like 10 20 + . and get 30 as you'd expect, or : GIMME_30 10 20 + . ; if you wanted to package it up as a new word.

Anyway, I need a Parrot language a bit higher-level than plain assembly for work, and if anything counts as "a bit higher level than assembly" it's Forth. Heck, the standard doesn't even require floating point numbers. Or integers larger than 16 bits as base for that matter. (Though 32 bit integers are required to work so you have to fake 'em if they aren't there) So, since I've been fond of Forth forever I figured it's time to go extend the thing and add in the missing bits.

Which, it turns out, is (at least for the non-control-flow words) darned easy and really compact. End result is maybe a dozen or so opcode_t cells for most of these things, which, honestly, is just damned cool. Currently the forth implementation unrolls user-defined words to a sequence of primitives and then executes the primitives, but I think I may see about generating bytecode on the fly with the built-in assembly functionality. That'd be damned cool too. :)

Posted by Dan at October 23, 2003 10:37 AM | TrackBack (2)

Hi Dan,

Does this mean that you're building some Parrot instructions out of FORTH words or that you're simply exercising Parrot with a little mini-FORTH language written using Parrot opcodes?


Posted by: steve jenson at October 23, 2003 05:12 PM

It means that I'm looking at building real code for my work (as opposed to Parrot, my hobby and occasional obsession) in Forth, running on Parrot. It's not for fun, as such, though it certainly is fun, nor am I planning on any of Parrot's infrastructure being written in Forth.

Having said that, if I teach the Forth engine (languages/forth/forth.pasm in the parrot distribution) to emit bytecode and it works, well... who knows what might happen? :)

Posted by: Dan at October 23, 2003 05:47 PM


I was vaguely planning on doing the same thing, but didn't need it for work and so kept prioritizing it way down. I'm really happy to see you doing this. Have you checked out some of Chuck's latest work in ColorForth? He's simplified the syntax even further. I really like the tail calls and his new IF structure. I even like the color syntax approach although I've always been a plain text kinda geek.

ColorForth Homepage:

ColorForth Mailing List:

Posted by: Bill Glover at October 24, 2003 08:39 PM

It is not good to read your journal, Dan, when I am on a kick to learn new languages. =)

You wouldn't happen to have a recommendation for a good book on Forth would you?

Posted by: Brett at October 24, 2003 10:50 PM

Inspired by Dan's foray into forth, I ordered used copies of 'Starting Forth' and 'Thinking Forth' by Leo Brodie from Amazon. They're both long out of print but look like excellent books. It's a seller's market though and the used prices start at $78 for a paperback copy.

Posted by: Dave Pippenger at October 25, 2003 03:07 PM

I'd definitely recommend Starting Forth. It's a cool little book, and what I learned the language from ages ago. As Dave said, it's out of print so it's a pain to get a copy. (Maybe someday they'll put PDFs of the thing up, the way that Paul Graham's Lisp book is, but I think it predates electronic manuscripts by just a little bit...)

Forth.org is also a good spot to check out. While they're a bit ratty with dead links in spots, there are pointers to a number of places around the 'Net with docs, tutorials, and resources. Google's also your friend here, as "forth", almost exclusively, refers to the language.

Posted by: Dan at October 25, 2003 03:34 PM

Thanks to both Dave and Dan. The second edition is now on my Amazon.com wish list.

Posted by: Brett at October 25, 2003 09:00 PM

It's only fair to point out that Marcel Hendrix has reimplemented Starting Forth at http://home.iae.nl/users/mhx/, for the patient and curious. It's not a page-by-page scan (more's the pity, as some of the cartoons present in the original have inspired awards and graphical models - see http://www.jwdt.com/~paysan/dragon/), but it covers almost all of the same territory, in the same order, with updated examples. Highly recommended.

Posted by: Gnomon at October 28, 2003 10:43 AM

just for you to help me plz. i need your help because of this i am a student and i need application

Posted by: kolawole at May 3, 2004 12:35 PM

I think, unfortunately, that you'll find that a blog isn't really the right place to learn about writing language compilers. Google ought to be able to find you some stuff, and citeseer's good for the more advanced papers and such.

Posted by: Dan at May 3, 2004 12:41 PM