-
Notifications
You must be signed in to change notification settings - Fork 5
/
Integer.pir
223 lines (155 loc) · 2.9 KB
/
Integer.pir
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
## $Id$
=head1 TITLE
CardinalInteger - Cardinal integers
=cut
.namespace [ 'CardinalInteger' ]
=head1 SUBROUTINES
=over 4
=item onload
=cut
.sub 'onload' :anon :init :load
.local pmc cardinalmeta, intproto
cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
intproto = cardinalmeta.'new_class'('CardinalInteger', 'parent'=>'Integer CardinalObject')
cardinalmeta.'register'('Float', 'parent'=>'CardinalObject', 'protoobject'=>intproto)
.end
=item ACCEPTS()
=cut
.sub 'ACCEPTS' :method
.param num topic
.return 'infix:=='(topic, self)
.end
=item perl()
Returns a Perl representation of the CardinalInteger.
=cut
.sub 'perl' :method
$S0 = self
.return($S0)
.end
.sub 'integer?' :method
$P0 = get_hll_global['Bool'], 'True'
.return($P0)
.end
=item to_s()
Returns a CardinalString representation of the CardinalInteger.
=cut
.sub 'to_s' :method
$P0 = new 'CardinalString'
$P0 = self
.return($P0)
.end
=item
to_i()
to_int()
floor()
ceil()
round()
truncate()
All return C<self>
=cut
.sub 'to_i' :method
.return(self)
.end
.sub 'to_int' :method
.return(self)
.end
.sub 'floor' :method
.return(self)
.end
.sub 'ceil' :method
.return(self)
.end
.sub 'round' :method
.return(self)
.end
.sub 'truncate' :method
.return(self)
.end
.sub 'numerator' :method
.return(self)
.end
=item
Returns 1
=cut
.sub 'denominator' :method
$P0 = new 'CardinalInteger'
$P0 = 1
.return($P0)
.end
=item gcd(num)
Return the greatest common divisor of C<self> and num
=cut
.sub 'gcd' :method
.param num other
$N0 = self
gcd $I0, $N0, other
.return($I0)
.end
=item downto(n, block)
Runs C<block> for each integer from the current value of the Integer down to n.
=cut
.sub 'downto' :method
.param int n
.param pmc block :named('!BLOCK')
$I1 = self
downto_loop:
$I0 = $I1 < n
if $I0, downto_done
block($I1)
dec $I1
goto downto_loop
downto_done:
.end
=item upto(n, block)
Runs C<block> for each integer from the current value of the Integer up to n.
=cut
.sub 'upto' :method
.param int n
.param pmc block :named('!BLOCK')
$I1 = self
upto_loop:
$I0 = $I1 > n
if $I0, upto_done
block($I1)
inc $I1
goto upto_loop
upto_done:
.end
=item
Runs C<block> for integer from 0 to value of C<self>
=cut
.include "hllmacros.pir"
.sub 'times' :method
.param pmc block
$I0 = 0
$I1 = self
.While($I0 < $I1, {
block($I0)
inc $I0
})
.end
=item succ()
Return C<self> plus 1
=cut
.sub 'succ' :method
$P0 = new 'CardinalInteger'
$P0 = 1
$P1 = 'infix:+'($P0, self)
.return ($P1)
.end
=item next()
Return C<self> plus 1
=cut
.sub 'next' :method
$P0 = new 'CardinalInteger'
$P0 = 1
$P1 = 'infix:+'($P0, self)
.return ($P1)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: