Skip to content

Commit

Permalink
Implement not
Browse files Browse the repository at this point in the history
Also fix a silly bunch in Environment: looking up a symbol with a
falsy binding was treated as an error. Whoops!
  • Loading branch information
hrs committed Sep 25, 2015
1 parent 1184b4e commit 9ee5546
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def initialize
end

def [](symbol)
lookup(symbol) || raise("symbol \"#{symbol}\" is unbound")
frame = lookup_frame_with(symbol)
if frame && frame.has_key?(symbol)
frame[symbol]
else
raise("symbol \"#{symbol}\" is unbound")
end
end

def []=(symbol, value)
Expand Down Expand Up @@ -54,11 +59,6 @@ def to_s

attr_accessor :stack

def lookup(symbol)
frame = lookup_frame_with(symbol)
frame && frame[symbol]
end

def lookup_frame_with(symbol)
stack.select { |frame| frame.has_key?(symbol) }.compact.last
end
Expand Down
5 changes: 5 additions & 0 deletions lib/standard-library.blu
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
(cons (f (first seq))
(map f (rest seq)))))

(define (not value)
(if value
false
true))

(define (null? exp)
(== exp (quote ())))

Expand Down
12 changes: 9 additions & 3 deletions spec/standard_library_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

describe "if" do
it "expands into the equivalent cond expression" do
expect_eval(
"(if (== 1 2) 3 4)"
).to eq(4)
expect_eval("(if (== 1 1) 3 4)").to eq(3)
expect_eval("(if (== 1 2) 3 4)").to eq(4)
end

it "doesn't inadvertently execute the alternative branch" do
Expand Down Expand Up @@ -82,6 +81,13 @@
end
end

describe "not" do
it "flips the result of a statement" do
expect_eval("(not false)").to eq(true)
expect_eval("(not true)").to eq(false)
end
end

describe "null?" do
it "returns true when its argument is the empty list" do
expect_eval("(null? (quote ()))").to eq(true)
Expand Down

0 comments on commit 9ee5546

Please sign in to comment.