-
-
Save floehopper/8548547 to your computer and use it in GitHub Desktop.
Evaluator for Little Schemer chapter 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Atom | |
attr_reader :symbol | |
def initialize(symbol) | |
@symbol = symbol | |
end | |
def ==(other) | |
self.symbol == other.symbol | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Evaluator | |
def initialize(program) | |
@program = program | |
end | |
def evaluate(env={}) | |
function, argument = @program.car, @program.cdr.car | |
operation = function.symbol | |
case argument | |
when Atom | |
env[argument.symbol].send(operation) | |
when List | |
self.class.new(argument).evaluate(env).send(operation) | |
else | |
raise | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require_relative 'atom' | |
require_relative 'list' | |
require_relative 'evaluator' | |
class EvaluatorTest < Minitest::Test | |
def test_car | |
assert_equal Atom.new(:a), evaluate( | |
List.new(Atom.new(:car), Atom.new(:l)), | |
l: List.new(Atom.new(:a), Atom.new(:b), Atom.new(:c)) | |
) | |
end | |
def test_car_car | |
assert_equal Atom.new(:hotdogs), evaluate( | |
List.new(Atom.new(:car), List.new(Atom.new(:car), Atom.new(:l))), | |
l: List.new(List.new(Atom.new(:hotdogs), Atom.new(:and))) | |
) | |
end | |
def test_cdr | |
assert_equal List.new(Atom.new(:b), Atom.new(:c)), evaluate( | |
List.new(Atom.new(:cdr), Atom.new(:l)), | |
l: List.new(Atom.new(:a), Atom.new(:b), Atom.new(:c)) | |
) | |
end | |
private | |
def evaluate(program, env={}) | |
Evaluator.new(program).evaluate(env) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class List | |
def initialize(*array) | |
@array = array | |
end | |
def car | |
@array.first | |
end | |
def cdr | |
self.class.new(*@array[1..-1]) | |
end | |
def ==(other) | |
self.array == other.array | |
end | |
protected | |
attr_reader :array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess what I mean is: the book doesn’t say “
(car (a b c))
isa
” or “(cdr (a b c))
is(b c)
”, but the tests do.