top | item 39154005

(no title)

akritid | 2 years ago

I think they are not ordinary words that consume arguments directly from the stack. They also need to look ahead in the input stream of tokens. With support for quoted code, there seems to be no need for any other words operating at the token stream level, like conditionals and def. They just need to consume the stack. I am still digesting this so happy to be corrected.

discuss

order

MaxBarraclough|2 years ago

Forth is very free-form, it doesn't require that your words be 'ordinary' in that sense. The : word isn't the only one that can consume from the input stream, neither is it the only word that can define words (such words are called defining words). The CONSTANT word, for instance, is a convenient syntax for defining a word to push a constant value onto the stack. [0]

You can even define your own 'mirror' of the : word, which involves some fiddly management of Forth's state, but it's doable. [1]

See also [2] on Forth's execution model and [3] on defining your own defining words.

[0] https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Con...

[1] https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Use... (The specific code shown is specific to Gforth, there's no stats or LATESTXT word in the ANS Forth standard.)

[2] https://www.forth.com/starting-forth/9-forth-execution/

[3] https://www.forth.com/starting-forth/11-forth-compiler-defin...

klyrs|2 years ago

Oh, I don't know about that. Forth words can take control of the input stream! The ordinary parser eats space-deliminated tokens, sure, but how does the " word work? It takes over the input, and reads until it finds a terminal quote! It's wild: learning forth, you get used to RPN... but then you can implement an infix parser, and your "forth" can bootstrap a C-like language all in one source file.