109 lines
3.4 KiB
Rust
109 lines
3.4 KiB
Rust
//! # Prometeu Compiler
|
|
//!
|
|
//! This crate provides the official compiler for the Prometeu ecosystem.
|
|
//! It translates TypeScript/JavaScript source code into Prometeu ByteCode (.pbc).
|
|
//!
|
|
//! ## Workflow:
|
|
//! 1. **Parsing**: Uses the `oxc` parser to generate an Abstract Syntax Tree (AST).
|
|
//! 2. **Validation**: Checks for unsupported features and ensures ABI compliance.
|
|
//! 3. **Codegen**: Traverses the AST and emits `prometeu-bytecode` instructions.
|
|
//! 4. **Assembly**: Resolves labels and serializes the program into the binary PBC format.
|
|
//!
|
|
//! ## Example Usage:
|
|
//! ```bash
|
|
//! # Build a project
|
|
//! prometeu-compiler build ./my-game --entry ./src/main.ts --out ./game.pbc
|
|
//! ```
|
|
|
|
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,
|
|
entry,
|
|
out,
|
|
emit_disasm,
|
|
emit_symbols,
|
|
} => {
|
|
let entry = entry.unwrap_or_else(|| project_dir.join("src/main.ts"));
|
|
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!("Entry: {:?}", entry);
|
|
println!("Output: {:?}", out);
|
|
|
|
let compilation_unit = compiler::compile(&entry)?;
|
|
compilation_unit.export(&out, emit_disasm, emit_symbols)?;
|
|
}
|
|
Commands::Verify { project_dir } => {
|
|
let entry = project_dir.join("src/main.ts");
|
|
println!("Verifying project at {:?}", project_dir);
|
|
println!("Entry: {:?}", entry);
|
|
|
|
compiler::compile(&entry)?;
|
|
println!("Project is valid!");
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|