Skip to content

Latest commit

 

History

History
144 lines (121 loc) · 2.63 KB

README.org

File metadata and controls

144 lines (121 loc) · 2.63 KB

An interpreter for a custom lisp dialect.

Ideas

The core language should be as simple as possible, complex features should be implemented in terms of the primitives where possible and reasonable. Let can be implemented with lambda, loops can be implemented with recusion, etc.

The language should support an optional static type system.

Differences

This lisp is intended to be similar to Emacs lisp, but functions and values are unified into a single namespace like in scheme.

There is only one equality function, “=”, no eq, eql, equal, etc.

This lisp also has an optional hindley-milner based type system, and built-in sum types.

Limitations

This language is very unstable and many things are not implemented yet, and everything is subject to change.

Working with Carpet Lisp

NOTE: You will need rust’s nightly toolchain installed to build this project.

Building

git clone https://github.com/Jturnerusa/lisp
cd lisp
cargo build

Running

Currently, the core lib is passed as an environment variable.

export CARPET_LISP_PATH="$PWD/lib/lisp"

Sample lisp for testing:

(require list)

(defun (fib int -> int) (n)
  (if (< n 2)
      n
      (+ (fib (- n 1))
         (fib (- n 2))))))

(named-let loop ((n 1))
  (print (fib n))
  (if (< n 10)
      (loop (+ n 1))
      nil))

Run the sample:

cargo run --bin eval sample.lisp

This should result in a fibonaci sequence up to 10 indices:

1
1
2
3
5
8
13
21
34
55

Features

Sum types, structs and generics

; 'a is a type parameter
(deftype tree (branch tree tree) (leaf 'a))

(defun (tree-map (fn 'a -> 'b) (tree 'a) -> (tree 'b)) (fn tree)
  (typecase tree
    ((tree-branch l r)
     (tree-branch (tree-map l) (tree-map r)))
    ((tree-leaf a)
     (tree-leaf (fn a)))))

(defstruct dog
  (name string)
  (age int))

(defun (dog-bark dog -> nil) (dog)
  (print (dog-name dog))
  (print (dog-age dog)))

(dog-bark (make-dog "lily" 4))

List of native functions:

  • +
  • -
  • *
  • /
  • cons
  • car
  • cdr
  • list
  • apply
  • cons?
  • function?
  • symbol?
  • string?
  • int?
  • char?
  • nil?
  • apply
  • lambda
  • defmacro
  • def
  • set!
  • eval-when-compile
  • quote
  • if
  • ===
  • >
  • <
  • assert
  • decl
  • map-create
  • map-insert!
  • map-retrieve
  • map-items
  • module
  • export
  • require
  • defstruct
  • deftype
  • if-let

Roadmap

  • More native Functions (string slicing, utils, etc.)
  • Pattern matching
  • Self hosting
  • Full global type inference