🧅 Onion Frontend
The compilation frontend for the Onion programming language. This package handles lexical analysis, parsing, semantic analysis, and intermediate representation generation.
📚 Overview
The onion-frontend package is responsible for transforming Onion source code into intermediate representation (IR) that can be executed by the Onion Virtual Machine. It implements a complete compilation pipeline from source text to executable bytecode.
🏗️ Architecture
Source Code → Lexer → Parser → AST → Analyzer → IR Generator → IR/Bytecode
Components
- Lexer (
parser/lexer.rs) - Tokenizes source code into a stream of tokens - Parser (
parser/ast.rs) - Builds Abstract Syntax Tree (AST) from tokens - Analyzer (
parser/analyzer.rs) - Performs semantic analysis and error checking - IR Generator (
ir_generator/) - Generates intermediate representation from AST - Compiler (
compile.rs) - Orchestrates the entire compilation process
✨ Features
- 🔍 Lexical Analysis - Complete tokenization with support for Unicode
- 🌳 AST Generation - Builds structured representation of source code
- 🔎 Semantic Analysis - Type checking and error detection
- 📦 IR Generation - Produces optimized intermediate representation
- 🚀 Bytecode Compilation - Generates executable bytecode for the VM
- 🔧 Error Reporting - Rich error messages with source location information
🚀 Usage
Basic Compilation
use ;
use DirectoryStack;
// Compile to IR
let mut dir_stack = new;
let source = r#"
@required stdlib;
main := () -> {
stdlib.io.println("Hello, World!");
};
main();
"#;
let ir_package = build_code?;
// Compile to bytecode
let bytecode = compile_to_bytecode?;
Advanced Usage
use lexer;
use ;
use analyze_ast;
// Manual compilation pipeline
let tokens = tokenize;
let tokens = reject_comment;
let gathered = from_stream;
let ast = build_ast?;
let analysis = analyze_ast;
📋 API Reference
Main Functions
-
build_code(code: &str, dir_stack: &mut DirectoryStack) -> Result<IRPackage, String>- Compiles source code to intermediate representation
-
compile_to_bytecode(code: &str, dir_stack: &mut DirectoryStack) -> Result<VMInstructionPackage, String>- Compiles source code directly to bytecode
Key Types
IRPackage- Contains intermediate representation and debug informationVMInstructionPackage- Executable bytecode packageDirectoryStack- Manages file paths and module resolution
🔧 Dependencies
onion-vm- Virtual machine types and instruction definitionscolored- Terminal color output for error messagesregex- Regular expression support for lexical analysisunicode-segmentation- Unicode-aware string processingbase64- Encoding support for bytecode serialization
🏗️ Module Structure
onion-frontend/
├── src/
│ ├── lib.rs # Public API exports
│ ├── compile.rs # Main compilation orchestration
│ ├── dir_stack.rs # Directory and path management
│ ├── parser/ # Lexical and syntactic analysis
│ │ ├── mod.rs
│ │ ├── lexer.rs # Tokenization
│ │ ├── ast.rs # AST construction
│ │ └── analyzer.rs # Semantic analysis
│ └── ir_generator/ # Intermediate representation
│ ├── mod.rs
│ └── ir_generator.rs # IR generation logic
└── Cargo.toml
🎯 Design Goals
- Correctness - Comprehensive error checking and validation
- Performance - Efficient compilation with minimal overhead
- Extensibility - Modular design for easy feature additions
- Developer Experience - Clear error messages and debugging support
🤝 Integration
This package is designed to work seamlessly with:
- onion-vm - Executes the generated bytecode
- Language Server - Provides IDE support through compilation services
- CLI Tools - Powers the
onion compilecommand
📄 License
This package is part of the Onion programming language project and is licensed under the MIT License.