Boxes

From Esolang
Jump to navigation Jump to search

Boxes is an esoteric programming language created by User:Dominicentek. It's code is stored in several boxes which can be put anywhere in the source code.

Boxes

How does a box look like

/- BoxName -------------\
| instruction parameter |
| ...                   |
\-----------------------/

How do they work

If a box has an incorrect syntax, it's ignored. Each box can hold one value, which can be a string or integer. Default value is a 0. The interpreter initially executes a box with a name of Main. If it cannot find that box, the program simply exits. When a bottom of the box is reached, it executes again.

Instructions

Inside the boxes, several instructions can be put.

Boxes
Instruction Syntax Description
open <box name> Acts like a goto statement. Code immediately jumps into that box
close Closes current box and returns to previous box. If it's in main box, it exits the program.
openwith <box name> Exactly like open instruction, but it stores the value from the current box to the next box.
return Exactly like close instruction, but it stores the value from current box to the previous box.
if <value> <is|not|greater|less> <value> <open|openwith> <box name> Performs an if statement. If it succeeds, a box gets opened.
else <open|openwith> <box name> If the above if statement fails, this box gets opened instead
Value Management
Instructiton Syntax Description
assign <value> Assigns a new value to the current box.
toint Converts the current value into an integer.
tostr Converts the currrent value into a string.
numinput Takes input from user as a number.
input Takes input from usuer as a string.
increase <value> If the value is an integer, it increases it's value by specified value.
decrease <value> If the value is an integer, it decreases it's value by specified value.
multiply <value> If the value is an integer, it multiplies it's value with specified value.
divide <value> If the value is an integer, it divides it's value with specified value.
modulo <value> If the value is an integer, it modules it's value with specified value.
Program
Instruction Syntax Description
print <value> Prints a value in the console.
println <value> Prints a value with a new line to the console.
exit Exits the program.

Boxes also have a global stack which can be accessed from any box.

Stack Manipulation
Instruction Syntax Description
push <value> Pushes a value onto the stack
pop Pops the value off the stack
get Pops the value off the stack and sets it as a box value
duplicate Duplicates the top value on the stack
swap Swaps the 2 items on top of the stack
reverse Reverses the order of the stack

Values

Values hold data used as parameters in instructions. If it's a valid number, it's an integer constant. If it's text surrounded by double quotes, it's a string constant. If the value is this, then it returns value current box holds, otherwise it is treated like a box name and it returns a value from the box specified.

Examples

Hello World

/- Main ----------------\
| print "Hello, World!" |
| exit                  |
\-----------------------/

Truth Machine

/- Main -----------------------\
| numinput                     |
| if this is 0 open PrintZero  |
| else open PrintOne           |
\------------------------------/

/- PrintZero -\
| print 0     |
| exit        |
\-------------/

/- PrintOne -\
| print 1    |
\------------/

99 Bottles of Beer

/- Main ----------\
| assign 99       |
| openwith Print |
\-----------------/

/- Print -----------------------------------------------------\
| print this                                                  |
| print " bottles of beer on the wall,\n"                     |
| print this                                                  |
| print " bottles of beer.\nTake one down, pass it around,\n" |
| decrease 1                                                  |
| print this                                                  |
| print " bottles of beer on the wall.\n\n"                   |
| if this is 0 open Exit                                      |
\-------------------------------------------------------------/

/- Exit -\
| exit   |
\--------/

Interpreter

Download