A simple lisp implemented in zig. (my first zig project :D)
zisp --path <PATH>
--show-ast
- Prints the abstract syntax tree to the console.--help
- Shows a help menu with more info.
The syntax for if statements is the following:
if (condition) (body)
Additionally, you can supply an else case:
if (condition) (body1) (body2)
You can call a function by running functionName arg1 arg2 ...
You can define a function with the following syntax:
def functionName (argname1 argname2) (body)
Unlike the global
function, def
is a sort of macro that can accept identifiers and contexts as their literal tokens. Functions reside in the local scope and can be run by value.
Zisp has a runMethod
function, which will pass a value into self
. Consider the following:
(var "epicTable" (createTable))
(put epicTable "counter" 0)
// first define a method that accepts `self` as the first argument.
(def count (self) (
(var "counter" (kget self "counter"))
(print "Count: ")
(println counter)
(put self "counter" (+ counter 1))
))
// next, put that method in the table like a regular value.
(put epicTable "count" count)
// now, we can use `runMethod` to run our method.
(runMethod epicTable "count" [])
bool
- either true or false. same format in literals.int
- a 32-bit signed integer. literals defined by just typing the actual number characters.str
- a variable-length string. literals defined using quotation marks.list
- A list containing variable types and of variable length. delimited by square brackets, with whitespace separating elements (ex: ["a" "b" "c"]).table
- Maps a key to a value. To create one, use thecreateTable
function.function
- A literal function as defined bydef
. This type is mainly only referenced in function calls and OOP.
A context is used to separate a call from other calls. For instance:
println (+ 1 2)
The output of + 1 2
is passed to println
as a single argument.
Contexts are also used in separating multiple sequential calls as well. Take this for example:
(println "foo")
(println "bar")
The contexts are used here to tell the interpreter that these are two separate calls.
Comments can be added with //
. When a comment character is reached, the interpreter will ignore the rest of the line.
+ a b
- adds two numbers together. can also concatenate two strings together- a b
- subtracts the second number from the first* a b
- multiplies two numbers together/ a b
- divides the first number by the second% a b
- gets the modulo of the two numbers. follows the true mathematical moduluoprint arg
- prints the provided text to the consoleprintln arg
- prints the provided text to the console (with a newline)input arg
- prints the provided text to the console and waits for user input. returns a string.global name val
- sets a global variable of name and valuevar name val
- sets a local variable of name and valueiget list index
- retrieves the value at the specified index from the listappend list item
- appends an item to the end of the listinsert list index item
- inserts an item at the index of the list and pushes everything else overextend list1 list2
- Adds the contents of list2 to list1pop list1 | pop list1 index
- Removes the value from the list at the optionally specified index and returns it. If the index is not specified, it defaults to the last element in the list.createTable
- Returns a new table.put table key val
- Puts a value in the table at a specified key.kget table key
- Returns the value in a table with a specified key.runMethod table key args
- Gets and runs a method in a table, passing the table as the first argument.args
is a list.eq arg1 arg2 ...
- Returns true if all args are equal, false otherwise.neq arg1 arg2 ...
- Opposite of eq.not arg
- Reverses a boolean value.or arg1 arg2 ...
- Returns true if any of the args are true, false otherwise.and arg1 arg2 ...
- Returns true if all of the args are true, false otherwise.