Chem

From Esolang
Jump to navigation Jump to search

Chem is a language designed to look like instructions in a chemistry textbook. It is surmised to be turing complete but has not been proven. It was created by user KerbalEngineer within the span of a day.

Overview

Chem is a stack-based language written in the form of several commands to "the reader" (the computer) to mix chemicals, clean beakers or perform various tasks in a lab. The language is stack based, but unlike most stack-based languages there are multiple stacks. It can be thought of as a stack-based language with a stack of stacks.

Chem runs on Python, and is thus not directly compiled.

Syntax

In Chem, there are two stacks: the default stack and the stack of local stacks. In the language, this corresponds to the main beaker and all the other beakers respectively.

There are two "scopes" in Chem: the global scope and the local scope. Contrary to most programming languages, this has nothing to do with functions ― the global scope refers to editing the default stack, and the local scope refers to editing a local stack. When the program starts, it is in the global scope by default. When you create new stacks or reference stacks, the program will switch to the local scope. It is possible to switch between scopes as much as is desired.

Instruction Set

Command Use in global scope Use in local scope
value g/ml name Assign value value to name name in the variable stack. Assign value value to name name in the local stack.
Add the name. Add the value assigned to name in the variable stack to the main stack. None
Add anti name. Multiply the value stored at name in the default stack by -1. Multiply the value stored at name in the current local stack by -1.
Any text that contains separate beaker Create a new local stack, and make it the current local stack. Create a new local stack, and make it the current local stack.
Wait time units. Wait time amount of time. Units may be seconds, minutes, or hours. Wait time amount of time. Units may be seconds, minutes, or hours.
Note: text A comment. Has no impact on code. A comment. Has no impact on code.
End of experiment. Ends code. Ends code.
Wash out the original beaker. Clear the original stack. Clear the original stack.
Wash out all beakers. Clear all local stacks (except the original stack). Clear all local stacks (except the original stack).
Dump out the last beakers. Clear the last local stack (and remove it). Clear the last local stack (and remove it).
Dump out beaker number. Clear the local stack at index number (and remove it).

Note that indices start at 1.

Clear the local stack at index number (and remove it).

Note that indices start at 1.

Go to step number. Go to line number. Go to line number.
Mix the contents of all beakers. Append contents of all local stacks to the original stack, and clear all local stacks. Append contents of all local stacks to the original stack, and clear all local stacks.
Dump/pour the contents of beaker number into this beaker. Append contents of local stack number to the original stack, and clear local stack number. Append contents of local stack number to the current local stack, and clear local stack number.
Going back to the original beaker, None Make the original stack the current stack.
Going back to beaker number, Make stack number the current stack. Make stack number the current stack.
Add more name Add one to value stored at name. Add one to value stored at name.
Filter out name Delete the value stored at name (along with the key name itself). Delete the value stored at name (along with the key name itself).
Add number more name Add number to value stored at name. Add number to value stored at name.
Record your findings. Convert all numbers stored in the original stack to characters, then output. Convert all numbers stored in the current local stack to characters, then output.
Share your findings. Output raw stack data from the original stack. Output raw stack data from the current local stack.
Evaluate your findings. Output all values stored in the original stack. Output all values stored in the current local stack.
Essential question: text User will be prompted with text. Their input will be the value assigned to Q+the question number in the original stack.

Note that the question number starts at 1 with the first user input, then 2 with the second, etc.

User will be prompted with text. Their input will be the value assigned to Q+the question number in the current local stack.

Note that the question number starts at 1 with the first user input, then 2 with the second, etc.

If there is not more/equal/less name than/and name2, go to step number. If the value at name is more/equal/less (specified in the statement) than the value at name2 in the original stack, go to line number.

If the not flag is specified, the code will perform the opposite.

If the value at name is more/equal/less (specified in the statement) than the value at name2 in the current local stack, go to line number.

If the not flag is specified, the code will perform the opposite.

Note that any unrelated text will be ignored.

Examples

Hello World

Chemicals needed:
72 g Ca
101 g Cl
Check to confirm you have the required chemicals before proceeding.
Chemicals are to be added to a small beaker.
Add the Ca.
Add the Cl.
Now find the following chemicals and mix together in a separate beaker:
108 ml water
108 g calcium
111 g sulfur
32 g carbon
87 ml mercury
111 g salt
114 g zinc
108 g tin
100 ml bromine
Going back to the original beaker,
Dump the contents of beaker 1 into this beaker.
Record your findings.

Hello World #2

First, find these chemicals:
72 g Ca
101 g Cl
Using the main beaker,
Add the Ca.
Add the Cl.
Now find the following chemicals and mix together in a separate beaker:
108 ml water
108 g calcium
111 g sulfur
32 g carbon
87 ml mercury
In another separate beaker, mix the following chemicals:
111 g salt
114 g zinc
108 g tin
100 ml bromine
Mix the contents of all beakers.
Wait 0.1 seconds.
Record your findings.
Go to step 27.