# 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.

```php
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:

<pre class="language-bash"><code class="lang-bash"><strong>> ./glados run --verbose out.gomc
</strong></code></pre>

### **Output:**

```bash
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

```bnf
<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

```rust
Push <value>
```

**Detailed Information:**

* **Description:** Pushes the value onto the stack.
* **Usage:** Example: `Push 5` pushes the value `5` onto the stack.

### JumpIfFalse

```rust
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

```rust
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, and `Jump back 2 instructions` jumps back by 2 instructions.

### PushArg

```rust
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

```rust
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

```rust
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

```rust
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

```rust
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

```rust
Access list
```

**Detailed Information:**

* **Description:** Accesses the list at index popped from top of the stack.
* **Usage:** Example: `Access list` accesses the list.

### Return

```rust
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://glados.guillaume-hein.fr/developpers/verbose-mode.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
