Skip to main content

ling/
entry.rs

1use crate::parser::ast::{Expr, Item};
2
3/// Entry-point binding names recognised across Ling's supported human languages.
4/// Shared by the tree-walker and the MIR backends so every execution path agrees
5/// on which top-level binding starts the program.
6pub const ENTRY_NAMES: &[&str] = &[
7    "start",
8    "main",
9    "启",
10    "เริ่ม",
11    "시작",
12    "начать",
13    "начало",
14    "inicio",
15    "comenzar",
16    "début",
17    "commencer",
18    "anfang",
19    "starten",
20    "início",
21    "शुरू",
22    "ابدأ",
23];
24
25/// Resolve the entry binding among top-level items: a bind named after a known
26/// entry keyword, otherwise the first bind whose value is a `do { }` block.
27/// Mirrors the tree-walker's `find_entry`.
28pub fn entry_name(items: &[Item]) -> Option<String> {
29    for key in ENTRY_NAMES {
30        if items
31            .iter()
32            .any(|i| matches!(i, Item::Bind(n, _) if n == key))
33        {
34            return Some((*key).to_string());
35        }
36    }
37    items.iter().find_map(|i| match i {
38        Item::Bind(n, Expr::Do(_)) => Some(n.clone()),
39        _ => None,
40    })
41}