Compilation Pipeline
Source Code (.mt)
↓
Lexer (Tokenization)
↓
Parser (AST Construction)
↓
AST Optimizer (optional, by optimization level)
├─ Constant Folding Pass
├─ Dead Code Elimination Pass
└─ Unused Declaration Elimination Pass
↓
Bytecode Compiler
↓
Bytecode Program (.mtc)
↓
Virtual Machine (stack-based execution)
↓
Result
Stages
1. Lexer
Translates source text to a token stream. Tracks source locations for diagnostics; balances brackets so the parser can recover from mismatched delimiters.
2. Parser
Constructs the AST from tokens. Specialized sub-parsers handle classes, interfaces, expressions, statements, lambdas, and types. Errors are accumulated rather than aborting on the first failure.
3. AST Optimizer
Optional, pluggable passes:
- Constant Folding — reduces
1 + 2to3at compile time. - Dead Code Elimination — removes unreachable branches. Preserves
@Scriptand@Throwannotations. - Unused Declaration Elimination — drops symbols nothing references.
Optimization level selects which passes run. Debug compiles skip optimization to keep source-line mapping faithful.
4. Bytecode Compiler
Lowers AST to a stack-based instruction stream. Emits:
- Instructions and their operands.
- A constant pool (strings, numbers).
- Class metadata (fields, method tables, generic parameters).
- Source locations for debugger and stack-trace use.
Output is either an in-memory BytecodeProgram (for mType script.mt) or a serialized .mtc file (for mType --compile).
5. Virtual Machine
Executes the bytecode. See VM.
Execution Modes
- Direct —
mType script.mt. Compiles AST → bytecode in memory and executes. - Compile —
mType --compile script.mt. Emitsscript.mtc. - Cached —
mType --run-cached script.mtc. Loads bytecode and executes. - Project Build —
mType --build [project.mtproj]. Compiles every source file to.mtc, optionally bundling into a.mtcLibor native.exe.