Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-vpi/util.rb,
lib/ruby-vpi/float.rb,
lib/ruby-vpi/verilog_parser.rb

Overview

Library for hardware-related floating point operations. – Copyright 2006 Suraj N. Kurapati See the file named LICENSE for details.

Instance Method Summary collapse

Instance Method Details

#rstrip_from(char) ⇒ Object

Strips off everything after the given character in this string.



13
14
15
# File 'lib/ruby-vpi/util.rb', line 13

def rstrip_from char
  sub(/#{char}[^#{char}]*$/, '')
end

#to_f(aRadix = 10) ⇒ Object

Converts this string into a floating point number using the given radix. The default radix is 10.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby-vpi/float.rb', line 9

def to_f aRadix = 10
  whole, frac = split('.', 2)
  whole = whole.to_i(aRadix).to_f

  if frac
    f = 0.0

    frac.length.times do |i|
      power = i.next
      weight = aRadix ** -power
      digit = frac[i, 1].to_i(aRadix)

      f += digit * weight
    end

    f = -f if self =~ /^-/
    whole += f
  end

  whole
end

#to_ruby_const_nameObject

Converts this string into a valid Ruby constant name.



8
9
10
# File 'lib/ruby-vpi/util.rb', line 8

def to_ruby_const_name
  self[0, 1].upcase << self[1..-1]
end

#verilog_to_rubyObject

Converts this string containing Verilog code into syntactically correct Ruby code.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby-vpi/verilog_parser.rb', line 70

def verilog_to_ruby
  content = self.dup

  # single-line comments
    content.gsub! %r{//(.*)$}, '#\1'

  # multi-line comments
    content.gsub! %r{/\*.*?\*/}m, "\n=begin\n\\0\n=end\n"

  # preprocessor directives
    content.gsub! %r{`include}, '#\0'

    content.gsub! %r{`define\s+(\w+)\s+(.+)} do
      "#{$1.to_ruby_const_name} = #{$2}"
    end

    content.gsub! %r{`+}, ''

  # numbers
    content.gsub! %r{\d*\'([dohb]\w+)}, '0\1'

  # ranges
    content.gsub! %r{(\S)\s*:\s*(\S)}, '\1..\2'

  content
end