Skip to content

Instantly share code, notes, and snippets.

@p8
Forked from fogus/lithp.rb
Created January 26, 2012 00:34
Show Gist options
  • 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
Fs = {
:label => lambda {|name,value| Fs[name] = lambda { value } },
:car => lambda {|sexp| sexp.first },
:cdr => lambda {|sexp| sexp.slice(1, sexp.size) },
:cons => lambda {|head,tail| [head] + tail },
:atom => lambda {|sexp| !sexp.is_a?(Array) },
:eq => lambda {|a,b| a == b },
:if => lambda {|cnd,thn,els| cnd ? thn : els }
}
def self.parse fn, *sexp
return sexp.flatten(1) if fn == :quote
args = sexp.map { |a| Fs[:atom].call(a) ? a : parse(*a) }
Fs.key?(fn) ? Fs[fn].call(*args) : fn
end
end
describe Lisp do
it "works with nested sexps" do
Lisp.parse(:eq, [:quote, [1, 2, 3]], [:cons, 1, [:quote, [2,3]]]).should == true
Lisp.parse(*Lisp.parse(:quote, [:cons, 1, [:quote, [2,3]]])).should == [1, 2, 3]
end
describe ":quote" do
it "returns the sexp" do
Lisp.parse(:quote, [:eq, 1, 2]).should == [:eq, 1, 2]
end
it "returns the value" do
Lisp.parse(:quote, [1, 2]).should == [1, 2]
end
end
describe ":car" do
it "returns the head of the sexp" do
Lisp.parse(:car, [:quote, [1, 2]]).should == 1
end
end
describe ":cdr" do
it "returns the tail of the sexp" do
Lisp.parse(:cdr, [:quote, [1, 2, 3]]).should == [2, 3]
end
end
describe ":cons" do
it "concats the the first argument to the second" do
Lisp.parse(:cons, 1, [:quote, [2, 3]]).should == [1, 2, 3]
end
end
describe ":if" do
it "returns the third sexp if the first is true" do
Lisp.parse(:if, [:eq, 2, 2], 42, 43).should == 42
end
it "returns the second sexp if the first is false" do
Lisp.parse(:if, [:eq, 1, 2], 42, 43).should == 43
end
end
describe ":atom" do
it "returns true if it's an atom" do
Lisp.parse(:atom, 42).should == true
end
it "returns false if it's not an atom" do
Lisp.parse(:atom, [:quote, [42]]).should == false
end
it "returns false if it's not an atom" do
Lisp.parse(:atom, [:quote, [42, 43]]).should == false
end
end
describe ":eq" do
it "returns true if values are equal" do
Lisp.parse(:eq, 42, 42).should == true
end
it "returns false if values are equal" do
Lisp.parse(:eq, 42, 43).should == false
end
end
describe ":label" do
it "sets the value" do
Lisp.parse(:label, :a, 42)
Lisp.parse(:a).should == 42
end
end
end
@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