Skip to main content

Basic Programs

This page demonstrates simple complete programs that showcase the compiler’s core functionality: variable declarations, arithmetic operations, and print statements.

Simple Variable Declaration

The most basic program declares a variable and prints its value.
1

Write the source code

let x = 10;
print x;
2

Lexical Analysis (Tokens)

The scanner converts the source code into tokens:
TIPO                 LEXEMA               LINEA    COLUMNA  VALOR          
----------------------------------------------------------------------------------
LET                  let                  1        1                       
IDENTIFICADOR        x                    1        5                       
IGUAL                =                    1        7                       
NUMERO               10                   1        9        10             
PUNTO_COMA           ;                    1        11                      
PRINT                print                2        1                       
IDENTIFICADOR        x                    2        7                       
PUNTO_COMA           ;                    2        8                       
FIN_ARCHIVO                               2        9
3

Syntax Analysis (AST)

The parser constructs an Abstract Syntax Tree:
Programa
├── DeclaracionVariable
│   ├── nombre: 'x'
│   └── valor:
│       └── NumeroLiteral(10)
└── SentenciaPrint
    └── expresion:
        └── Identificador('x')
4

Semantic Analysis

The semantic analyzer verifies that:
  • Variable x is declared before use ✓
  • No division by zero ✓
Result: 1 variable verified
5

Intermediate Representation (IR)

Three-address code is generated:
x = 10
print x
6

Execution

The interpreter executes the program:
Resultado:
→ 10

Arithmetic Operations

A program demonstrating basic arithmetic with multiple variables.
let a = 5;
let b = 3;
let sum = a + b;
let product = a * b;
print sum;
print product;

Complete Compilation Pipeline

A comprehensive example showing all compilation phases for a more complex program.
let a = 10;
let b = 20;
let c = a + b * 2;
print c;
Execution Result: The program outputs 50 because it evaluates as 10 + (20 * 2) = 10 + 40 = 50.Notice how operator precedence is correctly handled: multiplication is performed before addition.

Comments Support

The compiler supports single-line comments using //.
// This is a comment and will be ignored
let x = 42;  // Assign the answer to life
print x;     // Output the result
Comments are removed during lexical analysis and do not generate tokens.
Use comments to document your code and make it more readable. The compiler completely ignores everything after // until the end of the line.

Build docs developers (and LLMs) love