WTFZOMFG
A Python interpreter for the WTFZOMFG esoteric programming language, written in a functional style for the ATP course.
- Python
An interpreter made in Python for the WTFZOMFG esolang, with a focus on functional programming. Written for the ATP course — all must-haves implemented and several should-haves on top.
WTFZOMFG explanation
Brainfuck
WTFZOMFG is based on brainfuck, using simple commands to manipulate memory. The browser demo below uses the brainfuck subset — the full language has many more features.
Memory
WTFZOMFG has an infinite row of memory cells. Each cell holds an integer or character. The default cell is cell 0. The pointer selects which cell is active.
Pointer
The pointer can move one step left or right, or jump directly to a cell by number. Cell numbering starts at 0.
Video
Running a file
Clone the repository, then:
python ./main.py -f [filename] -m [amount of memory cells] -e [ignore errors, optional]
To run example.wtf:
python ./main.py -f ./example.wtf -m 10 -e
The -e flag ignores parse and lex errors. Execution errors are reported at the end of the run.
Assignment requirements
Must-haves
- Classes as data objects
- Inheritance through the error hierarchy
- Object printing via
__repr__and__str__ - Private variables in error objects
- Decorator tracking how many times
executewas called - Functional programming style throughout
- Type annotations on all functions
- At least three uses of higher-order functions (
map,filter, etc.)
Should-haves
- Simple error messaging (see errors section)
- Debug command
wto visualise pointer and memory - Advanced features: comments, I/O, arithmetic, program flow control
- Walkthrough video
Errors
Memory cell numbering starts at 0. With 5 cells they are numbered 0, 1, 2, 3, 4. Out-of-bounds access is reported as an error.
Writing WTFZOMFG code
Full command reference is on the esolang wiki. Quick reference below.
Commenting
# This is a single line comment
[This is a multi-line comment]
Printing
.H .i .\n # Print characters H, i, and newline
'Hi\n" # Print the string Hi followed by newline
Input / Output
^ v .\n # Input a character, print it
/ \ .\n # Input a number, print it
^ reads a character into the current cell, v prints the cell as a character. / reads a number, \ prints the cell value as a number.
Cell manipulation
=0 # Set cell to 0
+ + + # Increment by 3
- - - # Decrement by 3
=5 ~-2 # Set to 5, then add -2
| w | # Flip between 0 and 1; w prints debug info
& %4 # Copy cell value to cell+1, and to cell at position 4
=87 @- # Set to 87, subtract ASCII value of '-' (45)
Pointer manipulation
> > # Move right twice
< # Move left once
_7 *2 # Jump to cell 7, then move 2 right
Arithmetic
=6 & a # Set to 6, copy to right, add → result in current cell
> =2 < s # Set right cell to 2, subtract from left cell
> =3 < m # Multiply current cell by 3
& d # Copy current cell right, divide current by right
Division by zero is treated as an error and ignored.
Looping
=5
(
'Loops Left: " v -
>
+ + +
'Small countdown "
(
- v ' "
)
< .\n
)
( starts a while loop if the current cell is not 0. ) ends it and re-checks the condition.
Goto
:Start
'Go to start? 0 for no, anything else for yes: " / .\n
?Start # Jump to Start if cell != 0
!Skip # Jump to Skip if cell == 0
'This will be skipped"
;Skip
; declares a label. : jumps to it unconditionally. ? jumps if cell is not 0. ! jumps if cell is 0.
If
{ [commands] } # Execute if cell != 0
{ [if block] } | { [else block] }
Debug
The w command prints the current pointer position and memory contents:
=5 > > - - _6 =3 & *-3 w
Outputs:
3 [5, 0, -2, 0, 0, 0, 3, 3, 0, 0]
Browser demo
The interpreter above is in Python. Below is a version that is much simpler than the one I made for the project. But it might still be fun to mess around with it. (At least, it will be when I finish it. ¯\(ツ)/¯)