μLithp compiler

μLithp - a Lisp in 27 lines of Ruby source

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

Extending μLithp with a compiler

For the ultimate in ridiculousness, one could compile Lisp s-expressions into μLithp source code fairly easily, as shown below:

Compiler

require 'reader'

puts "Compiling: #{ARGV[0]}"

filename = ARGV[0]
source = File.open(filename, "r").read

reader = Reader.new(source)
forms = []

while((form = reader.read) != :"no more forms")
  forms << form
end

compiled_forms = Marshal.dump(forms)
outfile = filename.gsub /lisp/, 'ulithp'

puts "Writing #{forms.size} forms to #{outfile}"

File.open(outfile, 'w') { |f| f.write(compiled_forms) }

Now, starting with a very basic core library:

(label second (quote (lambda (l) (car (cdr l)))))

(label third (quote (lambda (l) (car (cdr (cdr l))))))

We can build a μLithp compatible core with the following:

> ruby -I . compiler.rb core.lisp

Compiling: core.lisp
Writing 2 forms to core.ulithp

Now that core.ulithp is in place the REPL will find it and load it up:

> ruby -I . repl.rb

[:label, :second, [:quote, [:lambda, [:l], [:car, [:cdr, :l]]]]]
[:label, :third, [:quote, [:lambda, [:l], [:car, [:cdr, [:cdr, :l]]]]]]
Done loading core library containing 2 forms.
ulithp> (third (quote (1 2 3)))
3

Oh my glob!

Date: 2012-12-28 15:41:56 EST

Author: Fogus

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0