Class: Rips::Variables::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/rips/variables/variable.rb

Direct Known Subclasses

Address, Inmediate, Port, Register

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length) ⇒ Variable

Returns a new instance of Variable.



9
10
11
# File 'lib/rips/variables/variable.rb', line 9

def initialize (length)
  @length = length
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/rips/variables/variable.rb', line 6

def length
  @length
end

Instance Method Details

#between?(value, range) ⇒ Boolean

Check if value is between in permitted range

Returns:

  • (Boolean)


29
30
31
# File 'lib/rips/variables/variable.rb', line 29

def between? (value, range)
  to_i(value).between?(range[0], range[1])
end

#error(value) ⇒ Object

Return error message about incorrent syntax



39
40
41
# File 'lib/rips/variables/variable.rb', line 39

def error (value)
  "unexpected `#{value}` (expected a `#{self.class.to_s.split(':').last}` argument like `#{@syntax}`)"
end

#number?(value) ⇒ Boolean

Check if value is a valid number (…,-1,0,1…)

Returns:

  • (Boolean)


24
25
26
# File 'lib/rips/variables/variable.rb', line 24

def number? (value)
  /\A[-]?\d+\z/ === value
end

#port?(value) ⇒ Boolean

Check if value is a port (@0..3)

Returns:

  • (Boolean)


19
20
21
# File 'lib/rips/variables/variable.rb', line 19

def port? (value)
  (value[0] == "@") && number?(value.slice(1..-1))
end

#register?(value) ⇒ Boolean

Check if value is a register ($0..15)

Returns:

  • (Boolean)


14
15
16
# File 'lib/rips/variables/variable.rb', line 14

def register? (value)
  (value[0] == "$") && number?(value.slice(1..-1))
end

#to_i(value) ⇒ Object

Return integer part of value



34
35
36
# File 'lib/rips/variables/variable.rb', line 34

def to_i (value)
  number?(value) ? value.to_i : value.slice(1..-1).to_i
end