Qwerty Reverse Polish Notation

From Esolang
Jump to navigation Jump to search

Qwerty Reverse Polish Notation is a Reverse Polish notation calculator that is designed to be generally Turing-complete. In Qwerty Reverse Polish Notation (QRPN), almost all opcodes (except literals, references, labels and variables) are as small as 1 keystroke. The opcodes are separated by space (" ").

Quick Reference

  +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
  | ~ not  | ! pop  | @ getn | # putn | $ swap | % mod  | ^ pow  | & and  | * mul  | ( dec  | ) inc  | _ neg  | + add  |
  | ` xor  | 1 ---- | 2 ---- | 3 ---- | 4 ---- | 5 ---- | 6 ---- | 7 ---- | 8 ---- | 9 ---- | 0 ---- | - sub  | = eq   |
  +--------+--------+--------+--------+--------+--------+--------+--------+-----------------+--------+--------+--------+
  | Q      | W      | E 10^x | R root | T atan | Y      | U      | I      | O      | P      | { shl  | } shr  | | or   |
  | q      | w      | e exp  | r sqrt | t tan  | y      | u      | i      | o      | p      | [ flr  | ] ceil | \ roun |
  +--------+--------+--------+--------+--------+--------+--------+--------+-----------------+--------+--------+--------+
  | A      | S asin | D      | F      | G      | H      | J      | K      | L log  | : dup  | "      |########|########|
  | a abs  | s sin  | d      | f      | g      | h      | j      | k      | l ln   | ;      | '      |########|########|
  +--------+--------+--------+--------+--------+--------+--------+--------+-----------------+--------+--------+--------+
  | Z      | X      | C acos | V      | B      | N      | M max  | < lt   | > gt   | ? rand |########|########|########|
  | z      | x exit | c cos  | v      | b bool | n      | m min  | , getc | . putc | / div  |########|########|########|
  +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+

Turing-Complete?

Brainfuck programs can be transformed into Qwerty RPN by using the array technique above. Here is the basic procedure of translating Brainfuck to Qwerty RPN.

The variable i tracks the program counter. The header of the code is therefore 0=i, which initializes it.

  • - and +: Get the value, increase or decrease it, and write back: $i $$ ( $i =$ or $i $$ ) $i =$
  • < and >: Simply decrease or increase the variable I: $i ( =i or $i ) =i
  • Loops ([BODY]): Loops are transformed into goto labels, and the condition is the current memory cell. Let L be a unique label: >L BODY $i $$ <L
  • I/O: , and .: , transforms into , $i =$, and . transforms into $i $$ .

See 99 bottles of beer example, translated from BF.

Examples

Ask user to input 2 numbers, and print its sum.

 @ @ + #

Digital root calculator

 1 @ 1 - + 9 %

Locals-based factorial

 @ =value               // value = input_number();
 1 =result              // result = 1;
 $value =i              // i = value;
 >loop                  // loop:
  $result $i * =result  //   result *= i;
  $i ( =i $i            //   i --;
 <loop                  // if ($i) { goto loop; }
 $result                // return result;

without comments:

 @ =value 1 =result $value =i >loop $result $i * =result $i ( =i $i <loop $result

External resources