SPB Git

Repository Public

check.rs 2.2 KB 71 lines rust

1//! ORIX Sonata2//! File: crates/sonata-compiler/examples/check.rs3//! Description: Developer tool: compiles one file and prints diagnostics, statistics, or the4//! AST / IR / graph / formatted source. The user-facing tool is the `sonata` CLI.5//!6//! Author: Simon-Pierre Boucher7//! Contact: contact@spboucher.ai89use sonata_compiler::{compile, CompileOptions, DirLoader};10use std::path::Path;1112fn main() {13    let args: Vec<String> = std::env::args().skip(1).collect();14    let Some(file) = args.iter().find(|a| !a.starts_with("--")) else {15        eprintln!("usage: check <file.sonata> [--ast|--ir|--graph|--fmt|--json]");16        std::process::exit(2);17    };18    let source = match std::fs::read_to_string(file) {19        Ok(s) => s,20        Err(e) => {21            eprintln!("cannot read {file}: {e}");22            std::process::exit(2);23        }24    };25    let has = |flag: &str| args.iter().any(|a| a == flag);26    if has("--fmt") {27        match sonata_compiler::format(&source) {28            Ok(text) => print!("{text}"),29            Err(d) => {30                eprintln!("{} syntax error(s); not formatted", d.len());31                std::process::exit(1);32            }33        }34        return;35    }36    let root = Path::new(file).parent().unwrap_or(Path::new("."));37    let out = compile(file, &source, &DirLoader::new(root), &CompileOptions::default());38    if has("--json") {39        println!("{}", out.diagnostics_json());40    } else {41        eprint!("{}", out.render_diagnostics());42    }43    if has("--ast") {44        println!("{}", out.ast_json());45    }46    if has("--ir") {47        println!("{}", out.program_json());48    }49    if has("--graph") {50        println!("{}", out.graph_json());51    }52    let s = &out.stats;53    eprintln!(54        "{}: {} error(s), {} warning(s); {} tracks, {} notes, polyphony {}, {} graph nodes; lex {:.0} µs, parse {:.0} µs, analysis {:.0} µs, graph {:.0} µs",55        file,56        out.errors().count(),57        out.diagnostics.len() - out.errors().count(),58        s.tracks,59        s.events,60        s.voices,61        s.nodes,62        s.lex_us,63        s.parse_us,64        s.sema_us,65        s.graph_us66    );67    if out.has_errors() {68        std::process::exit(1);69    }70}71
Files main
Menu

Theme