2.6 KiB
Instructions And Keywords
This explains all of the instructions and keywords
Instructions
Instructions modify the stack, the stack is an array, i will be using typescript to show how each instruction works
let stack: number[] = [];
PushInt ("Any Number")
PushInt pushes one number on the stack.
Usage:
420
How it works:
// num = 420
stack.push(num);
console.assert(stack == [420]);
PushStr ("Any String")
PushStr pushes 2 things on the stack, string length and string pointer
Usage:
"Henlo world!\n"
How it works:
stack.push(str_len);
stack.push(str_ptr);
Print ("print")
Print just prints a number from the top of the stack into stdout
Usage:
69 print
How it works:
num = stack.pop()
console.log(num);
Dup ("dup")
Dup duplicates the top most number on the stack
Usage:
69 dup
How it works:
stack = [12];
a = stack.pop();
stack.push(a);
stack.push(a);
console.assert(stack == [12, 12]);
Drop ("drop")
Drop removes the top most number on the stack
Usage:
69 drop
How it works:
stack = [69];
stack.pop();
console.assert(stack == []);
Rot ("rot")
Rot moves the third number from the top of the stack and moves it to the top
Usage:
1 2 3 rot
How it works:
stack = [1, 2, 3];
let a = stack.pop();
let b = stack.pop();
let c = stack.pop();
stack.push(b);
stack.push(a);
stack.push(c);
console.assert(stack == [3, 1, 2]);
Over ("over")
Over takes the second number from the top of the stack and copies it to the top
Usage:
1 2 over
How it works:
stack = [1, 2];
let a = stack.pop();
let b = stack.pop();
stack.push(b);
stack.push(a);
stack.push(b);
console.assert(stack == [1, 2, 1]);
Swap ("swap")
Swap swaps the first and second numbers from the stack
Usage:
1 2 stack
How it works:
stack = [1, 2];
let a = stack.pop();
let b = stack.pop();
stack.push(a);
stack.push(b);
console.assert(stack == [2, 1]);