# Facsimile A simple demonstrative Lisp-like language. Facsimile provides an interpreter for a small language based on Lisp's paradigms and syntax, usable through a simple command-line interface. ## Command-line usage ``` facsimile [arg ...] ``` The first command-line argument is the path to the Facsimile source file to interpret. Any additional arguments are passed to the program. The program exits with status 1 (and prints an error to stderr) if the source file cannot be opened or if evaluation fails. ### The `args` variable The CLI automatically defines a special variable, `args`, available to the program as a list of strings. Its first element is the path of the entrypoint source file, followed by any other command-line arguments. `args` therefore always contains at least one element. ### Examples ``` facsimile examples/factorial.fax 5 facsimile examples/fizzbuzz.fax facsimile examples/lambda.fax ``` See the example programs in the `assets/examples` directory. ## Language overview ### Syntax The elementary part of the syntax is the *atom*: a basic data value. - **Numbers** are single-precision IEEE-754 floats, given in standard decimal representation, optionally with exponents. - **Strings** are Unicode strings, beginning and ending with either `"` or `'`, and may contain escapes formed of two characters whose first is a backslash. - **Booleans** are the atoms `true` and `false`. - **Symbols** are alphanumeric identifiers (underscores allowed). They can reference other data or functions, but can also be treated as data. Program structure is formed by **lists**: whitespace-separated atoms enclosed in regular brackets (`(` and `)`). The special atom `nil` is equivalent to an empty list (`()`). When a list represents code, its first element is a symbol identifying the function to call (or a lambda function), and the remaining elements are the arguments. Any argument that is a list is evaluated as code before being passed to the function; any argument that is a symbol is resolved as a variable when possible. Comments are available either as single-line (preceded by `//`) or continuous (commencing with `/*` and ending with `*/`). ### Special functions - `if` — control flow. Takes one or more condition/block pairs, each governed by a condition, plus an optional final (`else`) block. Conditions are checked in order; the first that returns a truthy value has its following block evaluated, and `if` returns that block's result. If no condition is truthy, the final block is evaluated if provided, otherwise `nil` is returned. ``` (if a // if a { (print "a") // println("a"); b // } else if b { (print "b") // println("b"); c // } else if c { (print "c") // println("c"); (print "x") // } else { ) // } ``` - `and` (aliased as `all`) and `or` (aliased as `any`) — short-circuiting logical operators. `and` requires all of its arguments to be truthy and returns the first non-truthy argument, or `nil` if all are truthy. `or` requires any argument to be truthy and returns the first truthy argument, or `nil` if none are truthy. - `def` and `fun` — define user functions. Both use the same syntax except that `def` takes the intended function name as its first argument. The first (`fun`) / second (`def`) argument is a list of symbols naming the parameters. The remaining arguments form the body and are evaluated in order; the result of the last body expression is returned to the caller. ``` (def myfunction (a b c) // fn myfunction(a, b, c) { (print a) // println(a); (add a b c) // return a + b + c; ) // } ``` Functions can be called by their symbol, which delays dereferencing: ``` (def callwith1 (f n) (call f n 1)) (print 1 2 (callwith1 (quote add) 2)) ``` - `quote` — returns its first argument without evaluating it, e.g. to pass a symbol or list to a function as data. - `list` — constructs a list without evaluating its elements. ``` (somefunction ("hello" "world")) // error: "hello" isn't callable (somefunction (quote ("hello" "world"))) // works (somefunction (list "hello" "world")) // works ``` - `block` — evaluates a sequence of expressions, returning the result of the last one. ``` (if a (block (print "abc") (print "def") ) ) ``` ### Standard library The standard library is automatically available in all programs and provides: - `not` - `eq` - `ne` - `lt` - `gt` - `lte` - `gte` - `add` - `sub` - `mul` - `div` - `rem` - `get` - `num` - `fmt` - `cat` - `print` - `input` - `srand` - `rand`