feat: support bare name assignments in interactive and function scopes

This commit is contained in:
2026-10-20 18:16:54 +08:00
parent 6281e58647
commit df1b77fc1a
17 changed files with 259 additions and 37 deletions
+2
View File
@@ -2,4 +2,6 @@
[pipeline.rs](pipeline.rs) 将真实 Endor 示例交给 reader/parser/名称分析/代码生成/VM,检查函数值、前向引用、局部遮蔽、精确数字常量、语义诊断、宿主全局与属性回调、对象结果保活、跨 VM 模板装载、反复调用、错误帧清理、递归限额、共享交互命名空间及超过 255 的操作数。
裸赋值检查覆盖跨交互输入创建/更新 globals、参数重赋值、局部槽复用及未初始化读取;执行结束检查空堆。
同时检查重复名称、局部提前读取、未声明名称、不支持的执行特性和不合法 AST 原文范围。CLI 的文件/stdin 与退出码检查见 [cli/tests](https://gitea.entroia.com/Zayden-Jung/endorphin-lyra-cli/src/branch/main/crates/cli/tests/README.md)。源码说明验证意图;仅由 ces-control 的 cargo test --workspace --locked 执行,本地不运行,结果边界见 [验证](../../../docs/operations/validation.md)。返回 [compiler](../README.md)。
+78
View File
@@ -368,3 +368,81 @@ fn compiling_ast_rejects_invalid_source_ranges() {
}
assert!(compile_ast(&root, &source, &CompileOptions::default()).is_err());
}
#[test]
fn bare_assignments_create_and_replace_real_interactive_globals() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
for text in ["value = \"first\"", "value = \"second\"", "value"] {
let options = CompileOptions {
globals: vm
.dict_items(globals)
.unwrap()
.iter()
.filter_map(|(key, _)| match key {
Value::Text(name) => Some(name.clone()),
_ => None,
})
.collect(),
};
let compiled = compile_source(
&EndorReader::from_text("<session>", text),
ParseMode::Interactive,
&options,
)
.unwrap();
let code = compiled.load(&mut vm).unwrap();
let result = vm.execute(code, globals).unwrap();
if text == "value" {
assert_eq!(result, Value::Text("second".into()));
}
vm.release_value(result).unwrap();
vm.release(code).unwrap();
}
assert_eq!(
vm.dict_get(globals, &Value::Text("value".into())).unwrap(),
Some(Value::Text("second".into()))
);
vm.dict_clear(globals).unwrap();
vm.release(globals).unwrap();
vm.collect_cycles();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn assignments_update_parameters_and_allocate_one_local_slot_per_target() {
let compiled = compile(
"function main(value: String): String { copy = value copy = \"updated\" value = copy return value }",
ParseMode::Module,
);
let mut vm = Vm::new().unwrap();
let (code, globals) = module(&mut vm, &compiled);
let result = vm
.call(
function(&vm, globals, "main"),
&[Value::Text("original".into())],
)
.unwrap();
assert_eq!(result, Value::Text("updated".into()));
vm.release_value(result).unwrap();
cleanup(&mut vm, code, globals);
assert!(
compile_source(
&EndorReader::from_text("<uninitialized>", "function f(): T { x = x return x }"),
ParseMode::Module,
&CompileOptions::default()
)
.is_err()
);
assert!(
compile_source(
&EndorReader::from_text(
"<duplicate>",
"function f(): T { let x = 1 let x = 2 return x }"
),
ParseMode::Module,
&CompileOptions::default()
)
.is_err()
);
}