-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixnum.rb
More file actions
194 lines (163 loc) · 3.54 KB
/
fixnum.rb
File metadata and controls
194 lines (163 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class Fixnum < Integer
def initialize
# Can't use any Ruby expressions that use integers here,
# directly or indirectly, so best not use *any*, because
# it would cause recursion until running out of memory.
%s(assign @value 0)
end
def self.allocate
%s(assign ob (__array_leaf 2))
%s(assign (index ob 0) self)
ob
end
def % other
%s(assign r (callm other __get_raw))
%s(assign m (mod @value r))
%s(if (eq (ge m 0) (lt r 0))
(assign m (add m r)))
%s(__int m)
end
def __set_raw(value)
@value = value
end
def __get_raw
@value
end
def to_i
self
end
# FIXME
# Bit access
def [] i
1
end
def to_s(radix=10)
if radix < 2 || radix > 36
STDERR.puts("ERROR: Invalid radix #{radix.inspect} - must be between 2 and 36")
1/0
else
out = ""
n = self
neg = self < 0
if neg
n = 0 - n
end
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
while n != 0
r = n % radix
out << digits[r]
break if n < radix
n = n / radix
end
if out.empty?
out = "0"
elsif neg
out << "-"
end
out.reverse
end
end
def hash
self
end
def inspect
to_s
end
def chr
%s(let (buf)
(assign buf (__alloc_leaf 2))
(snprintf buf 2 "%c" @value)
(__get_string buf)
)
end
def + other
%s(call __int ((add @value (callm other __get_raw))))
end
def - other
%s(call __int ((sub @value (callm other __get_raw))))
end
def <= other
%s(if (le @value (callm other __get_raw)) true false)
end
def == other
if other.nil?
return false
end
return false if !other.is_a?(Numeric)
%s(if (eq @value (callm other __get_raw)) true false)
end
# FIXME: I don't know why '!' seems to get an argument...
def ! *args
false
end
def != other
return true if !other.is_a?(Numeric)
other = other.to_i
%s(if (ne @value (callm other __get_raw)) true false)
end
def < other
%s(if (lt @value (callm other __get_raw)) true false)
end
def > other
%s(if (gt @value (callm other __get_raw)) true false)
end
def >= other
%s(if (ge @value (callm other __get_raw)) true false)
end
def <=> other
return nil if !other.is_a?(Numeric)
if self > other
return 1
end
if self < other
return -1
end
return 0
end
def div other
%s(call __int ((div @value (callm other __get_raw))))
end
def mul other
%s(call __int ((mul @value (callm other __get_raw))))
end
# These two definitions are only acceptable temporarily,
# because we will for now only deal with integers
def * other
mul(other)
end
def / other
div(other)
end
def ord
self
end
def times
i = 0
while i < self
yield
i +=1
end
end
end
%s(assign FixNum_cache_size 1000)
%s(assign FixNum_cache (__array_leaf (mul FixNum_cache_size 2)))
%s(defun __int (val)
(let (num)
(if (and (ge val 0) (lt val FixNum_cache_size))
(do
# 32 bit class-ptr + 32 bit int; Naughty assumptions again. FIXME
(assign num (add FixNum_cache (mul val 8)))
(if (eq (index num 0) 0) (do
(assign (index num 0) Fixnum) # class-ptr
(callm num __set_raw (val))
(return num)
))
(return num)
)
)
(assign num (callm Fixnum allocate))
(callm num __set_raw (val))
(return num)
)
)
%s(__compiler_internal integer_list)