Class: I32

Inherits:
Object show all
Defined in:
lib/rlang/lib/type/i32.rb

Constant Summary collapse

DIGITS =
"0123456789ABCDEF"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.itoa(x, base) ⇒ Object

convert an integer to its string representation in a given base



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rlang/lib/type/i32.rb', line 16

def self.itoa(x,base)
  result :String

  raise "itoa base out of range" if base < 2 || base > DIGITS.length
  if x <= 0
    if x == 0
      return DIGITS[0]
    else
      return  "-" + self.itoa(0-x, base)
    end
  end

  result = ""
  while x > 0
    remainder = x % base
    x /= base
    result += DIGITS[remainder]
  end
  result.reverse!
end

.sizeObject



13
# File 'lib/rlang/lib/type/i32.rb', line 13

def self.size; 4; end

Instance Method Details

#chrObject



42
43
44
45
46
47
48
# File 'lib/rlang/lib/type/i32.rb', line 42

def chr
  result :String
  raise "out of char range" if self > 255 || self < 0
  stg = String.new(0,1)
  Memory.store32_8(stg.ptr, self)
  stg
end

#to_sObject



37
38
39
40
# File 'lib/rlang/lib/type/i32.rb', line 37

def to_s
  result :String
  I32.itoa(self, 10)
end