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.



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

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

Instance Attribute Details

#lvObject

Returns the value of attribute lv.



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

def lv
  @lv
end

#operandObject

Returns the value of attribute operand.



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

def operand
  @operand
end

Instance Method Details

#*(value) ⇒ Object



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

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#-@Object



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

def -@
  @operand = -@operand
  self
end

#evaluateObject



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

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

#to_sObject



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

def to_s
  @as_str ||= begin
    case @operand
    when -1 then " - #{@lv}"
    when 1 then " + #{@lv}"
    when ->(op){ op < 0} then " - #{@operand.abs} #{@lv}"
    else " + #{@operand} #{@lv}"
    end
  end
end

#variableObject



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

def variable
  @lv
end