- Blog -

(A cheesy homepage for Justin Collins)
Nicer version of my Scheme to LISP converter
This is really super simple, but it lets me use Dr. Scheme for my AI class, so it does what I need it to do.

#!/usr/bin/env ruby
#
#Scheme2Lisp - Basic conversion between Scheme and LISP syntax
#
#Author: Justin Collins
#
#Usage: ./scheme2lisp.rb <schemefile>
#
#or
#    ./scheme2lisp.rb <schemefile> > <lispfile>
#
#Output: LISP (hopefully) to standard output
#
#Version: 0.0.5

if ARGV[0]
    f = File.open(ARGV[0])
else
    f = $stdin
end

conversion = {
        "[" => "(",
        "]" => ")",
        "else" => "t",
        "#t" => "t",
        "#f" => "nil",
        "list?" => "listp",
        "symbol?" => "symbolp",
        "equal?" => "eql",
        "null?" => "null",
        "even?" => "evenp",
        "odd?" => "oddp",
        "list?" => "listp",
        "ceiling" => "ceil",
        "display" => "print",
        "(newline)" => "",
        "modulo" => "mod",
        "number?" => "atom" 
}

f.readlines.each do |line|
    line = line.gsub(Regexp.union(*conversion.keys)) { |match|
            conversion[match]
    }.gsub(/\(define \(([^ ]*) (.*?)\)/, '(defun \1 (\2)').gsub(/\(define (.*?) (.*?)\)/, '(defconstant \1 \2)')
    puts line if line
end