Class: Fabulator::Expr::BinExpr

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator/expr/bin_expr.rb

Direct Known Subclasses

AddExpr, BoolBinExpr, DivExpr, ModExpr, MpyExpr, RangeExpr, SubExpr

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ BinExpr

Returns a new instance of BinExpr.



4
5
6
7
# File 'lib/fabulator/expr/bin_expr.rb', line 4

def initialize(left, right)
  @left = left
  @right = right
end

Instance Method Details

#expr_type(context) ⇒ Object



9
10
11
12
13
# File 'lib/fabulator/expr/bin_expr.rb', line 9

def expr_type(context)
  lt = @left.expr_type(context)
  rt = @right.expr_type(context)
  Fabulator::TagLib.unify_types([ lt, rt ])
end

#result_type(t) ⇒ Object



42
43
44
# File 'lib/fabulator/expr/bin_expr.rb', line 42

def result_type(t)
  t
end

#run(context, autovivify = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fabulator/expr/bin_expr.rb', line 15

def run(context, autovivify = false)
  l = @left.run(context, autovivify)
  r = @right.run(context, autovivify)

  l = [ l ] unless l.is_a?(Array)
  r = [ r ] unless r.is_a?(Array)

  res = []

  l.each do |i|
    r.each do |j|
      ut = Fabulator::TagLib.unify_types([ i.vtype, j.vtype ])
      op = TagLib.find_op(ut, self.op)
      if(op && op[:proc])
        calc = op[:proc].call(i.to(ut), j.to(ut))
      else
        calc = self.calculate(i.to(ut).value,j.to(ut).value)
      end
      calc = [ calc ] unless calc.is_a?(Array)

      rut = self.result_type(ut)
      res = res + calc.collect { |c| c.is_a?(Fabulator::Expr::Node) ? c : context.root.anon_node(c, rut) }
    end
  end
  return res
end