Hacker News new | ask | show | jobs
by ret2pop 876 days ago
correct me if I am wrong, but from my knowledge concatenative programming often assumes a different syntax for defining words. Usually it looks something like : add a b + ; if I am not mistaken.
2 comments

You are correct, but in Forth for example, the colon is a word in the dictionary of words and so is semi-colon. They just happen to parse the input stream rather than taking arguments off the stack. Everything in Forth is called a "word".

So depending who your lawyer is, : and ; are "words". :=)

Yes. In forth some words have the "immediate" flag set. When such words are encountered during compilation they are executed nevertheless. This allows words such as ; to end a word definition and return to interpretation mode.
I am not generally knowledgeable about Forth and languages like it because the only experience I have writing in anything close to forth is in my own language. Thanks for explaining.
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...

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