exalted_math 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
@@ -1,49 +1,51 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{exalted_math}
8
- s.version = "0.1.3"
7
+ s.name = "exalted_math"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jonathan Stott"]
12
- s.date = %q{2010-06-09}
13
- s.description = %q{Parsing and evaluation of simple maths expressions for Exalted
14
-
15
- This intended to aid in evaluating simple calculations which appear on character sheets, especially for Exalted.}
16
- s.email = %q{jonathan.stott@gmail.com}
12
+ s.date = "2012-05-11"
13
+ s.description = "Parsing and evaluation of simple maths expressions for Exalted\n\nThis intended to aid in evaluating simple calculations which appear on character sheets, especially for Exalted."
14
+ s.email = "jonathan.stott@gmail.com"
17
15
  s.extra_rdoc_files = [
18
16
  "README.rdoc"
19
17
  ]
20
18
  s.files = [
21
- ".gitignore",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "exalted_math.gemspec",
26
- "lib/exalted_math.rb",
27
- "lib/exalted_math/ast.rb",
28
- "lib/exalted_math/math.rb",
29
- "lib/exalted_math/math.treetop",
30
- "spec/ast_spec.rb",
31
- "spec/parser_spec.rb",
32
- "spec/spec_helper.rb"
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "exalted_math.gemspec",
23
+ "lib/exalted_math.rb",
24
+ "lib/exalted_math/math.rb",
25
+ "lib/exalted_math/math.treetop",
26
+ "lib/exalted_math/node.rb",
27
+ "lib/exalted_math/node/add.rb",
28
+ "lib/exalted_math/node/divide.rb",
29
+ "lib/exalted_math/node/list.rb",
30
+ "lib/exalted_math/node/maximum.rb",
31
+ "lib/exalted_math/node/minimum.rb",
32
+ "lib/exalted_math/node/multiply.rb",
33
+ "lib/exalted_math/node/named.rb",
34
+ "lib/exalted_math/node/node.rb",
35
+ "lib/exalted_math/node/number.rb",
36
+ "lib/exalted_math/node/operator.rb",
37
+ "lib/exalted_math/node/subtract.rb",
38
+ "script/benchmark.rb",
39
+ "spec/node_spec.rb",
40
+ "spec/parser_spec.rb",
41
+ "spec/spec_helper.rb"
33
42
  ]
34
- s.homepage = %q{http://github.com/namelessjon/exalted_math}
35
- s.rdoc_options = ["--charset=UTF-8"]
43
+ s.homepage = "http://github.com/namelessjon/exalted_math"
36
44
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.7}
38
- s.summary = %q{Parsing and evaluation of simple maths expressions for Exalted}
39
- s.test_files = [
40
- "spec/ast_spec.rb",
41
- "spec/spec_helper.rb",
42
- "spec/parser_spec.rb"
43
- ]
45
+ s.rubygems_version = "1.8.24"
46
+ s.summary = "Parsing and evaluation of simple maths expressions for Exalted"
44
47
 
45
48
  if s.respond_to? :specification_version then
46
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
49
  s.specification_version = 3
48
50
 
49
51
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -2,9 +2,12 @@
2
2
  # Jonathan D. Stott <jonathan.stott@gmail.com>
3
3
  require 'treetop/runtime'
4
4
  require 'exalted_math/math'
5
- require 'exalted_math/ast'
5
+ require 'exalted_math/node'
6
6
 
7
- module Exalted
7
+ module ExaltedMath
8
+
9
+ class ContextError < StandardError; end
10
+ class ParseFailedError < StandardError; end
8
11
  class MathsParser
9
12
  def ast(text)
10
13
  txt = text.dup
@@ -12,9 +15,9 @@ module Exalted
12
15
  txt.gsub!(/\s+/," ")
13
16
  result = parse(txt)
14
17
  if result
15
- [true, result.ast]
18
+ result.ast
16
19
  else
17
- [false, failure_reason]
20
+ raise ParseFailedError, failure_reason
18
21
  end
19
22
  end
20
23
  end
@@ -1,7 +1,7 @@
1
1
  # Autogenerated from a Treetop grammar. Edits may be lost.
2
2
 
3
3
 
4
- module Exalted
4
+ module ExaltedMath
5
5
  module Maths
6
6
  include Treetop::Runtime
7
7
 
@@ -25,7 +25,7 @@ module Maths
25
25
 
26
26
  module Additive1
27
27
  def ast
28
- Ast.add(multitive.ast, additive.ast )
28
+ Node::Add.new(multitive.ast, additive.ast )
29
29
  end
30
30
  end
31
31
 
@@ -45,7 +45,7 @@ module Maths
45
45
 
46
46
  module Additive3
47
47
  def ast
48
- Ast.sub( multitive.ast, additive.ast )
48
+ Node::Subtract.new( multitive.ast, additive.ast )
49
49
  end
50
50
  end
51
51
 
@@ -156,7 +156,7 @@ module Maths
156
156
 
157
157
  module Multitive1
158
158
  def ast
159
- Ast.mul(primary.ast, multitive.ast )
159
+ Node::Multiply.new(primary.ast, multitive.ast )
160
160
  end
161
161
  end
162
162
 
@@ -176,7 +176,7 @@ module Maths
176
176
 
177
177
  module Multitive3
178
178
  def ast
179
- Ast.div( primary.ast, multitive.ast )
179
+ Node::Divide.new( primary.ast, multitive.ast )
180
180
  end
181
181
  end
182
182
 
@@ -395,7 +395,7 @@ module Maths
395
395
  end
396
396
 
397
397
  def ast
398
- Ast.spec(value)
398
+ Node::Spec.new(value)
399
399
  end
400
400
  end
401
401
 
@@ -532,7 +532,7 @@ module Maths
532
532
  end
533
533
 
534
534
  def ast
535
- Ast.max( count, list.asts )
535
+ Node::Maximum.new(list.asts, count)
536
536
  end
537
537
  end
538
538
 
@@ -677,7 +677,7 @@ module Maths
677
677
  end
678
678
  end
679
679
  def ast
680
- Ast.min( count, list.asts )
680
+ Node::Minimum.new(list.asts, count)
681
681
  end
682
682
  end
683
683
 
@@ -965,7 +965,7 @@ module Maths
965
965
  (negative.elements) ? -1 : 1
966
966
  end
967
967
  def ast
968
- Ast.num(value)
968
+ Node::Number.new(value)
969
969
  end
970
970
  end
971
971
 
@@ -1054,7 +1054,7 @@ module Maths
1054
1054
  statistic.text_value.downcase
1055
1055
  end
1056
1056
  def ast
1057
- Ast.stat(value)
1057
+ Node::Stat.new(value)
1058
1058
  end
1059
1059
  end
1060
1060
 
@@ -1,15 +1,15 @@
1
- module Exalted
1
+ module ExaltedMath
2
2
  grammar Maths
3
3
 
4
4
  rule additive
5
5
  multitive '+' space additive {
6
6
  def ast
7
- Ast.add(multitive.ast, additive.ast )
7
+ Node::Add.new(multitive.ast, additive.ast )
8
8
  end
9
9
  }
10
10
  / multitive '-' space additive {
11
11
  def ast
12
- Ast.sub( multitive.ast, additive.ast )
12
+ Node::Subtract.new( multitive.ast, additive.ast )
13
13
  end
14
14
  }
15
15
  / multitive
@@ -18,13 +18,13 @@ grammar Maths
18
18
  rule multitive
19
19
  primary '*' space multitive {
20
20
  def ast
21
- Ast.mul(primary.ast, multitive.ast )
21
+ Node::Multiply.new(primary.ast, multitive.ast )
22
22
  end
23
23
  }
24
24
  /
25
25
  primary '/' space multitive {
26
26
  def ast
27
- Ast.div( primary.ast, multitive.ast )
27
+ Node::Divide.new( primary.ast, multitive.ast )
28
28
  end
29
29
  }
30
30
  / primary
@@ -46,7 +46,7 @@ grammar Maths
46
46
  end
47
47
 
48
48
  def ast
49
- Ast.spec(value)
49
+ Node::Spec.new(value)
50
50
  end
51
51
  }
52
52
  end
@@ -62,7 +62,7 @@ grammar Maths
62
62
  end
63
63
 
64
64
  def ast
65
- Ast.max( count, list.asts )
65
+ Node::Maximum.new(list.asts, count)
66
66
  end
67
67
  }
68
68
  end
@@ -77,7 +77,7 @@ grammar Maths
77
77
  end
78
78
  end
79
79
  def ast
80
- Ast.min( count, list.asts )
80
+ Node::Minimum.new(list.asts, count)
81
81
  end
82
82
  }
83
83
  end
@@ -114,7 +114,7 @@ grammar Maths
114
114
  (negative.elements) ? -1 : 1
115
115
  end
116
116
  def ast
117
- Ast.num(value)
117
+ Node::Number.new(value)
118
118
  end
119
119
  }
120
120
  end
@@ -125,7 +125,7 @@ grammar Maths
125
125
  statistic.text_value.downcase
126
126
  end
127
127
  def ast
128
- Ast.stat(value)
128
+ Node::Stat.new(value)
129
129
  end
130
130
  }
131
131
  end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/number'
4
+ require 'exalted_math/node/named'
5
+ require 'exalted_math/node/operator'
6
+ require 'exalted_math/node/add'
7
+ require 'exalted_math/node/subtract'
8
+ require 'exalted_math/node/divide'
9
+ require 'exalted_math/node/multiply'
10
+ require 'exalted_math/node/list'
11
+ require 'exalted_math/node/maximum'
12
+ require 'exalted_math/node/minimum'
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/operator'
4
+ module ExaltedMath
5
+ class Node
6
+ class Add < Operator
7
+ def value(context={})
8
+ left.value(context) + right.value(context)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/operator'
4
+ module ExaltedMath
5
+ class Node
6
+ class Divide < Operator
7
+ def value(context={})
8
+ left.value(context) / right.value(context)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ module ExaltedMath
4
+ class Node
5
+ class List < Node
6
+ attr_reader :nodes
7
+
8
+ def initialize(nodes=[],*)
9
+ @nodes = nodes
10
+ end
11
+
12
+ def constant?
13
+ nodes.all? { |n| n.constant? }
14
+ end
15
+
16
+ def values(context={})
17
+ nodes.map { |node| node.value(context) }
18
+ end
19
+
20
+ def value(context={})
21
+ values(context).inject(0) { |total, value| total += value }
22
+ end
23
+
24
+ def simplify
25
+ if constant?
26
+ Number.new(value)
27
+ else
28
+ self
29
+ end
30
+ end
31
+
32
+ def ==(o)
33
+ return false unless self.class === o
34
+ nodes == o.nodes
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/list'
4
+ module ExaltedMath
5
+ class Node
6
+ class Maximum < List
7
+ attr_reader :count
8
+
9
+ def initialize(nodes=[],count)
10
+ super
11
+ @count = count
12
+ end
13
+
14
+ def values(context={})
15
+ nodes.map { |node| node.value(context) }.sort.slice(-count, count)
16
+ end
17
+
18
+ def ==(o)
19
+ return false unless super
20
+ count == o.count
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/list'
4
+ module ExaltedMath
5
+ class Node
6
+ class Minimum < List
7
+ attr_reader :count
8
+
9
+ def initialize(nodes=[],count)
10
+ super
11
+ @count = count
12
+ end
13
+
14
+ def values(context={})
15
+ nodes.map { |node| node.value(context) }.sort.slice(0, count)
16
+ end
17
+
18
+ def ==(o)
19
+ return false unless super
20
+ count == o.count
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ require 'exalted_math/node/operator'
4
+ module ExaltedMath
5
+ class Node
6
+ class Multiply < Operator
7
+ def value(context={})
8
+ left.value(context) * right.value(context)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ require 'exalted_math/node/node'
3
+ module ExaltedMath
4
+ class Node
5
+ class Named < Node
6
+ attr_reader :name
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+
11
+ def constant?
12
+ false
13
+ end
14
+
15
+ def value(context={})
16
+ context.fetch(name) { raise ContextError, name }
17
+ end
18
+
19
+ def ==(o)
20
+ return false unless self.class === o
21
+ self.name == o.name
22
+ end
23
+
24
+ def valid?(stats)
25
+ stats.include?(name)
26
+ end
27
+ end
28
+
29
+ class Stat < Named
30
+ end
31
+
32
+ class Spec < Named
33
+ end
34
+
35
+ end
36
+ end