Skip to content

Instantly share code, notes, and snippets.

@p8
Forked from fogus/lithp.rb
Created January 26, 2012 00:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save p8/1680015 to your computer and use it in GitHub Desktop.
Save p8/1680015 to your computer and use it in GitHub Desktop.
class Lisp
def initialize
@env = {
:label => lambda { |(name,val), _| @env[name] = val },
:quote => lambda { |sexpr, _| sexpr[0] },
:car => lambda { |(list), _| list[0] },
:cdr => lambda { |(list), _| list.drop 1 },
:cons => lambda { |(e,cell), _| [e] + cell },
:eq => lambda { |(l,r), _| l == r },
:if => lambda { |(cond, thn, els), ctx| eval(cond, ctx) ? eval(thn, ctx) : eval(els, ctx) },
:atom => lambda { |(sexpr), _| (sexpr.is_a? Symbol) or (sexpr.is_a? Numeric) }
}
end
def apply fn, args, ctx=@env
return @env[fn].call(args, ctx) if @env[fn].respond_to? :call
self.eval @env[fn][2], Hash[*(@env[fn][1].zip args).flatten]
end
def eval sexpr, ctx=@env
if @env[:atom].call [sexpr], ctx
return ctx[sexpr] if ctx[sexpr]
return sexpr
end
fn = sexpr[0]
args = (sexpr.drop 1)
args = args.map { |a| self.eval(a, ctx) } if not [:quote, :if].member? fn
apply(fn, args, ctx)
end
end
l = Lisp.new
l.eval [:label, :a, 42]
l.eval :a
#=> 42
l.eval [:eq, 42, :a]
#=> true
l.eval [:quote, [1, 2]]
#=> [1, 2]
l.eval [:car, [:quote, [1, 2]]]
#=> 1
l.eval [:cdr, [:quote, [1, 2]]]
#=> [2]
l.eval [:cons, 1, [:quote, [2,3]]]
#=> [1, 2, 3]
l.eval [:if, [:eq, 1, 2], 42, 43]
#=> 43
l.eval [:atom, [:quote, [1,2]]]
#=> false
@kalbasit
Copy link

kalbasit commented Feb 7, 2012

Very nice!! Clever parsing kudos!!

@p8
Copy link
Author

p8 commented Feb 7, 2012

Thanks!

@kalbasit
Copy link

kalbasit commented Feb 7, 2012

@duarnad See how cute Ruby is ? Don't you just love it ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment