This stage extends the Rationnel language with user-defined functions and full call semantics while keeping MVaP as the target virtual machine.
The language now supports:
- Function declarations with return types:
- entier
- booleen
- rationnel
- Parameter lists (one or more parameters)
- Local variables inside functions
- Mandatory
retourner valueinstruction - No nested function definitions
Functions can be:
- Called inside expressions
- Used in arithmetic or boolean contexts
- Recursive (e.g., factorial, fibonacci)
The compiler enforces:
- Correct number of arguments
- Correct argument types
- Correct return type usage in expressions
- Mandatory return instruction in each function
- Implemented CALL / RETURN mechanism for MVaP
- Managed stack frames and activation records
- Supported recursive function calls
- Ensured proper stack cleanup after function execution
- Designing activation records for stack-based machines
- Implementing call-by-value parameter passing
- Type checking across function boundaries
- Managing recursion at machine level
- Structuring a compiler with multi-phase logic
- Maintaining separation between grammar, tests, and generated code
antlr4 Functions.g4
antlr4 Rationnel.g4javac *.javagrun Rationnel start < tests/test_functions.ratGenerated .mvap files (e.g., code.mvap, ex1.mvap) can be assembled
and executed using the MVaP assembler and simulator.
project-root/
│
├── Functions.g4
├── Rationnel.g4
│
├── tests/
│ └── test_functions.rat
│
├── code.mvap
├── ex1.mvap
│
├── tp8.pdf
│
└── README.md
.mvapfiles are generated machine code outputs.- For clean repository management, generated files should ideally be
moved to a
build/directory and excluded from version control. - Only grammar files, test programs, and documentation should be tracked long-term.
This TP completes the transition from structured control flow to full function support with proper stack-based execution on the MVaP virtual machine.