Module: Extract::Tree::Math

Included in:
ParenMath
Defined in:
lib/extract/tree/math.rb

Instance Method Summary collapse

Instance Method Details

#deps(start = self) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/extract/tree/math.rb', line 39

def deps(start=self)
  res = []
  #res << get_math_exp.deps

  return [] unless start.elements

  start.elements.each do |e|
    #arg = e.elements[1]
    if e.respond_to?(:deps)
      res << e.deps 
    else
      res << deps(e)
    end
  end

  res.flatten.select { |x| x }

end

#evalObject



91
92
93
94
95
# File 'lib/extract/tree/math.rb', line 91

def eval
  #puts "evaling #{text_value}"
  #raise tokens.map { |x| x.text_value }.inspect + "\n" + inspect
  MathCalc.parse_eval(tokens)
end

#excel_valueObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/extract/tree/math.rb', line 17

def excel_value
  return eval
  raise 'foo'
  unless get_math_exp
    str = %w(math_exp naked_exp cell).map { |x| "#{x} #{respond_to?(x)}" }.join(", ")
    #raise str + "\n" + inspect
    #raise (methods - 7.methods).inspect + "\n" + inspect
  end


  res = 0
  #res = math_exp.excel_value if respond_to?(:math_exp)

  rest.elements.each do |e|
    #arg = e.elements[1]
    #res += arg.excel_value
    #res += 
  end

  res
end

#get_math_expObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/extract/tree/math.rb', line 4

def get_math_exp
  if respond_to?(:math_exp)
    math_exp
  elsif respond_to?(:math_exp_full)
    math_exp_full
  elsif respond_to?(:num)
    num
  elsif respond_to?(:primary)
    primary
  else
    nil
  end
end

#tokens(start = self) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/extract/tree/math.rb', line 58

def tokens(start=self)
  if start.respond_to?("paren?")
    res = start.math_exp.eval
    return [OpenStruct.new(:text_value => res.to_s)]
    #return [start.exp.eval]
  end
  #puts "parsing #{start.text_value} #{start.class}"
  res = []

  return res unless start.elements
  start.elements.each do |el|
    if el.respond_to?(:tt)
      t = el.tt
      if t == :num
        res << el
      elsif t == :operator
        res << el
      elsif t == :cell
        res << el
      elsif t == :formula
        res << el
      elsif t == :math
        res += el.tokens
      else
        raise "unknown"
      end
    else
      res += tokens(el)
    end
  end
  res
end