Nilton Constantino b5cafe1a4a
pr01
2026-01-27 10:55:10 +00:00

127 lines
4.1 KiB
Rust

//! # Prometeu Compiler
//!
//! This crate provides the official compiler for the Prometeu ecosystem.
//! It translates high-level source code (primarily TypeScript/JavaScript) into
//! Prometeu ByteCode (.pbc), which runs on the Prometeu Virtual Machine.
//!
//! ## Architecture Overview:
//!
//! The compiler follows a multi-stage pipeline:
//!
//! 1. **Frontend (Parsing & Analysis)**:
//! - Uses the `oxc` parser to generate an Abstract Syntax Tree (AST).
//! - Performs semantic analysis and validation (e.g., ensuring only supported TS features are used).
//! - Lowers the AST into the **Intermediate Representation (IR)**.
//! - *Example*: Converting a `a + b` expression into IR instructions like `Push(a)`, `Push(b)`, `Add`.
//!
//! 2. **IR Optimization (Optional/Planned)**:
//! - Simplifies the IR to improve performance or reduce bytecode size.
//!
//! 3. **Backend (Code Generation)**:
//! - **Lowering**: Converts the high-level IR into a flat list of ByteCode instructions.
//! - **Assembly**: Resolves branch labels into actual memory offsets.
//! - **Serialization**: Encodes the instructions into the binary PBC format.
//!
//! 4. **Artifact Export**:
//! - Generates the `.pbc` binary file.
//! - Optionally produces `.disasm` (disassembly for debugging) and `.json` (symbol maps).
//!
//! ## Example Usage (CLI):
//!
//! ```bash
//! # Build a project from a directory
//! prometeu-compiler build ./my-game --entry ./src/main.ts --out ./game.pbc
//! ```
//!
//! ## Programmatic Entry Point:
//!
//! See the [`compiler`] module for the main entry point to trigger a compilation programmatically.
pub mod common;
pub mod ir;
pub mod backend;
pub mod frontends;
pub mod compiler;
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
/// Command line interface for the Prometeu Compiler.
#[derive(Parser)]
#[command(name = "prometeu-compiler")]
#[command(version, about = "Official compiler for the PROMETEU Virtual Machine", long_about = None)]
pub struct Cli {
/// The action to perform (build or verify).
#[command(subcommand)]
pub command: Commands,
}
/// Available subcommands for the compiler.
#[derive(Subcommand)]
pub enum Commands {
/// Builds a Prometeu project by compiling source code into a PBC file.
Build {
/// Path to the project root directory.
project_dir: PathBuf,
/// Explicit path to the entry file (defaults to src/main.ts).
#[arg(short, long)]
entry: Option<PathBuf>,
/// Path to save the compiled .pbc file.
#[arg(short, long)]
out: Option<PathBuf>,
/// Whether to generate a .disasm file for debugging.
#[arg(long, default_value_t = true)]
emit_disasm: bool,
/// Whether to generate a .json symbols file for source mapping.
#[arg(long, default_value_t = true)]
emit_symbols: bool,
},
/// Verifies if a Prometeu project is syntactically and semantically valid without emitting code.
Verify {
/// Path to the project root directory.
project_dir: PathBuf,
},
}
/// Main entry point for the compiler library's execution logic.
/// Parses CLI arguments and dispatches to the appropriate compiler functions.
pub fn run() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Build {
project_dir,
out,
emit_disasm,
emit_symbols,
..
} => {
let build_dir = project_dir.join("build");
let out = out.unwrap_or_else(|| build_dir.join("program.pbc"));
if !build_dir.exists() {
std::fs::create_dir_all(&build_dir)?;
}
println!("Building project at {:?}", project_dir);
println!("Output: {:?}", out);
let compilation_unit = compiler::compile(&project_dir)?;
compilation_unit.export(&out, emit_disasm, emit_symbols)?;
}
Commands::Verify { project_dir } => {
println!("Verifying project at {:?}", project_dir);
compiler::compile(&project_dir)?;
println!("Project is valid!");
}
}
Ok(())
}