whitespace-language-converter
March 29, 2019 ยท View on GitHub
Provide a converter from our language to whitespace
Installation and Usage
Put to_ws.py to any location and type the following command
python to_ws.py readable.hws -o beautiful.ws
QuickStart
Put one command per line as follows:
push 5
push -2
add
printi
end
You can use labels if you want:
push 0
push 10
loop:
dup
push 0
swap
store
swap
push 0
retrieve
add
swap
push 1
sub
dup
jz end
jmp loop
end:
swap
printi
end
The above code calculates 10 + 9 + ... + 1 and output it.
Our Language
-
push <n>(n: integer) Push n to the top of the stack. -
copy <n>Copy the n-th element from the top of the stack. -
slide <n>Discards the 2, 3, ..., (n+1)-th element from the top of the stack. -
printiPrint the top of the stack as an integer and discard it. -
printcPrint the top of the stack as an character and discard it. -
readcRead a character from stdin and store the address pointed by the top of the stack and discard it. -
readiRead an integer from stdin and store the address pointed by the top of the stack and discard it. -
dupDuplicate the top of the stack. -
swapSwap the top two elements of the stack. -
dropDiscard the top of the stack. -
addLet X be the sum of the top two elements of the stack. Discard these two elements and push X. -
subLet X be the second element minus the first element of the stack. Discard these two elements and push X. -
mulLet X be the first element multiplied by the second element of the stack. Discard these two elements and push X. -
divLet X be the floor of the second element divided by the first element of the stack. Discard these two elements and push X. -
modLet X be the reminder of the second element divided by the first element of the stack. Discard these two elements and push X. -
storeStore the top of the stack to the address pointed by the second element of the stack and discard them. -
retrieveRetrieve a value from the address pointed by the top of the stack, discard it and push the value. -
endExit. -
<label>:Set a label. -
call <label>Call a subroutine starting from<label>. -
jmp <label>Jump to<label>. -
jz <label>Discard the top of the stack and if it's zero then jump to<label>. -
jn <label>Discard the top of the stack and if it's negative then jump to<label>. -
retExit the current subroutine. -
#...Comments. -
...Indents.