Class: FExpr::Evaluator::OpNode

Inherits:
Node
  • Object
show all
Defined in:
lib/fexpr/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#to_s, #x

Constructor Details

#initialize(op, a, b) ⇒ OpNode

Returns a new instance of OpNode.



41
42
43
44
45
# File 'lib/fexpr/base.rb', line 41

def initialize(op,a,b)
  @op = op
  @a  = a
  @b  = b
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



40
41
42
# File 'lib/fexpr/base.rb', line 40

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



40
41
42
# File 'lib/fexpr/base.rb', line 40

def b
  @b
end

#opObject (readonly)

Returns the value of attribute op.



40
41
42
# File 'lib/fexpr/base.rb', line 40

def op
  @op
end

Instance Method Details

#evalObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fexpr/base.rb', line 46

def eval
  a = @a.eval
  b = @b.eval
  case @op
  when  '|' then a and a != 0 ? a : b
  when  '&' then (a and a!=0 and b and b!=0) ? a : 0
  when  '=' then a == b
  when  '>' then a > b
  when '>=' then a >= b
  when  '<' then a < b
  when '<=' then a <= b
  when '!=' then a != b
  when  '+' then a + b
  when  '-' then a - b
  when  '*' then a * b
  when  'x' then a * b
  when  '/' then a / b
  when  '%' then a.to_i % b.to_i
  else error 'Invalid operator \'' + @op + '\''
  end
end

#to_string(n) ⇒ Object



67
68
69
70
71
72
# File 'lib/fexpr/base.rb', line 67

def to_string(n)
  str  = x(n) + @op + "\n"
  str += a.to_string(n+1) + "\n"
  str += b.to_string(n+1)
  return str
end