Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
manual:chapter5:basics [2019/09/11 07:36] claudioadmin [Variables] |
manual:chapter5:basics [2021/09/15 10:32] (current) claudio |
||
---|---|---|---|
Line 8: | Line 8: | ||
A typical sequence of a program is to place some objects on the stack, do one or more operations and leave the result in the stack. For example: | A typical sequence of a program is to place some objects on the stack, do one or more operations and leave the result in the stack. For example: | ||
- | < | + | < |
+ | « | ||
+ | | ||
+ | »</ | ||
Let's analyze the program above: | Let's analyze the program above: | ||
Line 18: | Line 21: | ||
And that's the most basic RPL program. Operators and functions always take some values from the stack and leave some results on it. A program may include multiple operations: | And that's the most basic RPL program. Operators and functions always take some values from the stack and leave some results on it. A program may include multiple operations: | ||
- | < | + | < |
+ | | ||
+ | | ||
+ | »</ | ||
The program above starts identical to the previous example, and after the + operator is executed, the resulting number 3 is left on the stack. As the program continues, the number 2 is put on level 1, relegating the number 3 to level 2. The division operator takes 2 numbers and performs the division of the number in level 2 divided by the number in level 1. In this case, level 2 had a 3 and level 1 had a 2, therefore the number 1.5 will be left on the stack as a result. | The program above starts identical to the previous example, and after the + operator is executed, the resulting number 3 is left on the stack. As the program continues, the number 2 is put on level 1, relegating the number 3 to level 2. The division operator takes 2 numbers and performs the division of the number in level 2 divided by the number in level 1. In this case, level 2 had a 3 and level 1 had a 2, therefore the number 1.5 will be left on the stack as a result. | ||
Line 24: | Line 30: | ||
---- | ---- | ||
- | ==== Operators and Commands ==== | + | ==== Operators, Functions |
+ | |||
+ | In the examples above we showed how + and / would take some objects from the stack, operate on them and place the result in the stack. This is the basic operation of all operators and commands in RPL. All commands, functions and operators take some (or none) arguments from the stack, and leave some (or none) results in it. There are hundreds of RPL keywords, all based on the same principle of taking and leaving objects on the stack. Future chapters will describe each keyword in depth, for now only a few ones will be introduced that define the basic core of the RPL language. | ||
+ | |||
+ | Fundamentally all the keywords can be divided into 4 classes: commands, operators, functions and analytic functions. Difference between classes isn't in the effect they have on the stack (as said above, almost any keyword manipulates the stack) rather in behavior and scope. | ||
+ | |||
+ | === Commands === | ||
+ | |||
+ | Commands perform actions. They are used to control program flow ('' | ||
+ | |||
+ | === Operators === | ||
+ | |||
+ | Operators manipulate objects, returning results. Usually these objects are [[manual: | ||
+ | |||
+ | Most operators are //binary// i.e. they need two arguments and when used within symbolic expressions are distinguished by the fact that they adopt the //infix// notation. There also are a few //unary// operators adopting the //prefix// notation in symbolics (unary '' | ||
+ | |||
+ | The basic arithmetic operators work as expected ('' | ||
+ | |||
+ | === Functions === | ||
+ | |||
+ | Functions, like operators, manipulate objects and return results. They can be inserted into symbolic expressions, | ||
- | In the examples above we showed how + and / would take some objects from the stack, operate on them and place the result in the stack. This is the basic operation of all operators and commands in RPL. All commands and operators take some (or none) arguments from the stack, and leave some (or none) results in it. There are hundreds of RPL commands, all based on the same principle of taking and leaving objects on the stack. Future chapters will describe each command in depth, for now only a few commands will be introduced that define the basic core of the RPL language. The basic arithmetic operators work as expected (+ and / were already introduced), | + | === Analytic Functions === |
+ | Analytic functions are a subset of the functions that conform to the layman' | ||
---- | ---- | ||
Line 46: | Line 73: | ||
Programs may take values from the stack, just like any RPL operator or command. To use values from the stack, a program simply needs to assume they are there and operate on them. Our previous example could be rewritten as: | Programs may take values from the stack, just like any RPL operator or command. To use values from the stack, a program simply needs to assume they are there and operate on them. Our previous example could be rewritten as: | ||
- | < | + | < |
+ | | ||
+ | | ||
+ | »</ | ||
This short program runs the operator + as its first command. Previously, we had included in the program the numbers 1 and 2, which were put on the stack. This version now expects the user to leave two numbers on the stack before running this program. The program will then add them together, and later divide the result by two (computing the average). | This short program runs the operator + as its first command. Previously, we had included in the program the numbers 1 and 2, which were put on the stack. This version now expects the user to leave two numbers on the stack before running this program. The program will then add them together, and later divide the result by two (computing the average). | ||
Line 61: | Line 91: | ||
Variables are created with the comand '' | Variables are created with the comand '' | ||
- | < | + | < |
+ | | ||
+ | »</ | ||
The program above stores the number 4 in a variable named '' | The program above stores the number 4 in a variable named '' | ||
to be put on the stack, to be used by the '' | to be put on the stack, to be used by the '' | ||
Line 357: | Line 389: | ||
will leave the numbers 1,3,5,7,9 on the stack. | will leave the numbers 1,3,5,7,9 on the stack. | ||
+ | |||
+ | The behavior of the '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | When using '' | ||
+ | |||
+ | When '' | ||
+ | |||
+ | < | ||
+ | « 1 ' | ||
+ | 1 10 FOR J | ||
+ | J | ||
+ | IF J 4 == THEN -1 ' | ||
+ | IF J 1 == THEN 2 ' | ||
+ | S STEP | ||
+ | » | ||
+ | </ | ||
+ | |||
+ | In the above example '' | ||
+ | The loop will now begin counting down 3,2,1 and the second '' | ||
+ | |||
+ | When '' | ||
+ | |||
+ | < | ||
+ | « A 10 FOR J J NEXT » | ||
+ | </ | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Notice that despite the starting value being higher than the end value and the loop using '' | ||
+ | |||
+ | Now let's review a loop using '' | ||
+ | |||
+ | < | ||
+ | « A 10 FOR J J 1 STEP » | ||
+ | </ | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Notice that while the loop appears to count up by looking at the code, the condition A>10 causes the loop to be counting down because the start value is larger than the end value, and therefore it will only end when J<10, regardless of the magnitude and direction of the step. Since the step increases the counter, the loop never ends. | ||
+ | |||
+ | In some situations it is desirable to keep the direction of the loop fixed regardless of the start/end values. The '' | ||
+ | * The direction of the loop is fixed and known, regardless of the values the limits might have at run time. | ||
+ | * The loop has one additional check when the word '' | ||
+ | |||
+ | Back to the example used above for '' | ||
+ | |||
+ | < | ||
+ | « A 10 FORUP J J NEXT » | ||
+ | </ | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | It differs from using '' | ||
+ | |||
+ | On the other end: | ||
+ | < | ||
+ | « A 10 FORDN J J -1 STEP » | ||
+ | </ | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Note that '' | ||
+ | |||
+ | Summarizing, | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
=== DO loops === | === DO loops === | ||
The syntax for this construct is: | The syntax for this construct is: |