HGFTSNOA

From Esolang
Jump to navigation Jump to search

HGFTSNOA is an esoteric programming language created by User:PythonshellDebugwindow.

Keywords

HGFTSNOA uses eight keywords. Each keyword has a normal identifier and a wimpmode identifier. Valid HGFTSNOA programs can only contain normal identifiers.

Normal Meaning/Usage Wimpmode
T Begin function body HELLO
TT End function body GOODBYE
TTT Begin if statement IF
TTTT Returning from functions; string indexing IT
TTTTT Function declarations; assignment; equality IS
TTTTTT End if statement THEN
TTTTTTT Begin else branch OR
TTTTTTTT Addition; string concatenation AND

These keywords are the origin of the name HGFTSNOA: each of its letters is derived from a key letter (e.g. H for HELLO, N for THEN) of each keyword's wimpmode identifier in order.

Character set

HGFTSNOA uses the following 16 characters: Tt0123456789"-(space)(newline). No other character can occur in a valid HGFTSNOA program.

Datatypes

HGFTSNOA uses two datatypes. Integers are unbounded integers, while strings are strings of arbitrary length.

Datatype Regex Example
Integer [0-9]+ 123
String ".*?" "Tt123"

There are no escape codes in HGFTSNOA strings; as such, they can only contain 15 characters (HGFTSNOA's 16 characters minus the double quote ", which cannot be escaped) without using the string expansion operator -.

Syntax

Main function

The nameless "main" function is the program's entry point. It cannot be called by any function within the program. Its statements are located between the T and TT keywords:

T
  ...
TT

The main function must be defined exactly once in every program.

Function declaration

To declare a function called fname which takes no arguments, the following syntax is used:

fname TTTTT T
  ...
TT

fname must consist of the lowercase letter t repeated one or more times. Multiple functions cannot have the same name.

Function declarations cannot be nested.

Function arguments and returning

When declaring a function, arguments are placed between the TTTTT and T keywords. For example, the following function takes two arguments (and does absolutely nothing):

t TTTTT tt ttt T
TT

The TTTT keyword can be used to return a value from within a function. As an example, the following function takes two arguments (ttt and tttt) and returns their sum. It first adds ttt and tttt using the TTTTTTTT keyword, and then returns the calculated value.

t TTTTT ttt tttt T
  TTTT ttt TTTTTTTT tttt
TT

Function calls use Polish notation: the name of the function being called precedes its arguments. As an example, the following function takes four arguments and uses the t function (defined in the previous example) to calculate their sum:

tt TTTTT ttt tttt ttttt tttttt T
  TTTT t t ttt tttt t ttttt tttttt
TT

It first adds its first two arguments, then its second two, and then returns the sum of those two sums.

The main function can return a value, but the returned value will be ignored.

Assignment

To set the variable vname to the value value, the following syntax is used:

vname TTTTT value

Like the name of a function, vname must consist of the lowercase letter t repeated one or more times. The name of a variable or function parameter cannot be the same as that of a function. Variables and function parameters are not constant, and can be assigned new values after being initialised. Attempting to reference an undefined variable will cause a runtime error.

If statements

To create an if statement whose condition is value, the following syntax is used:

TTT value
  code if true...
TTTTTT

The keyword TTTTTTT can be used to specify an "else" branch:

TTT value
  code if true...
TTTTTTT
  code if false...
TTTTTT

An if statement will run if and only if its condition is truthy, i.e. not equal to the empty string or 0. If an "else" branch is provided, it will be executed only if the statement's condition was not truthy.

Operators

There are 4 operators in HGFTSNOA: - (minus), TTTT (IT), TTTTT (IS), and TTTTTTTT (AND). They all have the same precedence and are evaluated from left to right. Function calls have a lower precedence, so that 1 TTTTTTTT tt 2 3 4 TTTTTTTT 5 would in many languages be 1 + tt(2, 3, 4 + 5).

Minus operator

The unary - operator performs two functions: negation and string expansion.

With an integer operand, - is the negation operator. It subtracts its operand from zero and evaluates to the result.

With a string operand, - is the expansion operator. It performs string expansion on its operand and evaluates to the result; see #String expansion for more information.

IT operator

TTTT also serves two functions. As mentioned earlier, it allows functions to return a value; when between two values, however, it is instead the binary indexing operator.

The indexing operator requires a string as its left operand and an integer as its right operand. If its operands are not of the right type, an error will be raised. The operator performs string indexing using its operands and evaluates to the result; see #String indexing for more information.

TTTT will be parsed as a return statement when it is after one of the keywords T, TTTTTTT, and TTTTTT; in all other cases, it will be parsed as the indexing operator.

IS operator

When not used for assignment, the TTTTT operator is the binary equality operator. It returns 1 if and only if its two operands have the same type and value; otherwise, it returns 0.

TTTTT will only be evaluated as the assignment operator if all of the following conditions are true:

  • It is the second token in its expression.
  • Its left operand is an identifier.
  • It is not in the condition of an if statement.
  • It is not in an argument in a function call.
  • It is not part of a return statement.

Otherwise, it will be evaluated as the equality operator.

AND operator

The TTTTTTTT operator serves two functions: addition and concatenation.

With an integer as its left operand, TTTTTTTT is the binary addition operator. It evaluates to the sum of its left operand and its right operand. If its right operand is a string, it is converted to an integer; if it is not a valid integer (at least one of its characters is not one of the digits 0-9), it will be treated as zero.

With a string as its left operand, TTTTTTTT is the binary concatenation operator. It concatenates its right operand to its left operand and returns the result. If its right operand is an integer, it is converted to a string before being concatenated.

String indexing

TTTT can function as the string indexing operator. Given a string S as its LHS and an integer N as its RHS, the operator evaluates to the (0-based) Nth character of S. If N is negative, then the operator will instead evaluate to the (1-based) Nth character from the end of the string. If N is out of bounds, the operator will evaluate to the empty string.

For example, the following program:

T
  "0123456789" TTTT 7
  "12345" TTTT -2
TT

will print:

7
4

String expansion

If the operand of the - operator is a string, the operator will perform string expansion. This means that it will evaluate to a version of the string in which all keywords are replaced with their wimpmode equivalents, and all variables are replaced with their values.

For example, the following program:

T
  t TTTTT 123
  -"T t"
  -"Tt"
  -"T" TTTTTTTT " t"
  tt TTTTT "T" TTTTTTTT " t"
  -tt
TT

will print:

HELLO 123
HELLO123
HELLO t
HELLO 123

String expansion is performed from left to right, and longer keywords and variable names are replaced before shorter ones. This means that the following program:

T
  tttt TTTTT 42
  -"ttttttt"
  t TTTTT "5"
  -"ttttttt"
  -"TTTTTTTTT"
TT

will print:

42ttt
42555
ANDHELLO

Expansion is not recursive. For example, the following program will print tt and not 2:

T
  tttt TTTTT "ttt"
  ttt TTTTT "tt"
  tt TTTTT "2"
  -tttt
TT

String expansion is the only way to output letters other than T and t. It allows you to output the letters A, B, D, E, F, G, H, I, L, N, O, R, S, and Y.

Scope

All functions are available at all places in the program; the sole exception to this is the main function, which is nameless and thus cannot be called in the program. By contrast, variables and function parameters are not subject to any "global scope", and they are only available in the function in which they were defined.

For example, the following program will output the number 123:

T
  t TTTTT 123
  tt
  t
TT
tt TTTTT T
  t TTTTT 456
TT

And the following program will raise an error, since the variable t is not available within tt:

T
  t TTTTT 42
  tt
TT
tt TTTTT T
  t
TT

Input and output

Output in HGFTSNOA is implicit. All values which are not used in expressions or returned by functions are automatically output, followed by a newline.

There is no way to request user input in HGFTSNOA.

Examples

HELLO GLOBE

The letter W cannot be output, nor can any lowercase letters; as a result, this is pretty much the closest HGFTSNOA can come to the classic "Hello, world!" program.

T
  t TTTTT -"TT" TTTT 0
  tt TTTTT -"T" TTTT 2
  ttt TTTTT -"TTTTTTT" TTTT 0
  tttt TTTTT -"TT" TTTT 4
  ttttt TTTTT -"T" TTTT 1
  -"T " TTTTTTTT t TTTTTTTT tt TTTTTTTT ttt TTTTTTTT tttt TTTTTTTT ttttt
TT

Truth-machine

Initialise the variable t to 0 or 1 as appropriate.

T
  t TTTTT (0 or 1)
  TTT t TTTTT 1
    tt
  TTTTTTT
    0
  TTTTTT
TT
tt TTTTT T
  1
  tt
TT

Output all characters of a string separately

The string in question is located on the second line.

T
  t TTTTT "0123456789T-t"
  tt 0 t
TT
tt TTTTT ttt t T
  tttt TTTTT t TTTT ttt
  TTT tttt TTTTT ""
  TTTTTTT
    tttt
    tt ttt TTTTTTTT 1 t
  TTTTTT
TT

Fibonacci sequence

This program outputs the first 15 Fibonacci numbers. The number of numbers can be adjusted by adjusting the number on the second line.

T
  tt 15
TT

tt TTTTT t T
  ttttttt 0 t 0 1
TT

ttttttt TTTTT ttt tttt ttttt tttttt T
  TTT ttt TTTTT tttt
  TTTTTTT
    ttttt
    ttttttt ttt TTTTTTTT 1 tttt ttttt TTTTTTTT tttttt ttttt
  TTTTTT
TT

Ackermann function

The inputs to the function are the two numbers on the second line.

T
  t 4 0
TT
t TTTTT tt ttt T
  TTT tt TTTTT 0
    TTT 0 TTTTTT
    TTTT ttt TTTTTTTT 1
  TTTTTT
  TTT ttt TTTTT 0
    TTT 0 TTTTTT
    TTTT t tt TTTTTTTT -1 1
  TTTTTT
  TTTT t tt TTTTTTTT -1 t tt ttt TTTTTTTT -1
TT

Digital root calculator

The number whose digital root is to be calculated is on the second line.

T
  t "" TTTTTTTT 34758
TT
t TTTTT tttt T
  tttt TTTTT "" TTTTTTTT tt tttt 0 0
  TTT tttt TTTT 1
    t tttt
  TTTTTTT
    0 TTTTTTTT tttt
  TTTTTT
TT
tt TTTTT tttt ttttt tttttt T
  TTT tttt TTTT ttttt
    ttttttt TTTTT tttt TTTT ttttt
    TTT 0 TTTTTT
    TTTT tt tttt ttttt TTTTTTTT 1 tttttt TTTTTTTT ttttttt
  TTTTTTT
    TTTT tttttt
  TTTTTT
TT

Factorial calculator

This program calculates the factorial of the number on the second line.

T
  t 30
TT
t TTTTT ttt T
  TTT ttt TTTTT 0
    0
  TTTTTTT
    ttttttt ttt 1 1
  TTTTTT
TT
ttttttt TTTTT ttt tttt ttttt T
  TTT ttt TTTTT tttt
    TTT 0 TTTTTT
    TTTT tt ttttt tttt 0 0
  TTTTTTT
    TTTT ttttttt ttt tttt TTTTTTTT 1 tt ttttt tttt 0 0
  TTTTTT
TT
tt TTTTT ttt tttt ttttt tttttt T
  TTT ttttt TTTTT tttt
    TTT 0 TTTTTT
    TTTT tttttt
  TTTTTTT
    TTTT tt ttt tttt ttttt TTTTTTTT 1 tttttt TTTTTTTT ttt
  TTTTTT
TT

FISSBOSS

HGFTSNOA cannot output the letters U or Z.

T
  tt TTTTT -"TTTTT" TTTT 1
  ttt TTTTT -"TTT" TTTT 1 TTTTTTTT -"TTTTTtt"
  tttt TTTTT -"T" TTTT 4 TTTTTTTT tt TTTTTTTT tt
  tttt TTTTT -"TT" TTTT 4 TTTTTTTT tttt
  t 1 100 1 1 ttt tttt
TT
t TTTTT tt ttt tttt ttttt tttttt ttttttt T
  TTT tttt TTTTT 3
    TTT ttttt TTTTT 5
      tttttt TTTTTTTT ttttttt
      ttttt TTTTT 0
    TTTTTTT
      tttttt
    TTTTTT
    tttt TTTTT 0
  TTTTTTT
    TTT ttttt TTTTT 5
      ttttttt
      ttttt TTTTT 0
    TTTTTTT
      tt
    TTTTTT
  TTTTTT
  TTT tt TTTTT ttt TTTTT 0
    t tt TTTTTTTT 1 ttt tttt TTTTTTTT 1 ttttt TTTTTTTT 1 tttttt ttttttt
  TTTTTT
TT

99 BOTTLES OF BEER ON THE FLOOR

Again, HGFTSNOA can only output certain letters. The following program attempts to work around this limitation while staying as close to the original song as possible.

T
  tttttttttttttttttttt 99
TT

tttttttttttttttttttt TTTTT tt T
  tttttt TTTTT -"T" TTTT 4
  ttttttt TTTTT -"T" TTTT 1
  ttttt TTTTT -"TT" TTTT 4

  tttt TTTTT -"ttttttTTTTTTT"
  tttt TTTTT -"T" TTTT 2 TTTTTTTT tttt
  tttt TTTTT -"TTT" TTTT 1 TTTTTTTT tttt
  tttt TTTTT -"TTTTTT" TTTT 2 TTTTTTTT " " TTTTTTTT tttt
  tttt TTTTT -"T" TTTT 0 TTTTTTTT tttt
  tttt TTTTT -"TTTT" TTTT 1 TTTTTTTT tttt
  tttt TTTTT -"TTTTTT" TTTT 3 TTTTTTTT -" tttt"
  tttt TTTTT -" tttttt" TTTTTTTT tttt

  ttt TTTTT -"TTTTTTT" TTTT 1
  ttt TTTTT ttttt TTTTTTTT ttttttt TTTTTTTT ttttttt TTTTTTTT ttt
  ttt TTTTT -"TTT" TTTT 1 TTTTTTTT -" ttt"
  ttt TTTTT " " TTTTTTTT tttttt TTTTTTTT ttt
  tttttttttt TTTTT -"T" TTTT 2 TTTTTTTT ttttttt
  t TTTTT -"TTTT" TTTT 1
  tttttttttt TTTTT t TTTTTTTT t TTTTTTTT tttttttttt
  tttttttttt TTTTT tttttt TTTTTTTT tttttttttt
  tttttttttt TTTTT -" ttttt" TTTTTTTT tttttttttt

  TTT tt TTTTT 1
    ttttttttttttttt TTTTT ttt TTTTTTTT tttt
    tttttttt TTTTT tttttttttt TTTTTTTT ttt TTTTTTTT tttt
  TTTTTTT
    ttttttttttttttttt TTTTT ttt TTTTTTTT tttt
    ttttttttttttttttt TTTTT tttttttttt TTTTTTTT ttttttttttttttttt
    tttttttttttttttt TTTTT ttt TTTTTTTT tttt
    tttttttt TTTTT -"TTTTT" TTTT 1 TTTTTTTT tttttttttttttttt
    tttttttt TTTTT tttttttttt TTTTTTTT tttttttt
  TTTTTT

  -"tt" TTTTTTTT tttttttt TTTTTTTT " -"
  TTT tt TTTTT 1
    -"tt" TTTTTTTT tttttttttt TTTTTTTT ttt
  TTTTTTT
    ttttttttttt TTTTT -"TTTTT" TTTT 1
    -"tt" TTTTTTTT tttttttttt TTTTTTTT ttttttttttt TTTTTTTT ttt
  TTTTTT

  tttt TTTTT -"TTT" TTTT 1
  ttt TTTTT -"TTTTTTT" TTTT 1 TTTTTTTT -" TTTT" TTTTTTTT " -"
  ttt TTTTT tttt TTTTTTTT ttttttt TTTTTTTT ttt
  ttt TTTTT -"TTTTT" TTTT 1 TTTTTTTT ttt
  ttt TTTTT -"TTTTTT" TTTT 3 TTTTTTTT ttt
  ttt TTTTT -"TTTTTTTT" TTTT 0 TTTTTTTT ttt
  ttt TTTTT -"TTTTTTT" TTTT 1 TTTTTTTT ttt
  ttt TTTTT -"TTTT" TTTT 1 TTTTTTTT ttt
  ttt TTTTT tttttt TTTTTTTT tttt TTTTTTTT -"tttt - " TTTTTTTT ttt
  ttt TTTTT -"TTTTTT" TTTT 3 TTTTTTTT -"ttttttt ttt"
  ttt TTTTT -"TTTT" TTTT 1 TTTTTTTT -" tttttt" TTTTTTTT ttt
  ttt TTTTT -"T" TTTT 2 TTTTTTTT -"TTTttt"
  ttt

  tt TTTTT tt TTTTTTTT -1

  TTT tt
    TTT tt TTTTT 1
      "1" TTTTTTTT ttttttttttttttttt
    TTTTTTT
      -"tt" TTTTTTTT tttttttt
    TTTTTT
    ""
    tttttttttttttttttttt tt
  TTTTTTT
    ttttttttttttttt TTTTT -"TTTTT" TTTT 1 TTTTTTTT ttttttttttttttt
    ttttttttttttttt TTTTT tttttttttt TTTTTTTT ttttttttttttttt
    -"TTTTTT" TTTT 3 TTTTTTTT tttttt TTTTTTTT ttttttttttttttt
  TTTTTT
TT

Computational class

HGFTSNOA is Turing-complete, as it can interpret a Turing-complete variation of brainfuck. See HGFTSNOA/Turing-Completeness proof for more details.

External resources