# 🧅 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 -> Comptime → 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

## 🏗️ Module Structure

```
onion-frontend/
├── src/
│   ├── lib.rs              # Public API exports
│   ├── compile.rs          # Main compilation orchestration
│   ├── parser/             # Lexical and syntactic analysis
│   │   ├── comptime/       # Compile-time evaluation and solving
│   │   ├── 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

1. **Correctness** - Comprehensive error checking and validation
2. **Performance** - Efficient compilation with minimal overhead
3. **Extensibility** - Modular design for easy feature additions
4. **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 compile` command

## 📄 License

This package is part of the Onion programming language project and is licensed under the MIT License.
