Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rpn v0.7.2

Interpreting calculator using Reverse Polish Notation.

Invocation

usage: rpn.py [-h] [-i] [-s SESSION] [-S] [filenames ...]

Interpreting calculator using Reverse Polish Notation.

positional arguments:
  filenames             Names of RPN script files to execute. If none, then
                        interactive mode.

options:
  -h, --help            show this help message and exit
  -i, --interactive     After executing files, leave in interactive mode.
  -s SESSION, --session SESSION
                        Session file to run first at startup and dump to at
                        exit.
  -S, --autosession     Use the session file defined by environment variable
                        RPN_SESSION_FILE.

Operators

General

Operator Description Stack
def Create a variable in the current keyspace. A variable is a keyed item (value, array, procedure). key item → -
del Delete a variable from the current keyspace. If no such variable, then no effect. key → -
run Open a new input file, read items from it and execute. filename → -
count Count the number of elements in the stack, and put that integer on the stack. → integer
dump Write out the current stack and keyspaces to the named file. filename → -
quit Quit from execution without saving to any session file. N/A

Stack

Operator Description Stack
pop Pop the top item from the stack, and print if interactive. item → -
dup Duplicate the top item. A full copy is created. item → item item
exch Exchange the two top items on the stack. item1 item2 → item2 item1
clear Clear all items from the stack. ... → <empty>
print Pop the top value and print it. item → -

Control

Operator Description Stack
if Conditional execution of a procedure. bool procedure → -
ifelse Conditional execution of one of two procedures. If bool is true, the first procedure is executed, else the second. bool procedure1 procedure2 → -
loop Infinite loop over the procedure. Use operator 'exit' to quit it. procedure → -
repeat Repeat the procedure a number of times. n procedure → -
for Loop the procedure from the initial number using the increment up to and including the limit. The loop number is pushed each time onto the stack before the procedure is executed. initial increment limit procedure → value
exit Exit the innermost loop. In none, then no action. -

Logic

Operator Description Stack
bool Convert the value to Bool; 0, 0.0, "" and [] are false, other values true. value → bool
not Boolean 'not'. bool → bool
and Boolean 'and'. bool1 bool2 → bool
or Boolean 'or'. bool1 bool2 → bool
xor Boolean 'xor' (exclusive or). bool1 bool2 → bool
gt Greater than; value1 > value2. The values must of comparable types. value2 value1 → bool
ge Greater than or equal to; value1 >= value2. The values must of comparable types. value2 value1 → bool
lt Less than; value1 < value2. The values must of comparable types. value2 value1 → bool
le Less than or equal to; value1 <= value2. The values must of comparable types. value2 value1 → bool
eq Equal to; value1 == value2. value1 value2 → bool
ne Not equal to; value1 != value2. value1 value2 → bool

Numbers

Operator Description Stack
integer Convert the value to an integer. value → integer
round Convert the value to the nearest integer. value → integer
float Convert the value to a float. value → float
abs Absolute value of the number. number → number
neg Negate the number. number → number

Math

Operator Description Stack
add Add the two numbers on the stack. Also available as '+'. number1 number2 → number
sub Subtract the top number on the stack from the next-to-top number; number1 - number2. Also available as '-'. number1 number2 → number
mul Multiply the two numbers on the stack. Also available as '*'. number1 number2 → number
div Divide the next-to-top number on the stack by the top number; number1 / number2. Also available as '/'. number1 number2 → float
log Natural logarithm of the number. number → float
log10 Base-10 logarithm of the number. number → float
exp 'e' raised to the power of the number. number → float
power The next-to-top number to the power of the top number; number1 ^ number2. number1 number2 → number
sqrt Square root of the number. number → float

Trigonometry

Operator Description Stack
cos Cosine of the number (radians). number → float
sin Sine of the number (radians). number → float
tan Tangent of the number (radians). number → float
acos Arc cosine of the number (radians). number → float
asin Arc sine of the number (radians). number → float
atan Arc tangent of the number (radians). number → float
atan2 Arc tangent of the next-to-top number divided by the top number (radians). number1 number2 → float
degrees Convert the number in radians to degrees. number → float
radians Convert the number in degrees to radians. number → float

Others

Operator Description Stack
noop No operation. No change.
length Return length of the item (String, Array). item → integer
error Raise an error. No change.

Interactive

The following one-character operators are available only in interactive mode.

Operator Description Stack
= Print the stack. No change.
§ Print the keyspaces. No change.
? Print the operators. No change.

Predefined variables

  • e: 2.718281828459045
  • pi: 3.141592653589793

Demo

# Script testing rpn.

/equality_check {eq {"OK" print} {"Error!" print} ifelse} def

/double {2 mul} def

3 double 6 equality_check
# > OK

-2 double -4 equality_check
# > OK

-2 double 4 equality_check
# > Error!

About

Interpreting calculator using Reverse Polish Notation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages