use super::*;
mod build;
mod check;
mod dx7;
mod fmt;
mod info;
mod library;
mod library_check;
mod play;
mod render;
mod synths;
mod tone;
pub(super) use build::build;
pub(super) use check::check;
pub(super) use dx7::dx7;
pub(super) use fmt::fmt;
pub(super) use info::info;
pub(super) use library::library;
pub(super) use library_check::library_check;
pub(super) use play::play;
pub(super) use render::render;
pub(super) use synths::synths;
pub(super) use tone::{tone, ToneArgs, ToneReference};
pub(super) fn counts(d: &[Diagnostic]) -> (usize, usize) {
let e = d.iter().filter(|d| d.severity == Severity::Error).count();
(e, d.len() - e)
}
pub(super) fn create_synth(song: &pikopiko::Song, sample_rate: u32) -> Box<dyn pikopiko::Synth> {
pikopiko::synth::create(
pikopiko::synth::all(),
&song.synth,
sample_rate,
song.tracks.len(),
)
}
pub(super) fn compile(src: &str, o: &Opts) -> (Option<pikopiko::Song>, Vec<Diagnostic>) {
let files = match o.file.as_deref() {
Some(p) if p != "-" => pikopiko::compiler::include::Files::beside(p),
_ => pikopiko::compiler::include::Files::new("."),
};
let with_libs = pikopiko::synth::tones::user::WithLibraries::new(Some(&files), &o.libs);
let opts = CompileOptions {
dialect: o.dialect,
synths: pikopiko::synth::all(),
include: Some(&with_libs),
..Default::default()
};
compile_with(src, &opts)
}
pub(super) fn print_diagnostics(src: &str, path: &str, diags: &[Diagnostic]) {
let lines: Vec<&str> = src.lines().collect();
let tr = i18n();
let suggestion_label = t("cli-diag-suggestion");
for d in diags {
let message = tr.diagnostic(d);
let suggestion = d.suggestion.as_ref();
let sev = match d.severity {
Severity::Error => "error",
Severity::Warning => "warning",
};
eprintln!("{sev}[{}]: {message}", d.code);
if let Some(file) = &d.file {
eprintln!(" --> {}:{}:{}", file, d.span.line, d.span.col);
if let Some(s) = suggestion {
eprintln!(" = {suggestion_label}: {s}");
}
eprintln!();
continue;
}
eprintln!(" --> {}:{}:{}", path, d.span.line, d.span.col);
if let Some(line) = lines.get((d.span.line as usize).wrapping_sub(1)) {
let prefix: String = line
.chars()
.take(d.span.col.saturating_sub(1) as usize)
.map(|c| {
if c == '\t' {
'\t'
} else if c.is_ascii() {
' '
} else {
' '
}
})
.collect();
eprintln!(" | {line}");
eprintln!(" | {prefix}{}", "^".repeat(d.span.len.max(1) as usize));
}
if let Some(s) = suggestion {
eprintln!(" = {suggestion_label}: {s}");
}
eprintln!();
}
}
pub(super) fn write_error(path: &str, e: std::io::Error) -> String {
ta(
"cli-err-write-file",
&[("path", path.into()), ("error", i18n().io_error(&e).into())],
)
}
pub(super) fn json_error(_: serde_json::Error) -> String {
t("cli-err-json")
}