Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Diehl committed May 7, 2008
0 parents commit e166c79
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.DS_Store
33 changes: 33 additions & 0 deletions parse_tree.rb
@@ -0,0 +1,33 @@
# make an "e" method to evaluate a parse tree
# make a "q" method to quote a parse tree
# make a "p" method to create a new parse tree

class ParseTree
def initialize(nodes)
@nodes = nodes
end
attr_accessor :nodes
def evaluate(array = nodes)
node_method = array.first.keys.first.to_sym
node_object = if array.first.values.first.is_a?(Array)
evaluate(array.first.values.first)
else
array.first.values.first
end
node_arguments = []
array.rest.each do |element|
node_arguments << if element.is_a?(Array)
evaluate(element)
else
element
end
end
node_object.send( *([node_method] + node_arguments) )
end
end

class Array
def rest
self[1..size]
end
end
70 changes: 70 additions & 0 deletions spec/parse_tree_spec.rb
@@ -0,0 +1,70 @@
require File.join(File.dirname(__FILE__), "..", "parse_tree")

describe ParseTree, "#nodes" do
it "should raise an Exception if not initialized" do
lambda {ParseTree.new}.should raise_error
end

it "should return an arary of nodes when initialized" do
ParseTree.new([{:+ => 1}, 2]).nodes.should == [{:+ => 1}, 2]
end

it "should be able to add nodes" do
parse_tree = ParseTree.new([{:+ => 1}])
parse_tree.nodes << 2
parse_tree.nodes.should == [{:+ => 1}, 2]
end
end

describe ParseTree, "#evaluate, with terminal parameters and objects" do
it "should be able to evaluate an expression for a parse tree with no parameters" do
parse_tree = ParseTree.new( [{:to_s => 1}] )
parse_tree.evaluate.should == "1"
end

it "should be able to evaluate an expression for a parse tree with a parameter" do
parse_tree = ParseTree.new( [{:+ => 1}, 2] )
parse_tree.evaluate.should == 3
end

it "should be able to evaluate an expression for a parse tree with multiple parameters" do
parse_tree = ParseTree.new( [{:new => Array}, 2, 1] )
parse_tree.evaluate.should == [1, 1]
end
end

describe ParseTree, "#evaluate, with function parameters" do
it "should be able to evaluate an expression for a simple parse tree" do
parse_tree = ParseTree.new( [ {:+ => 1}, [{:* => 2}, 2] ] )
parse_tree.evaluate.should == 5
end

it "should be able to evaluate an expression for a complex parse tree" do
parse_tree = ParseTree.new( [ {:+ => 2}, [{:+ => 1}, [{:* => 2}, 2]] ] )
parse_tree.evaluate.should == 7
end
end

describe ParseTree, "#evaluate, with function objects" do
it "should be able to evaluate an expression for a simple parse tree" do
parse_tree = ParseTree.new( [ {:+ => 1}, [{:* => 2}, 2] ] )
parse_tree.evaluate.should == 5
end

it "should be able to evaluate an expression for a complex parse tree" do
parse_tree = ParseTree.new( [ {:+ => 2}, [{:+ => 1}, [{:* => 2}, 2]] ] )
parse_tree.evaluate.should == 7
end
end

describe ParseTree, "#evaluate, for complex mixed parameters" do
it "should be able to evaluate" do
parse_tree = ParseTree.new(
[{:+ => [{:+ => 1}, 2]},
[{:+ => [{:+ => 1}, 2]},
[{:* => [{:+ => 1}, 2]},
[{:+ => 1}, 2]]]]
)
parse_tree.evaluate.should == 15
end
end

0 comments on commit e166c79

Please sign in to comment.