Didactic Lisp interpreter in Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
.gitignore
LICENSE
README.md
lispy.py
test_lispy.py

README.md

lispy

Didactic Lisp interpreter in Python

Usage

REPL mode:

$ python lispy.py
lispy v0.0.1

>>> (quote 1 2 3)
(1 2 3)
>>> (+ 1 2 3)
6
>>> (+ 1 2 (+ 3 4) 5)
15

Interpreter mode:

$ python lispy.py hello_world.lisp
Hello, world!

Test

$ python test_lispy.py

Standard Library

quote: Avoid evaluation of the given argument

>>> (quote foo)
foo
>>> (quote (1 2 foo))
(1 2 foo)

defun: Define functions in global scope

>>> (defun area (r)
      (* 3.14 (pow r 2)))
:area
>>> (area 10)
314.0

if: Conditional expression

>>> (set password "123456")
nil
>>> (if (= (get password) "123456")
        (write "Logged in")
        (write "Wrong password"))
Logged in
nil
>>> (set password "654321")
nil
>>> (if (= (get password) "123456")
        (write "Logged in")
        (write "Wrong password"))
Wrong password
nil

let: Create local variables

>>> (let ((x 1)
          (y 2))
      (+ x y))
3

progn: Execute sequential expressions

>>> (progn
      (write "Hello")
      (write "What's your name?"))
Hello
What's your name?
nil

atom: Return true for all values except lists

>>> (atom nil)
t
>>> (atom t)
t
>>> (atom 1)
t
>>> (atom 1.5)
t
>>> (atom "abc")
t
>>> (atom (list 1 2 3))
nil

car: Return first value of a list or nil

>>> (car (1 2 3))
1
>>> (car (1 2))
1
>>> (car (1))
1
>>> (car ())
nil
>>> (car nil)
nil

cdr: Return tail of list or nil

>>> (cdr (1 2 3))
(2 3)
>>> (cdr (1 2))
(2)
>>> (cdr (1))
nil
>>> (cdr ())
nil
>>> (cdr nil)
nil

cons: Append element to given list

>>> (cons 1 nil)
(1)
>>> (cons 1 (list 2 3))
(1 2 3)
>>> (cons 1 (cons 2 (cons 3 nil)))
(1 2 3)

set: Set value to global variable

>>> (set (quote *foo*) 42)
nil

get: Get value from previous defined global variable

>>> (set (quote *foo*) 42)
nil
>>> (get (quote *foo*))
42

list: Create list evaluating the given arguments

>>> (list 1)
(1)
>>> (list 1 2 (+ 3 4))
(1 2 7)

= or eq: Verify whether two elements are equivalents

>>> (= 1 1)
t
>>> (= 1 1.0)
t
>>> (= 1 2)
nil
>>> (= 1 "a")
nil

+ or sum: Sum all arguments

>>> (+ 1 2 3)
6
>>> (sum 1 2 (+ 3 4) 5)
15

- or sub: Subtract two arguments

>>> (- 1 2)
-1
>>> (sub 1 (- 2 3))
2

* or mul: Multiply all arguments

>>> (* 1 2 3)
6
>>> (mul 1 2 (* 3 4))
24

/ or div: Divide two arguments

>>> (/ 1 2)
0.5
>>> (div 1 (/ 2 3))
1.5

`pow`: Raise the first element to the power defined in the second one
```lisp
>>> (pow 10 3)
1000
>>> (pow 25 (/ 1 2))
5.0

write: Write string to the standard output

>>> (write "Hello, world!")
Hello, world!
nil

read: Return string read from standard input

>>> (set name (read))
Joe
nil
>>> (get name)
Joe
>>> (set age (int (read)))
25
nil
>>> (get age)
25

concat: Concatenate strings

>>> (concat "Hello" ", " "world" "!")
Hello, world!

float: Cast value to float number

>>> (float 10)
10.0

int: Cast value to integer number

>>> (int 10.0)
10
>>> (int 10.5)
10

str: Cast value to string

>>> (str 10)
10
>>> (str 10.5)
10.5