Jump to content

RPL (programming language)

From Alfawiki
RPL
Company typeOperating system and programming language
IndustryCalculator software
Founded1984
Headquarters
Corvallis, Oregon
,
United States
ProductsOperating system and programming language for HP calculators

RPL is a handheld calculator operating system and programming language designed for use on Hewlett-Packard's scientific graphing calculators featuring RPN, including the 28, 48, 49, and 50 lines. It also operates on non-RPN devices such as the 38 and the 39 and 40. Internally, it served as the core of models like the HP-17B, HP-18C, HP-19B, and HP-27S.

As a structured programming language, RPL is rooted in RPN but additionally supports algebraic expression evaluation through a threaded interpreter design. It exhibits characteristics similar to Forth and Lisp by employing a stack-based approach. Unlike previous HP calculators with fixed four-level stacks, RPL utilizes a dynamic stack constrained only by available memory, enhancing error management when RAM is limited.

The development of RPL began in 1984 at HP’s Corvallis, Oregon division to supplant assembly language-based operating systems. Its initial internal implementation appeared in the HP-18C, while user access was introduced with the HP-28C in 1986. The final calculator model supporting RPL, the HP 50g, was discontinued in 2015.

Despite its discontinuation, several emulators are available for iOS and Android that simulate HP's RPL calculators. Additionally, community-driven projects such as newRPL and DB48X strive to modernize and extend RPL’s capabilities on contemporary hardware by adding new functions and optimizing performance.

Variants

The lower- to mid-level internal form of RPL, known as System RPL or SysRPL, is employed within some earlier HP calculators as part of their operating system development. On the HP 48 series, SysRPL remains inaccessible to users without external utilities; however, the HP 49/50 series incorporate an integrated compiler for SysRPL code. Programming in SysRPL carries risks of severe system crashes, so it requires careful handling. The higher-level User RPL (or UserRPL) is accessible on these graphing calculators for creating both textual and graphical applications. UserRPL programs compile down to SysRPL but restrict commands to a safer subset with built-in error detection, resulting in slower execution compared to SysRPL. The UserRPL command SYSEVAL allows selective execution of embedded SysRPL code segments.

Control blocks

Control structures in RPL do not strictly follow postfix conventions. Although there are exceptions, their syntax more closely resembles that of conventional infix programming languages. The calculator facilitates this behavior by permitting control blocks to jump forward within the program stream when necessary.

IF/THEN/ELSE/END

Basic conditional logic in RPL utilizes IF/THEN/ELSE statements structured as follows:

 IF condition THEN if-true [ELSE if-false] END

For instance, to verify whether the number at the bottom of the stack equals "1" and replace it with "Equal to one":

 « IF 1 == THEN "Equal to one" END »

Here, IF evaluates the condition against the bottom stack element. RPL also supports FORTH-style IF constructs where the condition precedes the block; if no condition is provided, it uses the current bottom stack value:

 « 1 == IF THEN "Equal to one" END »

IFT/IFTE

Postfix conditional operations are performed using IFT ("if-then") and IFTE ("if-then-else") commands.

Both IFT and IFTE remove two or three elements from the stack respectively. The top element is interpreted as a Boolean; if true, IFT returns the second element. IFTE returns either the second or third element depending on whether the Boolean is true or false.

An example of IFT replacing a stack item equal to 1 with "One":

 « 1 == "One" IFT »

An example using IFTE to replace a stack item equal to 1 with "One", otherwise "Not one":

 « 1 == "One" "Not one" IFTE »

IFT and IFTE can accept program blocks as parameters, enabling concise conditional expressions. For example, replacing a stack value with "One", "Less", or "More" based on comparison with 1:

 «
   DUP 1 ==
   « DROP "One" »
   « 1 < "Less" "More" IFTE »
   IFTE
 »

CASE/THEN/END

RPL facilitates sophisticated conditional branching using the CASE/THEN/END construct, which ensures that only a single, mutually exclusive path is executed. The general format is:

 CASE 
  condition_1 THEN if-condition_1 END 
   ...
  condition_n THEN if-condition_n END
  if-none
 END

For example, to substitute a letter on the stack with its corresponding string name or default to "Unknown letter":

 « 
   CASE 
      DUP "A" == THEN "Alpha" END
      DUP "B" == THEN "Beta" END
      DUP "G" == THEN "Gamma" END
      "Unknown letter"
   END
   SWAP DROP  @ Eliminates the original letter from the stack
 »

This structure offers a more streamlined alternative to deeply nested IF/THEN/ELSE/END statements.

FOR/NEXT

The FOR/NEXT loop iterates between two indices, assigning the current index to a temporary local variable accessible inside the loop body. Its syntax is:

index_from index_to FOR variable_name loop_statement NEXT

An illustration summing integers from 1 through 10 using "I" as the index variable:

 « 
    0       @ Starting total on the stack
    1 10    @ Loop range from 1 to 10
    FOR I   @ Defines local variable "I"
       I +  @ Adds current "I" to total
    NEXT
 »

START/NEXT

START/NEXT functions similarly but does not expose the loop counter as a variable. The syntax is:

 index_from index_to START loop_statement NEXT

FOR/STEP and START/STEP

Both FOR/NEXT and START/NEXT loops accept a STEP keyword, enabling increments or decrements other than the default +1. For example, counting backward from 10 down to 2 in steps of -2:

 « 10 2 START -2 STEP »

WHILE/REPEAT/END

WHILE/REPEAT/END constructs indefinite loops where the condition is evaluated before each iteration:

 WHILE condition REPEAT loop_statement END

DO/UNTIL/END

DO/UNTIL/END supports indefinite looping with the condition checked after executing the loop body:

 DO loop_statement UNTIL condition END

See also

Notes

1. The term "RPL" was originally coined as Reverse Polish Lisp by its creators,[1][2][3][4][5][6] although Hewlett-Packard briefly promoted it as ROM-based Procedural Language in 1987.[7][6][8] Occasionally, it is mistakenly expanded as Reverse Polish Logic or Reverse Polish Language.[9]

Further reading

  • HP 48G Series – User's Guide (8th edition, 1994) by Hewlett-Packard [1]
  • HP 48G Series – Advanced User's Reference Manual (4th edition, 1994) by Hewlett-Packard [2]
  • HP 50g graphing calculator user's guide (1st edition, 2006) by Hewlett-Packard
  • HP 50g / 49g+ / 48gII graphing calculator advanced user's reference manual (2nd edition, 2009) by Hewlett-Packard
  • Programming in System RPL (2nd edition, 2002) by Eduardo de Mattos Kalinowski and Carsten Dominik [3]
  • An Introduction to HP 48 System RPL and Assembly Language Programming (2009) by James Donnelly

External links

  • Programming Manuals for HP 49/50 – HP Calculator Archive [4]
  • Programming Manuals for HP 48 – HP Calculator Archive [5]
  • RPL Overview – The Museum of HP Calculators (MoHPC) [6]
  • RPL/2 – A new Reverse Polish Lisp (GPL-licensed RPL clone) [7]
  • rpn – Open source RPL implementation with arbitrary precision – GitHub [8]
  • MyRPL – Hybrid language combining HP41 and HP48 features (Mixed RPL and FOCAL) [9]
  1. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named Wickes_1988
  2. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named Wickes_1991
  3. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named Schoorl_2000
  4. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named FAQ48
  5. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named Nelson_2012
  6. ↑ 6.0 6.1 Cite error: Invalid <ref> tag; no text was provided for refs named Jedrzejowicz_1996
  7. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named HPJ38
  8. ↑ Cite error: Invalid <ref> tag; no text was provided for refs named HP_2007
  9. ↑ Lua error in Module:Citation/CS1/Configuration at line 2123: attempt to index field '?' (a nil value).