?

Log in

Jones Forth - Luke's Weblog [entries|archive|friends|userinfo]
Luke Gorrie

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Jones Forth [Apr. 2nd, 2008|01:27 pm]
Luke Gorrie
[Tags|]
[Current Location |ao nang]

Jones Forth is the best program I've read in a long time. It's a complete Forth system written as a literate program by Rich Jones. There are two source files: jonesforth.s, the kernel written in GNU i386 assembler, and jonesforth.f, the higher-level parts of Forth written in itself. Recommended reading! See also the coverage on LtU.

I've truly wasted my life up to now by not programming in the one true language BASIC assembler C Java Scheme Erlang Emacs Lisp Common Lisp Smalltalk Forth!

LinkReply

Comments:
[User Picture]From: cmm
2008-04-02 07:31 am (UTC)

thank you, added the reading queue!
(you want to fix that LtU reference, it grew a stray quote.  even though I feel dumber for having taken a glance at the discussion).
(Reply) (Thread)
[User Picture]From: lukego
2008-04-03 02:52 am (UTC)
Howdy!

Do you eat Dragonfruit, those big round red things with flaps in the photo? You have them at the markets in Israel but people don't seem very aware of them -- they're great!

(Thanks everybody, I've fixed the links I hope.)
(Reply) (Parent) (Thread)
[User Picture]From: cmm
2008-04-03 08:35 am (UTC)
Do you eat Dragonfruit, those big round red things with flaps in the photo?

no, since I've been vaguely afraid of them.  will remedy at the first opportunity.
you've become a source of much good advice, I have to say! :)
(Reply) (Parent) (Thread)
[User Picture]From: lukego
2008-04-03 09:25 am (UTC)
You peel them with your hands by just taking a firm hold of one of the flaps near the end and pulling them off. The little yellow things with white flesh and a brown seed are delish too -- we call them "locuts" in Brisbane and I'd never seen them commercially available before.
(Reply) (Parent) (Thread)
[User Picture]From: cmm
2008-04-06 07:42 pm (UTC)
GAH, IT ATE MY BRANE!
(also, I just don't get how [COMPILE] works.  looks like I'll have to build the thing and trace it in debugger)
(Reply) (Parent) (Thread)
From: joshua.grams.myvidoop.com
2008-04-08 09:15 am (UTC)
[COMPILE] just looks up a word and adds its code to the current definition (direct-threaded code consists of a list of code addresses to call). It ignores whether the word is immediate, so [COMPILE] normal-word is a no-op. Does that help?
(Reply) (Parent) (Thread)
[User Picture]From: cmm
2008-04-08 10:48 am (UTC)
I understand the intention and the semantics of [COMPILE] all right (I think), it's just that by looking at its definition I don't see how it manages to pull off the "add the code to the current definition" bit.  the definition of [COMPILE] looks like a possible definition of TICK (') to me, in fact.
(Reply) (Parent) (Thread)
From: joshua.grams.myvidoop.com
2008-04-08 03:35 pm (UTC)
Ah, I see.

In traditional threaded forth code, a word looks like this:

call docolon  \ interpret the following list of code pointers
dd foo
dd literal, 3
dd bar
...
dd exit


So to add a word to the current definition, you just get a pointer to its code and append it to the heap.

: [COMPILE] IMMEDIATE   WORD FIND >CFA , ;


Note the comma at the end: comma takes a value off the stack and appends it to the end of the heap (store value at HERE, increase HERE by 4), in this case a pointer to the code for whatever you're [COMPILE]ing.

Hrm. I hadn't actually read jonesforth before. I have to say, it looks like he did a really bad job. :(

--Josh
(Reply) (Parent) (Thread)
[User Picture]From: cmm
2008-04-08 04:08 pm (UTC)
Note the comma at the end: comma takes a value off the stack and appends it to the end of the heap (store value at HERE, increase HERE by 4), in this case a pointer to the code for whatever you're [COMPILE]ing.

exactly.  but how is this different from TICK + COMMA?  should it be different from TICK + COMMA (I mean, if it isn't, then there's no good reason for it to exist, as it doesn't even save any typing)?  I feel I'm missing something blindingly obvious here; perhaps I'm missing the fact that that I miss nothing?

I hadn't actually read jonesforth before. I have to say, it looks like he did a really bad job.

I dunno, it read great up to that bit. :)
(Reply) (Parent) (Thread)
From: joshua.grams.myvidoop.com
2008-04-09 05:48 pm (UTC)
In jonesforth [COMPILE] isn't any different than TICK + COMMA. But the model that he presents is a bit simplified, and in many Forths there are reasons for it to exist.

The original Forth model just had normal and IMMEDIATE words. But most syntax words are only useful in compilation state. And you can wind up with two versions of some words; take 'CHAR' for instance. In jonesforth.f, he has a series of words of the form

: 'x' [ CHAR x ] LITERAL ;

So many forths have a word [CHAR] which does CHAR + LITERAL.

Then people started getting the bright idea of writing an immediate word which checked STATE so they could have both pieces of functionality under one name. But this has subtle problems: see Anton Ertl's paper State Smartness: Why it is Evil and How to Exorcise It (http://www.complang.tuwien.ac.at/papers/ertl98.ps.gz) for details.

The upshot is that the ANS Forth standard specifies semantics in terms of "interpretation semantics" and "compilation semantics", and to have a Standard Forth implementation, you pretty much have to allow for having two pieces of code associated with a name (though most only have one). So TICK may not return the "right" piece of code.




As for Jones doing (or not doing) a good job...I think he did a nice job of explaining how the code works. But it seems excessively focused on the specific implementation techniques that he has chosen, with no discussion of the basic model that he is implementing.

For instance, he describes the dictionary as "just a linked list of dictionary entries" and then shows the structure he's using. I think it would have been better if he had pointed out that the dictionary is a hash, associating word names with their definitions (code), and that since it usually doesn't need to be fast, the traditional implementation is a linked list, with the name and link fields in memory immediately before the related code.

Likewise, a Forth word is a concatenation of other words (do this, then that, then the other thing). Which implies that you need some sort of call/return mechanism, and some way to compile literals. But you could use the processor's call/ret instructions, or traditional threaded code like jonesforth, or some other sort of bytecode.

There are many ways to implement Forth, and most of them are reasonably compatible. So I think it would have been nice if he had given some inkling that he knew that jonesforth was merely one point in a design space, and tried to explain the concepts behind the technicalities.

But maybe that sort of thing was clear enough without being mentioned explicitly...?
(Reply) (Parent) (Thread)
[User Picture]From: redline6561
2008-04-02 12:28 pm (UTC)
This has been on my list of things to read for a while but I figure I should finish SICP first, then maybe CTM or PAIP. I always get discouraged when I think about how far I have to go to catch up to guys like you, psnively, etc. Anyway, if I might be so impertinent as to ask, how did you get this job hacking on the XO all over the world? Who are you actually working for at the moment? Sorry if this is a bit too personal.

Regards,
Brit
(Reply) (Thread)
[User Picture]From: lukego
2008-04-03 02:48 am (UTC)
FWIW I skipped the first and last chapters of SICP and haven't read CTM. PAIP is really excellent. I was extremely fortunate to have our friend darius as a mentor via the internet to make me read all of these.

I don't really want to field personal questions on this website. Suffice it to say that unpaid volunteer work and random telecommute projects are not that hard to come by and they are what you make of them!
(Reply) (Parent) (Thread)
[User Picture]From: redline6561
2008-04-03 04:34 pm (UTC)
That's really awesome(re: telecommute projects and volunteering). Thanks!
(Reply) (Parent) (Thread)
From: (Anonymous)
2008-04-02 01:31 pm (UTC)

Links?

The links jonesforth.s and jonesforth.f point to the same file. Was that intentional?
(Reply) (Thread)
From: (Anonymous)
2008-04-02 01:49 pm (UTC)

intel macosx?

Spotting that macbook air on the picture:

did you manage to build jonesforth.S on a intel macosx box?

Cheers,
Dirk
(Reply) (Thread)
[User Picture]From: lukego
2008-04-03 02:53 am (UTC)

Re: intel macosx?

No I've only been reading and not running. (And WOW Macbook Air is the best thing I've ever bought.)
(Reply) (Parent) (Thread)
From: (Anonymous)
2008-04-03 09:11 am (UTC)
Nice! Just what I was craving for. Started yesterday in earnest in 'Forth for Professionals' (I was-am sick... wouldn't dare doing recreational stuff in these crutial school deploying times... lalala), which disappointed after the impressive opening by actually starting with the basics. The nerve! That book has serious subject-ordering problems, just like this comment.

What I wanted from the book is what Jones is supplying: a quick fix of Forth implementation. But I'll probably read it anyway (the book). Don't want to get the feeling I wasted two days of my time copying it, and it's cool in it's own right, it seems up till now.

/Ties
(Reply) (Thread)
From: (Anonymous)
2008-04-04 06:14 am (UTC)

high-to-low

I found an interesting row in your blog. But I can't see what triggered the switch to Elisp. BASIC (what's under) assembler (to much work) C (shit, memory bugs) Java (too verbose) Scheme (mm.. light threads.. I like) Erlang (??) Emacs Lisp (need industry standard) Common Lisp (history have some perls) Smalltalk (too high level, need speed) Forth! Like netflix challenge, the next choice depends on the previous experience.
(Reply) (Thread)
[User Picture]From: lukego
2008-04-04 02:44 pm (UTC)

Re: high-to-low

I just like learning new languages. Otherwise I'd have stopped at Erlang :-)
(Reply) (Parent) (Thread)
From: (Anonymous)
2008-07-23 01:26 pm (UTC)

Jonesforth question

Hello,

I am trying to understand the difference between using lit vs ['] while compiling. I am looking for someone who might shed some light.

Any thoughts, please?

Thanks
Siva
sivaramreddy( at the rate of )hotmail.com
(Reply) (Thread)