This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
wmlslang.pir
669 lines (476 loc) · 12.7 KB
/
wmlslang.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
=head1 WMLScript Lang library
=head2 Description
This library contains a set of functions that are closely related to the
WMLScript language core.
See "WMLScript Standard Libraries Specification", section 7 "Lang".
=head2 Functions
=cut
.HLL 'wmlscript'
.loadlib 'wmls_group'
.loadlib 'wmls_ops'
.sub '__onload' :load :init
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 'getLang'
new $P0, 'Hash'
.const 'Sub' _lang_abs = '_lang_abs'
$P0[0] = _lang_abs
.const 'Sub' _lang_min = '_lang_min'
$P0[1] = _lang_min
.const 'Sub' _lang_max = '_lang_max'
$P0[2] = _lang_max
.const 'Sub' _lang_parseInt = '_lang_parseInt'
$P0[3] = _lang_parseInt
.const 'Sub' _lang_parseFloat = '_lang_parseFloat'
$P0[4] = _lang_parseFloat
.const 'Sub' _lang_isInt = '_lang_isInt'
$P0[5] = _lang_isInt
.const 'Sub' _lang_isFloat = '_lang_isFloat'
$P0[6] = _lang_isFloat
.const 'Sub' _lang_maxInt = '_lang_maxInt'
$P0[7] = _lang_maxInt
.const 'Sub' _lang_minInt = '_lang_minInt'
$P0[8] = _lang_minInt
.const 'Sub' _lang_float = '_lang_float'
$P0[9] = _lang_float
.const 'Sub' _lang_exit = '_lang_exit'
$P0[10] = _lang_exit
.const 'Sub' _lang_abort = '_lang_abort'
$P0[11] = _lang_abort
.const 'Sub' _lang_random = '_lang_random'
$P0[12] = _lang_random
.const 'Sub' _lang_seed = '_lang_seed'
$P0[13] = _lang_seed
.const 'Sub' _lang_characterSet = '_lang_characterSet'
$P0[14] = _lang_characterSet
.return ($P0)
.end
=head3 C<abs(value)>
=head4 DESCRIPTION
Returns the absolute value of the given number. If the given number is of
type integer then an integer value is returned. If the given number is of type
floating-point then a floating-point value is returned.
=head4 PARAMETERS
value = Number
=head4 RETURN VALUE
Number or invalid.
=cut
.sub '_lang_abs' :anon
.param pmc value
.local pmc res
res = clone value
res = abs value
.return (res)
.end
=head3 C<min(value1, value2)>
=head4 DESCRIPTION
Returns the minimum value of the given two numbers. The value and type
returned is the same as the value and type of the selected number. The
selection is done in the following way:
=over 4
=item *
WMLScript operator data type conversion rules for I<integers and
floating-points> (see [WMLScript]) must be used to specify the data
type (integer or floating-point ) for comparison.
=item *
Compare the numbers to select the smaller one.
=item *
If the values are equal then the first value is selected.
=back
=head4 PARAMETERS
value1 = Number
value2 = Number
=head4 RETURN VALUE
Number or invalid.
=cut
.sub '_lang_min' :anon
.param pmc value1
.param pmc value2
.local pmc res
$P1 = value1
$I0 = isa value1, 'WmlsString'
unless $I0 goto L1
$P1 = value1.'parseNumber'()
L1:
$P2 = value2
$I0 = isa value2, 'WmlsString'
unless $I0 goto L2
$P2 = value2.'parseNumber'()
L2:
$P0 = isle $P1, $P2
$I0 = isa $P0, 'WmlsInvalid'
unless $I0 goto L3
.return ($P0)
L3:
$I0 = $P0
unless $I0 goto L4
res = clone value1
goto L5
L4:
res = clone value2
L5:
.return (res)
.end
=head3 C<max(value1, value2)>
=head4 DESCRIPTION
Returns the maximum value of the given two numbers. The value and type
returned is the same as the value and type of the selected number. The
selection is done in the following way:
=over 4
=item *
WMLScript operator data type conversion rules for I<integers and
floating-points> (see [WMLScript]) must be used to specify the data
type (integer or floating-point ) for comparison.
=item *
Compare the numbers to select the larger one.
=item *
If the values are equal then the first value is selected.
=back
=head4 PARAMETERS
value1 = Number
value2 = Number
=head4 RETURN VALUE
Number or invalid.
=cut
.sub '_lang_max' :anon
.param pmc value1
.param pmc value2
.local pmc res
$P1 = value1
$I0 = isa value1, 'WmlsString'
unless $I0 goto L1
$P1 = value1.'parseNumber'()
L1:
$P2 = value2
$I0 = isa value2, 'WmlsString'
unless $I0 goto L2
$P2 = value2.'parseNumber'()
L2:
$P0 = isge $P1, $P2
$I0 = isa $P0, 'WmlsInvalid'
unless $I0 goto L3
.return ($P0)
L3:
$I0 = $P0
unless $I0 goto L4
res = clone value1
goto L5
L4:
res = clone value2
L5:
.return (res)
.end
=head3 C<parseInt(value)>
=head4 DESCRIPTION
Returns an integer value defined by the string value. The legal integer
syntax is specified by the WMLScript (see [WMLScript]) numeric string
grammar for I<decimal integer literals> with the following additional parsing
rule:
=over 4
=item *
Parsing ends when the first character is encountered that is not a
leading '+' or '-' or a decimal digit.
=back
The result is the parsed string converted to an integer value.
=head4 PARAMETERS
value = String
=head4 RETURN VALUE
Integer or invalid.
=head4 EXCEPTIONS
In case of a parsing error an C<invalid> value is returned.
=cut
.sub '_lang_parseInt' :anon
.param pmc value
.local pmc res
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
res = value.'parseInt'()
.return (res)
L1:
res = value
$I0 = isa value, 'WmlsInteger'
if $I0 goto L2
new res, 'WmlsInvalid'
L2:
.return (res)
.end
=head3 C<parseFloat(value)>
=head4 DESCRIPTION
Returns a floating-point value defined by the string value. The legal floatingpoint
syntax is specified by the WMLScript (see [WMLScript]) numeric
string grammar for I<decimal floating-point literals> with the following additional
parsing rule:
=over 4
=item *
Parsing ends when the first character is encountered that cannot
be parsed as being part of the floating-point representation.
=back
The result is the parsed string converted to a floating-point value.
=head4 PARAMETERS
value = String
=head4 RETURN VALUE
Floating-point or invalid.
=head4 EXCEPTIONS
In case of a parsing error an C<invalid> value is returned.
=cut
.sub '_lang_parseFloat' :anon
.param pmc value
.local pmc res
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
res = value.'parseFloat'()
.return (res)
L1:
res = value
$I0 = isa value, 'WmlsFloat'
if $I0 goto L2
$I0 = isa value, 'WmlsInteger'
unless $I0 goto L3
$I0 = value
new res, 'WmlsFloat'
set res, $I0
goto L2
L3:
new res, 'WmlsInvalid'
L2:
.return (res)
.end
=head3 C<isInt(value)>
=head4 DESCRIPTION
Returns a boolean value that is C<true> if the given value can be converted
into an integer number by using parseInt(value). Otherwise C<false> is
returned.
=head4 PARAMETERS
value = Any
=head4 RETURN VALUE
Boolean or invalid.
=cut
.sub '_lang_isInt' :anon
.param pmc value
.local pmc res
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
$P0 = value.'parseInt'()
res = defined $P0
.return (res)
L1:
$I0 = isa value, 'WmlsInvalid'
unless $I0 goto L2
new res, 'WmlsInvalid'
.return (res)
L2:
new res, 'WmlsBoolean'
$I0 = isa value, 'WmlsInteger'
set res, $I0
.return (res)
.end
=head3 C<isFloat(value)>
=head4 DESCRIPTION
Returns a boolean value that is C<true> if the given value can be converted
into a floating-point number using parseFloat(value). Otherwise C<false> is
returned.
=head4 PARAMETERS
value = Any
=head4 RETURN VALUE
Boolean or invalid.
=cut
.sub '_lang_isFloat' :anon
.param pmc value
.local pmc res
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
$P0 = value.'parseFloat'()
res = defined $P0
.return (res)
L1:
$I0 = isa value, 'WmlsInvalid'
unless $I0 goto L2
new res, 'WmlsInvalid'
.return (res)
L2:
new res, 'WmlsBoolean'
$I0 = isa value, 'WmlsFloat'
set res, $I0
if $I0 goto L3
$I0 = isa value, 'WmlsInteger'
set res, $I0
L3:
.return (res)
.end
=head3 C<maxInt()>
=head4 DESCRIPTION
Returns the maximum integer value.
=head4 RETURN VALUE
Integer 2147483647.
=cut
.sub '_lang_maxInt' :anon
.local pmc res
box res, 2147483647
.return (res)
.end
=head3 C<minInt()>
=head4 DESCRIPTION
Returns the minimum integer value.
=head4 RETURN VALUE
Integer -2147483648.
=cut
.sub '_lang_minInt' :anon
.local pmc res
box res, -2147483648
.return (res)
.end
=head3 C<float()>
=head4 DESCRIPTION
Returns true if floating-points are supported and false if not.
=head4 RETURN VALUE
Boolean.
=cut
.sub '_lang_float' :anon
.local pmc res
new res, 'WmlsBoolean'
set res, 1
.return (res)
.end
=head3 C<exit(value)>
=head4 DESCRIPTION
Ends the interpretation of the WMLScript bytecode and returns the control
back to the caller of the WMLScript interpreter with the given return value.
This function can be used to perform a normal exit from a function in cases
where the execution of the WMLScript bytecode should be discontinued.
=head4 PARAMETERS
value = Any
=head4 RETURN VALUE
None (this function ends the interpretation).
=cut
.sub '_lang_exit' :anon
.param pmc value
.local pmc res
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
$P0 = value.'parseInt'()
$I0 = $P0
exit $I0
L1:
$I0 = value
exit $I0
.end
=head3 C<abort(errorDescription)>
=head4 DESCRIPTION
Aborts the interpretation of the WMLScript bytecode and returns the control
back to the caller of the WMLScript interpreter with the return
errorDescription. This function can be used to perform an abnormal exit in
cases where the execution of the WMLScript should be discontinued due to
serious errors detected by the program. If the type of the errorDescription is
invalid, string "invalid" is used as the errorDescription instead.
=head4 PARAMETERS
errorDescription = String
=head4 RETURN VALUE
None (this function aborts the interpretation).
=cut
.sub '_lang_abort' :anon
.param pmc value
die value
.end
=head3 C<random(value)>
=head4 DESCRIPTION
Returns an integer value with positive sign that is greater than or equal to 0
but less than or equal to the given value. The return value is chosen
randomly or pseudo-randomly with approximately uniform distribution over
that range, using an implementation-dependent algorithm or strategy.
If the value is of type floating-point, Float.int() is first used to calculate the
actual integer value.
=head4 PARAMETERS
value = Number
=head4 RETURN VALUE
Integer or invalid.
=head4 EXCEPTIONS
If value is equal to zero (0), the function returns zero.
If value is less than zero (0), the function returns C<invalid>.
=cut
.sub '_lang_random' :anon
.param pmc value
.local pmc res
$P1 = value
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
$P1 = value.'parseNumber'()
L1:
$I0 = isa $P1, 'WmlsInvalid'
if $I0 goto L2
$I1 = $P1
if $I1 < 0 goto L2
$I0 = rand()
$I0 = mul $I1
$I2 = RAND_MAX()
inc $I2
$I0 = div $I2
box res, $I0
goto L3
L2:
new res, 'WmlsInvalid'
L3:
.return (res)
.end
=head3 C<seed(value)>
=head4 DESCRIPTION
Initialises the pseudo-random number sequence and returns an empty
string. If the value is zero or a positive integer then the given value is used
for initialisation, otherwise a random, system dependent initialisation value
is used. A seed value of greater than or equal to zero results in a repeatable
sequence of pseudo-random numbers. A seed value of less than zero
results in a non-repeatable sequence of random numbers.
If the value is of type floating-point, Float.int() is first used to calculate the
actual integer value. . If the value is non-numeric, invalid is returned and the
current seed is unchanged.
=head4 PARAMETERS
value = Number
=head4 RETURN VALUE
String or invalid.
=cut
.sub '_lang_seed' :anon
.param pmc value
.local pmc res
$P1 = value
$I0 = isa value, 'WmlsString'
unless $I0 goto L1
$P1 = value.'parseNumber'()
L1:
$I0 = isa $P1, 'WmlsInvalid'
if $I0 goto L2
$I1 = $P1
if $I0 >= 0 goto L3
$I1 = time
L3:
srand($I1)
new res, 'WmlsString'
set res, ''
goto L4
L2:
new res, 'WmlsInvalid'
L4:
.return (res)
.end
=head3 C<characterSet()>
=head4 DESCRIPTION
Returns the character set supported by the WMLScript Interpreter. The
return value is an integer that denotes a MIBEnum value assigned by the
IANA for all character sets (see [WSP] for more information).
=head4 RETURN VALUE
Integer.
=cut
.sub '_lang_characterSet' :anon
.local pmc res
box res, 4 # latin1
.return (res)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: