Skip to content

Commit

Permalink
Added some helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
namelessjon committed Jun 6, 2010
1 parent e6d908b commit 72f361a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
81 changes: 81 additions & 0 deletions lib/exalted_math/ast.rb
Expand Up @@ -129,6 +129,87 @@ def self.simplify(ast)
end
end

# Create a new num node
#
# @param value [Integer] The value of the new node
#
# @return [Ast] A num Ast Node
def self.num(value)
new('num', value)
end

# Create a new stat node
#
# @param value [String] The value of the new node
#
# @return [Ast] A stat Ast Node
def self.stat(value)
new('stat', value)
end

# Create a new spec node
#
# @param value [String] The value of the new node
#
# @return [Ast] A spec Ast Node
def self.spec(value)
new('spec', value)
end

# Create a new add node
#
# @param left [Ast] The left node of the add
# @param right [Ast] The right node of the add
# @return [Ast] An add Ast Node
def self.add(left, right)
new('add', left, right)
end

# Create a new sub node
#
# @param left [Ast] The left node of the sub
# @param right [Ast] The right node of the sub
# @return [Ast] An sub Ast Node
def self.sub(left, right)
new('sub', left, right)
end

# Create a new mul node
#
# @param left [Ast] The left node of the mul
# @param right [Ast] The right node of the mul
# @return [Ast] An mul Ast Node
def self.mul(left, right)
new('mul', left, right)
end

# Create a new div node
#
# @param left [Ast] The left node of the div
# @param right [Ast] The right node of the div
# @return [Ast] An div Ast Node
def self.div(left, right)
new('div', left, right)
end

# Create a new min node
#
# @param count [Integer] Number of values to sum for the min
# @param list [Array] Array of ASTs to calculate the min from
# @return [Ast] A min Ast Node
def self.min(count, list)
new('min', count, list)
end

# Create a new max node
#
# @param count [Integer] Number of values to sum for the max
# @param list [Array] Array of ASTs to calculate the max from
# @return [Ast] A max Ast Node
def self.max(count, list)
new('max', count, list)
end

def self.from_array(array)
case array[0]
when 'mul', 'div', 'add', 'sub'
Expand Down
33 changes: 32 additions & 1 deletion spec/ast_spec.rb
Expand Up @@ -3,12 +3,15 @@
require 'spec_helper'
require 'exalted_math/ast'

include Exalted


describe "Exalted::Ast" do
before do
@three = Exalted::Ast.new('num', 3 )
@seven = Exalted::Ast.new('num', 7 )
@foo = Exalted::Ast.new('stat', 'foo')
@bar = Exalted::Ast.new('stat', 'bar')
@bar = Exalted::Ast.new('spec', 'bar')
@invalid = Exalted::Ast.new('stat', 'invalid')
@add = Exalted::Ast.new('add', @three, @seven)
@sub = Exalted::Ast.new('sub', @three, @seven)
Expand All @@ -22,6 +25,34 @@
@context = { 'foo' => 3, 'bar' => 4 }
end

it ".num makes a number" do
@three.should == Ast.num(3)
end

it ".stat makes a stat" do
@foo.should == Ast.stat('foo')
end

it ".spec makes a spec" do
@bar.should == Ast.spec('bar')
end

it ".add makes an add" do
@add.should == Ast.add(@three, @seven)
end

it ".sub makes an sub" do
@sub.should == Ast.sub(@three, @seven)
end

it ".mul makes an mul" do
@mul.should == Ast.mul(@three, @seven)
end

it ".div makes an div" do
@div.should == Ast.div(@seven, @three)
end

it "a number is constant" do
@three.should.be.constant
end
Expand Down

0 comments on commit 72f361a

Please sign in to comment.