User Tools

Site Tools


xrpl:rpl_language_overview

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
xrpl:rpl_language_overview [2019/05/05 11:12] – [Warp Notation] Intermediate save Karsten75xrpl:rpl_language_overview [2019/12/02 09:14] Karsten75
Line 1: Line 1:
 ====== xRPL - The Language ====== ====== xRPL - The Language ======
-There are currently 3 scripting languages, one each for+ 
 + 
 +<wrap hi>xRPL</wrap> ((There are currently 3 scripting languages, one each for
   * Creeper World III (CRPL)   * Creeper World III (CRPL)
   * Particle Fleet (PRPL)   * Particle Fleet (PRPL)
   * Creeper World 4 (4RPL)   * Creeper World 4 (4RPL)
-When we talk about common aaspects of all the languages, it's easiest to write xRPL where the "x" denotes commonality.  +When we talk about common aspects of all the languages, it's easiest to write xRPL where the "x" denotes commonality. )) is a stack-based, Reverse Polish Notation language. There, now that we've said it, what does it mean?
- +
-xRPL is a stack-based, Reverse Polish Notation language. There, now that we've said it, what does it mean?+
  
-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, see the [[crpl:crpltutorial|Tutorial]].</wrap>+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, see the [[crpl:crpltutorial|CRPL Tutorial]].</wrap>
  
-An xRPL instruction either use arguments (data) that is on a "stack", or place arguments on the "stack". Arguments on the stack are in LIFO (**L**ast **I**n, **F**irst **O**ut) order. That means the argument tht was placed on the stack last will be the first to be retrieved. Imagine this as a stack of coins. If you put a coin on the top, then the first coin to be taken off will also be the one that was last placed on the stack. +An xRPL instruction either use arguments (data) that is on a "stack", or place arguments on the "stack". Arguments on the stack are in LIFO (**L**ast **I**n, **F**irst **O**ut) order. That means the argument taht was placed on the stack last will be the first to be retrieved. Imagine this as a stack of coins. If you put a coin on the top, then the first coin to be taken off will also be the one that was last placed on the stack. 
  
 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</code> and then these three numbers will be pushed onto the stack when your script executes. 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</code> and then these three numbers will be pushed onto the stack when your script executes.
Line 26: Line 26:
 </note> </note>
  
-===== Comments =====+==== 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. 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.
Line 44: Line 44:
 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. 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 =====+==== Warp Notation ====
  
 An extra and optional <wrap hi>operator</wrap> ((In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example x is an arithmetic operator that represents multiplication. In computer programs, one of the most familiar sets of operators, the Boolean operators, is used to work with true/false values.)) change how you can choose to write the syntax.  It doesn't change anything about the stack, or how xRPL works.  It only gives you a syntax alternative that can make things easier to read in some cases. An extra and optional <wrap hi>operator</wrap> ((In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example x is an arithmetic operator that represents multiplication. In computer programs, one of the most familiar sets of operators, the Boolean operators, is used to work with true/false values.)) change how you can choose to write the syntax.  It doesn't change anything about the stack, or how xRPL works.  It only gives you a syntax alternative that can make things easier to read in some cases.
Line 61: Line 61:
 <code prpl>3 4 add</code>  <code prpl>3 4 add</code> 
 can become can become
-<code>add (3 4)</code>+<code prpl>add (3 4)</code>
  
 The open parenthesis <wrap round box>(</wrap> means to warp the all arguments between it and the closing parenthesis <wrap round box>)</wrap> to **before** the command during compilation.  This is merely a syntax trick to improve readability of some code and has no run-time penalty. It is resolved during compilation, when the  compiler literally //warps// the 3 and 4 from inside the parenthesis to the front of the <wrap round box>add</wrap> operation when the code is compiled.  The open parenthesis <wrap round box>(</wrap> means to warp the all arguments between it and the closing parenthesis <wrap round box>)</wrap> to **before** the command during compilation.  This is merely a syntax trick to improve readability of some code and has no run-time penalty. It is resolved during compilation, when the  compiler literally //warps// the 3 and 4 from inside the parenthesis to the front of the <wrap round box>add</wrap> operation when the code is compiled. 
Line 106: Line 106:
 endif endif
 </code> </code>
-Notice that spaces before or after a warp operator ( which are parentheses) don't matter.  You can put spaces, or you can bump the warp operator up right next to something else.+Notice that spaces before or after a warp operator <wrap round box>(</wrap> and <wrap round box>)</wrap> don't matter.  You can put spaces, or you can bump the warp operator up right next to something else.
  
 Note also that this syntax is totally optional and can be intermixed with standard RPL notation as seems appropriate.  For instance, assignments still look better without warping. Note also that this syntax is totally optional and can be intermixed with standard RPL notation as seems appropriate.  For instance, assignments still look better without warping.
-<code> +<code prpl
-7 ->x</code> +7 ->x 
-This means to assign 7 to the variable "x"Or "seven goes into x". +->x(7) 
-So does this: +</code> 
-<code>->x(7)</code>+Both the above statements assign 7 to the variable "x"Choose which format you prefer and you think makes your code most readable - then be consistent. :)
  
-Like any language, you can write some really obfuscated code if you try ([[http://www.ioccc.org/years.html]]).+==== Symbol Aliasing ====
  
-For instance take this clean piece of code: +Many arithmetic operators  can be substituted with shorthand symbols **(4RPL only)**.  
-<code+ 
-CurrentCoords GetCreeper 1 gt if +^ Operator ^ Symbol ^ 
-   CurrentCoords -10 SetCreeper +| ADD | + |  
-endif+| SUB | - |  
 +| MUL | * |  
 +| DIV | / |  
 +| MOD | % |  
 +| AND | && |  
 +| OR  | %%||%% |  
 +| NOT | ! |  
 +| POW | %%^%% |  
 +| GT  | > |  
 +| GTE | >=|  
 +| LT  | < |  
 +| LTE | <= |   
 +| EQ  | == |  
 +| NEQ | != |  
 +  
 +==== Code translator ==== 
 +[[https://github.com/Arin112}Arin112]] wrote a code translator that can translate in-fix ((Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.)) code to the xRPL post-fix notation.  
 + 
 +For those struggling to master xRPL's post-fix format, this may be a useful tool. It can be obtained from [[https://github.com/Arin112/mplLang|github]] and is accompanied by many samples. Of course, it should be noted that the code and translation will only be up-to-date as long as the repository is maintained.  
 + 
 +Here are some sample translations, taken directly from the GitHub repository: 
 + 
 +<WRAP group
 +<WRAP half column> 
 +**mplLang code** 
 +<code c> 
 +x = 2 + 2 * 2;
 </code> </code>
 +</WRAP>
 +<WRAP half column>
 +**xRPL translated code**
 +<code 4rpl>
 +2 2 2 mul add ->x
 +</code>
 +</WRAP>
 +</WRAP>
  
-Here it is in bizarro form: +<WRAP group> 
-<code> +<WRAP half column> 
-endif(SetCreeper(-10(CurrentCoords(if(gt(1(GetCreeper(CurrentCoords))))))))</code>+<code c
 +z = f(x, y); 
 +</code> 
 +</WRAP> 
 +<WRAP half column> 
 +<code 4rpl> 
 +z = f(x, y)
 +</code
 +</WRAP> 
 +</WRAP>
  
 +<WRAP group>
 +<WRAP half column>
 +<code c>
 +[x, y] = CurrentCoords();
 +</code>
 +</WRAP>
 +<WRAP half column>
 +<code 4rpl>
 +CurrentCoords ->y ->x
 +</code>
 +</WRAP>
 +</WRAP>
  
-Here's the same code with just one oddball warp: +<WRAP group> 
-<code> +<WRAP half column> 
-endif ( if (GetCreeper(CurrentCoordsgt (1)) +<code c
-      SetCreeper(CurrentCoords -10+if(a<b && (c+1 == -c)) [a, b, c] = 1, 2.0, 3.14; 
-)+</code> 
 +</WRAP> 
 +<WRAP half column> 
 +<code 4rpl> 
 +<-a <-b lt <-c 1 add <-c neg eq and if 
 +1 2 3.140000 ->c ->b ->a 
 +endif 
 +</code> 
 +</WRAP> 
 +</WRAP> 
 + 
 +<WRAP group> 
 +<WRAP half column> 
 +<code c> 
 +do (1 .. 42){ 
 +a = refRead("num"); 
 +refWrite(7*(3.14+i), "num"); 
 +
 +</code> 
 +</WRAP> 
 +<WRAP half column> 
 +<code 4rpl> 
 +42 1 do 
 +"num" <-! ->a 
 +7 3.140000 i add mul "num" ->! 
 +loop
 </code> </code>
 +</WRAP>
 +</WRAP>