feat(value,ir): ref.ambiguous error, exact conversion helpers and mixed builtin calls

- value: add ErrorCategory::RefAmbiguous (code ref.ambiguous) for the S-07 rule
  that a reference matching several resources must never silently pick the
  first one; re-export float/integer conversion helpers at the crate root.
- ir/builtins: math/foreign calls accept positional arguments followed by named
  ones (matching the documented examples) instead of being named-only; arity
  and unknown-name validation still apply.
- tests updated for the new builtin call shape.
This commit is contained in:
cflmy committed 2026-11-20 10:43:50 +08:00
1 parent 8003907f4e
commit 46c9ca2fd3
3 files changed
+18 -10

No files matched your search

+10 -9
View File
@@ -115,14 +115,14 @@ const MATH_EVAL: Builtin = Builtin {
name: "math.eval", name: "math.eval",
aliases: &[], aliases: &[],
params: &[req("formula"), req("bindings"), opt("target")], params: &[req("formula"), req("bindings"), opt("target")],
named_only: true, named_only: false,
effectful: false, effectful: false,
}; };
const MATH_SUBSTITUTE: Builtin = Builtin { const MATH_SUBSTITUTE: Builtin = Builtin {
name: "math.substitute", name: "math.substitute",
aliases: &[], aliases: &[],
params: &[req("formula"), req("bindings")], params: &[req("formula"), req("bindings")],
named_only: true, named_only: false,
effectful: false, effectful: false,
}; };
const MATH_SIMPLIFY: Builtin = Builtin { const MATH_SIMPLIFY: Builtin = Builtin {
@@ -136,21 +136,21 @@ const MATH_DIFFERENTIATE: Builtin = Builtin {
name: "math.differentiate", name: "math.differentiate",
aliases: &[], aliases: &[],
params: &[req("formula"), req("变量")], params: &[req("formula"), req("变量")],
named_only: true, named_only: false,
effectful: false, effectful: false,
}; };
const FOREIGN_RUN: Builtin = Builtin { const FOREIGN_RUN: Builtin = Builtin {
name: "foreign.run", name: "foreign.run",
aliases: &[], aliases: &[],
params: &[req("ref"), opt("input")], params: &[req("ref"), opt("input")],
named_only: true, named_only: false,
effectful: true, effectful: true,
}; };
const FOREIGN_CALL: Builtin = Builtin { const FOREIGN_CALL: Builtin = Builtin {
name: "foreign.call", name: "foreign.call",
aliases: &[], aliases: &[],
params: &[req("ref"), req("input")], params: &[req("ref"), req("input")],
named_only: true, named_only: false,
effectful: true, effectful: true,
}; };
const FS_READ: Builtin = Builtin { const FS_READ: Builtin = Builtin {
@@ -254,7 +254,7 @@ mod tests {
fn lookup_aliases() { fn lookup_aliases() {
assert_eq!(find("print").unwrap().name, "打印"); assert_eq!(find("print").unwrap().name, "打印");
assert_eq!(find("打印").unwrap().name, "打印"); assert_eq!(find("打印").unwrap().name, "打印");
assert!(find("math.eval").unwrap().named_only); assert!(!find("math.eval").unwrap().named_only);
assert!(find("nope").is_none()); assert!(find("nope").is_none());
} }
@@ -270,8 +270,9 @@ mod tests {
&parse_arguments("(formula=ref(\"#x\"), bindings={}, target=\"A\")").unwrap() &parse_arguments("(formula=ref(\"#x\"), bindings={}, target=\"A\")").unwrap()
) )
.is_ok()); .is_ok());
// math.eval is named-only: positional arguments are rejected. // math.eval accepts positional arguments followed by named ones.
assert!(validate_args(eval, &parse_arguments("(ref(\"#x\"), {})").unwrap()).is_err()); assert!(validate_args(eval, &parse_arguments("(ref(\"#x\"), {})").unwrap()).is_ok());
assert!(validate_args(eval, &parse_arguments("(1, 2)").unwrap()).is_err()); assert!(validate_args(eval, &parse_arguments("(formula=1, nope=2)").unwrap()).is_err());
assert!(validate_args(eval, &parse_arguments("(1, 2, 3, 4)").unwrap()).is_err());
} }
} }
+4
View File
@@ -35,6 +35,9 @@ pub enum ErrorCategory {
MathUnsupported, MathUnsupported,
/// Plugin ABI negotiation failed. /// Plugin ABI negotiation failed.
PluginAbi, PluginAbi,
/// A resource reference matches more than one resource (S-07: never pick
/// the first silently).
RefAmbiguous,
/// A plugin reported an execution failure. /// A plugin reported an execution failure.
PluginFailure, PluginFailure,
/// A foreign kernel failed (non-zero exit, protocol violation). /// A foreign kernel failed (non-zero exit, protocol violation).
@@ -62,6 +65,7 @@ impl ErrorCategory {
ErrorCategory::KernelUnavailable => codes::KERNEL_UNAVAILABLE, ErrorCategory::KernelUnavailable => codes::KERNEL_UNAVAILABLE,
ErrorCategory::MathUnsupported => codes::MATH_UNSUPPORTED, ErrorCategory::MathUnsupported => codes::MATH_UNSUPPORTED,
ErrorCategory::PluginAbi => codes::PLUGIN_ABI_MISMATCH, ErrorCategory::PluginAbi => codes::PLUGIN_ABI_MISMATCH,
ErrorCategory::RefAmbiguous => "ref.ambiguous",
ErrorCategory::PluginFailure => "plugin.failure", ErrorCategory::PluginFailure => "plugin.failure",
ErrorCategory::ForeignFailure => "foreign.failure", ErrorCategory::ForeignFailure => "foreign.failure",
ErrorCategory::HostError => "host.error", ErrorCategory::HostError => "host.error",
+4 -1
View File
@@ -13,5 +13,8 @@ pub mod wire;
pub use diagnostic::{codes, Diagnostic, Related, Severity}; pub use diagnostic::{codes, Diagnostic, Related, Severity};
pub use error::{CallFrame, ErrorCategory, RuntimeError}; pub use error::{CallFrame, ErrorCategory, RuntimeError};
pub use marqdo_span::{LineCol, Span}; pub use marqdo_span::{LineCol, Span};
pub use value::{CodeValue, FormulaValue, FunctionRef, Table, Value}; pub use value::{
bigint_to_exact_f64, float_to_bigint, format_float, CodeValue, FormulaValue, FunctionRef,
Table, Value,
};
pub use wire::{from_wire, to_wire, WireError, WIRE_VERSION}; pub use wire::{from_wire, to_wire, WireError, WIRE_VERSION};