μLithp REPL

μLithp - a Lisp in 27 lines of Ruby source

Primordial ooze | A reader | REPL | A compiler | Ruby interop

Extending μLithp with a Read-Eval-Print-Loop (REPL)

My good friend Russ Olsen took some time to play with μLithp and wrote a little REPL. I extended it a bit to load a core library if it exists, as shown below:

REPL

require 'lithp'
require 'reader'

lisp = Lisp.new

if File.exist?("core.ulithp")
  source = File.open("core.ulithp", "r").read
  forms = Marshal.load(source)

  forms.each { |form| puts "#{form}" ; lisp.eval(form) }

  puts "Done loading core library containing #{forms.length} forms."
end

print "ulithp> "
while not $stdin.eof?
  line = readline
  s_expression = Reader.new(line).read
  p lisp.eval(s_expression)
  print "ulithp> "
end

Usage

From your shell of choice, execute the following:

> rvm use ruby-1.9.3
> ruby -I . repl.rb

This will put you in an interactive shell allowing a more traditional Lisp feel:

(car (quote (1 2 3)))
;;=> 1

(cdr (quote (1 2 3)))
;;=> [2, 3]

(label second (quote (lambda (x) (car (cdr x))))) 
(second (quote (1 2 3)))
;;=> 2

Fun!

Date: 2012-12-28 15:42:08 EST

Author: Fogus

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0