Permalink
Browse files

Refactor properly to using classes for Nodes

* Improve spec coverage, too
  • Loading branch information...
1 parent dbcb6da commit 2cf56dbe869b8b86390d94258d19292e18d2956f @namelessjon committed May 11, 2012
View
@@ -2,19 +2,22 @@
# Jonathan D. Stott <jonathan.stott@gmail.com>
require 'treetop/runtime'
require 'exalted_math/math'
-require 'exalted_math/ast'
+require 'exalted_math/node'
-module Exalted
+module ExaltedMath
+
+ class ContextError < StandardError; end
+ class ParseFailedError < StandardError; end
class MathsParser
def ast(text)
txt = text.dup
txt.strip!
txt.gsub!(/\s+/," ")
result = parse(txt)
if result
- [true, result.ast]
+ result.ast
else
- [false, failure_reason]
+ raise ParseFailedError, failure_reason
end
end
end
View
@@ -1,158 +0,0 @@
-#!/usr/bin/ruby
-# Jonathan D. Stott <jonathan.stott@gmail.com>
-module ExaltedMath
- class Node
- def initialize(*)
- end
-
- def constant?
- true
- end
-
- def value(context={})
- nil
- end
-
- def simplify
- self
- end
- end
-
- class NumberNode < Node
- def initialize(value)
- @value = value
- end
-
- def constant?
- true
- end
-
- def value(context={})
- @value
- end
- end
-
- class NamedNode < Node
- attr_reader :name
- def initialize(name)
- @name = name
- end
-
- def constant?
- false
- end
-
- def value(context={})
- context.fetch(name) { raise ContextError, name }
- end
- end
-
- class StatNode < NamedNode
- end
-
- class SpecNode < NamedNode
- end
-
- class OperatorNode < Node
- attr_reader :left, :right
-
- def initialize(left, right)
- @left = left
- @right = right
- end
-
- def constant?
- left.constant? and right.constant?
- end
-
- def value(context={})
- raise NotImplementedError
- end
-
- def simplify
- if constant?
- NumberNode.new(value)
- else
- self.class.new(left.simplify, right.simplify)
- end
- end
- end
-
- class AddNode < OperatorNode
- def value(context={})
- left.value(context) + right.value(context)
- end
- end
-
- class SubtractNode < OperatorNode
- def value(context={})
- left.value(context) - right.value(context)
- end
- end
-
- class MultiplyNode < OperatorNode
- def value(context={})
- left.value(context) * right.value(context)
- end
- end
-
- class DivideNode < OperatorNode
- def value(context={})
- left.value(context) / right.value(context)
- end
- end
-
- class ListNode < Node
- attr_reader :nodes
-
- def initialize(nodes=[],*)
- @nodes = nodes
- end
-
- def constant?
- nodes.all? { |n| n.constant? }
- end
-
- def values(context={})
- nodes.map { |node| node.value(context) }
- end
-
- def value(context={})
- values(context).inject(0) { |total, value| total += value }
- end
-
- def simplify
- if constant?
- NumberNode.new(value)
- else
- self
- end
- end
- end
-
- class MaximumNode < ListNode
- attr_reader :count
-
- def initialize(nodes=[],count)
- super
- @count = count
- end
-
- def values(context={})
- nodes.map { |node| node.value(context) }.sort.slice(-count, count)
- end
- end
-
- class MinimumNode < ListNode
- attr_reader :count
-
- def initialize(nodes=[],count)
- super
- @count = count
- end
-
- def values(context={})
- nodes.map { |node| node.value(context) }.sort.slice(0, count)
- end
- end
-end
View
@@ -25,7 +25,7 @@ def additive
module Additive1
def ast
- AddNode.new(multitive.ast, additive.ast )
+ Node::Add.new(multitive.ast, additive.ast )
end
end
@@ -45,7 +45,7 @@ def additive
module Additive3
def ast
- SubtractNode.new( multitive.ast, additive.ast )
+ Node::Subtract.new( multitive.ast, additive.ast )
end
end
@@ -156,7 +156,7 @@ def multitive
module Multitive1
def ast
- MultiplyNode.new(primary.ast, multitive.ast )
+ Node::Multiply.new(primary.ast, multitive.ast )
end
end
@@ -176,7 +176,7 @@ def multitive
module Multitive3
def ast
- MultiplyNode.new( primary.ast, multitive.ast )
+ Node::Divide.new( primary.ast, multitive.ast )
end
end
@@ -395,7 +395,7 @@ def value
end
def ast
- SpecNode.new(value)
+ Node::Spec.new(value)
end
end
@@ -532,7 +532,7 @@ def count
end
def ast
- MaximumNode.new(list.asts, count)
+ Node::Maximum.new(list.asts, count)
end
end
@@ -677,7 +677,7 @@ def count
end
end
def ast
- MinimumNode.new(list.asts, count)
+ Node::Minimum.new(list.asts, count)
end
end
@@ -965,7 +965,7 @@ def neg
(negative.elements) ? -1 : 1
end
def ast
- NumberNode.new(value)
+ Node::Number.new(value)
end
end
@@ -1054,7 +1054,7 @@ def value
statistic.text_value.downcase
end
def ast
- StatNode.new(value)
+ Node::Stat.new(value)
end
end
@@ -4,12 +4,12 @@ grammar Maths
rule additive
multitive '+' space additive {
def ast
- AddNode.new(multitive.ast, additive.ast )
+ Node::Add.new(multitive.ast, additive.ast )
end
}
/ multitive '-' space additive {
def ast
- SubtractNode.new( multitive.ast, additive.ast )
+ Node::Subtract.new( multitive.ast, additive.ast )
end
}
/ multitive
@@ -18,13 +18,13 @@ grammar Maths
rule multitive
primary '*' space multitive {
def ast
- MultiplyNode.new(primary.ast, multitive.ast )
+ Node::Multiply.new(primary.ast, multitive.ast )
end
}
/
primary '/' space multitive {
def ast
- MultiplyNode.new( primary.ast, multitive.ast )
+ Node::Divide.new( primary.ast, multitive.ast )
end
}
/ primary
@@ -46,7 +46,7 @@ grammar Maths
end
def ast
- SpecNode.new(value)
+ Node::Spec.new(value)
end
}
end
@@ -62,7 +62,7 @@ grammar Maths
end
def ast
- MaximumNode.new(list.asts, count)
+ Node::Maximum.new(list.asts, count)
end
}
end
@@ -77,7 +77,7 @@ grammar Maths
end
end
def ast
- MinimumNode.new(list.asts, count)
+ Node::Minimum.new(list.asts, count)
end
}
end
@@ -114,7 +114,7 @@ grammar Maths
(negative.elements) ? -1 : 1
end
def ast
- NumberNode.new(value)
+ Node::Number.new(value)
end
}
end
@@ -125,7 +125,7 @@ grammar Maths
statistic.text_value.downcase
end
def ast
- StatNode.new(value)
+ Node::Stat.new(value)
end
}
end
View
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+require 'exalted_math/node/node'
+require 'exalted_math/node/number'
+require 'exalted_math/node/named'
+require 'exalted_math/node/operator'
+require 'exalted_math/node/add'
+require 'exalted_math/node/subtract'
+require 'exalted_math/node/divide'
+require 'exalted_math/node/multiply'
+require 'exalted_math/node/list'
+require 'exalted_math/node/maximum'
+require 'exalted_math/node/minimum'
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+require 'exalted_math/node/node'
+require 'exalted_math/node/operator'
+module ExaltedMath
+ class Node
+ class Add < Operator
+ def value(context={})
+ left.value(context) + right.value(context)
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+require 'exalted_math/node/node'
+require 'exalted_math/node/operator'
+module ExaltedMath
+ class Node
+ class Divide < Operator
+ def value(context={})
+ left.value(context) / right.value(context)
+ end
+ end
+ end
+end
+
Oops, something went wrong.

0 comments on commit 2cf56db

Please sign in to comment.