Hacker News new | ask | show | jobs
by klyrs 876 days ago
That isn't syntax. : is a word, and so is ;

Cool article posted to HN a little while back: http://ratfactor.com/forth/the_programming_language_that_wri...

2 comments

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.
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...

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.
I was not aware of this, thanks for giving me an explanation. I don't have any experience writing in forth, my friend described this type of language to me and I implemented it.
Some related goodness from Forth: there's another word, :NONAME, for defining anonymous words. [0][1]

The more common : word enters the newly defined word into the dictionary, which is Forth speak for the newly defined word now being considered 'in scope'. The :NONAME word instead returns an execution token on the stack, which is analogous to a function pointer.

See also [2], on Forth's execution model more broadly.

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

[1] http://lars.nocrew.org/forth2012/core/ColonNONAME.html

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

You should go implement the FORTH-83 wordlist. (arbitrary pick, but it's easy to find documentation for the words) Doing that will reveal exactly how powerful and succinct Forth has always been, because the compilation/execution distinction in Forth is just a bitflip.
yes, i have actually thought about this solution independently, but i still find it a little bit ugly to have to tell the parser that everything past a certain point is runtime, if I am understanding this correctly.
Your language is really neat, thanks for putting it out there
Hey thanks! I didn't think people would actually care about this lol