Edit this page

herkforth's alternative to immediates

reasoning

This is (to my knowledge) a unique approach to immediates and I hope, much more simple and usable.

I have a naming convention (starting a word with a comma) that identifies a word as one that compiles something. I found that the words that I wanted to be able to use without switching to execute color and back all started with a comma (such as: ,if ,then ,for ,next etc.). My idea is to have the editor take advantage of this convention and allow you to write pretty code (like immediates allow), while keeping the interpreter very simple (and modeless.)

-- Jason Woofenden

Explanation

Lets say we have the following definition:

: ,if ,0= here 0 ,beq [ ,;

,if will compile the code for the conditional branch. To use it we must do:

: .-or-h. [ ,if ] . [ ,then ] h. [ ,;

This is a pain for the user, and stifles optimization. But it's very nice and straightforward for the interpreter.

I would like to be able to type this:

: .-or-h. if . then h. ;

And get the same result.

My editor makes this possible by doing the following:

When you type a green word and hit space the editor will search the dictionary for a word with the same name, but prefixed by a comma. If found, the editor will insert this other word in the execute color instead.

The editor will of course do the reverse translation and display:

: .-or-h. if . then h. ;

even though the source block really contains this:

: .-or-h. [ ,if ] . [ ,then ] h. [ ,;

Summary

When you enter a green word, the editor will replace it with a yellow word with the same name except starting with a comma (if it can find one.)

Any yellow word starting with a comma, will be displayed as a green word without the comma.

Advantages

Easy to optimize

This system makes it easy to write optimizations for words like dup. Dup is one instruction, so it is very silly to be compiling a branch to dup, when it would take the same space and go way faster if we could compile that instruction instead of a branch. You can simply do this:

: ,dup $95edfffc w, ;

and whenever there is a green dup, that will be executed. You still need do define dup though so it can be used in yellow, but that's easy:

: dup dup ;

Access to both "compiler word" and normal "runtime" definition

For optimizations, such as dup, there will be two words dup and ,dup

This comes in very handy. You can compile a call to ,dup with no extra fuss (no syntax or funky words like "postpone" or "compile".)

No Modes

There is no need for modes. There is no "interpret mode" and "compile mode" like conventional forths. This makes everything simpler. You don't have to write "mode aware" words. You simply write a version for compiling and a version for executing, and you can access either with no fuss.

See Also:

how herkforth works

herkforth docs

herkforth

Edit this page · home ·