Class: Fragment

Inherits:
Object show all
Defined in:
lib/rulp/expression.rb

Overview

An expression fragment. An expression can consist of many fragments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lv, operand) ⇒ Fragment

Returns a new instance of Fragment.



64
65
66
67
# File 'lib/rulp/expression.rb', line 64

def initialize(lv, operand)
  @lv = lv
  @operand = operand
end

Instance Attribute Details

#lvObject

Returns the value of attribute lv.



62
63
64
# File 'lib/rulp/expression.rb', line 62

def lv
  @lv
end

#operandObject

Returns the value of attribute operand.



62
63
64
# File 'lib/rulp/expression.rb', line 62

def operand
  @operand
end

Instance Method Details

#*(value) ⇒ Object



77
78
79
# File 'lib/rulp/expression.rb', line 77

def *(value)
  Fragment.new(@lv, @operand * value)
end

#+(other) ⇒ Object



69
70
71
# File 'lib/rulp/expression.rb', line 69

def +(other)
  return Expressions.new([self] + Expressions[other].expressions)
end

#-(other) ⇒ Object



73
74
75
# File 'lib/rulp/expression.rb', line 73

def -(other)
  self.+(-other)
end

#-@Object



89
90
91
92
# File 'lib/rulp/expression.rb', line 89

def -@
  @operand = -@operand
  self
end

#evaluateObject



81
82
83
84
85
86
87
# File 'lib/rulp/expression.rb', line 81

def evaluate
  if [TrueClass,FalseClass].include? @lv.value.class
    @operand * (@lv.value ? 1 : 0)
  else
    @operand * @lv.value
  end
end

#to_sObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rulp/expression.rb', line 104

def to_s
  @as_str ||= begin
    case @operand
    when -1
      " - #{@lv}"
    when 1
      " + #{@lv}"
    else
    " + #{@operand} #{@lv}"
    end
  end
end

#variableObject



94
95
96
# File 'lib/rulp/expression.rb', line 94

def variable
  @lv
end