Tea

From Esolang
Jump to navigation Jump to search
Tea
Paradigm(s) Imperative
Designed by User:waffelz
Appeared in 2025
Memory system Stack-based
Computational class Unknown computational class
Reference implementation See [1]
File extension(s) *.tea

Tea is a minimal stack-based esolang invented by User:waffelz on November 25th, 2025. It is titled "Tea" because he really likes tea.

Language overview

Tea operates on two separate stacks, called the primary and secondary stacks. Operations on the secondary stack are quite limited, and it is mainly intended for temporary storage for important values.

Symbol Function Needed Elements Notes
N Push a number to the stack. 0 Can be any integer literal.
: Duplicate the top element of the stack. 1 N/A
% Swap the top two elements of the stack. 2 N/A
! Pop the top element of the stack. 1 N/A
(...) Defines a codeblock 0 N/A
* Pop the top value of the stack and run the given code block that many times. 1 Will raise an error if there is not a code block immediately after.
+ Increment the top element of the stack. 1 N/A
- Decrement the top element of the stack. 1 Will raise an error if trying to decrement 0.
{ Pop the top element of the primary stack and push it to the secondary stack. 1 N/A
} Pop the top element of the secondary stack and push it to the primary stack. 1 (secondary) N/A
@ Run the given code block until the top of the second stack is zero, without popping it. 1 (secondary) Will raise an error if there is not a code block immediately after.

Any character not in the above table will raise an error.

Tea has a twenty-one-character alphabet (1234567890:%!*()+-{}@) and has ten commands (N:%!*+-{}@).

Errors

Tea will error if any of these conditions is met:

  • The program contains mismatched parentheses.
  • The program contains a symbol not in the Tea alphabet.
  • The program contains an empty code block.
  • The program contains a * or @ symbol that is not directly followed by a left parenthesis.
  • The - command is used when the top of the stack is zero.
  • An instruction is executed when the stack does not have enough elements.

Semantics

Because consecutive digits are treated as one number and whitespace is not allowed in any form, pushing multiple numbers in a row is a bit more complex than in other languages. In Tea, you have to include a noop in between each number. Something like :!, +-, or %% can be used, but the last one only works if there is at least 2 elements on the primary stack.

Tea has no input or output commands. The "output," similar to a Turing machine, is the final state of the two stacks. Inputs are gathered via editing the program.

Code snippets

Long code snippets have been split by every 100 characters to improve readability. These linefeeds must be removed to make the program executable.

Numbers

This snippet adds the top two numbers:

*(+)

This snippet subtracts the top two numbers, but errors if the top number is larger than the second:

*(-)

This snippet implements saturated subtraction (max(0, a-b)):

*(::*(!1):{*(-)}1%*(-)!)

This snippet multiplies the top two numbers:

0{*(:*(}+{))!}

This snippet divides the top two numbers:

:{%:{%:{%:{%*(::*(!1):{*(-)}1%*(-)!):*(!1)1%*(-)}}%*(::*(!1):{*(-)}1%*(-)!):*(!1)1%*(-)1%*(-)%1%*(-)
*(+):*(!1)1%*(-)}}{{0{}}}:{%:{%*(::*(!1):{*(-)}1%*(-)!):*(!1)}%}%{@(:{*(::*(!1):{*(-)}1%*(-)!)}{{+}}
:{%:{*(::*(!1):{*(-)}1%*(-)!):*(!1)1%*(-):*(!1)}%}%}!{)!!}!%*(+)

Booleans

Booleans can be represented on the stack using 0 for false and 1 for true. All below snippets assume the top two elements on the primary stack are Booleans. A NOT gate can be implemented with this snippet that calculates 1-A:

1%*(-)

Using this, an if-else statement can then be represented using this snippet:

:{*(true)}1%*(-)*(false)

This snippet coerces an integer to a Boolean such that any nonzero number becomes 1, and 0 stays 0:

:*(!1)

An OR gate can be implemented with this snippet that calculates min(1,A+B):

*(+):*(!1)

Using the fact that A AND B is equal to NOT(NOT(A) OR NOT(B)), an AND gate can be implemented using this snippet:

1%*(-)%1%*(-)*(+):*(!1)1%*(-)

Other Boolean operators are easy to derive using the above snippets.

Examples

In the below programs, replace all instances of N with a number of your choice.

Factorial

N::*(!1)1%*(-):{*(!1)}1%*(-)*(::{*(:-)!}-*(0{*(:*(}+{))!}))

A+B problem

N:!N*(+)

Truth machine

N:!0%*(!1{@(1))

Computational class

Tea is Turing complete, as it can be compiled from 3 cell brainfuck. The most easy translations are given below:

start program with 0{0{0{
+ -> }+{
- -> }-{
[...] -> @(...)

The arrows are slightly harder:

When the pointer moves between the two edge cells, do }}}{{{
And when the pointer moves between an edge cell and the middle cell, do }}{{

Note: this proof assumes that the @(...) only checks for zero at the start of the loop. This does not work otherwise.

External resources