Thursday, June 8, 2023

Listack v0.4.0 is live

 I have spent the last few months creating and refining my own new computer language:  Listack.  Originally written in Python, I spent the last two months rewriting it in Nim, so it is now much, much faster.  What is Listack?  Besides a portmanteau of List and Stack, of course.

Listack is a concatenative, stack-based language inspired by Factor and False, among others.  It is interpreted.  It is flat - all functions are in-line functions.  Like all concatenative languages, it encourages the use of "higher" functions (called words) from "functional programming".  Listack has the concept of good/bad data baked into its core.  It has a very basic type system, but user created objects can use user-created types of arbitrary complexity.  

It is polymorphic with limited arity, meaning each command always uses the same number of arguments but the types of those arguments can differ through redefinitions of the word.  For example, '+' is used for both numbers and strings.  1 + 2 --> 3, where "abc" + "def" --> "abcdef".

The main problem with stack-based languages is that their syntax is weird.  Every command word depends on the data it needs to work upon already being on the stack, waiting for it.  This is postfix form:  1 2 +, instead of the more usual 1+ 2 or even + 1 2.  Listack gets around this by employing uniform function call syntax.  Every word except immediate ones can go in front of, between , or after its arguments.  The following are all equivalent in Listack:
+: 1 2        prefix form - before all arguments
1 + 2        infix form - after first argument
1 2 .+        postfix form - after all arguments
+(1, 2)        callable form - immediately before parenthesis surrounding all arguments

You can, to a degree, pick and choose how you want your program to look and feel.  Just keep in mind that every word actually executes in postfix form.  (Immediate words are essentially postfix without the leading period.)  Oh, and the commas are just syntactic sugar.  The parser converts them to spaces when they're not inside a quote.  The following snippets all add 1 to 2 and then multiply the result by 3.
1 2 .+ 3 .*
1 + 2 * 3
(1 + 2) * 3
*: (+: 1,  2) 3
(+: 1 2) 3 .*
*(+(1, 2) 3)
2.inc * 3

The following doubles and also squares a list of numbers to produce 6 10 20 9 25 100.
[3 5 10] {dup .+} {dup .*} bi_apply

This does a similar thing but compiles the numbers into lists [6 10 20] [9 25 100]
[3 5 10] {{dup .+} .map} keep {dup .*} .map


Listack has an interactive REPL.  After you compile it (nim c -d:release listack.nim), execute it by typing: 
./listack

You can run a program file as follows:
./listack filename
with options for -debug and -verbose

Don't have Nim?  Check out their website.

No comments:

Post a Comment

I reserve the right to remove egregiously profane or abusive comments, spam, and anything else that really annoys me. Feel free to agree or disagree, but let's keep this reasonably civil.