Simple LISP

Simple Lisp is a toy programming language that uses the LISP syntax without the advanced things (macros, keywords, quotes, etc).

Syntax

Everything is a list of items in a pair of parentheses and returns a value, e.g.: (x y z (a b c)).

Comments are written between /* and */.

Types

Keywords

abort, add, class, dec, div, eq, fiber, fun, gt, if, inc, isdone, let, list, load, lt, mul, new, neq, not, nth, print, return, set, sub, while, yield

API

? that item is optional.
+ one or more items.
* zero or more items.

Function declaration

(fun name? (arg*) body+) /* example: */ (fun addition (a (b 0)) (add a b) ) (addition 3) /* return 3 + 0 */ (addition 3 4) /* returns 3 + 4 */

Each arg must be either identifier or a list of an identifier and some expression (its default value).

Function call

(function arg*)

To call a function, just put its name as the first item in a list, followed by its arguments.

Binary operator

(operator left right)

operator can be: add, sub, mul, div, lt, gt, le, ge, eq, neq.

Conditional

(if condition if_true if_false?)

If if_false isn't there, it is equal to null.

Loop

(while condition body+)

Returns the last item of body.

Switch

(switch value (case first second)* default )

Compares value with the first value of each pair and returns the second value if they are equal. Returns default if nothing matched.

Let and set

(let name value)

"let" creates a new variable in the current scope (shadowing if a similar named variable exists in an outer scope).

(set name value)

"set" sets an existing variable even if in an outer scope. Errors if no variable exists.

List

(list value+)

Creates a list.

Return

(return value?)

Returns prematurely from a function or from a loop. Outside of a function or loop does basically nothing (i.e. just returns the value).

Call

(call function arg*)

Call a function, identical to (function arg*), but was made for lambdas stored in variables.

Load

(load filename)

Load a file and imports all its symbols. The loaded file has access to the outer scope (defined functions and variables before the "load" call), but can only add new functions.

Block

(block item+)

Execute all items in order, useful for when only a single item is accepted, like "if". Returns its last item.

Class definition

(class name parent_class? (let name value)* (fun ...)* )

A class is defined by a name, an optional parent class, some fields (using "let") and functions (using "fun"). The order of fields and functions doesn't matter.

Class instantiation

(new name)

Creates a variable whose type is an instance of the class name.

Class function call

(class test (fun my-print () (dump "debug") ) ) (let var (new test)) (my-print var) /* prints "string: debug" */

To call a function of a class, you call it like a function but its first argument is the variable returned by "new".