13.1.1 How it works
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.
| 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.
ProgramA small program
Algorithm Pequeno
Define n As Integer <- 2;
If n > 1 Then
Write "grande";
EndIf
EndAlgorithmOutput
grande
The program finishes normally.
Intermediate representation
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.