An interpreter for the esoteric language A=B (pronounced "a to b"), written entirely in Haskell.
A=B is a minimalist esoteric programming language that operates solely through text replacement rules. The program consists of a series of transformation instructions, and an input string is passed in at runtime. The instructions modify the string in a loop until no more transformations can be made.
Each line of an A=B program contains:
string1=string2
- If
string1exists in the current string, it is replaced bystring2. - If not, the program moves to the next line.
- When a rule is applied, the interpreter restarts from the top.
- The program halts only when no rule applies anymore.
A=B features several special keywords that add conditional behavior to replacement rules:
| Syntax | Meaning |
|---|---|
(start)string1=string2 |
Match string1 only at the start of the input string |
(end)string1=string2 |
Match string1 only at the end of the input string |
string1=(start)string2 |
Insert string2 at start after removing string1 |
string1=(end)string2 |
Insert string2 at end after removing string1 |
(start)string1=(end)string2 |
Match string1 at start, delete it, append string2 at end |
string1=(return)string2 |
If string1 is found, replace it with string2 and terminate |
(once)string1=string2 |
Instruction can only be executed once during entire execution |
If the string ends in abc, remove it and add xyz to the front — but only once.
- You cannot combine
(start),(end), or(return)as this makes no sense. - You can only use
(once)on the left-hand-side of the instruction. - You can only use
(return)on the right-hand-side of the instruction. (once)must come before(start)/(end)if used in combination.- A=B doesn't traditionally support combining
(once)with another keyword on LHS; however, it makes sense to allow this functionality
- A=B doesn't traditionally support combining
(once)(end)abc=(start)xyz
Make sure you have Stack installed.
A simple Zsh wrapper script is provided to simplify running programs.
- Make it Executable:
chmod +x to
- Run a Program:
./to programname inputstring
To check if a string is a palindrome using the provided palindrome.to program:
%./to code/palindrome abcabc
falsestack run -- programname inputstringNote: Omit the
.toextension when passingprogramname.
Don't use quotation marks around the input string.
- Program files must end with extension
.to. - Input and instruction strings must be:
- no spaces
- no reserved characters:
()=# - No empty input strings, but empty instruction strings are permitted (and useful!)
Use # at the beginning or middle of a line for a comment. Only one instruction per line.
haskell-atob/
├── code/ # The .to A=B program files used for tests
├── test/ # Integration tests' input/output cases
├── app/ # Main
├── src/ # Parsing and interpreter logic
├── README.md
└── package.yaml
Test cases are located in test/cases/. Each .txt file contains alternating lines:
input1
expected_output1
input2
expected_output2
...
The interpreter is tested using these pairs to ensure correctness.
To run tests:
stack testYou can also see the code solutions to these tests under code/ (all of which primarily assume only "a", "b", and "c" are used to makeup input strings)
See the LICENSE file for details about the license.
Made by Edward when I definitely should've been studying for exams.