This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
xrpl:rpl_language_overview [2019/07/26 19:52] – add Symbol Aliasing Karsten75 | xrpl:rpl_language_overview [2021/03/24 15:16] (current) – removed Karsten75 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== xRPL - The Language ====== | ||
- | |||
- | <wrap hi> | ||
- | * Creeper World III (CRPL) | ||
- | * Particle Fleet (PRPL) | ||
- | * Creeper World 4 (4RPL) | ||
- | When we talk about common aspects of all the languages, it's easiest to write xRPL where the " | ||
- | |||
- | xRPL programming is similar to programming a HP calculator or Forth language programming. If this is not something you have done, read on for a brief introduction to xRPL and stack-based programming. <wrap info>For a more detailed CRPL explanation, | ||
- | |||
- | An xRPL instruction either use arguments (data) that is on a " | ||
- | |||
- | You can place arguments on the stack by typing them, or executing an instruction that will push arguments on to the stack. For instance, you can type <code prpl>2 4 5</ | ||
- | |||
- | As an illustration, | ||
- | *<code prpl>2 4 5 add</ | ||
- | *<code prpl>2 4 5 | ||
- | add</ | ||
- | | ||
- | Either of the scripts above will read the two most recent arguments on the stack (4 and 5), add them and push the sum on to the stack. After the instruction has completed, there will be two numbers on the stack. The " | ||
- | <code prpl>2 9</ | ||
- | |||
- | < | ||
- | You can find more commands and detailed explanations of them in the [[crpl: | ||
- | </ | ||
- | |||
- | ==== Comments ==== | ||
- | |||
- | Adding comments makes code easier to understand, and sometimes helps the programmer or other readers to grasp complex pieces of logic. Also, after some time interval, it refreshes one's memory about exactly what a certain piece of code was intended to do. | ||
- | |||
- | Comments in xRPL can be either a whole line or a partial line. The comment terminates when a line ends. | ||
- | |||
- | Comments are indicated by the " | ||
- | |||
- | |||
- | <code prpl># This is how we add two of the three numbers | ||
- | # that are on the stack | ||
- | # Below is code # | ||
- | 2 4 5 add # adds 4 and 5 to get 9</ | ||
- | |||
- | <wrap important> | ||
- | |||
- | Likewise, note that the most recent item pushed on to the stack will also be the first item to be removed. This is referred to as LIFO (**L**ast **I**n, **F**irst **O**ut) processing. | ||
- | |||
- | ==== Warp Notation ==== | ||
- | |||
- | An extra and optional <wrap hi> | ||
- | |||
- | This operator is called the **warp** | ||
- | |||
- | <code prpl>3 4 add </ | ||
- | |||
- | This means to push 3 to the stack, push 4 to the stack, then to call <wrap round box> | ||
- | |||
- | This means two items on the stack before the operation, and one item on the stack afterwards. | ||
- | This is all xRPL (or RPL, or Forth...) standard stuff and the primary principle of the language. | ||
- | |||
- | Introducing the Warp operator. | ||
- | |||
- | <code prpl>3 4 add</ | ||
- | can become | ||
- | <code prpl>add (3 4)</ | ||
- | |||
- | The open parenthesis <wrap round box> | ||
- | |||
- | Here the warp operator is used to make the code slightly more readable. | ||
- | <code prpl>3 add(4)</ | ||
- | Here, the <wrap round box> | ||
- | |||
- | Take a second example: | ||
- | |||
- | <code prpl> | ||
- | # CurrentCoords obtains the (X, y) location and places it on the stack | ||
- | # GetCreeper uses the (X,Y) coordinates to look at a cell on a map | ||
- | # and returns the amount of creeper on that cell | ||
- | # The " | ||
- | # other words, creeper exists in a significant amount) and if true | ||
- | # displays a message in the trace output. | ||
- | |||
- | CurrentCoords GetCreeper 1 gt if | ||
- | " | ||
- | endif | ||
- | </ | ||
- | |||
- | The comments in the sample above explains what we're trying to achieve with this code snippet. | ||
- | |||
- | It uses the current coordinates, | ||
- | |||
- | Because of the way the stack works, you have to push two coordinates to the stack first (<wrap round box> | ||
- | |||
- | That's xRPL in a nutshell. | ||
- | |||
- | Using **warp notation**, we can make this slightly more readable. | ||
- | |||
- | <code prpl> | ||
- | # CurrentCoords obtains the (X, y) location and places it on the stack | ||
- | # GetCreeper uses the (X,Y) coordinates to look at a cell on a map | ||
- | # and returns the amount of creeper on that cell | ||
- | # The " | ||
- | # other words, creeper exists in a significant amount) and if true | ||
- | # displays a message in the trace output. | ||
- | |||
- | if ( GetCreeper(CurrentCoords) | ||
- | Trace (" | ||
- | endif | ||
- | </ | ||
- | Notice that spaces before or after a warp operator <wrap round box> | ||
- | |||
- | Note also that this syntax is totally optional and can be intermixed with standard RPL notation as seems appropriate. | ||
- | <code prpl> | ||
- | 7 ->x | ||
- | ->x(7) | ||
- | </ | ||
- | Both the above statements assign 7 to the variable " | ||
- | |||
- | ==== Symbol Aliasing ==== | ||
- | |||
- | Many arithmetic operators | ||
- | |||
- | ^ Operator | ||
- | | ADD | " | ||
- | | SUB | " | ||
- | | MUL | " | ||
- | | DIV | "/" | ||
- | | MOD | " | ||
- | | AND | "&&" | ||
- | | OR | " | ||
- | | NOT | " | ||
- | | POW | " | ||
- | | GT | ">" | ||
- | | GTE | "> | ||
- | | LT | "<" | ||
- | | LTE | "< | ||
- | | EQ | " | ||
- | | NEQ | " | ||
- | |||
- | ==== Code translator ==== | ||
- | [[https:// | ||
- | |||
- | For those struggling to master xRPL's post-fix format, this may be a useful tool. It can be obtained from [[https:// | ||
- | |||
- | Here are some sample translations, | ||
- | |||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | **mplLang code** | ||
- | <code c> | ||
- | x = 2 + 2 * 2; | ||
- | </ | ||
- | </ | ||
- | <WRAP half column> | ||
- | **xRPL translated code** | ||
- | <code 4rpl> | ||
- | 2 2 2 mul add ->x | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | <code c> | ||
- | z = f(x, y); | ||
- | </ | ||
- | </ | ||
- | <WRAP half column> | ||
- | <code 4rpl> | ||
- | z = f(x, y); | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | <code c> | ||
- | [x, y] = CurrentCoords(); | ||
- | </ | ||
- | </ | ||
- | <WRAP half column> | ||
- | <code 4rpl> | ||
- | CurrentCoords ->y ->x | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | <code c> | ||
- | if(a<b && (c+1 == -c)) [a, b, c] = 1, 2.0, 3.14; | ||
- | </ | ||
- | </ | ||
- | <WRAP half column> | ||
- | <code 4rpl> | ||
- | <-a <-b lt <-c 1 add <-c neg eq and if | ||
- | 1 2 3.140000 ->c ->b ->a | ||
- | endif | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | <code c> | ||
- | do (1 .. 42){ | ||
- | a = refRead(" | ||
- | refWrite(7*(3.14+i), | ||
- | } | ||
- | </ | ||
- | </ | ||
- | <WRAP half column> | ||
- | <code 4rpl> | ||
- | 42 1 do | ||
- | " | ||
- | 7 3.140000 i add mul " | ||
- | loop | ||
- | </ | ||
- | </ | ||
- | </ |