ling-lang 2030.1.22

Ling - The Omniglot Systems Language
Documentation
// src/lib.rs - Public API entry point
pub mod core;
pub mod lexer;
pub mod parser;
pub mod semantic;
pub mod borrowck;
pub mod mir;
pub mod codegen;
pub mod lexicon;
pub mod polyglot;
pub mod gfx;
pub mod runtime;
pub mod diag;
pub mod utils;
pub mod visualize;
pub mod astviz;
#[cfg(not(target_arch = "wasm32"))]
pub mod convert;

#[cfg(not(target_arch = "wasm32"))]
pub use ling_audio;

// Re-exports
pub use core::{LingCompiler, CompilerConfig, OptimizationLevel};
pub use lexicon::{CanonicalToken, Lexicon, LexiconRegistry};
pub use polyglot::{normalize_source, ScriptDetector};

// Version constant
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Run a Ling source string through the interpreter.
/// Lexes → parses → executes the `start` binding.
pub fn run(source: &str) -> Result<(), String> {
    run_named(source, None, None)
}

/// Self-extract resources packed into the executable by `ling build --pack`.
///
/// Each `(relative_path, bytes)` is written under a per-app temp directory, then
/// the process's current directory is switched there so the app's relative asset
/// paths (e.g. `music/song.wav`) resolve against the extracted files. A no-op for
/// an empty table.
pub fn unpack_resources(app: &str, resources: &[(&str, &[u8])]) {
    if resources.is_empty() {
        return;
    }
    let base = std::env::temp_dir().join(format!("ling-pack-{app}"));
    for (rel, bytes) in resources {
        let dst = base.join(rel);
        if let Some(parent) = dst.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let _ = std::fs::write(&dst, bytes);
    }
    let _ = std::env::set_current_dir(&base);
}

/// Run with an optional source directory for relative `use` imports.
pub fn run_file(source: &str, source_dir: Option<std::path::PathBuf>) -> Result<(), String> {
    run_named(source, source_dir, None)
}

/// Run with an optional source directory and the source file name (used to label
/// diagnostics). Parse and runtime errors are returned as fully-rendered,
/// colored, localized diagnostics (see [`diag`]).
pub fn run_named(
    source: &str,
    source_dir: Option<std::path::PathBuf>,
    file: Option<&str>,
) -> Result<(), String> {
    let lang = diag::OutputLang::from_env();
    let program = parser::parse(source)
        .map_err(|e| diag::render_parse(&e, source, file, lang))?;
    let mut interp = runtime::Interpreter::new();
    interp.source_dir = source_dir;
    match interp.run_program(&program) {
        Ok(()) => Ok(()),
        Err(msg) => {
            let trace = interp.take_error_trace();
            Err(diag::render_runtime(&msg, source, file, &trace, lang))
        }
    }
}

/// Detect the primary human language used for keywords in a Ling source file.
pub fn detect_language(source: &str) -> &'static str {
    let languages: &[(&[&str], &str)] = &[
        (&["", "灵符", "", "", "", "", "否则", "", "", "", "", "", "格式"], "Chinese (中文)"),
        (&["束縛", "実行", "もし", "一方", "ために", "試す", "待つ", "帰る"], "Japanese (日本語)"),
        (&["바인드", "만약", "동안", "출력", "시작"], "Korean (한국어)"),
        (&["связать", "сделать", "если", "иначе", "пока", "для", "вернуть", "вывести"], "Russian (русский)"),
        (&["ผูก", "ทำ", "ถ้า", "มิฉะนั้น", "สำหรับ", "คืน", "พิมพ์", "รูปแบบ", "เริ่ม"], "Thai (ภาษาไทย)"),
        (&["बाँधो", "करो", "अगर", "जबकि", "वापस", "सत्य"], "Hindi (हिन्दी)"),
        (&["ربط", "افعل", "إذا", "وإلا", "بينما", "أعد"], "Arabic (العربية)"),
        (&["enlazar", "hacer", "mientras", "retornar", "verdadero"], "Spanish (Español)"),
        (&["lier", "faire", "sinon", "tantque", "retourner", "vrai"], "French (Français)"),
        (&["binden", "machen", "wenn", "solange", "zurück", "wahr"], "German (Deutsch)"),
        (&["ligar", "fazer", "enquanto", "retornar", "verdadeiro"], "Portuguese (Português)"),
    ];

    let best = languages.iter()
        .map(|(keywords, lang)| {
            let count = keywords.iter().filter(|&&k| source.contains(k)).count();
            (count, *lang)
        })
        .max_by_key(|&(count, _)| count);

    match best {
        Some((count, lang)) if count > 0 => lang,
        _ => "English",
    }
}