This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
xrpl:rpl_language_overview [2019/05/05 14:49] – created/ intermediate save Karsten75 | xrpl:rpl_language_overview [2021/03/24 15:16] (current) – removed Karsten75 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== xRPL - The Language ====== | ||
- | There are currently 3 scripting languages, one each for | ||
- | * Creeper World III (CRPL) | ||
- | * Particle Fleet (PRPL) | ||
- | * Creeper World 4 (4RPL) | ||
- | When we talk about common aaspects of all the languages, it's easiest to write xRPL where the " | ||
- | xRPL is a stack-based, | ||
- | |||
- | 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 | ||
- | < | ||
- | |||
- | 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 (**CurrentCoords** does that), then call **GetCreeper**. | ||
- | |||
- | 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 ( which are parentheses) don't matter. | ||
- | |||
- | Note also that this syntax is totally optional and can be intermixed with standard RPL notation as seems appropriate. | ||
- | < | ||
- | 7 -> | ||
- | This means to assign 7 to the variable " | ||
- | So does this: | ||
- | < | ||
- | |||
- | Like any language, you can write some really obfuscated code if you try ([[http:// | ||
- | |||
- | For instance take this clean piece of code: | ||
- | < | ||
- | CurrentCoords GetCreeper 1 gt if | ||
- | | ||
- | endif | ||
- | </ | ||
- | |||
- | Here it is in bizarro form: | ||
- | < | ||
- | endif(SetCreeper(-10(CurrentCoords(if(gt(1(GetCreeper(CurrentCoords))))))))</ | ||
- | |||
- | |||
- | Here's the same code with just one oddball warp: | ||
- | < | ||
- | endif ( if (GetCreeper(CurrentCoords) gt (1)) | ||
- | SetCreeper(CurrentCoords -10) | ||
- | ) | ||
- | </ |