Skip to content

Commit

Permalink
Partly done with implementation of LABELS
Browse files Browse the repository at this point in the history
It is not yet adding the new binding to the existing bindings.
  • Loading branch information
verdammelt committed Aug 11, 2012
1 parent 62829de commit 37236a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/yarlisp.rb
Expand Up @@ -34,6 +34,9 @@ def equal(a, b)
false
end
end
def null(val)
(equal val, :NIL)
end
def assoc(x, a)
if (equal (CAR (CAR a)), x)
(CAR a)
Expand All @@ -42,31 +45,27 @@ def assoc(x, a)
end
end
def cond(x, a)
def is_true?(val)
!(val.eql? :NIL)
end
condition = (EVAL (CAR (CAR x)), a)
if (is_true?(condition))
if (!(null condition))
(EVAL (CAR (CDR (CAR x))), a)
else
(cond (CDR x), a)
end
end

def evlis(m, a)
def null(val)
(equal val, :NIL)
end
if (null m)
:NIL
else
(CONS (EVAL (CAR m), a), (evlis (CDR m), a))
end
end

puts "(EVAL #{expr} #{env})"

if (ATOM expr)
(CDR (assoc expr, env))
else
elsif (ATOM (CAR expr))
fn=(CAR expr)
args=(CDR expr)

Expand All @@ -89,6 +88,18 @@ def null(val)
else
(EVAL (CONS (CDR (assoc fn, env)), (evlis args, env)), env)
end
else
if (EQ (CAR (CAR expr)), :LABEL)
(EVAL (CONS (CAR (CDR (CDR (CAR expr)))), (CDR expr)),
env)
end
end
end
end

#(eval ((label, f, E), e1, ... en), a)
#(eval (E e1...en) (append ((f (label f E))) a))
#
#The evaluation of ((LABEL, f, E), e1, · · · , en) is accomplished by eval-
# uating (E, e1, · · · , en) with the pairing (f, (LABEL, f, E)) put on the front of
# the previous list a of pairs.
7 changes: 7 additions & 0 deletions spec/yarlisp_spec.rb
Expand Up @@ -47,6 +47,7 @@

it "handles QUOTE function" do
(EVAL [:QUOTE, [:x, :NIL]], []).should eq :x
(EVAL [:QUOTE, [[:x, :y], :NIL]], []).should eq [:x, :y]
end

it "handles ATOM function" do
Expand Down Expand Up @@ -81,5 +82,11 @@
(EVAL [:x, [:y, [:z, :NIL]]],
[[:x, :CONS], [[:y, [:QUOTE, [:b, :NIL]]], [[:z, [:QUOTE, [:c, :NIL]]], :NIL]]]).should eq [:b, :c]
end

it "handles LABELS function (used to create a binding))" do
(EVAL [[:LABEL, [:x, [:CONS, :NIL]]],
[[:QUOTE, [:a, :NIL]], [[:QUOTE, [:b, :NIL]], :NIL]]],
[]).should eq [:a, :b]
end
end
end

0 comments on commit 37236a2

Please sign in to comment.