# 1.2 The journey of a program: from text to execution

> Before running a program, the engine breaks it into pieces, checks that it makes sense and translates it into simple instructions for its virtual machine.

Web page: https://pseudolearn.app/en/spec/vision/viaje

> **In one sentence**
>
> Before running a program, the engine breaks it into pieces, checks that it makes sense and translates it into simple instructions for its virtual machine.

## 1.2.1 How it works

When you press "Run", the program text travels along a path of five stages. Each stage produces something new from the previous one. Step through the figure to follow the program `Doble` on its journey.

### Program: The travelling program

```pseudolearn
Algorithm Doble
  Define n As Integer <- 21;
  Write n * 2;
EndAlgorithm
```

**Output**

```text
42
```

*The program finishes normally.*

**Figure 1.2-1: The five stages of the engine**

A column of boxes joined by arrows: the source text, lexical analysis, syntax analysis, semantic analysis, translation and execution. A dashed red line next to the first three marks that they form the analysis. Each step highlights one stage.

The five stages of the engine. The first three happen before anything runs; if any of them finds errors, the program never runs.

1. Step 1: **Lexical analysis.** The text is split into pieces called tokens: `Algorithm`, `Doble`, `Define`, `n`, `As`, `Integer`… Spaces and comments are kept aside.
2. Step 2: **Syntax analysis.** The tokens are grouped into a tree: an algorithm containing a definition and a write.
3. Step 3: **Semantic analysis.** The engine checks names and types: `n` exists, it is an `Integer` and so is `n * 2`.
4. Step 4: **Translation.** The tree becomes an intermediate representation and then simple instructions for the virtual machine.
5. Step 5: **Execution.** The virtual machine runs the instructions one by one and writes 42.

## 1.2.2 Check first, run afterwards

The first three stages form the **analysis**. If they find an error, the program does not run: it would make no sense to run something that is not understood. That is why a typing mistake shows up immediately, without waiting for the program to reach that line.

Some errors cannot be seen by the analysis because they depend on real values: dividing by zero, going outside an array or reading a variable without a value. Those are detected when running, at the exact moment they happen.

> **Under the hood**
>
> This is the real intermediate representation of the program `Doble`. Each line is a simple instruction; `stmt.enter` and `stmt.exit` mark where each statement starts and ends, which is what makes step-by-step execution possible.
>
> **Intermediate representation**
>
> ```text
> module Doble en
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @2:3-2:28
>   %0 = const.i64 21 @2:26-2:28
>   stmt.exit @2:3-2:28
>   stmt.enter @3:3-3:14
>   %1 = const.i64 2 @3:13-3:14
>   %2 = int.mul %0, %1 @3:11-3:12
>   out.write %2, newline @3:3-3:14
>   stmt.exit @3:3-3:14
>   return @4:1-4:13
> }
> ```

**In short**
- The engine works in five stages: lexical, syntax, semantics, translation and execution.
- If the analysis finds errors, the program does not run.
- Errors that depend on concrete values appear when running.

**See also**
- [1.3 Analysing, running and observing](https://pseudolearn.app/en/spec/vision/analizar-ejecutar.md)
- [13.1 The engine's layers](https://pseudolearn.app/en/spec/motor/capas.md)
- [13.1 The engine's layers](https://pseudolearn.app/en/spec/motor/capas.md)
