Hat Trick

From Esolang
Jump to navigation Jump to search
Hat Trick
Paradigm(s) dataflow
Designed by cyanidesDuality
Appeared in 2020
Computational class unknown
Reference implementation On Github
Major implementations None
File extension(s) .htrck

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.

Syntax

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.

Semantics

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

would print 5 to console. Writing

"Returning with: " stdin "" coerce + => stdout

would print 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.

Operator Arity Function
+ 2 Performs addition between numbers, concatenation between strings, and logical A OR B between booleans.
- 2 Performs subtraction between numbers, is undefined for strings, and logical A AND NOT B between booleans.
* 2 Performs multiplication between numbers, is undefined for strings, and logical A AND B between booleans. Additionally, string number * returns string repeated number times, like in Python.
/ 2 Performs divison between numbers, and is undefined otherwise. If hitting 0/0 or val/0 respectively (where val is nonzero), the outputs will be "INDETERMINATE" and "UNDEFINED" respectively.
abs 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 A XOR B between booleans.
? 3 A if C else B. The last argument must be a boolean, and otherwise is unrestricted.
<,>,<=,>= 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.
! 1 Logical NOT on booleans. Undefined otherwise.
coerce 2 Coerces the type of A to the type of B; this includes evaluating numbers, checking if a value is truthy/falsy, and getting string representations.
find 2 Python A.find(B) for strings, undefined otherwise.
slice 3 Python C[A:B] for strings, undefined otherwise.
len 1 Gets length of strings, undefined otherwise.
stdin 0 Gets input from the user.

Implementations

In Python 3, at https://github.com/wompking/hattrick.

See Also