Skip to main content

Language Overview

The Mini-Compilador Educativo uses a simple imperative language with two primary statement types: variable declarations and print statements. Programs are composed of sequences of these statements, each terminated with a semicolon.

Basic Syntax

Variable Declarations

Declare variables using the let keyword:
let variable_name = expression;
All variables must be declared before they can be used in expressions.
Examples:
let x = 10;
let sum = 5 + 3;
let result = x * 2;
Output values using the print keyword:
print expression;
Examples:
print 42;
print x;
print x + y * 2;

Expressions and Operators

Arithmetic Operators

The language supports four basic arithmetic operators with standard precedence:

Addition (+)

Adds two values
let sum = 10 + 5;  // 15

Subtraction (-)

Subtracts the right operand from the left
let diff = 20 - 8;  // 12

Multiplication (*)

Multiplies two values
let product = 6 * 7;  // 42

Division (/)

Integer division (result is truncated)
let quotient = 15 / 4;  // 3

Operator Precedence

Operators follow standard mathematical precedence rules:
  1. Parentheses () - Highest priority
  2. Multiplication and Division *, /
  3. Addition and Subtraction +, - - Lowest priority
Example:
let a = 3 + 4 * 2;      // Result: 11 (not 14)
let b = (3 + 4) * 2;    // Result: 14

Using Parentheses

Use parentheses to override default precedence:
let result1 = 10 + 5 * 2;       // 20
let result2 = (10 + 5) * 2;     // 30
let result3 = 100 / (2 + 3);    // 20

Data Types

Numbers

The language supports integer literals only:
let a = 0;
let b = 42;
let c = 1000;
Negative numbers are not supported as literals. Use subtraction instead: let x = 0 - 5;

Variables

Variable names (identifiers) must:
  • Start with a letter (a-z, A-Z) or underscore (_)
  • Contain only letters, digits, and underscores
  • Not be a reserved word
Valid identifiers:
let x = 10;
let myVariable = 20;
let sum_total = 30;
let _private = 40;
let counter1 = 50;
Invalid identifiers:
let 1variable = 10;   // Cannot start with digit
let my-var = 20;      // Cannot contain hyphen
let let = 30;         // 'let' is reserved

Reserved Words

The following keywords are reserved:
  • let - Variable declaration
  • print - Output statement
  • leo - Reserved (no operation)
  • diego - Reserved (no operation)
The keywords leo and diego are recognized by the lexer but have no semantic effect. They’re included as educational examples of reserved words.

Comments

Single-line comments start with //:
// This is a comment
let x = 10;  // Inline comment

// Comments are ignored by the compiler
print x;

Complete Program Examples

Example 1: Basic Arithmetic

// Calculate the area of a rectangle
let width = 10;
let height = 5;
let area = width * height;
print area;  // Output: 50

Example 2: Expression Evaluation

// Demonstrate operator precedence
let a = 5;
let b = 10;
let c = a + b * 2;
print c;  // Output: 25

let d = (a + b) * 2;
print d;  // Output: 30

Example 3: Multi-step Calculation

// Calculate final value with multiple operations
let base = 100;
let increment = 20;
let multiplier = 3;

let step1 = base + increment;
let step2 = step1 * multiplier;
let final = step2 / 2;

print final;  // Output: 180

Best Practices

Choose names that indicate the variable’s purpose:
// Good
let totalPrice = 100;
let itemCount = 5;
let averagePrice = totalPrice / itemCount;

// Avoid
let x = 100;
let y = 5;
let z = x / y;
Always declare variables with let before referencing them:
// Correct
let x = 10;
let y = x + 5;

// Error: y not declared
let result = x + y;
let x = 10;
Even when not required, parentheses improve readability:
// Clear intent
let result = (a + b) * (c - d);

// Harder to read
let result = a + b * c - d;
Document complex calculations:
// Calculate compound interest
let principal = 1000;
let rate = 5;        // 5% per period
let periods = 3;

// Manual compound calculation: P * (1 + r)^n
let multiplier = 100 + rate;  // 105
let year1 = principal * multiplier / 100;
let year2 = year1 * multiplier / 100;
let year3 = year2 * multiplier / 100;

print year3;
The compiler detects division by literal zero:
// Compiler error
let invalid = 10 / 0;

// Safe
let divisor = 5;
let result = 10 / divisor;

Common Errors

Syntax Errors

let x = 10  // Error: expected ';'

Semantic Errors

let x = y + 5;  // Error: variable 'y' not declared

Next Steps

Using the CLI

Learn how to compile programs from the command line

Using the GUI

Use the graphical interface to write and compile programs

Interpreting Output

Understand the compiler’s output and error messages

Grammar Reference

Complete language specification and grammar