Class: TPPlus::Nodes::ExpressionNode

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_plus/nodes/expression_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left_op, op_string, right_op) ⇒ ExpressionNode

Returns a new instance of ExpressionNode.



5
6
7
8
9
# File 'lib/tp_plus/nodes/expression_node.rb', line 5

def initialize(left_op, op_string, right_op)
  @left_op  = left_op
  @op       = OperatorNode.new(op_string)
  @right_op = right_op
end

Instance Attribute Details

#left_opObject (readonly)

Returns the value of attribute left_op.



4
5
6
# File 'lib/tp_plus/nodes/expression_node.rb', line 4

def left_op
  @left_op
end

#opObject (readonly)

Returns the value of attribute op.



4
5
6
# File 'lib/tp_plus/nodes/expression_node.rb', line 4

def op
  @op
end

#right_opObject (readonly)

Returns the value of attribute right_op.



4
5
6
# File 'lib/tp_plus/nodes/expression_node.rb', line 4

def right_op
  @right_op
end

Instance Method Details

#boolean_result?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/tp_plus/nodes/expression_node.rb', line 31

def boolean_result?
  case @op.string
  when "&&","||","!","==","<>",">",">=","<","<="
    true
  else
    false
  end
end

#contains_expression?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/tp_plus/nodes/expression_node.rb', line 26

def contains_expression?
  [@left_op, @right_op].map {|op| op.is_a? ExpressionNode }.any? ||
    [@left_op, @right_op].map { |op| op.is_a? ParenExpressionNode }.any?
end

#eval(context, options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/tp_plus/nodes/expression_node.rb', line 61

def eval(context,options={})
  options[:force_parens] = true if grouped?

  string_val(context, options)
end

#grouped?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/tp_plus/nodes/expression_node.rb', line 11

def grouped?
  return false if @right_op.nil? # this is for NOT (!) operator

  @left_op.is_a?(ParenExpressionNode) || @right_op.is_a?(ParenExpressionNode)
end

#requires_mixed_logic?(context) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/tp_plus/nodes/expression_node.rb', line 17

def requires_mixed_logic?(context)
  contains_expression? ||
    grouped? ||
    [@op, @left_op, @right_op].map { |op|
      next if op.nil?
      op.requires_mixed_logic?(context)
    }.any?
end

#string_val(context, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tp_plus/nodes/expression_node.rb', line 40

def string_val(context, options={})
  if @op.bang?
    # this is for skip conditions, which do not
    # support mixed logic
    if options[:disable_mixed_logic]
      "#{@left_op.eval(context)}=OFF"
    else
      "#{@op.eval(context,options)}#{@left_op.eval(context)}"
    end
  else
    if @op.boolean?
      # if operator is &&, || or !, we flip the operator and the operands
      "#{@left_op.eval(context,options)}#{@op.eval(context, options)}#{@right_op.eval(context, options)}"
    else
      # flip the operator if options[:opposite]
      # flip the operands only if opposite and the operand is an expression
      "#{@left_op.eval(context, opposite: (@left_op.is_a?(ExpressionNode) && options[:opposite]))}#{@op.eval(context, options)}#{@right_op.eval(context, opposite: (@right_op.is_a?(ExpressionNode) && options[:opposite]))}"
    end
  end
end