Home

Awesome

Dumblisp

C

A dumb lisp interpreter, made for practice.

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- END doctoc generated TOC please keep comment here to allow auto update -->

Features

Compiling

# Install dependencies.
sudo apt-get install libreadline-dev libgc-dev # On Linux
brew install bdw-gc # On Mac OSX

# Get the source.
git clone https://github.com/rameshvarun/dumblisp.git
cd dumblisp
make && make test

Built-in Functions

I/O Utilities

(print a b ...)

print can be used to write to stdout. Print takes at least 1 argument.

(print "Hello World" 3)

Function / Macro Creation

(defun name (arg-a arg-b ...) stmnt-a stmnt-b ...)

(defun fib (n)
  (if (or (= n 0) (= n 1))
    1
    (+ (fib (- n 1)) (fib (- n 2)))))

(lambda (arg-a arg-b ...) stmnt-a stmnt-b ...)

(defun create-counter ()
  (print "Creating new counter...")
  (let ((i 0)) ; Start counter at 0
    (lambda ()
      (set i (+ i 1))
      i)))

(defmacro name (arg-a arg-b ...) stmnt-a stmnt-b ...)

Macros essentially act like functions that take in their arguments as literal expressions, returning an expression that is then immediately evaluated.

(defmacro ++ (x)
  (list 'set x (list '+ x 1)))

Variable arguments

For both macros and functions, you can take in variable arguments by simply providing a symbol instead of an argument list.

(defun print-wrapper args
  (print (len args) "arguments supplied.")
  (apply print args))

Scope / Variable Manipulation

(let ((var-a val-a) (var-b val-b) ...) expr-a expr-b ...)

let creates a new scope, binds identifiers to values in that scope (in order), and evaluates expressions. plet binds all of the variables in parallel.

(set symbol value)

Set sets the value of the symbol, obeying lexical scope. If no binding exists in the scope stack, then set automatically creates it in the closes scope.

Control Flow

(if cond true-expr false-expr)

The false-expr is optional.

(while cond stmt-a stmnt-b ...)

A basic while loop.

(case var (value-a stmnts...) (value-b stmnts...))

The case statement itself is implemented as a macro on top of the if builtin.

Other Builtins