3 XS::APItest::KeywordRPN - write arithmetic expressions in RPN
7 use XS::APItest::KeywordRPN qw(rpn calcrpn);
9 $triangle = rpn($n $n 1 + * 2 /);
11 calcrpn $triangle { $n $n 1 + * 2 / }
15 This module supplies plugged-in keywords, using the new mechanism in Perl
16 5.11.2, that allow arithmetic to be expressed in reverse Polish notation,
17 in an otherwise Perl program. This module has serious limitations and
18 is not intended for real use: its purpose is only to test the keyword
19 plugin mechanism. For that purpose it is part of the Perl core source
20 distribution, and is not meant to be installed.
22 =head2 RPN expression syntax
24 Tokens of an RPN expression may be separated by whitespace, but such
25 separation is usually not required. It is required only where unseparated
26 tokens would look like a longer token. For example, C<12 34 +> can be
27 written as C<12 34+>, but not as C<1234 +>.
29 An RPN expression may be any of:
35 A sequence of digits is an unsigned decimal literal number.
39 An alphanumeric name preceded by dollar sign refers to a Perl scalar
40 variable. Only variables declared with C<my> or C<state> are supported.
41 If the variable's value is not a native integer, it will be converted
42 to an integer, by Perl's usual mechanisms, at the time it is evaluated.
50 Difference of I<A> and I<B>, the result of subtracting I<B> from I<A>.
54 Product of I<A> and I<B>.
58 Quotient when I<A> is divided by I<B>, rounded towards zero.
59 Division by zero generates an exception.
63 Remainder when I<A> is divided by I<B> with the quotient rounded towards zero.
64 Division by zero generates an exception.
68 Because the arithmetic operators all have fixed arity and are postfixed,
69 there is no need for operator precedence, nor for a grouping operator
70 to override precedence. This is half of the point of RPN.
72 An RPN expression can also be interpreted in another way, as a sequence
73 of operations on a stack, one operation per token. A literal or variable
74 token pushes a value onto the stack. A binary operator pulls two items
75 off the stack, performs a calculation with them, and pushes the result
76 back onto the stack. The stack starts out empty, and at the end of the
77 expression there must be exactly one value left on the stack.
81 package XS::APItest::KeywordRPN;
87 our $VERSION = "0.005";
90 XSLoader::load(__PACKAGE__, $VERSION);
94 These are the operators being added to the Perl language.
100 This construct is a Perl expression. I<EXPRESSION> must be an RPN
101 arithmetic expression, as described above. The RPN expression is
102 evaluated, and its value is returned as the value of the Perl expression.
104 =item calcrpn VARIABLE { EXPRESSION }
106 This construct is a complete Perl statement. (No semicolon should
107 follow the closing brace.) I<VARIABLE> must be a Perl scalar C<my>
108 variable, and I<EXPRESSION> must be an RPN arithmetic expression as
109 described above. The RPN expression is evaluated, and its value is
110 assigned to the variable.
116 This module only performs arithmetic on native integers, and only a
117 small subset of the arithmetic operations that Perl offers. This is
118 due to it being intended only for demonstration and test purposes.
120 The RPN parser is liable to leak memory when a parse error occurs.
121 It doesn't leak on success, however.
126 L<perlapi/PL_keyword_plugin>
130 Andrew Main (Zefram) <zefram@fysh.org>
134 Copyright (C) 2009 Andrew Main (Zefram) <zefram@fysh.org>
138 This module is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.