triad

From Esolang
Jump to navigation Jump to search

triad is a language created by User:Quelklef based heavily off of 3var but with the goal of being cleaner and having a more precise spec. Execution of triad handles only 3 values, called A, B, and R, all of which are unbounded integers. Code consists of two-character instructions. Each instruction is designed to be mnemonic signifying what it does. Typically, the first character represents the "source" of some data and the second character represents the "destination" of some data.

Instructions

Value Manipulation

+a  [A <- A + 1]    Increments A
+b  [B <- B + 1]    Increments B
-a  [A <- A - 1]    Decrements A
-b  [B <- B - 1]    Decrements B
+r  [R <- A + B]    Sets R to the sum of A and B
-r  [R <- A - B]    Sets R to the difference of A and B
*r  [R <- A * B]    Sets R to the product of A and B
/r  [R <- A div B]  Sets R to the quotient of A and B. Precisely, this is floor(A / B), where '/' is mathematical division, not floating-point division.
%r  [R <- A mod B]  Sets R to the modulus of A and B. Precisely, this is A - B * (A div B).
^r  [R <- A pow B]  Sets R to the result of raising A to the Bth power
ar  [R <- A]        Sets R to A
br  [R <- B]        Sets R to B
ra  [A <- R]        Sets A to R
rb  [B <- R]        Sets B to R
0a  [A <- 0]        Sets A to 0
0b  [A <- 0]        Sets B to 0
0r  [R <- 0]        Sets R to 0

Input & Output

rO  Prints R. Note that that is the letter 'O', not zero '0'. (Mnemonic: "R Out")
rA  Prints the ascii value associated with R, treating R as modulo 128. (Mnemonic: "R Ascii")
Nr  Reads a number as input and stores it into R. The number must match the regex [0-9]*. If there is no matching number, stores 0. (Mnemonic: "Number R")
Ar  Reads one character input and stores its Ascii value into R. If there are no characters left, stores 0. (Mnemonic: "Ascii R")

Control Flow

>[  Begins a conditional that executes only if A > B
<[  Begins a conditional that executes only if A < B
=[  Begins a conditional that executes only if A = B
].  Ends a conditional

>{  Begins a loop that executes while A > B
<{  Begins a loop that executes while A < B
={  Begins a loop that executes while A = B
1{  Begins a loop that executes forever
a{  Begins a loop that executes A times
b{  Begins a loop that executes B times
}.  Ends a loop

Comments

((  Begins a comment
))  Ends a comment

Example Programs

Cat:

+b          (( let B=1 ))
<{          (( while A<B, aka while B>0, ))
  Ar rb rA  (( input then output an ascii char and store it in B ))
}.

Truth Machine:

Nr rb           (( input a number into R and B ))
+a              (( let A=1 ))
=[ 1{ rO }. ].  (( if A=B, aka if B=1, infinitely output R=B=1 ))
0r rO           (( otherwise, let R=0 and output R ))

Implementations

A triad implementation is built-in to esoglot. It is written in Haskell.