Tuga Compiler & Virtual Machine

Java
ANTLR 4

Full compilation pipeline for Tuga, an imperative language with Portuguese-based keywords. Includes ANTLR-generated lexer/parser, two-phase semantic analysis (scoped symbol table + type checking), bytecode generation, and a custom stack-based virtual machine (S-VM) supporting functions, locals, control flow, and forward references.

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