Class: Lm::Variable

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

Constant Summary collapse

LETTERS =
%(ABCDEFGHJKLMNPQRSTUVWXYZ)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, pos) ⇒ Variable

Returns a new instance of Variable.



7
8
9
10
# File 'lib/lm/variable.rb', line 7

def initialize(value, pos)
  @value = value
  @pos = pos
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/lm/variable.rb', line 5

def value
  @value
end

Instance Method Details

#barObject



20
21
22
# File 'lib/lm/variable.rb', line 20

def bar
  @value == "0" ? "\u0305" : ""
end

#invalid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/lm/variable.rb', line 12

def invalid?
  @value != "1" && @value != "0"
end

#notopObject



16
17
18
# File 'lib/lm/variable.rb', line 16

def notop
  @value == "0" ? "~" : ""
end

#subscriptObject



24
25
26
27
28
29
30
31
32
# File 'lib/lm/variable.rb', line 24

def subscript
  result = ""

  @pos.to_s.split("").each do |digit|
    result += (0x2080 + digit.to_i).chr("UTF-8")
  end

  result
end

#to_abc(letters = LETTERS) ⇒ Object



36
37
38
39
40
41
# File 'lib/lm/variable.rb', line 36

def to_abc(letters = LETTERS)
  return to_x if @pos >= letters.length
  return "" if invalid?

  letters[@pos] + bar
end

#to_s(input = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lm/variable.rb', line 55

def to_s(input = nil)
  input ||= LETTERS
  if input.is_a?(String) && input.end_with?("#")
    to_x(input[0])
  elsif input == :verilog
    to_verilog(x)
  elsif input.is_a?(Hash) && input[:verilog]
    to_verilog(input[:verilog])
  else
    to_abc(input)
  end
end

#to_verilog(letter) ⇒ Object



49
50
51
52
53
# File 'lib/lm/variable.rb', line 49

def to_verilog(letter)
  return nil if invalid?

  "#{notop}#{letter}[#{@pos}]"
end

#to_x(input = "X") ⇒ Object



43
44
45
46
47
# File 'lib/lm/variable.rb', line 43

def to_x(input = "X")
  return "" if invalid?

  input.to_s + bar + subscript
end