Tuga — Compiler & Stack VM

Java

A complete compiler and stack-based virtual machine for the Tuga programming language (a small imperative language with Portuguese keywords), written in Java with ANTLR 4. Covers lexing, parsing, semantic analysis, code generation, and execution.

Built for the Compilers course (2024/2025), Tuga is a full compilation pipeline from source code to execution — five distinct phases, each a separate, well-bounded module.

Pipeline

PhaseComponentWhat it does
LexingTugaLexerANTLR-generated, custom error listener
ParsingTugaParserANTLR grammar (Tuga.g4), syntax errors with line numbers
SemanticDefPhase + RefPhaseScoped symbol table, reference resolution, type checking
CodegenCodeGenerationVisitorEmits S-VM bytecode + populates the constant pool
ExecutionVirtualMachineSStack-based interpreter with IP/FP registers

Language features

Stack Virtual Machine

I designed a small stack VM (VirtualMachineS) with:

Sample run

Input (factorial_test.tuga):

funcao principal()
inicio
  escreve fact(3);
fim

funcao fact(n: inteiro): inteiro
inicio
  se (n igual 0) retorna 1;
  retorna n * fact(n-1);
fim

Output (after lexing, parsing, type checking, codegen, and execution):

*** Constant pool ***
*** Instructions ***
0: call 2
1: halt
2: iconst 3
3: call 6
4: iprint
5: ret 0
6: lload -1
7: iconst 0
8: ieq
9: jumpf 12
10: iconst 1
11: retval 1
...
*** VM output ***
6