Junk

From Esolang
Jump to navigation Jump to search

Junk is a language developed by Mark Schad in 2009 which uses stacks and queues to execute instructions. Programs are written in the form of instructions which are added to a stack. When the stack runs out of instructions the program has finished running. The instructions come in the form of lists of elements, which are executed one after the other. Whenever an instruction is encountered (denoted by '[' and ']') the instruction is added to the stack. When the program reaches the end of the code it will then execute each instruction in the stack.

Instructions

Instructions are made up of two components. The first component is the id of the instruction, the second component is a list of elements. This list is arbitrary in length and each element is made up of a command and an argument. All commands take a memory address as their argument except for 'push' and 'acc'. Instead of a constant integer, the '@' character may be used as an argument, and will pass the value of the accumulator into the command.

The arithmetic operators take the value in the memory and apply it to the accumulator.

+      Add
-      Subtract
*      Multiply
/      Divide

The comparison operators take the value in memory and compares it to the accumulator. If the comparison is true then execution of the remaining elements continues.

=      Equal
~      Not Equal
<      Less Than
>      Greater Than

These commands take user input and store it in memory and print memory to the user.

in     Input
out    Output
out$   Output Char

The value of the accumulator can be set from memory or memory can be set to the accumulator with these commands.

sto    Store
ret    Retrieve

The accumulator can be set to a value using the following command or by setting an element to a single integer.

acc    Accumulator

Finally, an instruction can be pushed onto the stack with the following command. The argument is the id of the instruction to push.

push   Push

Execution

The code is read from the source file and the instruction list is parsed. As each new instruction is parsed it is pushed onto the instruction stack. There is a block of memory with 256 addresses initialized to zero. The accumulator is also set to zero. After all of the code has been parsed the program can execute by popping an instruction from the stack and executing it. Repetition can be achieved by having an instruction push itself onto the stack. Comments can be added by simply adding text anywhere in the source that is not surrounded by a '[' or ']' character.

The hello world program in Junk.

[0|ret 0,- 1,sto 0,+ 2,out$ @,ret 0,> 15,push 0]
[1|32,sto 8,101,sto 4,72,sto 3]
[2|33,sto 14,100,sto 13,108,sto 12,sto 6,sto 5,114,sto 11,111,sto 10,sto 7,87,sto 9]
[3|12,sto 0,1,sto 1,3,sto 2,0,sto 15]