Skip to main content

ling/
lib.rs

1// src/lib.rs - Public API entry point
2pub mod core;
3pub mod lexer;
4pub mod parser;
5pub mod semantic;
6pub mod borrowck;
7pub mod mir;
8pub mod codegen;
9pub mod lexicon;
10pub mod polyglot;
11pub mod gfx;
12pub mod runtime;
13pub mod utils;
14pub mod visualize;
15#[cfg(not(target_arch = "wasm32"))]
16pub mod convert;
17
18#[cfg(not(target_arch = "wasm32"))]
19pub use ling_audio;
20
21// Re-exports
22pub use core::{LingCompiler, CompilerConfig, OptimizationLevel};
23pub use lexicon::{CanonicalToken, Lexicon, LexiconRegistry};
24pub use polyglot::{normalize_source, ScriptDetector};
25
26// Version constant
27pub const VERSION: &str = env!("CARGO_PKG_VERSION");
28
29/// Run a Ling source string through the interpreter.
30/// Lexes → parses → executes the `start` binding.
31pub fn run(source: &str) -> Result<(), String> {
32    run_file(source, None)
33}
34
35/// Run with an optional source directory for relative `use` imports.
36pub fn run_file(source: &str, source_dir: Option<std::path::PathBuf>) -> Result<(), String> {
37    let program = parser::parse(source)
38        .map_err(|e| format!("parse error: {e}"))?;
39    let mut interp = runtime::Interpreter::new();
40    interp.source_dir = source_dir;
41    interp.run_program(&program)
42        .map_err(|e| format!("runtime error: {e}"))
43}
44
45/// Detect the primary human language used for keywords in a Ling source file.
46pub fn detect_language(source: &str) -> &'static str {
47    let languages: &[(&[&str], &str)] = &[
48        (&["令", "灵符", "执", "函", "核", "若", "否则", "历", "于", "配", "归", "印", "格式"], "Chinese (中文)"),
49        (&["束縛", "実行", "もし", "一方", "ために", "試す", "待つ", "帰る"], "Japanese (日本語)"),
50        (&["바인드", "만약", "동안", "출력", "시작"], "Korean (한국어)"),
51        (&["связать", "сделать", "если", "иначе", "пока", "для", "вернуть", "вывести"], "Russian (русский)"),
52        (&["ผูก", "ทำ", "ถ้า", "มิฉะนั้น", "สำหรับ", "คืน", "พิมพ์", "รูปแบบ", "เริ่ม"], "Thai (ภาษาไทย)"),
53        (&["बाँधो", "करो", "अगर", "जबकि", "वापस", "सत्य"], "Hindi (हिन्दी)"),
54        (&["ربط", "افعل", "إذا", "وإلا", "بينما", "أعد"], "Arabic (العربية)"),
55        (&["enlazar", "hacer", "mientras", "retornar", "verdadero"], "Spanish (Español)"),
56        (&["lier", "faire", "sinon", "tantque", "retourner", "vrai"], "French (Français)"),
57        (&["binden", "machen", "wenn", "solange", "zurück", "wahr"], "German (Deutsch)"),
58        (&["ligar", "fazer", "enquanto", "retornar", "verdadeiro"], "Portuguese (Português)"),
59    ];
60
61    let best = languages.iter()
62        .map(|(keywords, lang)| {
63            let count = keywords.iter().filter(|&&k| source.contains(k)).count();
64            (count, *lang)
65        })
66        .max_by_key(|&(count, _)| count);
67
68    match best {
69        Some((count, lang)) if count > 0 => lang,
70        _ => "English",
71    }
72}