# 13.1 The engine's layers

> The engine processes a program in layers: each one receives the previous result, checks or transforms it and hands it to the next.

Web page: https://pseudolearn.app/en/spec/motor/capas

> **In one sentence**
>
> The engine processes a program in layers: each one receives the previous result, checks or transforms it and hands it to the next.

## 13.1.1 How it works

**Figure 13.1-1: Engine layers**

A column of layers from top to bottom: text, lexical, syntax, semantics and intermediate representation. Two arrows leave the intermediate representation: virtual machine and native code.

From text to execution. The first three layers analyse; the intermediate representation is where the virtual machine and native code part ways.

| Layer | Receives | Delivers |
| --- | --- | --- |
| lexical | text | tokens: words, numbers, signs |
| syntax | tokens | the program tree |
| semantics | tree | tree with types and names resolved |
| intermediate representation | checked tree | blocks of simple instructions |
| virtual machine or native code | intermediate representation | the run |

The **intermediate representation** (IR) is the central piece. Every statement is marked with an entry and an exit, and every decision with its branch: that way execution can account for exactly what happens. Translating to the IR **does not change the meaning**: running it gives the same outputs, in the same order, the language prescribes.

### Program: A small program

```pseudolearn
Algorithm Pequeno
  Define n As Integer <- 2;
  If n > 1 Then
    Write "grande";
  EndIf
EndAlgorithm
```

**Output**

```text
grande
```

*The program finishes normally.*

**Intermediate representation**

```text
module Pequeno en
func suspendible @main() -> unit {
b0() mass 2:
  stmt.enter @2:3-2:27
  %0 = const.i64 2 @2:26-2:27
  stmt.exit @2:3-2:27
  stmt.enter @3:3-5:8
  %1 = const.i64 1 @3:10-3:11
  %2 = int.cmp gt, %0, %1 @3:8-3:9
  decide.bool %2 @3:3-5:8
  branch %2, b1(), b2()
b1() mass 1:
  stmt.enter @4:5-4:19
  %3 = const.str "grande" @4:11-4:19
  out.write %3, newline @4:5-4:19
  stmt.exit @4:5-4:19
  jump b3()
b2() mass 0:
  jump b3()
b3() mass 0:
  stmt.exit @3:3-5:8
  return @6:1-6:13
}
```

**In short**
- The engine works in layers: lexical, syntax, semantics, IR and execution.
- The IR marks statements and decisions so execution is observable.
- Translating to the IR keeps the meaning.

**See also**
- [1.2 The journey of a program: from text to execution](https://pseudolearn.app/en/spec/vision/viaje.md)
- [13.5 Observing execution](https://pseudolearn.app/en/spec/motor/observacion.md)
- [13.7 Native code and the host](https://pseudolearn.app/en/spec/motor/nativo.md)
