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.



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

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

Instance Attribute Details

#lvObject

Returns the value of attribute lv.



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

def lv
  @lv
end

#operandObject

Returns the value of attribute operand.



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

def operand
  @operand
end

Instance Method Details

#*(value) ⇒ Object



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

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#-@Object



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

def -@
  @operand = -@operand
  self
end

#evaluateObject



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

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

#to_sObject



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

def to_s
  case @operand
  when -1
    "-#{@lv}"
  when 1
    "#{@lv}"
  else
  "#{@operand} #{@lv}"
  end
end

#variableObject



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

def variable
  @lv
end