We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Listack

From Esolang
Jump to navigation Jump to search

Listack: A symmetric, flat, stack-based, concatenative language

Listack is a dynamically typed, symmetric, flat, concatenative, polymorphic, interpreted programming language. Like most concatenative languages, it is stack based. Listack was inspired by the stack-based languages Factor, False, Joy, XY, and Forth. The manual scope system was inspired by Fish. The dependent type system for objects was inspired by Euphoria. This document refers to words (the traditional concatenative language term) interchangeably with functions and procedures. Listack is an impure functional language, in which functions are data, data are functions, and every element contains a validity marker.

Listack is symmetric because it employs a uniform function call syntax, where almost every word (command) can be in a prefix (+: 1 2), infix (1 .+ 2), or postfix (1 2 +) form. Prefix (word:) and infix (.word) forms are translated at run time into postfix (word) form. The infix form is always used after the first argument. There is also a more “normal looking” callable form: +(1, 2). This is translated to postfix by the parser when a program is loaded and is thus more efficient than the prefix and infix forms. It is important to understand that all words are implemented and executed as either postfix or immediate.

Listack is flat because every function is inlined. There is no call stack of functions to return to. Each word is consumed as it reaches the front of the command queue and is evaluated. Looping is conducted purely by recursive inlining of functions. The only data flow primitives in the language are variants of if and <=>. All the “higher level” words are written in Listack, and can be examined in the system.ls file.

Listack is concatenative because data flow is through the stack. Functions are composed simply by writing them one after another (concatenation). They all take their data from and add it to the stack, much like the *nix pipe ‘|’ command. The order of operations is quite simple: first come, first served. For example, 1 2 + 3 * leaves 9 on top of the stack.

Listack is polymorphic through multiple dispatch because every word (function) definition requires a list of argument types. Each word can be defined multiple times using different arguments. Listack does, however, restrict each word to using the same number of arguments for each variant (each word has a single arity). This can be avoided somewhat by using a collection type (List, Block, or Seq) as one or more arguments. Listack also allows a singular type “Otherwise” to handle error conditions where the expected data types are not found on the stack.

Listack makes use of name spaces. Program files may be imported, and each definition occupies space in an environment based on the name of the file. Name spaces can also be created on the fly, as desired. The default name space is “default”. Note that “core” words cannot be altered, although their use may be extended through non-Otherwise variants. “System” words can have user created “Otherwise” variants”. All variables are local, and are not part of a name space.

Listack has two types of built-in variables: local variables a through z, and global stacks A through Z. Users may create (and destroy) new variables as desired. Local variable scope (indicated by “|> <|”) is manually handled by the programmer, but must be properly nested. Every local scope redefines a..z and initializes each to none. Variable names are searched from newest to oldest scope. Local scope also creates a new data stack, optionally taking data from the stack beneath it upon creation. Closing scope moves all current stack items to the next lower data stack. The data stack can also be saved and restored independently of scope.

Another feature of Listack is that the functional forms of “good” and “bad” are built in to every piece of data. (These correspond to the functional concepts of ‘some’ and ‘none’.) An invalid or corrupt piece of data is marked bad(type), and will contaminate everything it gets used with if not handled in a timely manner. Local variables a..z are initialized to the special none value, which is implemented as >bad(nil) .

Listack handles errors in a quiet way. Most words will log non-parsing errors and write the total number of errors to the screen without halting the program. Users can create their own error handling through querying the error stack and through use of “Otherwise” variants of words.

Blocks are code blocks (lambda functions). (The traditional concatenative term is ‘quote’, short for “quoted program”.) Lists are collections of data. Seqs (sequences) are code blocks that are immediately executed. Other than words, variable names, and sequences, each item in the command queue is, upon evaluation, simply moved to the top of the data stack. “\” defers evaluation of the following item, so \(1+2) will move (1+2) to the top of the data stack instead of being immediately evaluated to 3.

Line comments begin with ‘# ’ and go to the end of the line. Single word comments begin with '#' and are useful as syntactic sugar. Block comments begin with '#:' and continue until '.#' is found.

Commas (outside strings and characters) are considered to be white space by the parser. Underscores in numbers are ignored, so 100_000 is a valid integer.

Strings are surrounded by either ‘single’ or “double” quotes. Characters are preceded by `a back tick. The normal escape characters are used. Listack currently supports only ASCII characters (codes 0 to 127).

“TOS” hereafter refers to the Top Of Stack.

To compile Listack v0.4.1, place all the files together in one directory and type the following on the *nix command line. (This assumes you have already installed Nim 2.4.2 or better.) nim c --deepcopy:on -d:release listack.nim

Call Listack from the command prompt as follows: To execute a program file: ./listack filename.ls [-debug] [-verbose] To explore Listack with the interactive REPL: ./listack

Listack may be freely used and distributed with attribution under the MIT public license.

Example Code

The manual can be found in the Github repository here:

Different ways to do the same thing:

 1 2 + 3 *
 1 .+ 2 .* 3
 +: 1 2 3 *
 *(+(1, 2) 3)
 1 2 3 \+ dip *

Here are the actual in-language definitions of while and until:

 # while helpers
 create_global: "_while_cond"
 create_global: "_while_body"
 
 # internal use only
 alias: "_while_"
   {@!_while_cond                          # eval the condition for 'when'
     {@!_while_body                        # eval the loop body
       _begin_loop_                        # set the cont point
       _while_}                            # recurse
   when}                                   # 'when' is an if without an else
 
 # {condition to continue} {body to execute} while
 # Executes body zero or more times.
   # {true} {println:"infinite loop"} while
 
 def: "while" [Executable, Executable]
   {@>_while_body @>_while_cond              # set up variables
     _while_                                 # call the helper function
     _end_loop_                              # establish the break point
     @<_while_body drop @<_while_cond drop}  # tear down variables
 
 def: "while" [Bool, Executable]
   {@>_while_body @>_while_cond 
     _while_ _end_loop_
   @<_while_body drop @<_while_cond drop}
 
 # {body} {condition} until
 # Always executes body at least once.
   # {println:"infinite loop"} false until
 
 def: "until" [Executable, Executable]
   { {not} concat @>_while_cond @>_while_body
   @!_while_body _begin_loop_ _while_ _end_loop_}
 
 def: "until" [Executable, Bool]
   { {not} concat @>_while_cond @>_while_body
   @!_while_body _begin_loop_ _while_ _end_loop_}

See also

External resources