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.



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

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

#groupedObject

Returns the value of attribute grouped.



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

def grouped
  @grouped
end

#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)


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

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

#contains_expression?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/tp_plus/nodes/expression_node.rb', line 21

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

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



58
59
60
61
62
# File 'lib/tp_plus/nodes/expression_node.rb', line 58

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

  with_parens(string_val(context, options), context, options)
end

#requires_mixed_logic?(context) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# File 'lib/tp_plus/nodes/expression_node.rb', line 12

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
# 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? && options[:opposite]
      "!#{@left_op.eval(context)}#{@op.eval(context,options)}!#{@right_op.eval(context)}"
    else
      "#{@left_op.eval(context)}#{@op.eval(context,options)}#{@right_op.eval(context)}"
    end
  end
end

#with_parens(string, context, options = {}) ⇒ Object



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

def with_parens(string, context, options={})
  return string unless options[:force_parens] || options[:as_condition] && requires_mixed_logic?(context)

  "(#{string})"
end