Verbose Mode
In GoMelan when running a compiled .gom program you can print its compiled instructions before execution.
Run verbose
If you use -v
or --verbose
option, you can run the debug mode.
fn factorial(n: Int) -> Int
{
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
fn main() -> Int
{
return factorial(5);
}
The debug mode command:
> ./glados run --verbose out.gomc
Output:
Compiled instructions:
factorial:
Push to stack, arg 0
Add to env "n"
Push to stack, env key "n"
Push 1
Push <Operator '<='>
Call with 2 args
If false 3
Push 1
Return
Jump 10 instructions
Push to stack, env key "n"
Push to stack, env key "n"
Push 1
Push <Operator '-'>
Call with 2 args
Push to stack, env key "factorial"
Call with 1 args
Push <Operator '*'>
Call with 2 args
Return
main:
Push 5
Push to stack, env key "factorial"
Call with 1 args
Return
Understanding the output
Procedures or functions
The representation of a procedure will be a label followed by instructions in this format
<procedure> ::= <label-name> ":" <new-line>
<tabulation> <instructions>
Instructions
A procedure is defined by multiple successive instructions.
Each line of the output corresponds to an internal instruction executed by the GoMelan Virtual Machine.
The GoMelan Virtual Machine (VM) executes programs by interpreting a set of instructions. Here are the details of various VM instructions.
Push
Push <value>
Detailed Information:
Description: Pushes the value onto the stack.
Usage: Example:
Push 5
pushes the value5
onto the stack.
JumpIfFalse
If false <number-of-instructions>
Detailed Information:
Description: Jumps forward in the instruction sequence by the specified number of instructions if the top of the stack is false (the top of the stack is popped).
Usage: Example:
If false 3
jumps forward by 3 instructions if the top of the stack is false.
Jump
Jump <number-of-instructions>
Detailed Information:
Description: Jumps forward or backward in the instruction sequence by the specified number of instructions.
Usage: Example:
Jump 3 instructions
jumps forward by 3 instructions, andJump back 2 instructions
jumps back by 2 instructions.
PushArg
Push to stack, arg <argument-index>
Detailed Information:
Description: Pushes the value of the specified argument onto the stack.
Usage: Example:
Push to stack, arg 0
pushes the value of the first argument onto the stack.
PushEnv
Push to stack, env key <variable-name>
Detailed Information:
Description: Pushes the value of the specified environment variable onto the stack.
Usage: Example:
Push to stack, env key "n"
pushes the value of the environment variable named "n" onto the stack.
AddEnv
Add to env <variable-name>
Detailed Information:
Description: Adds a variable to the environment.
Usage: Example:
Add to env "n"
adds a variable named "n" to the environment.
Call
Call with <number-of-arguments> args
Detailed Information:
Description: Calls the function with the specified number of arguments.
Usage: Example:
Call with 2 args
calls a function with two arguments.
BuildList
Build list with <number-of-elements> args
Detailed Information:
Description: Builds a list with the specified number of elements.
Usage: Example:
Build list with 3 args
builds a list with three elements.
AccessList
Access list
Detailed Information:
Description: Accesses the list at index popped from top of the stack.
Usage: Example:
Access list
accesses the list.
Return
Return
Detailed Information:
Description: Returns from the current function, popping the current frame from the call stack.
Usage: Example:
Return
is used to exit from a function and return to the calling function.
Last updated
Was this helpful?