RPL programming language
Encyclopedia
The RPL programming language (RPL meaning ROM
-based procedural language following Hewlett-Packard
or, alternatively, Reverse Polish
LISP
) is a handheld calculator
system and application programming language
used on Hewlett-Packard
's engineering graphing RPN
calculators of the HP-28, HP-48, HP-49 and HP-50 series, but it is also usable on non-RPN calculators, such as the HP-39 series.
RPL is a structured programming
language based on RPN but equally capable of processing algebraic
expressions and formulae, implemented as a threaded interpreter
. RPL has many similarities to Forth, both languages being stack
-based, and of course the list-based LISP. Contrary to previous HP RPN calculators, which had a fixed four-level stack, the stack used by RPL is only limited by available calculator RAM
.
RPL originated from HP's Corvallis, Oregon
development facility in 1984 as a replacement for the previous practice of implementing the operating systems of calculators in assembly language
. According to a quote by Dr. William Wickes, one of the original RPL developers, "the development team never calls it anything but (the initials) RPL."
implementation language. This variant of RPL is not accessible to the calculator user without the use of external tools. It is possible to cause a serious crash while coding in SysRPL, so caution must be used while using it. The high-level User RPL (or UserRPL) version of the language is available on said graphing calculators for developing textual as well as graphical application programs. All UserRPL programs are internally represented as SysRPL programs, but use only a safe subset of the available SysRPL commands. The error checking that is a part of UserRPL commands, however, makes UserRPL programs noticeably slower than equivalent SysRPL programs. The UserRPL command SYSEVAL tells the calculator to process designated parts of a UserRPL program as SysRPL code.
The following example tests to see if the number at the bottom of the stack is "1" and, if so, replaces it with "Equal to one":
The IF construct evaluates the condition then tests the bottom of the stack for the result. As a result RPL can optionally support FORTH-style IF blocks, allowing the condition to be determined before the block. By leaving the condition empty, the IF statement will not make any changes to the stack during the condition execution and will use the existing result at the bottom of the stack for the test:
IFT and IFTE pop two or three commands off the stack, respectively. The topmost value is evaluated as a boolean and, if true, the second topmost value is pushed back on the stack. IFTE allows a third "else" value that will be pushed back on the stack if the boolean is false.
The following example uses the IFT function to pop an object from the bottom of the stack and, if it is equal to 1, replaces it with "One":
The following example uses the IFTE function to pop an object from the bottom of the stack and, if it is equal to 1, replaces it with "One". If it does not equal 1, it replaces it with the string "Not one":
IFT and IFTE will evaluate a program block given as one of its arguments, allowing a more compact form of conditional logic than an IF/THEN/ELSE/END structure. The following example pops an object from the bottom of the stack, and replaces it with "One", "Less", or "More", depending on whether it is equal to, less than, or greater than 1.
The following code illustrates the use of a CASE/THEN/END block. Given a letter at the bottom of the stack, it replaces it with its string equivalent or "Unknown letter":
This code is identical to the following nested IF/THEN/ELSE/END block equivalent:
The following example uses the FOR loop to sum the numbers from 1 to 10. The index variable of the FOR loop is "I":
External links
Read-only memory
Read-only memory is a class of storage medium used in computers and other electronic devices. Data stored in ROM cannot be modified, or can be modified only slowly or with difficulty, so it is mainly used to distribute firmware .In its strictest sense, ROM refers only...
-based procedural language following Hewlett-Packard
Hewlett-Packard
Hewlett-Packard Company or HP is an American multinational information technology corporation headquartered in Palo Alto, California, USA that provides products, technologies, softwares, solutions and services to consumers, small- and medium-sized businesses and large enterprises, including...
or, alternatively, Reverse Polish
Reverse Polish notation
Reverse Polish notation is a mathematical notation wherein every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as Postfix notation and is parenthesis-free as long as operator arities are fixed...
LISP
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...
) is a handheld calculator
Calculator
An electronic calculator is a small, portable, usually inexpensive electronic device used to perform the basic operations of arithmetic. Modern calculators are more portable than most computers, though most PDAs are comparable in size to handheld calculators.The first solid-state electronic...
system and application programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
used on Hewlett-Packard
Hewlett-Packard
Hewlett-Packard Company or HP is an American multinational information technology corporation headquartered in Palo Alto, California, USA that provides products, technologies, softwares, solutions and services to consumers, small- and medium-sized businesses and large enterprises, including...
's engineering graphing RPN
Reverse Polish notation
Reverse Polish notation is a mathematical notation wherein every operator follows all of its operands, in contrast to Polish notation, which puts the operator in the prefix position. It is also known as Postfix notation and is parenthesis-free as long as operator arities are fixed...
calculators of the HP-28, HP-48, HP-49 and HP-50 series, but it is also usable on non-RPN calculators, such as the HP-39 series.
RPL is a structured programming
Structured programming
Structured programming is a programming paradigm aimed on improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and for and while loops - in contrast to using simple tests and jumps such as the goto statement which could...
language based on RPN but equally capable of processing algebraic
Infix notation
Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on . It is not as simple to parse by computers as prefix notation or postfix notation Infix notation is the common arithmetic and logical formula notation,...
expressions and formulae, implemented as a threaded interpreter
Threaded code
In computer science, the term threaded code refers to a compiler implementation technique where the generated code has a form that essentially consists entirely of calls to subroutines...
. RPL has many similarities to Forth, both languages being stack
Stack-oriented programming language
A stack-oriented programming language is one that relies on a stack machine model for passing parameters. Several programming languages fit this description, notably Forth, RPL, PostScript, and also many Assembly languages ....
-based, and of course the list-based LISP. Contrary to previous HP RPN calculators, which had a fixed four-level stack, the stack used by RPL is only limited by available calculator RAM
Random-access memory
Random access memory is a form of computer data storage. Today, it takes the form of integrated circuits that allow stored data to be accessed in any order with a worst case performance of constant time. Strictly speaking, modern types of DRAM are therefore not random access, as data is read in...
.
RPL originated from HP's Corvallis, Oregon
Corvallis, Oregon
Corvallis is a city located in central western Oregon, United States. It is the county seat of Benton County and the principal city of the Corvallis, Oregon Metropolitan Statistical Area, which encompasses all of Benton County. As of the 2010 United States Census, the population was 54,462....
development facility in 1984 as a replacement for the previous practice of implementing the operating systems of calculators in assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
. According to a quote by Dr. William Wickes, one of the original RPL developers, "the development team never calls it anything but (the initials) RPL."
Variants
The internal low- to medium-level variant of RPL, called System RPL (or SysRPL) is used on some earlier HP calculators as well as the aforementioned ones, as part of their operating systemOperating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
implementation language. This variant of RPL is not accessible to the calculator user without the use of external tools. It is possible to cause a serious crash while coding in SysRPL, so caution must be used while using it. The high-level User RPL (or UserRPL) version of the language is available on said graphing calculators for developing textual as well as graphical application programs. All UserRPL programs are internally represented as SysRPL programs, but use only a safe subset of the available SysRPL commands. The error checking that is a part of UserRPL commands, however, makes UserRPL programs noticeably slower than equivalent SysRPL programs. The UserRPL command SYSEVAL tells the calculator to process designated parts of a UserRPL program as SysRPL code.
Control blocks
RPL control blocks are not strictly postfix. Although there are some notable exceptions, the control block structures appear as they would in a standard infix language. The calculator manages this by allowing the implementation of these blocks to skip ahead in the program stream as necessary.IF/THEN/ELSE/END
RPL supports basic conditional testing through the IF/THEN/ELSE structure. The basic syntax of this block is:
IF condition THEN if-true [ELSE if-false] END
The following example tests to see if the number at the bottom of the stack is "1" and, if so, replaces it with "Equal to one":
« IF 1 THEN "Equal to one" END »
The IF construct evaluates the condition then tests the bottom of the stack for the result. As a result RPL can optionally support FORTH-style IF blocks, allowing the condition to be determined before the block. By leaving the condition empty, the IF statement will not make any changes to the stack during the condition execution and will use the existing result at the bottom of the stack for the test:
« 1 IF THEN "Equal to one" END »
IFT/IFTE
Postfix conditional testing may be accomplished by using the IFT ("if-then") and IFTE ("if-then-else") functions.IFT and IFTE pop two or three commands off the stack, respectively. The topmost value is evaluated as a boolean and, if true, the second topmost value is pushed back on the stack. IFTE allows a third "else" value that will be pushed back on the stack if the boolean is false.
The following example uses the IFT function to pop an object from the bottom of the stack and, if it is equal to 1, replaces it with "One":
« 1 "One" IFT »
The following example uses the IFTE function to pop an object from the bottom of the stack and, if it is equal to 1, replaces it with "One". If it does not equal 1, it replaces it with the string "Not one":
« 1 "One" "Not one" IFTE »
IFT and IFTE will evaluate a program block given as one of its arguments, allowing a more compact form of conditional logic than an IF/THEN/ELSE/END structure. The following example pops an object from the bottom of the stack, and replaces it with "One", "Less", or "More", depending on whether it is equal to, less than, or greater than 1.
«
DUP 1
« DROP "One" »
« 1 < "Less" "More" IFTE »
IFTE
»
CASE/THEN/END
To support more complex conditional logic, RPL provides the CASE/THEN/END structure for handling multiple exclusive tests. Only one of the branches within the CASE statement will be executed. The basic syntax of this block is:
CASE
condition_1 THEN if-condition_1 END
...
condition_n THEN if-condition_n END
if-none
END
The following code illustrates the use of a CASE/THEN/END block. Given a letter at the bottom of the stack, it replaces it with its string equivalent or "Unknown letter":
«
CASE
DUP "A" THEN "Alpha" END
DUP "B" THEN "Beta" END
DUP "G"
THEN "Gamma" END
"Unknown letter"
END
SWAP DROP @ Get rid of the original letter
»
This code is identical to the following nested IF/THEN/ELSE/END block equivalent:
«
IF DUP "A"
THEN
"Alpha"
ELSE
IF DUP "B"
THEN
"Beta"
ELSE
IF DUP "G" THEN
"Gamma"
ELSE
"Unknown letter"
END
END
END
SWAP DROP @ Get rid of the original letter
»
FOR/NEXT
RPL provides a FOR/NEXT statement for looping from one index to another. The index for the loop is stored in a temporary local variable that can be accessed in the loop. The syntax of the FOR/NEXT block is:
index_from index_to FOR variable_name loop_statement NEXT
The following example uses the FOR loop to sum the numbers from 1 to 10. The index variable of the FOR loop is "I":
«
0 @ Start with zero on the stack
1 10 @ Loop from 1 to 10
FOR I @ "I" is the local variable
I + @ Add "I" to the running total
NEXT @ Repeat...
»
START/NEXT
The START/NEXT block is used for a simple block that runs from a start index to an end index. Unlike the FOR/NEXT loop, the looping variable is not available. The syntax of the START/NEXT block is:
index_from index_to START loop_statement NEXT
FOR/STEP and START/STEP
Both FOR/NEXT and START/NEXT support a user-defined step increment. By replacing the terminating NEXT keyword with an increment and the STEP keyword, the loop variable will be incremented or decremented by a different value than the default of +1. For instance, the following loop steps back from 10 to 2 by decrementing the loop index by 2:
« 10 2 START -2 STEP »
WHILE/REPEAT/END
The WHILE/REPEAT/END block in RPL supports an indefinite loop with the condition test at the start of the loop. The syntax of the WHILE/REPEAT/END block is:
WHILE condition REPEAT loop_statement END
DO/UNTIL/END
The DO/UNTIL/END block in RPL supports an indefinite loop with the condition test at the end of the loop. The syntax of the DO/UNTIL/END block is:
DO loop_statement UNTIL condition END
External links
- Advanced User's Reference Manual, command reference and RPL guide – From HP
- RPL Programming Articles – From hpcalc.org
- Article on RPL, with code examples – From The Museum of HP Calculators (MoHPC)
- www.rpl2.net – Gnu RPL/2 Homepage