Skip to content
cptaffe edited this page Sep 24, 2014 · 1 revision

Welcome to the lispy wiki!

Lispy

lispy is a simple interpreter written in python for a lisp-like language.

evaluation

It supports line-by-line evaluation with pretty printed results, for example:

(+ 1 2)
3

The first line is the input, the second is the output. This would also work:

(+
1
2)
3

Newline characters are counted as spaces characters, so it only worries about the number of parentheses. Once you have a coherent statement, it is processed.

Lambdas

lispy also supports defining functions, for example:

(def sqr (lambda (n) (* n n)))
sqr
(sqr 4)
16

The variable sqr was defined as a lambda that takes n and returns n * n. We then sqr'd 4, 4 * 4 = 16 so it worked.

Imports

lispy has a standard library, sort of. Right now lispy has a folder whose path is coded in optim.py (you can edit it for your system). Here's an example of the only current library file, math:

(import "std:math")
(fac sqr pwr)
(fac 20)
2432902008176640000

Right now lispy uses python numbers, so it returns whatever numbers the standard python operations would.

Recursion

This is how the std lib function fac is defined:

(: fac (@ (n) (if n 1 (* n (self (- n 1))))))

Here we see some cooler features of lispy. self is a variable defined in the current scope when a lambda is evaluated, it is defined as a copy of the current lambda, so it can recurse. if checks if a value is true (not zero) or false (0), there are macros for these (#t, #f). If true, if evaluates the first if true, the second if false. @ is a shorter macro for lambda and : is a shorter macro for def. These "macros" are found in the same builtins table so they execute at the same speed.

Clone this wiki locally