public
Last active — forked from fogus/lithp.rb

  • Download Gist
lithp.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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

Very nice!! Clever parsing kudos!!

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.