tagged release 0.7.1
[parrot.git] / languages / WMLScript / runtime / wmlslang.pir
blob3e028b7b4ab51a2e59cfc598442d51c27d709d0f
1 # Copyright (C) 2006-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 runtime/wmlslang.pir - WMLScript Lang library
8 =head1 DESCRIPTION
10 This library contains a set of functions that are closely related to the
11 WMLScript language core.
13 See "WMLScript Standard Libraries Specification", section 7 "Lang".
15 =head1 FUNCTIONS
17 =cut
19 .loadlib 'wmls_ops'
20 .HLL 'WMLScript', 'wmls_group'
23 .sub 'getLang'
24     new $P0, 'Hash'
26     .const .Sub _lang_abs = '_lang_abs'
27     $P0[0]  = _lang_abs
28     .const .Sub _lang_min = '_lang_min'
29     $P0[1]  = _lang_min
30     .const .Sub _lang_max = '_lang_max'
31     $P0[2]  = _lang_max
32     .const .Sub _lang_parseInt = '_lang_parseInt'
33     $P0[3]  = _lang_parseInt
34     .const .Sub _lang_parseFloat = '_lang_parseFloat'
35     $P0[4]  = _lang_parseFloat
36     .const .Sub _lang_isInt = '_lang_isInt'
37     $P0[5]  = _lang_isInt
38     .const .Sub _lang_isFloat = '_lang_isFloat'
39     $P0[6]  = _lang_isFloat
40     .const .Sub _lang_maxInt = '_lang_maxInt'
41     $P0[7]  = _lang_maxInt
42     .const .Sub _lang_minInt = '_lang_minInt'
43     $P0[8]  = _lang_minInt
44     .const .Sub _lang_float = '_lang_float'
45     $P0[9]  = _lang_float
46     .const .Sub _lang_exit = '_lang_exit'
47     $P0[10] = _lang_exit
48     .const .Sub _lang_abort = '_lang_abort'
49     $P0[11] = _lang_abort
50     .const .Sub _lang_random = '_lang_random'
51     $P0[12] = _lang_random
52     .const .Sub _lang_seed = '_lang_seed'
53     $P0[13] = _lang_seed
54     .const .Sub _lang_characterSet = '_lang_characterSet'
55     $P0[14] = _lang_characterSet
57     .return ($P0)
58 .end
61 =head2 C<abs(value)>
63 =head3 DESCRIPTION
65 Returns the absolute value of the given number. If the given number is of
66 type integer then an integer value is returned. If the given number is of type
67 floating-point then a floating-point value is returned.
69 =head3 PARAMETERS
71 value = Number
73 =head3 RETURN VALUE
75 Number or invalid.
77 =cut
79 .sub '_lang_abs' :anon
80     .param pmc value
81     .local pmc res
82     res = clone value
83     res = abs value
84     .return (res)
85 .end
88 =head2 C<min(value1, value2)>
90 =head3 DESCRIPTION
92 Returns the minimum value of the given two numbers. The value and type
93 returned is the same as the value and type of the selected number. The
94 selection is done in the following way:
96 =over 4
98 =item *
100 WMLScript operator data type conversion rules for I<integers and
101 floating-points> (see [WMLScript]) must be used to specify the data
102 type (integer or floating-point ) for comparison.
104 =item *
106 Compare the numbers to select the smaller one.
108 =item *
110 If the values are equal then the first value is selected.
112 =back
114 =head3 PARAMETERS
116 value1 = Number
118 value2 = Number
120 =head3 RETURN VALUE
122 Number or invalid.
124 =cut
126 .sub '_lang_min' :anon
127     .param pmc value1
128     .param pmc value2
129     .local pmc res
130     $P1 = value1
131     $I0 = isa value1, 'WmlsString'
132     unless $I0 goto L1
133     $P1 = value1.'parseNumber'()
134   L1:
135     $P2 = value2
136     $I0 = isa value2, 'WmlsString'
137     unless $I0 goto L2
138     $P2 = value2.'parseNumber'()
139   L2:
140     $P0 = isle $P1, $P2
141     $I0 = isa $P0, 'WmlsInvalid'
142     unless $I0 goto L3
143     .return ($P0)
144   L3:
145     $I0 = $P0
146     unless $I0 goto L4
147     res = clone value1
148     goto L5
149   L4:
150     res = clone value2
151   L5:
152     .return (res)
153 .end
156 =head2 C<max(value1, value2)>
158 =head3 DESCRIPTION
160 Returns the maximum value of the given two numbers. The value and type
161 returned is the same as the value and type of the selected number. The
162 selection is done in the following way:
164 =over 4
166 =item *
168 WMLScript operator data type conversion rules for I<integers and
169 floating-points> (see [WMLScript]) must be used to specify the data
170 type (integer or floating-point ) for comparison.
172 =item *
174 Compare the numbers to select the larger one.
176 =item *
178 If the values are equal then the first value is selected.
180 =back
182 =head3 PARAMETERS
184 value1 = Number
186 value2 = Number
188 =head3 RETURN VALUE
190 Number or invalid.
192 =cut
194 .sub '_lang_max' :anon
195     .param pmc value1
196     .param pmc value2
197     .local pmc res
198     $P1 = value1
199     $I0 = isa value1, 'WmlsString'
200     unless $I0 goto L1
201     $P1 = value1.'parseNumber'()
202   L1:
203     $P2 = value2
204     $I0 = isa value2, 'WmlsString'
205     unless $I0 goto L2
206     $P2 = value2.'parseNumber'()
207   L2:
208     $P0 = isge $P1, $P2
209     $I0 = isa $P0, 'WmlsInvalid'
210     unless $I0 goto L3
211     .return ($P0)
212   L3:
213     $I0 = $P0
214     unless $I0 goto L4
215     res = clone value1
216     goto L5
217   L4:
218     res = clone value2
219   L5:
220     .return (res)
221 .end
224 =head2 C<parseInt(value)>
226 =head3 DESCRIPTION
228 Returns an integer value defined by the string value. The legal integer
229 syntax is specified by the WMLScript (see [WMLScript]) numeric string
230 grammar for I<decimal integer literals> with the following additional parsing
231 rule:
233 =over 4
235 =item *
237 Parsing ends when the first character is encountered that is not a
238 leading '+' or '-' or a decimal digit.
240 =back
242 The result is the parsed string converted to an integer value.
244 =head3 PARAMETERS
246 value = String
248 =head3 RETURN VALUE
250 Integer or invalid.
252 =head3 EXCEPTIONS
254 In case of a parsing error an C<invalid> value is returned.
256 =cut
258 .sub '_lang_parseInt' :anon
259     .param pmc value
260     .local pmc res
261     $I0 = isa value, 'WmlsString'
262     unless $I0 goto L1
263     res = value.'parseInt'()
264     .return (res)
265   L1:
266     res = value
267     $I0 = isa value, 'WmlsInteger'
268     if $I0 goto L2
269     new res, 'WmlsInvalid'
270   L2:
271     .return (res)
272 .end
275 =head2 C<parseFloat(value)>
277 =head3 DESCRIPTION
279 Returns a floating-point value defined by the string value. The legal floatingpoint
280 syntax is specified by the WMLScript (see [WMLScript]) numeric
281 string grammar for I<decimal floating-point literals> with the following additional
282 parsing rule:
284 =over 4
286 =item *
288 Parsing ends when the first character is encountered that cannot
289 be parsed as being part of the floating-point representation.
291 =back
293 The result is the parsed string converted to a floating-point value.
295 =head3 PARAMETERS
297 value = String
299 =head3 RETURN VALUE
301 Floating-point or invalid.
303 =head3 EXCEPTIONS
305 In case of a parsing error an C<invalid> value is returned.
307 =cut
309 .sub '_lang_parseFloat' :anon
310     .param pmc value
311     .local pmc res
312     $I0 = isa value, 'WmlsString'
313     unless $I0 goto L1
314     res = value.'parseFloat'()
315     .return (res)
316   L1:
317     res = value
318     $I0 = isa value, 'WmlsFloat'
319     if $I0 goto L2
320     $I0 = isa value, 'WmlsInteger'
321     unless $I0 goto L3
322     $I0 = value
323     new res, 'WmlsFloat'
324     set res, $I0
325     goto L2
326   L3:
327     new res, 'WmlsInvalid'
328   L2:
329     .return (res)
330 .end
333 =head2 C<isInt(value)>
335 =head3 DESCRIPTION
337 Returns a boolean value that is C<true> if the given value can be converted
338 into an integer number by using parseInt(value). Otherwise C<false> is
339 returned.
341 =head3 PARAMETERS
343 value = Any
345 =head3 RETURN VALUE
347 Boolean or invalid.
349 =cut
351 .sub '_lang_isInt' :anon
352     .param pmc value
353     .local pmc res
354     $I0 = isa value, 'WmlsString'
355     unless $I0 goto L1
356     $P0 = value.'parseInt'()
357     res = defined $P0
358     .return (res)
359   L1:
360     $I0 = isa value, 'WmlsInvalid'
361     unless $I0 goto L2
362     new res, 'WmlsInvalid'
363     .return (res)
364   L2:
365     new res, 'WmlsBoolean'
366     $I0 = isa value, 'WmlsInteger'
367     set res, $I0
368     .return (res)
369 .end
372 =head2 C<isFloat(value)>
374 =head3 DESCRIPTION
376 Returns a boolean value that is C<true> if the given value can be converted
377 into a floating-point number using parseFloat(value). Otherwise C<false> is
378 returned.
380 =head3 PARAMETERS
382 value = Any
384 =head3 RETURN VALUE
386 Boolean or invalid.
388 =cut
390 .sub '_lang_isFloat' :anon
391     .param pmc value
392     .local pmc res
393     $I0 = isa value, 'WmlsString'
394     unless $I0 goto L1
395     $P0 = value.'parseFloat'()
396     res = defined $P0
397     .return (res)
398   L1:
399     $I0 = isa value, 'WmlsInvalid'
400     unless $I0 goto L2
401     new res, 'WmlsInvalid'
402     .return (res)
403   L2:
404     new res, 'WmlsBoolean'
405     $I0 = isa value, 'WmlsFloat'
406     set res, $I0
407     if $I0 goto L3
408     $I0 = isa value, 'WmlsInteger'
409     set res, $I0
410   L3:
411     .return (res)
412 .end
415 =head2 C<maxInt()>
417 =head3 DESCRIPTION
419 Returns the maximum integer value.
421 =head3 RETURN VALUE
423 Integer 2147483647.
425 =cut
427 .sub '_lang_maxInt' :anon
428     .local pmc res
429     new res, 'WmlsInteger'
430     set res, 2147483647
431     .return (res)
432 .end
435 =head2 C<minInt()>
437 =head3 DESCRIPTION
439 Returns the minimum integer value.
441 =head3 RETURN VALUE
443 Integer -2147483648.
445 =cut
447 .sub '_lang_minInt' :anon
448     .local pmc res
449     new res, 'WmlsInteger'
450     set res, -2147483648
451     .return (res)
452 .end
455 =head2 C<float()>
457 =head3 DESCRIPTION
459 Returns true if floating-points are supported and false if not.
461 =head3 RETURN VALUE
463 Boolean.
465 =cut
467 .sub '_lang_float' :anon
468     .local pmc res
469     new res, 'WmlsBoolean'
470     set res, 1
471     .return (res)
472 .end
475 =head2 C<exit(value)>
477 =head3 DESCRIPTION
479 Ends the interpretation of the WMLScript bytecode and returns the control
480 back to the caller of the WMLScript interpreter with the given return value.
481 This function can be used to perform a normal exit from a function in cases
482 where the execution of the WMLScript bytecode should be discontinued.
484 =head3 PARAMETERS
486 value = Any
488 =head3 RETURN VALUE
490 None (this function ends the interpretation).
492 =cut
494 .sub '_lang_exit' :anon
495     .param pmc value
496     .local pmc res
497     $I0 = isa value, 'WmlsString'
498     unless $I0 goto L1
499     $P0 = value.'parseInt'()
500     $I0 = $P0
501     exit $I0
502   L1:
503     $I0 = value
504     exit $I0
505 .end
508 =head2 C<abort(errorDescription)>
510 =head3 DESCRIPTION
512 Aborts the interpretation of the WMLScript bytecode and returns the control
513 back to the caller of the WMLScript interpreter with the return
514 errorDescription. This function can be used to perform an abnormal exit in
515 cases where the execution of the WMLScript should be discontinued due to
516 serious errors detected by the program. If the type of the errorDescription is
517 invalid, string "invalid" is used as the errorDescription instead.
519 =head3 PARAMETERS
521 errorDescription = String
523 =head3 RETURN VALUE
525 None (this function aborts the interpretation).
527 =cut
529 .sub '_lang_abort' :anon
530     .param pmc value
531     die value
532 .end
535 =head2 C<random(value)>
537 =head3 DESCRIPTION
539 Returns an integer value with positive sign that is greater than or equal to 0
540 but less than or equal to the given value. The return value is chosen
541 randomly or pseudo-randomly with approximately uniform distribution over
542 that range, using an implementation-dependent algorithm or strategy.
544 If the value is of type floating-point, Float.int() is first used to calculate the
545 actual integer value.
547 =head3 PARAMETERS
549 value = Number
551 =head3 RETURN VALUE
553 Integer or invalid.
555 =head3 EXCEPTIONS
557 If value is equal to zero (0), the function returns zero.
559 If value is less than zero (0), the function returns C<invalid>.
561 =cut
563 .sub '_lang_random' :anon
564     .param pmc value
565     .local pmc res
566     $P0 = value
567     $I0 = isa value, 'WmlsString'
568     unless $I0 goto L1
569     $P0 = value.'parseNumber'()
570   L1:
571     $I0 = isa $P0, 'WmlsInvalid'
572     if $I0 goto L2
573     $I0 = $P0
574     if $I0 < 0 goto L2
575     new $P0, 'Random'
576     $N0 = $P0
577     $N0 = mul $I0
578     $I0 = $N0
579     new res, 'WmlsInteger'
580     set res, $I0
581     goto L3
582   L2:
583     new res, 'WmlsInvalid'
584   L3:
585     .return (res)
586 .end
589 =head2 C<seed(value)>
591 =head3 DESCRIPTION
593 Initialises the pseudo-random number sequence and returns an empty
594 string. If the value is zero or a positive integer then the given value is used
595 for initialisation, otherwise a random, system dependent initialisation value
596 is used. A seed value of greater than or equal to zero results in a repeatable
597 sequence of pseudo-random numbers. A seed value of less than zero
598 results in a non-repeatable sequence of random numbers.
600 If the value is of type floating-point, Float.int() is first used to calculate the
601 actual integer value. . If the value is non-numeric, invalid is returned and the
602 current seed is unchanged.
604 =head3 PARAMETERS
606 value = Number
608 =head3 RETURN VALUE
610 String or invalid.
612 =cut
614 .sub '_lang_seed' :anon
615     .param pmc value
616     .local pmc res
617     $P0 = value
618     $I0 = isa value, 'WmlsString'
619     unless $I0 goto L1
620     $P0 = value.'parseNumber'()
621   L1:
622     $I0 = isa $P0, 'WmlsInvalid'
623     if $I0 goto L2
624     $I0 = $P0
625     if $I0 >= 0 goto L3
626     $I0 = time
627   L3:
628     new $P0, 'Random'
629     $P0 = $I0
630     new res, 'WmlsString'
631     set res, ''
632     goto L4
633   L2:
634     new res, 'WmlsInvalid'
635   L4:
636     .return (res)
637 .end
640 =head2 C<characterSet()>
642 =head3 DESCRIPTION
644 Returns the character set supported by the WMLScript Interpreter. The
645 return value is an integer that denotes a MIBEnum value assigned by the
646 IANA for all character sets (see [WSP] for more information).
648 =head3 RETURN VALUE
650 Integer.
652 =cut
654 .sub '_lang_characterSet' :anon
655     .local pmc res
656     new res, 'WmlsInteger'
657     res = 4     # latin1
658     .return (res)
659 .end
662 =head1 AUTHORS
664 Francois Perrad.
666 =cut
669 # Local Variables:
670 #   mode: pir
671 #   fill-column: 100
672 # End:
673 # vim: expandtab shiftwidth=4 ft=pir: