Files
marqdo/src/aliases.rs
T
cflmy 050d71927f feat(foreign): persistent Python/Node kernels and state bridge
F3/F3.5: session_open/eval/close/list, session_set/get/names, Python and Node bootstraps, strict serial protocol, per-session timeout, crash isolation, drop cleanup, and node/bash gold tests (python conditional).
2026-10-18 01:32:48 +08:00

228 lines
9.8 KiB
Rust

//! Canonical names and Chinese aliases for L0 keywords / builtins / params.
//!
//! See `doc/design/keywords-i18n.md`. No language mode — aliases are equivalent.
/// Map a literal / logic keyword to its English canonical form, if reserved.
pub fn canonical_keyword(name: &str) -> Option<&'static str> {
match name {
"True" | "真" => Some("True"),
"False" | "假" => Some("False"),
"None" | "空" => Some("None"),
"and" | "且" => Some("and"),
"or" | "或" => Some("or"),
"not" | "非" => Some("not"),
_ => None,
}
}
pub fn is_reserved_keyword(name: &str) -> bool {
canonical_keyword(name).is_some()
}
/// Map a builtin callee (EN or ZH alias) to its English canonical name.
/// Host primitives without ZH L0 aliases only match English.
pub fn canonical_builtin(name: &str) -> Option<&'static str> {
if let Some(h) = crate::host::HostFn::from_name(name) {
return Some(h.name());
}
match name {
"print" | "打印" => Some("print"),
"input" | "输入" => Some("input"),
"len" | "长度" => Some("len"),
"str" | "文本" => Some("str"),
"int" | "整数" => Some("int"),
"type" => Some("type"),
"trim" => Some("trim"),
"split" => Some("split"),
"join" => Some("join"),
"at" => Some("at"),
"call_fn" | "调用" => Some("call_fn"),
_ => None,
}
}
/// Normalize a named-argument key for a builtin to the English param name.
pub fn canonical_param(builtin: &str, param: &str) -> String {
let b = canonical_builtin(builtin).unwrap_or(builtin);
match (b, param) {
("print", "text" | "内容") => "text".into(),
("input", "prompt" | "提示") => "prompt".into(),
("len" | "str" | "int" | "type" | "trim", "value" | "值") => "value".into(),
("split" | "join", "value" | "值") => "value".into(),
("split" | "join", "sep" | "分隔") => "sep".into(),
("at", "value" | "值") => "value".into(),
("at", "index" | "下标") => "index".into(),
("call_fn", "name" | "名") => "name".into(),
(
"host_writeback_record" | "host_writeback_get" | "host_writeback_clear",
"key" | "键",
) => "key".into(),
("host_writeback_ensure", "key" | "键") => "key".into(),
(
"host_writeback_ensure",
"placeholder" | "占位",
) => "placeholder".into(),
(
"host_writeback_record" | "host_writeback_get" | "host_writeback_clear",
"at_end" | "末尾",
) => "at_end".into(),
(
"host_writeback_record"
| "host_writeback_get"
| "host_writeback_clear"
| "host_writeback_ensure",
"line" | "行",
) => "line".into(),
(
"host_writeback_record",
"value" | "值",
) => "value".into(),
(
"host_plot" | "host_plot_points" | "host_plot_conic",
"grid" | "网格",
) => "grid".into(),
("host_plot" | "host_plot_points" | "host_plot_conic", "path" | "路径") => {
"path".into()
}
("host_dotenv_load" | "load_dotenv", "path" | "路径") => "path".into(),
("host_meta_get" | "meta_get", "key" | "键") => "key".into(),
("host_exec" | "exec", "cmd" | "命令") => "cmd".into(),
("host_exec" | "exec", "args" | "参数") => "args".into(),
("host_exec" | "exec", "capture" | "捕获") => "capture".into(),
(
"host_stream_publish" | "stream_publish",
"event" | "事件",
) => "event".into(),
(
"host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_post_sse"
| "http_post_sse" | "host_http_request" | "http_request",
"headers" | "头",
) => "headers".into(),
(
"host_http_post" | "http_post" | "host_http_post_sse" | "http_post_sse"
| "host_http_request" | "http_request",
"content_type" | "类型",
) => "content_type".into(),
(
"host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_post_sse"
| "http_post_sse" | "host_http_request" | "http_request",
"url" | "地址",
) => "url".into(),
(
"host_http_post" | "http_post" | "host_http_post_sse" | "http_post_sse"
| "host_http_request" | "http_request",
"body" | "内容",
) => "body".into(),
(
"host_http_post_sse" | "http_post_sse" | "host_openai_sse_parse" | "openai_sse_parse",
"echo" | "打印增量" | "print_deltas",
) => "echo".into(),
(
"host_openai_sse_parse" | "openai_sse_parse",
"text" | "内容",
) => "text".into(),
("host_json_stringify" | "stringify", "indent" | "缩进") => "indent".into(),
("host_plot", "steps" | "步数") => "steps".into(),
("host_plot", "derivative" | "导数") => "derivative".into(),
("host_foreign_run" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many" | "host_foreign_session_eval", "code" | "代码") => "code".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke"
| "host_foreign_call",
"stdin" | "标准输入",
) => "stdin".into(),
(
"host_foreign_set_cmd"
| "host_foreign_run_lang"
| "host_foreign_invoke"
| "host_foreign_probe"
| "host_foreign_register"
| "host_foreign_unset"
| "host_foreign_call"
| "host_foreign_call_many"
| "host_foreign_session_open",
"lang" | "语言",
) => "lang".into(),
(
"host_foreign_set_cmd"
| "host_foreign_invoke"
| "host_foreign_register"
| "host_foreign_call_many"
| "host_foreign_session_open",
"cmd" | "命令",
) => "cmd".into(),
("host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many" | "host_foreign_session_eval", "source" | "源码") => "source".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many" | "host_foreign_session_open" | "host_foreign_session_eval",
"env" | "环境",
) => "env".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many" | "host_foreign_session_open" | "host_foreign_session_eval",
"cwd" | "工作目录",
) => "cwd".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call" | "host_foreign_call_many" | "host_foreign_session_open" | "host_foreign_session_eval",
"timeout" | "超时",
) => "timeout".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke"
| "host_foreign_register" | "host_foreign_call" | "host_foreign_call_many",
"args" | "参数",
) => "args".into(),
(
"host_foreign_run" | "host_foreign_run_lang" | "host_foreign_invoke" | "host_foreign_call_many",
"max_output" | "最大输出",
) => "max_output".into(),
("host_foreign_invoke" | "host_foreign_call" | "host_foreign_session_eval", "input" | "输入") => "input".into(),
("host_foreign_session_eval" | "host_foreign_session_set" | "host_foreign_session_get" | "host_foreign_session_names" | "host_foreign_session_close", "id" | "标识") => "id".into(),
("host_foreign_session_set" | "host_foreign_session_get", "name" | "名称") => "name".into(),
("host_foreign_session_set", "value" | "值") => "value".into(),
("host_foreign_invoke", "output" | "输出") => "output".into(),
("host_foreign_invoke", "keep" | "保留") => "keep".into(),
("host_foreign_register", "template" | "模板") => "template".into(),
("host_foreign_register", "ext" | "扩展名") => "ext".into(),
("host_foreign_register", "version_args" | "版本参数") => "version_args".into(),
("host_foreign_load_config", "path" | "路径") => "path".into(),
("host_foreign_run_many", "runs" | "任务表") => "runs".into(),
("host_foreign_call_many", "inputs" | "输入表") => "inputs".into(),
("host_foreign_run_many" | "host_foreign_call_many", "concurrency" | "并发") => "concurrency".into(),
("host_ext_native_path", "name" | "名") => "name".into(),
_ => param.to_string(),
}
}
/// Rewrite callee + named args to English canonical forms when this is a builtin.
/// Returns the callee to dispatch on (canonical builtin or original user/fn name).
pub fn normalize_call_callee_and_args(callee: &str, args: &mut [crate::ast::Arg]) -> String {
match canonical_builtin(callee) {
Some(canon) => {
for a in args.iter_mut() {
if let crate::ast::Arg::Named { name, .. } = a {
*name = canonical_param(canon, name);
}
}
canon.to_string()
}
None => callee.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keyword_aliases() {
assert_eq!(canonical_keyword("真"), Some("True"));
assert_eq!(canonical_keyword("且"), Some("and"));
assert!(is_reserved_keyword("空"));
}
#[test]
fn builtin_aliases() {
assert_eq!(canonical_builtin("打印"), Some("print"));
assert_eq!(canonical_builtin("长度"), Some("len"));
assert_eq!(canonical_param("打印", "内容"), "text");
assert_eq!(canonical_param("print", "内容"), "text");
}
}