项目作者: spiralman

项目描述 :
A Lisp interpreter implemented in LLVM IR
高级语言: LLVM
项目地址: git://github.com/spiralman/llisp.git
创建时间: 2014-06-08T02:43:54Z
项目社区:https://github.com/spiralman/llisp

开源协议:MIT License

下载


llisp

A Lisp interpreter implemented in LLVM IR.

Inspiration

Peter Norvig’s lis.py both inspired me
to attempt this project and guided my implementation of it. The reader
algorithm in llisp is a simplified implementation of that described
in the
Reader Algorithm
section (2.2) of the Common Lisp HyperSpec.

Building

Run make to build it; run make test to run the test suite. You
will need LLVM installed.

For fun, run make count to count the number of source lines in the
project (excluding tests).

Using

After building it, run ./llisp to get an interactive REPL, or
./llisp somefile.llisp to execute the code in a file.

The language

llisp implements an extremely limited lisp-like language (maybe a
proto-lisp?), the goal of which is to explore an extremely simple
implementation of a lisp in an implementation language (LLVM IR) which
makes any “hidden” complexities painfully obvious.

As such, there isn’t much to it, but you can perform
computations with it.

false | nil | ()

The simplest value is (), which is an empty list, and is often
spelled nil. false is also defined as an alias for nil.

true

The symbol true evaluates to the token true, which, since it is
not nil, is “true” in the boolean sense of the word.

define

You can define a symbol using define, which adds the symbol to the
global namespace:

  1. (define my-truth true)

if

You can conditionally evaluate expressions using if. The first
expression is always evaluated. If it evaluates to non-nil, the second
expression will be evaluated. If it evaluates to nil, the third
expression will be evaluated. The third expressions is optional, in
which case nil will be returned when the condition evaluates to
nil.

  1. (if cond then else)

Example:

  1. llisp> (if true false true)
  2. nil

lambda

You can define an anonymous function using lambda. The first
form should be a list naming parameters, and the second form will be
evaluated when the function is invoked, and its return value will be
the return value of the invocation.

You invoke a function by making it the first argument to a list.

  1. (lambda (params) body)

Example:

  1. llisp> ((lambda (x) (if x false true)) nil)
  2. true

You can name functions using define:

  1. llisp> (define not (lambda (x) (if x false true)))
  2. nil
  3. llisp> (not true)
  4. nil
  5. llisp> (not false)
  6. true

quote

quote is a special form which returns its body without it being
evaluated.

  1. (quote body)

Example:

  1. llisp> (quote foo)
  2. foo

You can save a lot of typing with quote:

  1. llisp> (cons true (cons true (cons true true)))
  2. (true true true true)
  3. llisp> (quote (true true true true))
  4. (true true true true)

cons

cons returns a new list in which the first argument is the first
element of the list, and the second argument is the rest of the
list. For convenience, if the second argument is not a list, cons
will create a list of one element using the second argument.

  1. (cons first rest)

Example:

  1. llisp> (cons true nil)
  2. (true)
  3. llisp> (cons true true)
  4. (true true)
  5. llisp> (cons true (cons nil (cons true nil)))
  6. (true nil true)

first

first returns the first element of a list.

  1. (first a-list)

Example:

  1. llisp> (first (cons true true))
  2. true

rest

rest returns a list containing all the elements of the list except
the first element.

  1. (rest a-list)

Example:

  1. llisp> (rest (cons true true))
  2. (true)
  3. llisp> (rest (cons true nil))
  4. nil

print

print prints its argument to standard out, followed by a newline,
and returns nil.

  1. (print someval)

Example:

  1. llisp> (print (cons true true))
  2. (true true)
  3. nil

eval

You can evaluate data as code using eval. The code is evaluated in
the current execution context, with any previously defined names
being available to it.

  1. (eval code)

Example:

  1. llisp> (define f (lambda (a b) (cons a b)))
  2. nil
  3. llisp> (eval (quote (f true true)))
  4. (true true)