|Reference implementation||On Github|
Hat Trick, devised by cyanidesDuality in 2020, is an assignment-based Esoteric programming language implementing a version of call/cc.
Data Types and Variables
Hat Trick has four data types, being the number, the string, the boolean, and null:
Number: 10 Number: -10 Number: 10.5 Number: .5 String: "beans are cool!" String: 'hello world' NOT a string: "oops' NOT a string: 'oops', i did it again' String: 'yay,\' fixed now' Boolean: True Boolean: False Null: #yes there's literally just nothing there
These can be stored inside variables, which are created when written to. There are no restrictions on variable identifiers.
Every line of a Hat Trick program is of a specific format:
<expression> => <variable name>|stdout OR => [<entrance variable name>|<exit variable name>]
Everything on a line following a
# is ignored as a comment.
See the Expressions and Operators section regarding syntax of expressions.
Lines of code in Hat Trick that follow the first format are for computing/copying things. A very simple cat program would be:
stdin => stdout
because the first half of the line is evaluated, taking input from the user, and then the value is pushed to the second half of the line, outputting the value.
See the Expressions and Operators section regarding the semantics of expressions.
stdout is not a proper variable and can only appear on the RHS of an assignment.
Lines of the second format define a hat pair. Hat pairs are the defining feature of Hat Trick; they allow for time travel and loops. When a value is read from the variable defined as a hat's exit, the program's internal state is cached. When a value is then written to the variable defined as that same hat's entrance, the interpreter checks if the value most recently retrieved from the exit variable was the same as the value written to the entrance variable. If so, execution continues as normal. If not, the cached internal state is reset to the cached state and the exit variable is given the value assigned to the entrance variable that triggered the rewind. Execution then continues from there. For example:
=> [ent|ext] 2 => ext ext => stdout 3 => ent
would print 2, and then 3.
Writing to the exit variable of a hat pair works as expected; the value inside the exit simply changes. When a value is read from the exit hat without anything being put into the entrance hat, null is produced.
Expressions and Operators
The first half of an "assignment" command in Hat Trick is the expression; it takes in variable names and values, and evaluates it based on RPN arithmetic. For example:
3 2 + => stdout
5 to console. Writing
"Returning with: " stdin "" coerce + => stdout
Returning with: and then whatever you input the program.
The following is a table of operators in Hat Trick. Any operator will error if called on null.
||2||Performs addition between numbers, concatenation between strings, and logical |
||2||Performs subtraction between numbers, is undefined for strings, and logical |
||2||Performs multiplication between numbers, is undefined for strings, and logical |
||2||Performs divison between numbers, and is undefined otherwise. If hitting |
||1||Absolute value of numbers. Undefined otherwise.|
||2||Performs modulus between numbers, and is undefined otherwise.|
||2||Performs exponentiation between numbers, is undefined for strings, and logical |
||2||Performs magnitude comparison. Outputs a boolean, takes in numbers, undefined otherwise.|
||2||Checks for equality and inequality; for more details, use the Python definition.|
||2||Coerces the type of |
||1||Gets length of strings, undefined otherwise.|
||0||Gets input from the user.|
In Python 3, at https://github.com/wompking/hattrick.