Grapheme

From Esolang
Jump to navigation Jump to search

Grapheme is an esolang by User:PythonshellDebugwindow where the only valid characters are uppercase Latin letters.

Memory

Grapheme uses an unbounded stack, which can hold integers, strings, and functions. It also uses an untyped variable system, which maps integers/strings/functions to integers/strings/functions. Integers and strings are unbounded, integers are signed, and functions can be of any length.

Syntax

Grapheme programs are made of a series of independent Latin uppercase letters, each one representing a command.

Modes

The default mode is normal mode, which uses the commands described in #Commands. The other modes are intmode, stringmode, and funcmode.

Commands

Normal mode and funcmode

Character Effect
A Pop A, pop B, push A + B
B Pop A, pop B, push A - B
C Pop A, pop B, set var A to B
D Pop A, push var A
E Toggle stringmode
F Toggle intmode
G Pop A, execute A as Grapheme code (starts with and modifies the same stack and variables as the executing program, as if A was typed out as part of the code)
H Toggle funcmode
I Pop A, if A is a function then execute A, else push A back
J Pop A, push A as an integer (integer to integer = nop; string to integer = the same way as described in #intmode, even stopping conversion early if it hits an F; function to integer = the number of commands in the function)
K Pop A, push A back, push A back again
L Pop A, pop B, push A, push B
M Pop A
N Pop A, push A as a string (integer to string = the reverse of the process described in #intmode, but using only A-J where J is 0; string to string = nop; function to string = the function's body, minus its enclosing Hs)
O Pop A, if A is a string then push the length of A, else push A back
P Reverse the stack
Q Pop A, pop B, execute function A if A is a function and B is truthy (falsy values are zero, the empty string, and the empty function)
R Pop A, pop B, push A ÷ B (floor division)
S Pop A, pop B, push A × B
T Pop A, if A is truthy then push the number zero, else push the number one
U Pop A, skip the next instruction if A is falsy
V Pop A, B, skip the next B commands if A is falsy
W Push a user-input string
X Pop A, if A is falsy then skip the next command, else skip the command after that (like an if-else statement)
Y Pop A, output A
Z Pop A, execute function A while the stack is not empty

intmode

Initialize res to 0. For each character c in the "integer" (actually a list of uppercase Latin letters, just like the rest of the program), add (ord(c) - 64) if c != "Z" else 0 to res and multiply res by 10. IMPORTANT: F can't be used, as it terminates intmode and there is no escape character.

stringmode

Initialize res to the empty string. For each character c in the string, add it to res. Push res when done. Uppercase Latin letters are the only valid characters allowed in strings. IMPORTANT: E can't be used, as it terminates stringmode and there is no escape character.

Edge cases

When doing math (+/-/÷/×) on two strings A and B, the following Python code is essentially run: ord((A or "\0")[0]) <OP> ord((B or "\0")[0]), replacing <OP> with the operation. If A is a string but B is not, or vice versa, then only the string will get the ord conversion.

Examples

Hello, World!

All capitals, with no punctuation or whitespace, and no "E" in "HELLO", as these are the constraints of the language.

EHLLOWORLDEY

Infinite cat

WKYHWYHZ

Truth-machine

HFAFYHWJUZFZFY

Using variables

This should print VARIABL:

EVARIABLEEMYVAREKCDY