This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
luamath.pir
486 lines (414 loc) · 9.45 KB
/
luamath.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# Copyright (C) 2005-2009, Parrot Foundation.
# $Id$
=head1 Lua Mathematical Library
=head2 Description
This library is an interface to the standard C math library. It provides all
its functions inside the table C<math>. The library provides the following
functions:
math.abs math.acos math.asin math.atan math.atan2
math.ceil math.cos math.cosh math.deg math.exp
math.floor math.fmod math.frexp math.ldexp math.log
math.log10 math.max math.min math.modf math.pow
math.rad math.random math.randomseed math.sin
math.sinh math.sqrt math.tan math.tanh
plus a variable C<math.pi> and a variable C<math.huge>, with the value
C<HUGE_VAL>. Most of these functions are only interfaces to the corresponding
functions in the C library. All trigonometric functions work in radians. The
functions C<math.deg> and C<math.rad> convert between radians and degrees.
The function C<math.max> returns the maximum value of its numeric arguments.
Similarly, C<math.min> computes the minimum. Both can be used with 1, 2, or
more arguments.
The function C<math.modf> corresponds to the C<modf> C function. It returns
two values: The integral part and the fractional part of its argument.
The function C<math.frexp> also returns 2 values: The normalized fraction
and the exponent of its argument.
The functions C<math.random> and C<math.randomseed> are interfaces to the
simple random generator functions C<rand> and C<srand> that are provided by
ANSI C. (No guarantees can be given for their statistical properties.) When
called without arguments, C<math.random> returns a pseudo-random real number
in the range [0,1]. When called with a number I<n>, C<math.random> returns
a pseudo-random integer in the range I<[1,n]>. When called with two arguments,
I<l> and I<u>, C<math.random> returns a pseudo-random integer in the range
I<[l,u]>. The C<math.randomseed> function sets a "seed" for the pseudo-random
generator: Equal seeds produce equal sequences of numbers.
See "Lua 5.1 Reference Manual", section 5.6 "Mathematical Functions",
L<http://www.lua.org/manual/5.1/manual.html#5.6>.
=cut
.HLL 'lua'
.loadlib 'lua_group'
.namespace [ 'math' ]
.sub 'luaopen_math'
# print "init Lua Math\n"
.local pmc _lua__GLOBAL
_lua__GLOBAL = get_hll_global '_G'
new $P1, 'LuaString'
.local pmc _math
new _math, 'LuaTable'
set $P1, 'math'
_lua__GLOBAL[$P1] = _math
$P2 = split "\n", <<'LIST'
abs
acos
asin
atan
atan2
ceil
cos
deg
exp
floor
fmod
frexp
ldexp
log
log10
max
min
modf
pow
rad
random
randomseed
sin
sqrt
tan
LIST
lua_register($P1, _math, $P2)
# LUA_COMPAT_MOD
set $P1, 'fmod'
$P0 = _math[$P1]
set $P1, 'mod'
_math[$P1] = $P0
new $P0, 'LuaNumber'
set $P0, 3.14159265358979323846
set $P1, 'pi'
_math[$P1] = $P0
new $P0, 'LuaNumber'
set $P0, 1.0
new $P2, 'LuaNumber'
set $P2, 0.0
div $P0, $P2
set $P1, 'huge'
_math[$P1] = $P0
load_bytecode 'Math/Rand.pir'
$P0 = get_hll_namespace ['Math'; 'Rand']
$P1 = get_namespace
$P2 = split ' ', 'rand srand RAND_MAX'
$P0.'export_to'($P1, $P2)
.end
.sub 'abs'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = abs $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'acos'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = acos $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'asin'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = asin $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'atan'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = atan $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'atan2'
.param pmc y :optional
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, y)
$N2 = lua_checknumber(2, x)
$N0 = atan $N1, $N2
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'ceil'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = ceil $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'cos'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = cos $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'deg'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = 3.14159265358979323846 / 180.0
$N0 = $N1 / $N0
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'exp'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = exp $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'floor'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = floor $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'fmod'
.param pmc x :optional
.param pmc y :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N2 = lua_checknumber(1, y)
$N0 = cmod $N1, $N2
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'frexp'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
lua_checknumber(1, x)
res = x.'frexp'()
.return (res :flat)
.end
.sub 'ldexp'
.param pmc x :optional
.param pmc nexp :optional
.param pmc extra :slurpy
.local pmc res
lua_checknumber(1, x)
lua_checknumber(2, nexp)
res = x.'ldexp'(nexp)
.return (res)
.end
.sub 'log'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = ln $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'log10'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = log10 $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'max'
.param pmc argv :slurpy
.local int argc
.local int i
.local pmc dmax
argc = argv
unless argc == 0 goto L1
lua_typerror(1, "no value", 'number')
L1:
$P0 = argv[0]
i = 1
dmax = lua_checknumber(i, $P0)
L2:
if i >= argc goto L3
$P0 = argv[i]
inc i
$P1 = lua_checknumber(i, $P0)
if dmax > $P1 goto L2
dmax = $P1
goto L2
L3:
$P0 = clone dmax
.return ($P0)
.end
.sub 'min'
.param pmc argv :slurpy
.local int argc
.local int i
.local pmc dmin
argc = argv
unless argc == 0 goto L1
lua_typerror(1, "no value", 'number')
L1:
$P0 = argv[0]
i = 1
dmin = lua_checknumber(i, $P0)
L2:
if i >= argc goto L3
$P0 = argv[i]
inc i
$P1 = lua_checknumber(i, $P0)
if dmin < $P1 goto L2
dmin = $P1
goto L2
L3:
$P0 = clone dmin
.return ($P0)
.end
.sub 'modf'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
lua_checknumber(1, x)
res = x.'modf'()
.return (res :flat)
.end
.sub 'pow'
.param pmc x :optional
.param pmc y :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N2 = lua_checknumber(2, y)
$N0 = pow $N1, $N2
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'rad'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = 3.14159265358979323846 / 180.0
$N0 = $N1 * $N0
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'random'
.param pmc arg1 :optional
.param pmc arg2 :optional
.param pmc extra :slurpy
.local pmc res
.local int u
.local int l
$I0 = rand()
$I1 = RAND_MAX()
inc $I1
$N0 = $I0 / $I1
new res, 'LuaNumber'
unless null arg1 goto L1
set res, $N0
goto L2
L1:
unless null arg2 goto L3
u = lua_checknumber(1, arg1)
unless 1 <= u goto L4
mul $N0, u
$I0 = floor $N0
inc $I0
set res, $I0
goto L2
L3:
if extra goto L5
l = lua_checknumber(1, arg1)
u = lua_checknumber(2, arg2)
unless l <= u goto L4
$I0 = u - l
inc $I0
mul $N0, $I0
$I0 = floor $N0
add $I0, l
set res, $I0
goto L2
L4:
lua_argerror(1, "interval is empty")
L5:
lua_error("wrong number of arguments")
L2:
.return (res)
.end
.sub 'randomseed'
.param pmc seed :optional
.param pmc extra :slurpy
$I1 = lua_checknumber(1, seed)
srand($I1)
.end
.sub 'sin'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = sin $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'sqrt'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = sqrt $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
.sub 'tan'
.param pmc x :optional
.param pmc extra :slurpy
.local pmc res
$N1 = lua_checknumber(1, x)
$N0 = tan $N1
new res, 'LuaNumber'
set res, $N0
.return (res)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: