# Treetree language specification ## Input and Output operations - INP _Input_ **put 1 str** Get a string from the active in-stream (cin is default), terminated by the set in-stream termination character (newline is default) - OUT _Output_ **pop 1 str** Put a string in the active out-stream (cout is default), terminated by the set out-stream termination character (newline is default) - IFI _Input File_ **pop 1 str** **put 1 int** Get a filename, open that file for reading and set the active in-stream to that file. Returns a boolean value with true for succes and false for any error that has occured while trying to open the file. - OFI _Output File_ **pop 1 str** **put 1 int** Get a filename, open that file for writing and set the active out-stream to that file. Returns a boolean value with true for succes and false for any error that has occured while trying to open the file. - IUS _Input User_ Set the active in-stream to cin and if required close the file previously set as the active in-stream. - OUS _Output User_ Set the active out-stream to cout and if required close the file previously set as the active out-stream. - ICH _Input Character_ **pop 1 str** Change the in-stream termination character (newline is default). - OCH _Output Character_ ** pop 1 str ** Change the out-stream termination character (newline is default). ## Tree operations - PSH _Push_ **pop 1 int** Take the root node and its secondary children and put it between the n-th and (n+1)-th primary child of the root node. Negative numbers invoke a PLL with that number's absolute value. - PLL _Pull_ **pop 1 int** Take the n-th node and its secondary children and make it the root node, with the previous root node as its primary child. Negative numbers invoke a PSH with that number's absolute value. - SWP _Swap_ Swap the root node's primary and secondary child. - POP _Pop_ Remove the root node and its secondary children from the tree, making the previous root node's primary child the root node. - DUP _Duplicate_ Copy the root node and its secondary children and make it the root node, making the previous root node the new root node's primary child. - GRB _Grab_ **pop 1 int** Take n generations of primary children of the root node with all of their secondary children and insert them between the root node and the root node's secondary child. - REL _Release_ **pop 1 int** Take n-1 generations of primary children of the root node's secondary child and the root node's secondary child itself with all of their secondary children and insert them between the root node and and the root node's primary child. ## Datatypes - INT _Integer_ **pop 1 datatype** **put 1 int** Convert datatype to integer. When impossible, the NaN type is put instead of an integer. - FLT _Floating point_ **pop 1 datatype** **put 1 flt** Convert datatype to floating point. When impossible, the NaN type is put instead of a floating point. - STR _String_ **pop 1 datatype** **put 1 str** Convert datatype to string. This always succeeds. - NAN _Not a Number_ **put 1 nan** Put a Not a Number type. - LEN _Length_ **pop 1 str** **put 1 int** Put the number of characters in the string. If the datatype is not a string, NaN is put instead of an integer. - SPL _Split_ **pop 1 int** **put 1 str** Split the string at the root node (when the int is popped) in two between the the (n-1)-th and the n-th character from the beginning of the string for positive numbers and between the (length-n-1)-th and the (length-n)-th character for negative values of n. For all values for n that are out of string bounds or if no string was found at all, throw an error. ## Math, Arithmetic and Bitwise operators All operations are applied on the root node and optionaly its primary child as first and second parameter. One of these parameters is allowed to have secondary children, on which the operation is also executed. This allows for a large number of operations in a small number of commands. All math and arithmetic functions work for the integer and floating point type. The operation will return a floating point, when one of the parameters is a floating point. Otherwise it will return an integer. Bitwise operators require integers, although the SUM and PRO operations work on strings as well. Operations on illegal types will throw an error, and math errors will result in the NaN type. (ex. 1/0 -> NaN) - SUM _Sum_ 1. The sum of two numbers. 2. Concatenation of two strings. - DIF _Difference_ The difference between two numbers - PRO _Product_ 1. The product of two numbers 2. A number of times a string concatenated to itself. - QUO _Quotient_ The quotient of two numbers - POW _Power_ The first parameter raised to the second parameter's power. - ROO _Root_ The first parameter's root of the first parameter's degree. - LOG _Logarithm_ The second parameter's log of the first parameter. - MOD _Modulo_ The remainder of a division of the first and second parameter. - SIG _Sigma/Summation_ The sum of the root node and all its secondary children. - PI\_ _Pi/Product_ The product of the root node and all its secondary children. - OR\_ _Bitwise Or_ The bitwise or of two integers. - AND _Bitwise And_ The bitwise and of two integers. - XOR _Bitwise Xor_ The bitwise xor of two integers. - INV _Bitwise Invert_ The bitwise inversion of an integer. - NOT _Not_ A boolean not of an integer. (equal to n<1) - EQU _Equal_ Compare to datatypes and put true when they are equal and otherwise put false. - NEQ _Not Equal_ Compare to datatypes and put true when they are not equal and otherwise put false. - SML _Smaller than_ Compare to datatypes and put true when the first is smaller than the second and otherwise put false. - GRT _Greater than_ Compare to datatypes and put true when the first is greater than the second and otherwise put false. ## Constants and Generated Variables - TAU _Tau_ **put 1 flt** Put the ratio of a circle's circumference to its radius, equal to 2π. - E__ _e_ **put 1 flt** Put Euler's number. - RND _Random_ **put 1 int** Put a random integer between zero and an implementation-defined number, at least 32767, inclusively. ## Loops and Conditions - JMP _Jump_ **pop 1 int** Let the n-th command be the next one. - EXJ _Extended Jump_ **pop 1 int** **put 1 int** Let the n-th command be the next and put back an integer with the position of the command after this EXJ. Useful for functions. - IFJ _If Jump_ **pop 1 int** If the second parameter is true, jump to the first parameters position. - STP _Stop_ Terminate the program ## Debug functions - STK _Stack_ Output the stack without popping anything. ## Literals Integers and floating points are written down as one normally would, accepting an 'e' for an exponent, a minus sign in front for negative and a dot ('.') as the decimal separator. Strings are enclosed in "double quotes" and comments in 'single quotes' or, more preferably, in {braces}. Special characters in strings can be written down as they are, with the sole exception of the double quote ("), which cannot be put in a string literal. Bare literals in the code get pushed into the tree such that the old root node becomes the first child of the newly pushed thing, and thus the newly pushed thing becomes the new root node.