News for hackers. Stuff that matters.
Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Friday January 26, 2024 @08:40PM (#39151473)
I decided to undertake the project after my friend Matthew Hinton introduced me to stack based programming -- he claimed that they were elegant because they allowed for both easy metaprogramming and side effects, to which I agreed after writing a little bit of my own language. It was really easy to implement, too -- most of it took only one day, and getting the basic functionality working took only 2-3 days.

Goals -- I eventually want to write an emacs-like text editor in stem by writing a C library that does bindings for ncurses. I think it would be fun, for one, and I think it would not be that hard of an end product to make. I also had plans of writing a stem compiler, and I already pretty much know how to do that, but there are a couple of difficulties in doing that. Namely, right now, there is no way of telling the difference between compile-time and runtime operations, which will slow the language down considerably. My solution to this currently is basically "interpret once, run binary forever", but the details are still need to be worked out.

Problems it solves -- to be honest, you can really do anything in any programming language, but this langauge aims to be a higher level rendition of stack based programming languages, mostly for scripting and creating abstract representations of things.

Note that I am still trying to write in a couple things, namely, an actual include statement as a builtin that reads from some predetermined standard include directory, and an OOP implementation using metaprogramming in the standard library.

Not to mention the fact that I have been really interested in writing programming languages since I was in high school. One interesting thing about stack based languages is that the AST need not be generated at all. I taught myself a lot of linguistics, specifically generative grammar, as a result of attempting writing various programming languages in the past, and so these types of projects are just interesting to me.

If you ARE looking for one unique thing about this language, though, it is that `def`, the method by which you define functions, is itself a word. I don't think a lot of stack based languages do this (because it is much harder to compile languages like this as mentioned above), but it offers much more internal consistency within the language as a result.

Re:Stem, a stack-based language with metaprogramming and a C... by lebuffon on Saturday January 27, 2024 @12:17AM (#39152821)
Very neat. You have tried to make a very orthogonal language from what I can see. One of the critiques of Forth is that it is postfix at runtime but typically prefix for compiling stuff. Your use of an unassigned text label being the default to which things can be attached with def is a nice solution.

How would say the language performs at runtime vs other REPL languages. Is it compiling to native code behind the curtain or is there a VM back there?

Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @12:38AM (#39152917)
Intepreted... right now. There isn't a vm currently, but I'm planning to do native compilation pretty soon, as I already have an idea of how I would go about doing it.
Re:Stem, a stack-based language with metaprogramming and a C... by transfire on Saturday January 27, 2024 @12:09AM (#39152783)
> `def` … is itself a word.

But of course, that is the way of the Forth.

Hmmm… Have a look at JonesForth. I think it might be very rewarding for you.

Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @12:15AM (#39152810)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by klyrs on Saturday January 27, 2024 @12:22AM (#39152837)
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...

Re:Stem, a stack-based language with metaprogramming and a C... by akritid on Saturday January 27, 2024 @04:24AM (#39154005)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by MaxBarraclough on Saturday January 27, 2024 @06:21AM (#39154636)
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...

Re:Stem, a stack-based language with metaprogramming and a C... by klyrs on Saturday January 27, 2024 @12:08PM (#39157374)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @12:36AM (#39152908)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by klyrs on Saturday January 27, 2024 @02:28AM (#39153447)
Your language is really neat, thanks for putting it out there
Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @06:01PM (#39160766)
Hey thanks! I didn't think people would actually care about this lol
Re:Stem, a stack-based language with metaprogramming and a C... by crq-yml on Saturday January 27, 2024 @05:55AM (#39154496)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @06:00PM (#39160758)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by MaxBarraclough on Saturday January 27, 2024 @05:58AM (#39154506)
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/

Re:Stem, a stack-based language with metaprogramming and a C... by lebuffon on Saturday January 27, 2024 @12:23AM (#39152841)
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". :=)

Re:Stem, a stack-based language with metaprogramming and a C... by ret2pop on Saturday January 27, 2024 @12:35AM (#39152904)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by ithkuil on Saturday January 27, 2024 @07:32AM (#39155004)
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.
Re:Stem, a stack-based language with metaprogramming and a C... by akritid on Saturday January 27, 2024 @04:13AM (#39153959)
Indeed using a quote to provide the word’s body seems very intuitive. I was asking about this on the retro forth IRC and turned out it could be bolted on to retro forth like: [ body … ] :name