ext(00-08): RFC-EXT-0003 core/extension boundary, math detach and release stdlib
Implements issue #3 (RFC-EXT-0003) slices on the post-3.0.0-beta.1 tree. PR-00/01/H-06: reproduce and fix sibling heading mode inheritance. build_units now pops the current and same-or-higher-level headings from the ancestor stack before deriving the inherited mode, so an unmarked heading can never inherit the previous sibling. Covered by the units matrix, conformance fixtures 22/23, and black-box heading_inheritance through check --emit/run/call. PR-02/H-08: the deterministic release archive now ships the complete verified stdlib/ tree. package.py and smoke.py recompute the runtime tree/alias digests, record them in RELEASE_INFO.json.stdlib, and fail on mismatch; smoke removes the bundle and requires a fail-closed run. Release discovery no longer falls back to the compile-time source tree (debug builds keep that development convenience). PR-03/H-04: one marqdo_host::CORE_RESOURCE_KINDS registry is used by both the CLI provider-manifest validator and every trusted host registration path; all six core kinds are rejected as provider name or declared kind before registration. H-03: docs/architecture/resource-operations.md records every reviewed resource_request operation and architecture_boundaries pins the exact allowlist; new domain operations are forbidden in core (ADR-0062). PR-04/H-01: remove math.parse/eval/substitute/simplify/differentiate from BUILTINS, HostOp, the runtime, the host kernel and the workspace (crates/marqdo-math deleted). Formula stays inert; Int/Float arithmetic stays. Math algorithms are a pure examples/math-library package plus an optional examples/math-backend ABI plugin that requests the math.compute capability (ADR-0061). Removed calls are static symbol.unknown; the math import namespace is released again. Tests/examples/specs migrated; new math_library black-box test. PR-05/06 Step A: ADR-0060 classifies @exec/@knowledge as the closed Core Modes and specifies the Proposed metadata-only Heading Annotations v1 (target 3.1, sem.invalid_mode_marker unchanged, Analysis v2 unchanged). ADR-0063 records the foreign F-A retention and deferred F-B removal. Docs/status/compatibility/changelog and the RFC-EXT-0003 evidence file are updated. Full local gate: cargo fmt PASS, clippy -D warnings PASS, workspace tests 586 passed/0 failed/2 ignored, python tools/ci 28 passed.
This commit is contained in:
1 parent
0fe5f42a62
commit
a67f344b6b
88 files changed
+2633
-3503
No files matched your search
@@ -15,7 +15,6 @@ marqdo-expression = { workspace = true }
|
||||
marqdo-markdown = { workspace = true }
|
||||
marqdo-ir = { workspace = true }
|
||||
marqdo-frontend = { workspace = true }
|
||||
marqdo-math = { workspace = true }
|
||||
marqdo-foreign = { workspace = true }
|
||||
marqdo-plugin-abi = { workspace = true }
|
||||
marqdo-host = { workspace = true }
|
||||
|
||||
@@ -49,11 +49,10 @@ pub const MAX_PROVIDER_ARTIFACT_BYTES: u64 = 256 * 1024 * 1024;
|
||||
/// Maximum manifest size (1 MiB); a manifest is metadata, not an artifact.
|
||||
pub const MAX_PROVIDER_MANIFEST_BYTES: u64 = 1024 * 1024;
|
||||
|
||||
/// Core resource kinds owned by the host; third parties may never override
|
||||
/// them (RFC-EXT-0001 §5.3.1). `stdio`/`file`/`tcp`/`tls`/`udp`/`tcp-listener`
|
||||
/// are dispatched by `marqdo-host` before the provider registry is consulted.
|
||||
pub const RESERVED_RESOURCE_KINDS: &[&str] =
|
||||
&["stdio", "file", "tcp", "tls", "udp", "tcp-listener"];
|
||||
/// Core resource kinds are owned by the host and may never be overridden by a
|
||||
/// provider (RFC-EXT-0001 §5.3.1, ADR-0062). The single source of truth is
|
||||
/// [`marqdo_host::CORE_RESOURCE_KINDS`]; this alias keeps the CLI wording.
|
||||
pub const RESERVED_RESOURCE_KINDS: &[&str] = marqdo_host::CORE_RESOURCE_KINDS;
|
||||
|
||||
/// Raw `provider.json` schema version 2 document.
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -835,13 +834,26 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserved_kind_is_rejected() {
|
||||
fn every_core_kind_is_rejected_as_name_and_as_kind() {
|
||||
let dir = tempdir("reserved");
|
||||
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
|
||||
let manifest = write_manifest(&dir, &manifest_text("demo.sensor", &["file"], &path, &sha));
|
||||
let error = read_provider_manifests(&[manifest]).unwrap_err();
|
||||
assert_eq!(error.code, "ext.manifest_invalid");
|
||||
assert!(error.message.contains("reserved"), "{}", error.message);
|
||||
for core_kind in marqdo_host::CORE_RESOURCE_KINDS {
|
||||
let as_kind = write_manifest(
|
||||
&dir,
|
||||
&manifest_text("demo.sensor", &[core_kind], &path, &sha),
|
||||
);
|
||||
let error = read_provider_manifests(&[as_kind]).unwrap_err();
|
||||
assert_eq!(error.code, "ext.manifest_invalid");
|
||||
assert!(error.message.contains("reserved"), "{}", error.message);
|
||||
|
||||
let as_name = write_manifest(
|
||||
&dir,
|
||||
&manifest_text(core_kind, &["demo.sensor"], &path, &sha),
|
||||
);
|
||||
let error = read_provider_manifests(&[as_name]).unwrap_err();
|
||||
assert_eq!(error.code, "ext.manifest_invalid");
|
||||
assert!(error.message.contains("reserved"), "{}", error.message);
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
|
||||
+60
-13
@@ -46,25 +46,39 @@ impl StdlibBundle {
|
||||
/// Discover the bundle.
|
||||
///
|
||||
/// Order: `MARQDO_STDLIB_DIR` (explicit, fail if invalid), a `stdlib`
|
||||
/// directory next to the executable, then the development tree.
|
||||
/// directory next to the executable, and finally — **debug builds only** — the
|
||||
/// compile-time source tree. A release binary never falls back to a build
|
||||
/// machine path: the release archive must ship a verified `stdlib/` next to
|
||||
/// the binary (RFC-EXT-0003 H-08, ADR).
|
||||
pub fn discover() -> Result<Option<Self>, String> {
|
||||
if let Some(dir) = std::env::var_os(STDLIB_DIR_ENV) {
|
||||
return Self::load(PathBuf::from(dir)).map(Some);
|
||||
}
|
||||
if let Ok(exe) = std::env::current_exe() {
|
||||
if let Some(parent) = exe.parent() {
|
||||
let candidate = parent.join("stdlib");
|
||||
if candidate.is_dir() {
|
||||
return Self::load(candidate).map(Some);
|
||||
}
|
||||
Self::discover_from_exe_dir(
|
||||
std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|exe| exe.parent().map(Path::to_path_buf)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Discover from the directory that holds the executable, without the explicit
|
||||
/// environment override. Release builds do not read the source tree.
|
||||
fn discover_from_exe_dir(exe_dir: Option<PathBuf>) -> Result<Option<Self>, String> {
|
||||
if let Some(parent) = exe_dir {
|
||||
let candidate = parent.join("stdlib");
|
||||
if candidate.is_dir() {
|
||||
return Self::load(candidate).map(Some);
|
||||
}
|
||||
}
|
||||
let dev = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("..")
|
||||
.join("stdlib");
|
||||
if dev.is_dir() {
|
||||
return Self::load(dev).map(Some);
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let dev = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("..")
|
||||
.join("stdlib");
|
||||
if dev.is_dir() {
|
||||
return Self::load(dev).map(Some);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
@@ -457,6 +471,39 @@ mod tests {
|
||||
assert!(bundle.resolve("../escape").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sibling_stdlib_is_found_without_environment_override() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let bundle = dir.path().join("stdlib");
|
||||
std::fs::create_dir_all(&bundle).unwrap();
|
||||
for entry in std::fs::read_dir(dev_bundle()).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let target = bundle.join(entry.file_name());
|
||||
if entry.path().is_dir() {
|
||||
copy_dir(&entry.path(), &target);
|
||||
} else {
|
||||
std::fs::copy(entry.path(), target).unwrap();
|
||||
}
|
||||
}
|
||||
let found = StdlibBundle::discover_from_exe_dir(Some(dir.path().to_path_buf()))
|
||||
.unwrap()
|
||||
.expect("sibling stdlib");
|
||||
assert_eq!(found.root, bundle.canonicalize().unwrap());
|
||||
}
|
||||
|
||||
fn copy_dir(source: &std::path::Path, target: &std::path::Path) {
|
||||
std::fs::create_dir_all(target).unwrap();
|
||||
for entry in std::fs::read_dir(source).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let destination = target.join(entry.file_name());
|
||||
if entry.path().is_dir() {
|
||||
copy_dir(&entry.path(), &destination);
|
||||
} else {
|
||||
std::fs::copy(entry.path(), destination).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digest_tamper_is_rejected() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -30,7 +30,6 @@ const WORKSPACE_CRATES: &[&str] = &[
|
||||
"marqdo-markdown",
|
||||
"marqdo-ir",
|
||||
"marqdo-frontend",
|
||||
"marqdo-math",
|
||||
"marqdo-foreign",
|
||||
"marqdo-plugin-abi",
|
||||
"marqdo-provider-worker",
|
||||
@@ -387,6 +386,148 @@ fn official_stdlib_public_aliases_are_verified_metadata() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reviewed ownership of every `Host::resource_request` operation
|
||||
/// (`docs/architecture/resource-operations.md`, ADR-0062).
|
||||
///
|
||||
/// The gate extracts the literal operation names dispatched inside
|
||||
/// `resource_request` and compares them with this reviewed allowlist. Adding a new
|
||||
/// domain operation requires updating the architecture doc, the ADR and this list —
|
||||
/// a bare source change cannot silently widen the core/resource boundary.
|
||||
#[test]
|
||||
fn resource_request_operation_allowlist_is_reviewed() {
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
const DOMAIN_OPERATIONS: &[&str] = &[
|
||||
"process.run",
|
||||
"regex.match",
|
||||
"regex.captures",
|
||||
"compress.gzip",
|
||||
"compress.gunzip",
|
||||
"random.bytes",
|
||||
"env.get",
|
||||
"clock.now",
|
||||
];
|
||||
const FS_OPERATIONS: &[&str] = &[
|
||||
"read_at", "append", "walk", "exists", "metadata", "list", "mkdir", "remove", "rename",
|
||||
];
|
||||
|
||||
let source = read(&repo_root().join("crates/marqdo-host/src/host.rs"));
|
||||
let impl_start = source
|
||||
.find("impl Host for StdHost {")
|
||||
.expect("StdHost must implement Host");
|
||||
let start = source[impl_start..]
|
||||
.find(" fn resource_request(")
|
||||
.expect("StdHost::resource_request must exist")
|
||||
+ impl_start;
|
||||
let rest = &source[start + 1..];
|
||||
let end = rest
|
||||
.find("\n fn consume_fuel(")
|
||||
.expect("resource_request must end before consume_fuel");
|
||||
let body = &rest[..end];
|
||||
|
||||
let mut domain = BTreeSet::new();
|
||||
for line in body.lines() {
|
||||
let mut cursor = line;
|
||||
while let Some(index) = cursor.find("operation == \"") {
|
||||
let text = &cursor[index + "operation == \"".len()..];
|
||||
let name = text.split('"').next().expect("operation literal");
|
||||
domain.insert(name.to_string());
|
||||
cursor = &text[name.len()..];
|
||||
}
|
||||
}
|
||||
let expected_domain: BTreeSet<String> = DOMAIN_OPERATIONS
|
||||
.iter()
|
||||
.map(|name| (*name).to_string())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
domain, expected_domain,
|
||||
"new/changed resource_request domain operation; update docs/architecture/resource-operations.md, ADR-0062 and this allowlist"
|
||||
);
|
||||
|
||||
assert!(
|
||||
body.contains("strip_prefix(\"fs.\")"),
|
||||
"the fs.* operation group must stay the only unprefixed dispatch path"
|
||||
);
|
||||
let mut fs = BTreeSet::new();
|
||||
for line in body.lines() {
|
||||
let mut cursor = line;
|
||||
while let Some(index) = cursor.find("name == \"") {
|
||||
let text = &cursor[index + "name == \"".len()..];
|
||||
let name = text.split('"').next().expect("fs operation literal");
|
||||
fs.insert(name.to_string());
|
||||
cursor = &text[name.len()..];
|
||||
}
|
||||
}
|
||||
// The remaining fs operations are grouped in the arity match block.
|
||||
let arity_start = body
|
||||
.find("let expected = match name {")
|
||||
.expect("fs arity match block");
|
||||
let arity_end = body[arity_start..]
|
||||
.find(" };")
|
||||
.expect("fs arity match block end");
|
||||
for line in body[arity_start..arity_start + arity_end].lines() {
|
||||
let Some(arrow) = line.find("=>") else {
|
||||
continue;
|
||||
};
|
||||
let mut in_quote = false;
|
||||
let mut name = String::new();
|
||||
for ch in line[..arrow].chars() {
|
||||
if ch == '"' {
|
||||
if in_quote && !name.is_empty() {
|
||||
fs.insert(name.clone());
|
||||
}
|
||||
name.clear();
|
||||
in_quote = !in_quote;
|
||||
} else if in_quote {
|
||||
name.push(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
let expected_fs: BTreeSet<String> = FS_OPERATIONS
|
||||
.iter()
|
||||
.map(|name| (*name).to_string())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
fs, expected_fs,
|
||||
"new/changed fs.* resource operation; update docs/architecture/resource-operations.md, ADR-0062 and this allowlist"
|
||||
);
|
||||
}
|
||||
|
||||
/// RFC-EXT-0003 H-01: the CLI/host must not link the removed domain math
|
||||
/// kernel and `resource_request` must not dispatch a math.* domain operation.
|
||||
#[test]
|
||||
fn domain_math_kernel_is_not_part_of_the_core() {
|
||||
let root = repo_root();
|
||||
for manifest in [
|
||||
"Cargo.toml",
|
||||
"crates/marqdo-host/Cargo.toml",
|
||||
"crates/cli/Cargo.toml",
|
||||
] {
|
||||
let text = read(&root.join(manifest));
|
||||
assert!(
|
||||
!text.contains("marqdo-math"),
|
||||
"{manifest} must not depend on the removed domain math kernel"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
!root.join("crates/marqdo-math").exists(),
|
||||
"crates/marqdo-math was removed from the core repository"
|
||||
);
|
||||
let host = read(&root.join("crates/marqdo-host/src/host.rs"));
|
||||
for removed in [
|
||||
"MathEval",
|
||||
"MathParse",
|
||||
"MathSubstitute",
|
||||
"MathSimplify",
|
||||
"MathDifferentiate",
|
||||
] {
|
||||
assert!(
|
||||
!host.contains(removed),
|
||||
"host.rs still dispatches {removed}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
let mut out = String::with_capacity(bytes.len() * 2);
|
||||
for byte in bytes {
|
||||
|
||||
@@ -140,12 +140,12 @@ fn stdout(output: &Output) -> String {
|
||||
fn import_alias_conflicting_with_builtin_namespace_is_rejected() {
|
||||
let dir = TempDir::new("call01");
|
||||
write(
|
||||
&dir.path().join("math.mq.md"),
|
||||
"## @exec eval\n\n`params(a: Int)`\n\n`return a + 100`\n",
|
||||
&dir.path().join("resource.mq.md"),
|
||||
"## @exec select\n\n`params(a: Int)`\n\n`return a + 100`\n",
|
||||
);
|
||||
write(
|
||||
&dir.path().join("index.mq.md"),
|
||||
"---\nimport:\n math: \"./math.mq.md\"\n---\n\n## @exec main\n\n`return math.eval(a=1)`\n",
|
||||
"---\nimport:\n resource: \"./resource.mq.md\"\n---\n\n## @exec main\n\n`return resource.select(a=1)`\n",
|
||||
);
|
||||
let check = cli(&["check", "index.mq.md"], dir.path());
|
||||
assert_eq!(check.status.code(), Some(1), "{}", stderr(&check));
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
//! Black-box regression tests for RFC-EXT-0003 H-06 sibling heading modes.
|
||||
//!
|
||||
//! A heading that does not declare `@exec`/`@knowledge` inherits its nearest
|
||||
//! *ancestor*, never the previous sibling. These tests drive the real `marqdo`
|
||||
//! binary and assert that `check --emit`, `run` and `call` agree.
|
||||
|
||||
#![cfg(unix)]
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
static COUNTER: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new(tag: &str) -> Self {
|
||||
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"marqdo-heading-test-{}-{id}-{tag}",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn write(&self, name: &str, text: &str) {
|
||||
std::fs::write(self.0.join(name), text).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn cli(args: &[&str], dir: &Path) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn stderr(output: &Output) -> String {
|
||||
String::from_utf8_lossy(&output.stderr).to_string()
|
||||
}
|
||||
|
||||
fn stdout(output: &Output) -> String {
|
||||
String::from_utf8_lossy(&output.stdout).to_string()
|
||||
}
|
||||
|
||||
fn units(document: &serde_json::Value) -> Vec<&serde_json::Value> {
|
||||
document["modules"][0]["units"]
|
||||
.as_array()
|
||||
.expect("units array")
|
||||
.iter()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn unit<'a>(document: &'a serde_json::Value, name: &str) -> &'a serde_json::Value {
|
||||
units(document)
|
||||
.into_iter()
|
||||
.find(|unit| unit["name"] == name)
|
||||
.unwrap_or_else(|| panic!("missing unit {name}"))
|
||||
}
|
||||
|
||||
fn exec_document() -> &'static str {
|
||||
"# @knowledge 数学\n\n## @exec main\n\n`return 1`\n\n## 普通说明\n\n这段是知识叙述。\n"
|
||||
}
|
||||
|
||||
fn knowledge_document() -> &'static str {
|
||||
"# @exec 项目\n\n## @knowledge 解释\n\n背景。\n\n## main\n\n`return 2`\n"
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_emit_marks_unmarked_sibling_knowledge() {
|
||||
let dir = TempDir::new("emit-knowledge");
|
||||
dir.write("index.mq.md", exec_document());
|
||||
let analysis = dir.path().join("analysis.json");
|
||||
let check = cli(
|
||||
&["check", "index.mq.md", "--emit", analysis.to_str().unwrap()],
|
||||
dir.path(),
|
||||
);
|
||||
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
|
||||
let document: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(&analysis).unwrap()).unwrap();
|
||||
assert_eq!(unit(&document, "数学")["kind"], "namespace");
|
||||
assert_eq!(unit(&document, "main")["kind"], "function");
|
||||
assert_eq!(unit(&document, "普通说明")["kind"], "knowledge");
|
||||
assert!(unit(&document, "普通说明")["function"].is_null());
|
||||
// The analysis exposes exactly one function and it is the Exec sibling.
|
||||
let functions = document["modules"][0]["functions"].as_array().unwrap();
|
||||
assert_eq!(functions.len(), 1);
|
||||
assert_eq!(functions[0]["local_name"], "main");
|
||||
assert_eq!(functions[0]["namespace"], "数学");
|
||||
assert_eq!(functions[0]["unit_id"], unit(&document, "main")["id"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_emit_marks_unmarked_sibling_exec() {
|
||||
let dir = TempDir::new("emit-exec");
|
||||
dir.write("index.mq.md", knowledge_document());
|
||||
let analysis = dir.path().join("analysis.json");
|
||||
let check = cli(
|
||||
&["check", "index.mq.md", "--emit", analysis.to_str().unwrap()],
|
||||
dir.path(),
|
||||
);
|
||||
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
|
||||
let document: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(analysis).unwrap()).unwrap();
|
||||
assert_eq!(unit(&document, "解释")["kind"], "knowledge");
|
||||
assert_eq!(unit(&document, "main")["kind"], "function");
|
||||
let functions = document["modules"][0]["functions"].as_array().unwrap();
|
||||
assert_eq!(functions.len(), 1);
|
||||
assert_eq!(functions[0]["local_name"], "main");
|
||||
assert_eq!(functions[0]["namespace"], "项目");
|
||||
assert_eq!(functions[0]["unit_id"], unit(&document, "main")["id"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_and_call_not_callable_knowledge_sibling() {
|
||||
let dir = TempDir::new("call");
|
||||
dir.write("index.mq.md", exec_document());
|
||||
let run = cli(&["run", "index.mq.md"], dir.path());
|
||||
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
|
||||
// `run` streams program stdout only; the returned value is reported by
|
||||
// `call`, whose output is asserted below and must agree.
|
||||
assert!(stdout(&run).is_empty());
|
||||
|
||||
let call = cli(&["call", "index.mq.md", "数学.main"], dir.path());
|
||||
assert_eq!(call.status.code(), Some(0), "{}", stderr(&call));
|
||||
assert_eq!(stdout(&call), "1\n");
|
||||
|
||||
let knowledge_sibling = cli(&["call", "index.mq.md", "数学.普通说明"], dir.path());
|
||||
assert_eq!(knowledge_sibling.status.code(), Some(1));
|
||||
assert!(
|
||||
stderr(&knowledge_sibling).contains("sem.no_entry_function"),
|
||||
"{}",
|
||||
stderr(&knowledge_sibling)
|
||||
);
|
||||
assert!(stdout(&knowledge_sibling).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_function_run_and_call_agree() {
|
||||
let dir = TempDir::new("exec");
|
||||
dir.write("index.mq.md", knowledge_document());
|
||||
let run = cli(&["run", "index.mq.md"], dir.path());
|
||||
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
|
||||
assert!(stdout(&run).is_empty());
|
||||
|
||||
let call = cli(&["call", "index.mq.md", "项目.main"], dir.path());
|
||||
assert_eq!(call.status.code(), Some(0), "{}", stderr(&call));
|
||||
assert_eq!(stdout(&call), "2\n");
|
||||
|
||||
let knowledge = cli(&["call", "index.mq.md", "项目.解释"], dir.path());
|
||||
assert_eq!(knowledge.status.code(), Some(1));
|
||||
assert!(stderr(&knowledge).contains("sem.no_entry_function"));
|
||||
}
|
||||
@@ -653,7 +653,7 @@ fn multi_module_imports_and_cycles() {
|
||||
let dir = TempDir::new("modules");
|
||||
dir.write(
|
||||
"geometry.mq.md",
|
||||
"## @exec 圆面积\n\n`params(r: Number)`\n\n`return math.eval(ref(\"#公式\"), {\"r\": r}, target=\"A\")`\n\n### @knowledge 公式\n\n$$\nA = \\pi r^2\n$$\n",
|
||||
"## @exec 圆面积\n\n`params(r: Number)`\n\n`return 3.141592653589793 * r * r`\n\n### @knowledge 公式\n\n$$\nA = \\pi r^2\n$$\n",
|
||||
);
|
||||
dir.write(
|
||||
"index.mq.md",
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
//! RFC-EXT-0003 H-01 / T-MATH-01..06: the core has no `math.*` domain
|
||||
//! builtins; algorithms live in pure `.mq.md` packages and optionally in an
|
||||
//! explicitly selected ABI backend. This test drives the real CLI binary and the
|
||||
//! checked-in examples.
|
||||
|
||||
#![cfg(unix)]
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
static COUNTER: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new(tag: &str) -> Self {
|
||||
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"marqdo-math-test-{}-{id}-{tag}",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn write(&self, name: &str, text: &str) {
|
||||
std::fs::write(self.0.join(name), text).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn repo() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn cli(args: &[&str], dir: &Path) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.output()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn stderr(output: &Output) -> String {
|
||||
String::from_utf8_lossy(&output.stderr).to_string()
|
||||
}
|
||||
|
||||
fn stdout(output: &Output) -> String {
|
||||
String::from_utf8_lossy(&output.stdout).to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pure_math_library_checks_calls_and_runs_without_math_builtins() {
|
||||
let repo = repo();
|
||||
let check = cli(&["check", "examples/math-library-demo.mq.md"], &repo);
|
||||
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
|
||||
|
||||
let call = cli(&["call", "examples/math-library-demo.mq.md", "main"], &repo);
|
||||
assert_eq!(call.status.code(), Some(0), "{}", stderr(&call));
|
||||
assert_eq!(stdout(&call), "6\n");
|
||||
|
||||
let run = cli(&["call", "examples/math.mq.md", "计算圆面积"], &repo);
|
||||
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removed_math_builtins_are_static_symbol_errors() {
|
||||
let dir = TempDir::new("removed");
|
||||
dir.write(
|
||||
"index.mq.md",
|
||||
"# @knowledge 公式\n\n$$\nA = r + 1\n$$\n\n## @exec main\n\n`return math.eval(ref(\"#公式\"), {\"r\": 2}, target=\"A\")`\n",
|
||||
);
|
||||
let check = cli(&["check", "index.mq.md"], dir.path());
|
||||
assert_eq!(check.status.code(), Some(1));
|
||||
let error = stderr(&check);
|
||||
assert!(error.contains("symbol.unknown"), "{error}");
|
||||
// No fallback execution and no removed `math.unsupported` runtime path.
|
||||
let run = cli(&["run", "index.mq.md"], dir.path());
|
||||
assert_eq!(run.status.code(), Some(1));
|
||||
assert!(run.stdout.is_empty());
|
||||
assert!(stderr(&run).contains("symbol.unknown"), "{}", stderr(&run));
|
||||
}
|
||||
@@ -65,7 +65,7 @@ const BASES: &[Base] = &[
|
||||
|
||||
半径叙述。
|
||||
|
||||
`return math.eval(ref("#公式"), {"r": r}, target="A")`
|
||||
`return 3.141592653589793 * r * r`
|
||||
|
||||
### @knowledge 公式
|
||||
|
||||
|
||||
@@ -789,11 +789,11 @@ mod tests {
|
||||
"main",
|
||||
&[],
|
||||
SignatureKind::Explicit,
|
||||
vec![return_call("math.eval(x=1)")],
|
||||
vec![return_call("resource.request(x=1)")],
|
||||
)],
|
||||
imports: vec![marqdo_ir::ImportDecl {
|
||||
alias: "math".into(),
|
||||
target: "./math.mq.md".into(),
|
||||
alias: "resource".into(),
|
||||
target: "./resource.mq.md".into(),
|
||||
span: Span::new(0, 1),
|
||||
}],
|
||||
resources: vec![],
|
||||
|
||||
@@ -140,7 +140,7 @@ impl<'a> Analyzer<'a> {
|
||||
}
|
||||
Expr::Call { callee, args, .. } => {
|
||||
// A callee identifier names a function; a `ns.name` callee is a
|
||||
// builtin namespace (math.eval) or an imported module alias.
|
||||
// builtin namespace (resource.request) or an imported module alias.
|
||||
// Neither is a variable read.
|
||||
match callee.as_ref() {
|
||||
Expr::Ident(_) => {}
|
||||
|
||||
@@ -221,6 +221,16 @@ pub fn build_units(doc: &MarkdownDocument, module: &ModuleId) -> UnitIndex {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Resolve the ancestor scope before deriving the mode: remove every
|
||||
// same-or-higher-level heading first so a heading can only inherit from
|
||||
// its nearest *ancestor*, never from the previous sibling.
|
||||
while let Some((parent_level, _, _)) = stack.last() {
|
||||
if *parent_level >= level {
|
||||
stack.pop();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let inherited = stack
|
||||
.last()
|
||||
.map(|(_, _, mode)| *mode)
|
||||
@@ -242,13 +252,6 @@ pub fn build_units(doc: &MarkdownDocument, module: &ModuleId) -> UnitIndex {
|
||||
continue;
|
||||
};
|
||||
|
||||
while let Some((parent_level, _, _)) = stack.last() {
|
||||
if *parent_level >= level {
|
||||
stack.pop();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let parent_path = stack
|
||||
.last()
|
||||
.map(|(_, path, _)| path.clone())
|
||||
@@ -551,4 +554,88 @@ mod tests {
|
||||
assert!(index.diagnostics.is_empty());
|
||||
assert_eq!(index.functions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unmarked_sibling_inherits_ancestor_not_previous_exec_sibling() {
|
||||
// Regression for RFC-EXT-0003 H-06: the previous sibling is Exec but the
|
||||
// nearest ancestor is Knowledge, so 普通说明 must stay Knowledge.
|
||||
let index =
|
||||
index_of("# @knowledge 数学\n\n## @exec 运行\n\n`return 1`\n\n## 普通说明\n\n叙述。\n");
|
||||
assert_eq!(index.functions.len(), 1);
|
||||
assert_eq!(index.functions[0].id.name, "数学.运行");
|
||||
let sibling = index
|
||||
.headings
|
||||
.iter()
|
||||
.find(|heading| heading.name == "普通说明")
|
||||
.expect("sibling heading");
|
||||
assert_eq!(sibling.kind, UnitKind::Knowledge);
|
||||
assert_eq!(sibling.mode, SemanticMode::Knowledge);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unmarked_sibling_inherits_exec_ancestor_not_previous_knowledge_sibling() {
|
||||
let index =
|
||||
index_of("# @exec 项目\n\n## @knowledge 解释\n\n叙述。\n\n## 计算\n\n`return 1`\n");
|
||||
assert_eq!(index.diagnostics.len(), 0, "{:?}", index.diagnostics);
|
||||
assert_eq!(index.functions.len(), 1);
|
||||
assert_eq!(index.functions[0].id.name, "项目.计算");
|
||||
let sibling = index
|
||||
.headings
|
||||
.iter()
|
||||
.find(|heading| heading.name == "计算")
|
||||
.expect("sibling heading");
|
||||
assert_eq!(sibling.mode, SemanticMode::Exec);
|
||||
assert_eq!(sibling.kind, UnitKind::Function);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_sections_follow_their_own_ancestors() {
|
||||
// H3 under an Exec H2 is an Exec section; H3 under a Knowledge H2 is not
|
||||
// polluted by the earlier Exec chain.
|
||||
let index = index_of(
|
||||
"# @knowledge 数学\n\n## @exec 计算\n\n### 细节\n\n`return 1`\n\n## 说明\n\n### 例子\n\n叙述。\n",
|
||||
);
|
||||
let depth = |name: &str| {
|
||||
index
|
||||
.headings
|
||||
.iter()
|
||||
.find(|heading| heading.name == name)
|
||||
.unwrap_or_else(|| panic!("missing heading {name}"))
|
||||
.mode
|
||||
};
|
||||
assert_eq!(depth("细节"), SemanticMode::Exec);
|
||||
assert_eq!(depth("说明"), SemanticMode::Knowledge);
|
||||
assert_eq!(index.functions.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_marker_does_not_corrupt_later_siblings() {
|
||||
let index = index_of("# @knowledge 数学\n\n## @Exec 坏标记\n\n## 正常\n\n`return 1`\n");
|
||||
assert!(index
|
||||
.diagnostics
|
||||
.iter()
|
||||
.any(|d| d.code == "sem.invalid_mode_marker"));
|
||||
let normal = index
|
||||
.headings
|
||||
.iter()
|
||||
.find(|heading| heading.name == "正常")
|
||||
.expect("valid sibling heading");
|
||||
assert_eq!(normal.mode, SemanticMode::Knowledge);
|
||||
assert_eq!(normal.kind, UnitKind::Knowledge);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn knowledge_scope_payloads_and_sections_stay_inert() {
|
||||
let index = index_of(
|
||||
"# @knowledge 数学\n\n$$\nA = \\pi r^2\n$$\n\n`数学表达式`\n\n## @knowledge 公式\n\n**不是函数**\n",
|
||||
);
|
||||
assert!(index.functions.is_empty(), "{:?}", index.functions);
|
||||
for heading in &index.headings {
|
||||
assert_eq!(heading.mode, SemanticMode::Knowledge);
|
||||
}
|
||||
assert!(index
|
||||
.units
|
||||
.iter()
|
||||
.all(|unit| !matches!(unit.kind, UnitKind::Function)));
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ regex = { workspace = true }
|
||||
glob = { workspace = true }
|
||||
marqdo-span = { workspace = true }
|
||||
marqdo-value = { workspace = true }
|
||||
marqdo-math = { workspace = true }
|
||||
marqdo-foreign = { workspace = true }
|
||||
marqdo-sandbox = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -29,7 +29,9 @@ use crate::observe::{
|
||||
use crate::plugin_worker::WorkerPlugin;
|
||||
use crate::provider_c::{load_package, CResourceProvider};
|
||||
use crate::provider_worker::WorkerResourceProvider;
|
||||
use crate::resource::{ResourceProvider, ResourceRead, ResourceTable, SharedResourceProvider};
|
||||
use crate::resource::{
|
||||
is_core_resource_kind, ResourceProvider, ResourceRead, ResourceTable, SharedResourceProvider,
|
||||
};
|
||||
use crate::trace::{EffectTrace, SemanticEvent, TraceBudget, TraceEntry, TraceState};
|
||||
use marqdo_sandbox::{PlatformCapabilities, SandboxMode};
|
||||
|
||||
@@ -41,16 +43,6 @@ mod resource_ops;
|
||||
/// Host operations reachable from the language surface.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum HostOp {
|
||||
/// Parse a formula.
|
||||
MathParse,
|
||||
/// Evaluate a formula.
|
||||
MathEval,
|
||||
/// Substitute variables in a formula.
|
||||
MathSubstitute,
|
||||
/// Simplify a formula.
|
||||
MathSimplify,
|
||||
/// Differentiate a formula.
|
||||
MathDifferentiate,
|
||||
/// One-shot foreign execution.
|
||||
ForeignRun,
|
||||
/// Typed foreign call.
|
||||
@@ -67,11 +59,6 @@ impl HostOp {
|
||||
/// Stable operation name used in traces and diagnostics.
|
||||
pub const fn name(self) -> &'static str {
|
||||
match self {
|
||||
HostOp::MathParse => "math.parse",
|
||||
HostOp::MathEval => "math.eval",
|
||||
HostOp::MathSubstitute => "math.substitute",
|
||||
HostOp::MathSimplify => "math.simplify",
|
||||
HostOp::MathDifferentiate => "math.differentiate",
|
||||
HostOp::ForeignRun => "foreign.run",
|
||||
HostOp::ForeignCall => "foreign.call",
|
||||
HostOp::FsRead => "fs.read",
|
||||
|
||||
@@ -72,11 +72,6 @@ pub(crate) fn dispatch(
|
||||
match op {
|
||||
HostOp::FsRead => host.op_fs_read(args, ctx),
|
||||
HostOp::FsWrite => host.op_fs_write(args, ctx),
|
||||
HostOp::MathParse
|
||||
| HostOp::MathEval
|
||||
| HostOp::MathSubstitute
|
||||
| HostOp::MathSimplify
|
||||
| HostOp::MathDifferentiate => crate::kernels::math(host, op, args, ctx),
|
||||
HostOp::ForeignRun | HostOp::ForeignCall => crate::kernels::foreign(host, op, args, ctx),
|
||||
HostOp::PluginInvoke => host.op_plugin_invoke(args, ctx),
|
||||
}
|
||||
|
||||
@@ -84,11 +84,18 @@ impl StdHost {
|
||||
));
|
||||
}
|
||||
let name = provider.name().to_string();
|
||||
if name.is_empty() || name == "stdio" || name == "file" {
|
||||
if name.is_empty() {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
None,
|
||||
format!("resource provider name `{name}` is reserved or empty"),
|
||||
"resource provider name is empty".to_string(),
|
||||
));
|
||||
}
|
||||
if is_core_resource_kind(&name) {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
None,
|
||||
format!("resource provider name `{name}` is a reserved core resource kind"),
|
||||
));
|
||||
}
|
||||
let kinds = provider.kind_strings();
|
||||
@@ -100,11 +107,18 @@ impl StdHost {
|
||||
));
|
||||
}
|
||||
for kind in &kinds {
|
||||
if kind.is_empty() || kind == "stdio" || kind == "file" {
|
||||
if kind.is_empty() {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
None,
|
||||
format!("resource provider `{name}` declares reserved kind `{kind}`"),
|
||||
format!("resource provider `{name}` declares an empty kind"),
|
||||
));
|
||||
}
|
||||
if is_core_resource_kind(kind) {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
None,
|
||||
format!("resource provider `{name}` declares reserved core kind `{kind}`"),
|
||||
));
|
||||
}
|
||||
if let Some((existing, _)) = self.resource_providers.get(kind) {
|
||||
|
||||
@@ -1,147 +1,18 @@
|
||||
//! Math and foreign kernel adapters.
|
||||
//! Foreign-kernel adapters.
|
||||
//!
|
||||
//! These functions are the only bridge between the capability host and the
|
||||
//! native math / foreign crates. Every operation is capability-checked before
|
||||
//! the kernel is touched; kernel failures are converted to structured runtime
|
||||
//! errors, never panics.
|
||||
//! This module is the bridge between the capability host and the native foreign
|
||||
//! kernel crate. Every operation is capability-checked before the kernel is
|
||||
//! touched; kernel failures are converted to structured runtime errors, never
|
||||
//! panics. Domain math is not a host kernel (ADR-0061): it belongs to a pure
|
||||
//! `.mq.md` package or an explicitly selected ABI backend.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::time::Duration;
|
||||
|
||||
use marqdo_value::{to_wire, ErrorCategory, FormulaValue, RuntimeError, Value};
|
||||
use marqdo_value::{to_wire, ErrorCategory, RuntimeError, Value};
|
||||
|
||||
use crate::context::ExecutionContext;
|
||||
use crate::host::{arity, expect_text, HostOp, StdHost};
|
||||
|
||||
pub(crate) fn math(
|
||||
host: &mut StdHost,
|
||||
op: HostOp,
|
||||
args: Vec<Value>,
|
||||
ctx: &ExecutionContext,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
if !host.capabilities.math {
|
||||
return Err(denied(ctx, "the math capability was not granted"));
|
||||
}
|
||||
let span = ctx.span;
|
||||
match op {
|
||||
HostOp::MathParse => {
|
||||
let source = expect_text(&args, 0, "math.parse source", span)?;
|
||||
let formula = marqdo_math::parse(&source).map_err(|error| math_error(error, span))?;
|
||||
Ok(Value::Formula(FormulaValue {
|
||||
latex: formula.source,
|
||||
unit: None,
|
||||
display: false,
|
||||
span,
|
||||
}))
|
||||
}
|
||||
HostOp::MathEval => {
|
||||
if args.len() < 2 {
|
||||
return Err(arity(span, "math.eval", 2, args.len()));
|
||||
}
|
||||
let formula = formula_source(&args[0], span)?;
|
||||
let bindings = math_bindings(&args[1], span)?;
|
||||
let target = match args.get(2) {
|
||||
Some(Value::Text(target)) => Some(target.clone()),
|
||||
Some(Value::None) | None => None,
|
||||
Some(other) => {
|
||||
return Err(RuntimeError::type_error(
|
||||
span,
|
||||
format!("math.eval target must be Text, got {}", other.type_name()),
|
||||
))
|
||||
}
|
||||
};
|
||||
let parsed = marqdo_math::parse(&formula).map_err(|error| math_error(error, span))?;
|
||||
marqdo_math::evaluate(&parsed, &bindings, target.as_deref())
|
||||
.map_err(|error| math_error(error, span))
|
||||
}
|
||||
HostOp::MathSubstitute => {
|
||||
if args.len() < 2 {
|
||||
return Err(arity(span, "math.substitute", 2, args.len()));
|
||||
}
|
||||
let source = formula_source(&args[0], span)?;
|
||||
let bindings = math_bindings(&args[1], span)?;
|
||||
let parsed = marqdo_math::parse(&source).map_err(|error| math_error(error, span))?;
|
||||
let substituted = marqdo_math::substitute(&parsed, &bindings)
|
||||
.map_err(|error| math_error(error, span))?;
|
||||
Ok(Value::Formula(FormulaValue {
|
||||
latex: substituted.source,
|
||||
unit: None,
|
||||
display: false,
|
||||
span,
|
||||
}))
|
||||
}
|
||||
HostOp::MathSimplify | HostOp::MathDifferentiate => Err(RuntimeError::new(
|
||||
ErrorCategory::MathUnsupported,
|
||||
span,
|
||||
format!(
|
||||
"`{}` is not part of the supported math subset in this build",
|
||||
op.name()
|
||||
),
|
||||
)),
|
||||
_ => Err(RuntimeError::new(
|
||||
ErrorCategory::Internal,
|
||||
span,
|
||||
"unexpected math operation",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn formula_source(value: &Value, span: Option<marqdo_span::Span>) -> Result<String, RuntimeError> {
|
||||
match value {
|
||||
Value::Formula(formula) => Ok(formula.latex.clone()),
|
||||
Value::Text(text) => Ok(text.clone()),
|
||||
other => Err(RuntimeError::type_error(
|
||||
span,
|
||||
format!("expected Formula or Text, got {}", other.type_name()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn math_bindings(
|
||||
value: &Value,
|
||||
span: Option<marqdo_span::Span>,
|
||||
) -> Result<BTreeMap<String, Value>, RuntimeError> {
|
||||
match value {
|
||||
Value::Map(map) => {
|
||||
let mut bindings = BTreeMap::new();
|
||||
for (name, value) in map {
|
||||
match value {
|
||||
Value::Int(_) | Value::Float(_) => {
|
||||
bindings.insert(name.clone(), value.clone());
|
||||
}
|
||||
other => {
|
||||
return Err(RuntimeError::type_error(
|
||||
span,
|
||||
format!(
|
||||
"math binding `{name}` must be Int or Float, got {}",
|
||||
other.type_name()
|
||||
),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(bindings)
|
||||
}
|
||||
other => Err(RuntimeError::type_error(
|
||||
span,
|
||||
format!("math bindings must be a Map, got {}", other.type_name()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn math_error(error: marqdo_math::MathError, span: Option<marqdo_span::Span>) -> RuntimeError {
|
||||
use marqdo_math::MathErrorKind;
|
||||
let category = match error.kind {
|
||||
MathErrorKind::Parse | MathErrorKind::Unsupported => ErrorCategory::MathUnsupported,
|
||||
MathErrorKind::UndefinedVariable => ErrorCategory::UnknownSymbol,
|
||||
MathErrorKind::Type => ErrorCategory::Type,
|
||||
};
|
||||
let mut message = error.message;
|
||||
if let Some(position) = error.position {
|
||||
message = format!("{message} (at byte {position} of the formula)");
|
||||
}
|
||||
RuntimeError::new(category, span, message)
|
||||
}
|
||||
use crate::host::{HostOp, StdHost};
|
||||
|
||||
pub(crate) fn foreign(
|
||||
host: &mut StdHost,
|
||||
@@ -296,9 +167,5 @@ fn foreign_error(
|
||||
RuntimeError::new(category, span, message)
|
||||
}
|
||||
|
||||
fn denied(ctx: &ExecutionContext, message: impl Into<String>) -> RuntimeError {
|
||||
RuntimeError::new(ErrorCategory::PermissionDenied, ctx.span, message)
|
||||
}
|
||||
|
||||
/// Default per-call foreign timeout used by the CLI.
|
||||
pub const DEFAULT_FOREIGN_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
@@ -35,5 +35,8 @@ pub use observe::{
|
||||
ValueState, DEFAULT_MAX_OBSERVATION_BYTES, DEFAULT_MAX_OBSERVATION_EVENTS,
|
||||
DEFAULT_MAX_VALUE_BYTES, MAX_STREAM_CHUNK_BYTES,
|
||||
};
|
||||
pub use resource::{ResourceProvider, ResourceRead, ResourceTable, SharedResourceProvider};
|
||||
pub use resource::{
|
||||
is_core_resource_kind, ResourceProvider, ResourceRead, ResourceTable, SharedResourceProvider,
|
||||
CORE_RESOURCE_KINDS,
|
||||
};
|
||||
pub use trace::{EffectTrace, SemanticEvent, TraceBudget, TraceEntry, TraceState, TraceTruncation};
|
||||
@@ -18,6 +18,20 @@ use marqdo_value::{ErrorCategory, ResourceHandle, RuntimeError};
|
||||
|
||||
static NEXT_RUN_ID: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
/// Core resource kinds owned by the host itself.
|
||||
///
|
||||
/// A third-party [`ResourceProvider`] may neither use one of these names as its
|
||||
/// provider name nor declare one of them as a served kind: the host dispatches
|
||||
/// these kinds before consulting the provider table. This is the single source of
|
||||
/// truth used by both host registration and the CLI provider-manifest validator
|
||||
/// ([`spec/resource-provider-v2.md`], ADR-0033/0062).
|
||||
pub const CORE_RESOURCE_KINDS: &[&str] = &["stdio", "file", "tcp", "tls", "udp", "tcp-listener"];
|
||||
|
||||
/// Whether `name` is a host-owned core resource kind.
|
||||
pub fn is_core_resource_kind(name: &str) -> bool {
|
||||
CORE_RESOURCE_KINDS.contains(&name)
|
||||
}
|
||||
|
||||
/// A versioned trusted provider of additional resource kinds (ADR-0033).
|
||||
///
|
||||
/// In-process providers share the host address space and are **not** a
|
||||
|
||||
@@ -279,6 +279,7 @@ struct MockProvider {
|
||||
written: Vec<u8>,
|
||||
version: u32,
|
||||
kinds: Vec<&'static str>,
|
||||
provider_name: &'static str,
|
||||
}
|
||||
|
||||
impl MockProvider {
|
||||
@@ -289,13 +290,19 @@ impl MockProvider {
|
||||
written: Vec::new(),
|
||||
version,
|
||||
kinds,
|
||||
provider_name: "mock",
|
||||
}
|
||||
}
|
||||
|
||||
fn with_name(mut self, name: &'static str) -> Self {
|
||||
self.provider_name = name;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl marqdo_host::ResourceProvider for MockProvider {
|
||||
fn name(&self) -> &str {
|
||||
"mock"
|
||||
self.provider_name
|
||||
}
|
||||
fn protocol_version(&self) -> u32 {
|
||||
self.version
|
||||
@@ -421,10 +428,30 @@ fn provider_registration_rejects_versions_reserved_names_and_conflicts() {
|
||||
.register_resource_provider(Box::new(MockProvider::new(b"x", 2, vec!["mock.sensor"])))
|
||||
.unwrap_err();
|
||||
assert_eq!(bad_version.category, ErrorCategory::ResourceUnsupported);
|
||||
let reserved = host
|
||||
.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["stdio"])))
|
||||
.unwrap_err();
|
||||
assert_eq!(reserved.category, ErrorCategory::ResourceUnsupported);
|
||||
for core_kind in marqdo_host::CORE_RESOURCE_KINDS {
|
||||
let reserved_kind = host
|
||||
.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec![core_kind])))
|
||||
.unwrap_err();
|
||||
assert_eq!(reserved_kind.category, ErrorCategory::ResourceUnsupported);
|
||||
assert!(
|
||||
reserved_kind.message.contains("reserved core kind"),
|
||||
"{core_kind}: {}",
|
||||
reserved_kind.message
|
||||
);
|
||||
let reserved_name = host
|
||||
.register_resource_provider(Box::new(
|
||||
MockProvider::new(b"x", 1, vec!["mock.sensor"]).with_name(core_kind),
|
||||
))
|
||||
.unwrap_err();
|
||||
assert_eq!(reserved_name.category, ErrorCategory::ResourceUnsupported);
|
||||
assert!(
|
||||
reserved_name
|
||||
.message
|
||||
.contains("reserved core resource kind"),
|
||||
"{core_kind}: {}",
|
||||
reserved_name.message
|
||||
);
|
||||
}
|
||||
host.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["mock.sensor"])))
|
||||
.unwrap();
|
||||
let conflict = host
|
||||
|
||||
@@ -28,7 +28,7 @@ const fn opt(name: &'static str) -> BuiltinParam {
|
||||
/// A builtin function or host operation.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Builtin {
|
||||
/// Canonical call name (may be namespaced, e.g. `math.eval`).
|
||||
/// Canonical call name (may be namespaced, e.g. `foreign.run`).
|
||||
pub name: &'static str,
|
||||
/// Alternative names (Chinese/English aliases).
|
||||
pub aliases: &'static [&'static str],
|
||||
@@ -104,41 +104,6 @@ const REF: Builtin = Builtin {
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const MATH_PARSE: Builtin = Builtin {
|
||||
name: "math.parse",
|
||||
aliases: &[],
|
||||
params: &[req("formula")],
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const MATH_EVAL: Builtin = Builtin {
|
||||
name: "math.eval",
|
||||
aliases: &[],
|
||||
params: &[req("formula"), req("bindings"), opt("target")],
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const MATH_SUBSTITUTE: Builtin = Builtin {
|
||||
name: "math.substitute",
|
||||
aliases: &[],
|
||||
params: &[req("formula"), req("bindings")],
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const MATH_SIMPLIFY: Builtin = Builtin {
|
||||
name: "math.simplify",
|
||||
aliases: &[],
|
||||
params: &[req("formula")],
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const MATH_DIFFERENTIATE: Builtin = Builtin {
|
||||
name: "math.differentiate",
|
||||
aliases: &[],
|
||||
params: &[req("formula"), req("变量")],
|
||||
named_only: false,
|
||||
effectful: false,
|
||||
};
|
||||
const FOREIGN_RUN: Builtin = Builtin {
|
||||
name: "foreign.run",
|
||||
aliases: &[],
|
||||
@@ -331,11 +296,6 @@ pub const BUILTINS: &[Builtin] = &[
|
||||
FLOAT,
|
||||
TABLE,
|
||||
REF,
|
||||
MATH_PARSE,
|
||||
MATH_EVAL,
|
||||
MATH_SUBSTITUTE,
|
||||
MATH_SIMPLIFY,
|
||||
MATH_DIFFERENTIATE,
|
||||
FOREIGN_RUN,
|
||||
FOREIGN_CALL,
|
||||
FS_READ,
|
||||
@@ -427,7 +387,8 @@ mod tests {
|
||||
fn lookup_aliases() {
|
||||
assert_eq!(find("print").unwrap().name, "打印");
|
||||
assert_eq!(find("打印").unwrap().name, "打印");
|
||||
assert!(!find("math.eval").unwrap().named_only);
|
||||
assert!(find("table").is_some());
|
||||
assert!(find("math.eval").is_none());
|
||||
assert!(find("nope").is_none());
|
||||
}
|
||||
|
||||
@@ -437,15 +398,11 @@ mod tests {
|
||||
let print = find("打印").unwrap();
|
||||
assert!(validate_args(print, &parse_arguments("(1)").unwrap()).is_ok());
|
||||
assert!(validate_args(print, &parse_arguments("()").unwrap()).is_err());
|
||||
let eval = find("math.eval").unwrap();
|
||||
assert!(validate_args(
|
||||
eval,
|
||||
&parse_arguments("(formula=ref(\"#x\"), bindings={}, target=\"A\")").unwrap()
|
||||
)
|
||||
.is_ok());
|
||||
// math.eval accepts positional arguments followed by named ones.
|
||||
assert!(validate_args(eval, &parse_arguments("(ref(\"#x\"), {})").unwrap()).is_ok());
|
||||
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());
|
||||
let table = find("table").unwrap();
|
||||
assert!(validate_args(table, &parse_arguments("(表头=[1], 行=[])").unwrap()).is_ok());
|
||||
// Builtins accept positional arguments followed by named ones.
|
||||
assert!(validate_args(table, &parse_arguments("([1], [])").unwrap()).is_ok());
|
||||
assert!(validate_args(table, &parse_arguments("(表头=[1], nope=[])").unwrap()).is_err());
|
||||
assert!(validate_args(table, &parse_arguments("(1, 2, 3)").unwrap()).is_err());
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ impl fmt::Display for CallSiteKey {
|
||||
pub enum BoundTarget {
|
||||
/// A fixed language builtin, by canonical builtin name.
|
||||
Builtin {
|
||||
/// Canonical name (for example `math.eval`), not a user alias.
|
||||
/// Canonical name (for example `foreign.run`), not a user alias.
|
||||
canonical_name: String,
|
||||
},
|
||||
/// A function defined by a Markdown heading.
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
[package]
|
||||
name = "marqdo-math"
|
||||
description = "Native Math/Formula kernel: parse, substitute and evaluate a defined subset"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
marqdo-value = { workspace = true }
|
||||
num-bigint = { workspace = true }
|
||||
num-rational = { workspace = true }
|
||||
num-traits = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -1,394 +0,0 @@
|
||||
//! Formula AST, exact-number representation and deterministic rendering.
|
||||
//!
|
||||
//! The AST is public so that `math.inspect` and tests can observe the parsed
|
||||
//! structure instead of re-parsing the source string. [`Formula::source`] keeps
|
||||
//! the original LaTeX exactly as supplied to [`crate::parse`]; rendering is only
|
||||
//! used when an operation (currently [`crate::substitute`]) produces a new
|
||||
//! formula.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use num_rational::Ratio;
|
||||
use num_traits::{One, Signed, Zero};
|
||||
|
||||
/// Exact rational number used internally by the kernel.
|
||||
///
|
||||
/// This is `num-rational`'s generic `Ratio` instantiated over the same
|
||||
/// `num-bigint` `BigInt` type used by [`marqdo_value::Value::Int`], so values can
|
||||
/// cross the kernel boundary without a lossy or string round-trip.
|
||||
pub type Rational = Ratio<BigInt>;
|
||||
|
||||
/// Named mathematical constants supported by the subset.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Constant {
|
||||
/// The circle ratio, written `\pi` or `pi`.
|
||||
Pi,
|
||||
/// Euler's number, written `\e` or `e`.
|
||||
E,
|
||||
}
|
||||
|
||||
impl Constant {
|
||||
/// The approximate `f64` value of this constant.
|
||||
pub fn value(self) -> f64 {
|
||||
match self {
|
||||
Constant::Pi => std::f64::consts::PI,
|
||||
Constant::E => std::f64::consts::E,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A node of the supported expression grammar.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Expr {
|
||||
/// An exact numeric literal. Decimal literals are reduced to exact
|
||||
/// rationals (for example `0.25` becomes `1/4`).
|
||||
Number(Rational),
|
||||
/// A free variable resolved from the binding map at evaluation time.
|
||||
Variable(String),
|
||||
/// A named constant (`\pi`, `pi`, `\e`, `e`).
|
||||
Constant(Constant),
|
||||
/// Unary negation.
|
||||
Neg(Box<Expr>),
|
||||
/// Addition.
|
||||
Add(Box<Expr>, Box<Expr>),
|
||||
/// Subtraction.
|
||||
Sub(Box<Expr>, Box<Expr>),
|
||||
/// Multiplication.
|
||||
Mul(Box<Expr>, Box<Expr>),
|
||||
/// Division. `\frac{a}{b}` is parsed as `Div(a, b)`.
|
||||
Div(Box<Expr>, Box<Expr>),
|
||||
/// Exponentiation. The exponent must evaluate to an integer.
|
||||
Pow(Box<Expr>, Box<Expr>),
|
||||
/// Square root. `\sqrt{x}` is a dedicated node so that perfect squares can
|
||||
/// stay exact.
|
||||
Sqrt(Box<Expr>),
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
/// Collect every free variable name (constants are not variables).
|
||||
pub fn variables(&self) -> BTreeSet<String> {
|
||||
let mut names = BTreeSet::new();
|
||||
self.collect_variables(&mut names);
|
||||
names
|
||||
}
|
||||
|
||||
fn collect_variables(&self, names: &mut BTreeSet<String>) {
|
||||
match self {
|
||||
Expr::Number(_) | Expr::Constant(_) => {}
|
||||
Expr::Variable(name) => {
|
||||
names.insert(name.clone());
|
||||
}
|
||||
Expr::Neg(inner) | Expr::Sqrt(inner) => inner.collect_variables(names),
|
||||
Expr::Add(left, right)
|
||||
| Expr::Sub(left, right)
|
||||
| Expr::Mul(left, right)
|
||||
| Expr::Div(left, right)
|
||||
| Expr::Pow(left, right) => {
|
||||
left.collect_variables(names);
|
||||
right.collect_variables(names);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Number of syntactic occurrences of `name`.
|
||||
pub fn occurrences(&self, name: &str) -> usize {
|
||||
match self {
|
||||
Expr::Number(_) | Expr::Constant(_) => 0,
|
||||
Expr::Variable(variable) => usize::from(variable == name),
|
||||
Expr::Neg(inner) | Expr::Sqrt(inner) => inner.occurrences(name),
|
||||
Expr::Add(left, right)
|
||||
| Expr::Sub(left, right)
|
||||
| Expr::Mul(left, right)
|
||||
| Expr::Div(left, right)
|
||||
| Expr::Pow(left, right) => left.occurrences(name) + right.occurrences(name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Top level shape of a parsed formula: an expression or a single equation.
|
||||
///
|
||||
/// A formula with one `=` is an [`FormulaKind::Equation`]; it is never an
|
||||
/// assignment. More than one `=` is a parse error.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum FormulaKind {
|
||||
/// A standalone arithmetic expression.
|
||||
Expression(Expr),
|
||||
/// `left = right`.
|
||||
Equation {
|
||||
/// Left-hand side.
|
||||
left: Expr,
|
||||
/// Right-hand side.
|
||||
right: Expr,
|
||||
},
|
||||
}
|
||||
|
||||
/// A parsed formula plus the original source text.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Formula {
|
||||
/// Original LaTeX source exactly as supplied to [`crate::parse`], or the
|
||||
/// canonical rendering produced by [`crate::substitute`].
|
||||
pub source: String,
|
||||
/// Parsed structure.
|
||||
pub kind: FormulaKind,
|
||||
}
|
||||
|
||||
impl Formula {
|
||||
/// Whether this formula is an equation.
|
||||
pub fn is_equation(&self) -> bool {
|
||||
matches!(self.kind, FormulaKind::Equation { .. })
|
||||
}
|
||||
|
||||
/// Borrow the expression when this formula is not an equation.
|
||||
pub fn expression(&self) -> Option<&Expr> {
|
||||
match &self.kind {
|
||||
FormulaKind::Expression(expr) => Some(expr),
|
||||
FormulaKind::Equation { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrow both sides when this formula is an equation.
|
||||
pub fn equation(&self) -> Option<(&Expr, &Expr)> {
|
||||
match &self.kind {
|
||||
FormulaKind::Expression(_) => None,
|
||||
FormulaKind::Equation { left, right } => Some((left, right)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Every free variable appearing in the formula, in sorted order.
|
||||
pub fn variables(&self) -> BTreeSet<String> {
|
||||
match &self.kind {
|
||||
FormulaKind::Expression(expr) => expr.variables(),
|
||||
FormulaKind::Equation { left, right } => {
|
||||
let mut names = left.variables();
|
||||
names.extend(right.variables());
|
||||
names
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render this formula back to the supported LaTeX subset deterministically.
|
||||
///
|
||||
/// The result is always re-parseable by [`crate::parse`]. This is used by
|
||||
/// [`crate::substitute`] to build the new [`Formula::source`].
|
||||
pub fn render(&self) -> String {
|
||||
match &self.kind {
|
||||
FormulaKind::Expression(expr) => render_expr(expr),
|
||||
FormulaKind::Equation { left, right } => {
|
||||
format!("{} = {}", render_expr(left), render_expr(right))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rendering precedence levels, from loosest to tightest.
|
||||
const PREC_ADD: u8 = 1;
|
||||
const PREC_MUL: u8 = 2;
|
||||
const PREC_UNARY: u8 = 3;
|
||||
const PREC_POW: u8 = 4;
|
||||
const PREC_ATOM: u8 = 5;
|
||||
|
||||
/// Fractional digits beyond which a terminating rational is rendered as
|
||||
/// `\frac{..}{..}` instead of a long decimal. Chosen so that substituted
|
||||
/// `f64` values that are exactly representable in a short decimal stay readable,
|
||||
/// while binary-exact values with long expansions use a compact fraction.
|
||||
const MAX_RENDERED_DECIMAL_DIGITS: u32 = 32;
|
||||
|
||||
/// Render an expression without surrounding parentheses.
|
||||
pub fn render_expr(expr: &Expr) -> String {
|
||||
render(expr, 0)
|
||||
}
|
||||
|
||||
fn render(expr: &Expr, parent_precedence: u8) -> String {
|
||||
match expr {
|
||||
Expr::Number(value) => {
|
||||
let (text, precedence) = render_rational(value);
|
||||
parenthesize(text, precedence, parent_precedence)
|
||||
}
|
||||
Expr::Variable(name) => name.clone(),
|
||||
Expr::Constant(constant) => match constant {
|
||||
Constant::Pi => "\\pi".to_string(),
|
||||
Constant::E => "\\e".to_string(),
|
||||
},
|
||||
Expr::Neg(inner) => {
|
||||
let text = format!("-{}", render(inner, PREC_UNARY));
|
||||
parenthesize(text, PREC_UNARY, parent_precedence)
|
||||
}
|
||||
Expr::Add(left, right) => {
|
||||
let text = format!(
|
||||
"{} + {}",
|
||||
render(left, PREC_ADD),
|
||||
render(right, PREC_ADD + 1)
|
||||
);
|
||||
parenthesize(text, PREC_ADD, parent_precedence)
|
||||
}
|
||||
Expr::Sub(left, right) => {
|
||||
let text = format!(
|
||||
"{} - {}",
|
||||
render(left, PREC_ADD),
|
||||
render(right, PREC_ADD + 1)
|
||||
);
|
||||
parenthesize(text, PREC_ADD, parent_precedence)
|
||||
}
|
||||
Expr::Mul(left, right) => {
|
||||
let text = format!(
|
||||
"{} * {}",
|
||||
render(left, PREC_MUL),
|
||||
render(right, PREC_MUL + 1)
|
||||
);
|
||||
parenthesize(text, PREC_MUL, parent_precedence)
|
||||
}
|
||||
Expr::Div(left, right) => {
|
||||
let text = format!(
|
||||
"{} / {}",
|
||||
render(left, PREC_MUL),
|
||||
render(right, PREC_MUL + 1)
|
||||
);
|
||||
parenthesize(text, PREC_MUL, parent_precedence)
|
||||
}
|
||||
Expr::Pow(base, exponent) => {
|
||||
let text = format!(
|
||||
"{} ^ {}",
|
||||
render(base, PREC_POW + 1),
|
||||
render(exponent, PREC_POW)
|
||||
);
|
||||
parenthesize(text, PREC_POW, parent_precedence)
|
||||
}
|
||||
Expr::Sqrt(inner) => format!("\\sqrt{{{}}}", render(inner, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
fn parenthesize(text: String, precedence: u8, parent_precedence: u8) -> String {
|
||||
if precedence < parent_precedence {
|
||||
format!("({text})")
|
||||
} else {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
/// Render an exact rational, returning the text and its rendering precedence.
|
||||
///
|
||||
/// Integral values render as integers. Values with a terminating decimal
|
||||
/// expansion of at most [`MAX_RENDERED_DECIMAL_DIGITS`] digits render as
|
||||
/// decimals; everything else renders as `\frac{numerator}{denominator}`.
|
||||
fn render_rational(value: &Rational) -> (String, u8) {
|
||||
if value.is_integer() {
|
||||
let integer = value.to_integer();
|
||||
let negative = integer.sign() == num_bigint::Sign::Minus;
|
||||
let precedence = if negative { PREC_UNARY } else { PREC_ATOM };
|
||||
return (integer.to_string(), precedence);
|
||||
}
|
||||
|
||||
if let Some(places) = decimal_places(value.denom()) {
|
||||
if places <= MAX_RENDERED_DECIMAL_DIGITS {
|
||||
let scale = BigInt::from(10_u32).pow(places);
|
||||
let scaled = value.numer() * &scale / value.denom();
|
||||
let negative = scaled.sign() == num_bigint::Sign::Minus;
|
||||
let digits = scaled.abs().to_string();
|
||||
let places = places as usize;
|
||||
let decimal = if digits.len() <= places {
|
||||
format!("0.{}{digits}", "0".repeat(places - digits.len()))
|
||||
} else {
|
||||
let split = digits.len() - places;
|
||||
format!("{}.{}", &digits[..split], &digits[split..])
|
||||
};
|
||||
let precedence = if negative { PREC_UNARY } else { PREC_ATOM };
|
||||
let text = if negative {
|
||||
format!("-{decimal}")
|
||||
} else {
|
||||
decimal
|
||||
};
|
||||
return (text, precedence);
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
format!("\\frac{{{}}}{{{}}}", value.numer(), value.denom()),
|
||||
PREC_ATOM,
|
||||
)
|
||||
}
|
||||
|
||||
/// Number of fractional digits needed for the terminating decimal expansion of
|
||||
/// `denominator`, or `None` when the expansion does not terminate or is longer
|
||||
/// than [`MAX_RENDERED_DECIMAL_DIGITS`].
|
||||
fn decimal_places(denominator: &BigInt) -> Option<u32> {
|
||||
let mut reduced = denominator.clone();
|
||||
let two = BigInt::from(2_u32);
|
||||
let five = BigInt::from(5_u32);
|
||||
let mut twos = 0_u32;
|
||||
while reduced.is_positive() && (&reduced % &two).is_zero() {
|
||||
reduced /= &two;
|
||||
twos += 1;
|
||||
if twos > MAX_RENDERED_DECIMAL_DIGITS {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let mut fives = 0_u32;
|
||||
while reduced.is_positive() && (&reduced % &five).is_zero() {
|
||||
reduced /= &five;
|
||||
fives += 1;
|
||||
if fives > MAX_RENDERED_DECIMAL_DIGITS {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if reduced.is_one() {
|
||||
Some(twos.max(fives))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn number(numerator: i64, denominator: i64) -> Expr {
|
||||
Expr::Number(Ratio::new(
|
||||
BigInt::from(numerator),
|
||||
BigInt::from(denominator),
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rendering_round_trips_through_parse() {
|
||||
// The renderer must always emit text the parser accepts.
|
||||
for source in [
|
||||
"1 + 2 * 3",
|
||||
"(1 + 2) * 3",
|
||||
"2 ^ (3 + 1)",
|
||||
"\\frac{1}{3}",
|
||||
"-4 ^ 2",
|
||||
"2 ^ (-3)",
|
||||
"\\sqrt{2}",
|
||||
"a = 2 * b + 1",
|
||||
"(2 ^ 3) ^ 2",
|
||||
"1 - (2 - 3)",
|
||||
"a / (b * c)",
|
||||
] {
|
||||
let formula = crate::parse(source).expect("fixture parses");
|
||||
let rendered = formula.render();
|
||||
let reparsed = crate::parse(&rendered)
|
||||
.unwrap_or_else(|err| panic!("rendered {rendered:?} does not parse: {err}"));
|
||||
assert_eq!(reparsed.kind, formula.kind, "source {source:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_and_fraction_rendering_is_deterministic() {
|
||||
assert_eq!(render_expr(&number(5, 2)), "2.5");
|
||||
assert_eq!(render_expr(&number(-5, 2)), "-2.5");
|
||||
assert_eq!(render_expr(&number(1, 8)), "0.125");
|
||||
assert_eq!(render_expr(&number(1, 3)), "\\frac{1}{3}");
|
||||
assert_eq!(render_expr(&number(0, 1)), "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn variables_are_collected_without_constants() {
|
||||
let formula = crate::parse("2 * x + y * \\pi").unwrap();
|
||||
assert_eq!(
|
||||
formula.variables().into_iter().collect::<Vec<_>>(),
|
||||
vec!["x".to_string(), "y".to_string()]
|
||||
);
|
||||
assert_eq!(formula.expression().unwrap().occurrences("x"), 1);
|
||||
}
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
//! Structured errors returned by the native math kernel.
|
||||
//!
|
||||
//! The kernel never panics on untrusted LaTeX. Every failure is reported as a
|
||||
//! [`MathError`] with a stable [`MathErrorKind`], a human readable message and
|
||||
//! (for parse failures) the byte offset into the original LaTeX source. The
|
||||
//! host layer is expected to map [`MathErrorKind::Unsupported`] to the
|
||||
//! `math.unsupported` diagnostic code and the remaining kinds to the matching
|
||||
//! expression diagnostics.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
/// Broad category of a math kernel failure.
|
||||
///
|
||||
/// The enum is deliberately small: the kernel only distinguishes "the input is
|
||||
/// not in the supported grammar", "the input is in the grammar but outside the
|
||||
/// supported subset", "a name is unbound" and "a value has the wrong type".
|
||||
/// Resource-bound violations (exponent/result size) are reported as
|
||||
/// [`MathErrorKind::Unsupported`] because they are a documented limitation of
|
||||
/// the subset rather than a parse or type problem.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum MathErrorKind {
|
||||
/// The input is malformed LaTeX for the supported subset.
|
||||
Parse,
|
||||
/// The input is well-formed but outside the deliberately limited subset, or
|
||||
/// the requested operation/equation shape is not supported.
|
||||
Unsupported,
|
||||
/// A variable used by the formula has no binding.
|
||||
UndefinedVariable,
|
||||
/// A binding or operand has an incompatible type, or an operation is
|
||||
/// undefined for the supplied values (for example division by zero).
|
||||
Type,
|
||||
}
|
||||
|
||||
impl MathErrorKind {
|
||||
/// Stable lowercase label used by diagnostics and tests.
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
MathErrorKind::Parse => "parse",
|
||||
MathErrorKind::Unsupported => "unsupported",
|
||||
MathErrorKind::UndefinedVariable => "undefined-variable",
|
||||
MathErrorKind::Type => "type",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MathErrorKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// A structured error produced by [`crate::parse`], [`crate::evaluate`] or
|
||||
/// [`crate::substitute`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct MathError {
|
||||
/// Failure category.
|
||||
pub kind: MathErrorKind,
|
||||
/// Human readable explanation. Messages are diagnostic text; callers should
|
||||
/// branch on [`MathError::kind`] rather than on the wording.
|
||||
pub message: String,
|
||||
/// Byte offset into the original LaTeX source, when the failure is tied to
|
||||
/// a specific token.
|
||||
pub position: Option<usize>,
|
||||
}
|
||||
|
||||
impl MathError {
|
||||
/// Construct an error with an explicit kind and optional byte position.
|
||||
pub fn new(kind: MathErrorKind, message: impl Into<String>, position: Option<usize>) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
message: message.into(),
|
||||
position,
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a [`MathErrorKind::Parse`] error.
|
||||
pub fn parse(message: impl Into<String>, position: Option<usize>) -> Self {
|
||||
Self::new(MathErrorKind::Parse, message, position)
|
||||
}
|
||||
|
||||
/// Construct a [`MathErrorKind::Unsupported`] error.
|
||||
pub fn unsupported(message: impl Into<String>, position: Option<usize>) -> Self {
|
||||
Self::new(MathErrorKind::Unsupported, message, position)
|
||||
}
|
||||
|
||||
/// Construct a [`MathErrorKind::UndefinedVariable`] error.
|
||||
pub fn undefined_variable(message: impl Into<String>, position: Option<usize>) -> Self {
|
||||
Self::new(MathErrorKind::UndefinedVariable, message, position)
|
||||
}
|
||||
|
||||
/// Construct a [`MathErrorKind::Type`] error.
|
||||
pub fn type_error(message: impl Into<String>, position: Option<usize>) -> Self {
|
||||
Self::new(MathErrorKind::Type, message, position)
|
||||
}
|
||||
|
||||
/// The documented result-size guard was hit. Reported as `Unsupported` so
|
||||
/// callers can surface a stable `math.unsupported` diagnostic instead of a
|
||||
/// crash or an out-of-memory abort.
|
||||
pub fn size_bound(message: impl Into<String>) -> Self {
|
||||
Self::unsupported(message, None)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MathError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.kind)?;
|
||||
if let Some(position) = self.position {
|
||||
write!(f, " at byte {position}")?;
|
||||
}
|
||||
write!(f, ": {}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for MathError {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn display_includes_kind_and_position() {
|
||||
let err = MathError::parse("expected an expression", Some(3));
|
||||
assert_eq!(err.to_string(), "parse at byte 3: expected an expression");
|
||||
assert_eq!(err.kind.as_str(), "parse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constructors_use_stable_kinds() {
|
||||
assert_eq!(
|
||||
MathError::unsupported("x", None).kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
MathError::undefined_variable("x", None).kind,
|
||||
MathErrorKind::UndefinedVariable
|
||||
);
|
||||
assert_eq!(MathError::type_error("x", None).kind, MathErrorKind::Type);
|
||||
assert_eq!(MathError::size_bound("x").kind, MathErrorKind::Unsupported);
|
||||
assert_eq!(
|
||||
MathErrorKind::UndefinedVariable.to_string(),
|
||||
"undefined-variable"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,695 +0,0 @@
|
||||
//! Exact evaluation, equation solving and substitution.
|
||||
//!
|
||||
//! Numeric semantics are documented on the crate root. In short: literals,
|
||||
//! integer/float bindings and the operators `+ - * / ^` are evaluated as exact
|
||||
//! rationals; `\pi`, `\e` and non-perfect square roots introduce an `f64`
|
||||
//! approximation. The final value is [`Value::Int`] exactly when the exact
|
||||
//! result is an integer and [`Value::Float`] otherwise.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use num_bigint::{BigInt, Sign};
|
||||
use num_rational::Ratio;
|
||||
use num_traits::{One, Signed, ToPrimitive, Zero};
|
||||
|
||||
use marqdo_value::Value;
|
||||
|
||||
use crate::ast::{Expr, Formula, FormulaKind, Rational};
|
||||
use crate::error::MathError;
|
||||
|
||||
/// Maximum magnitude of an integer exponent accepted by `^`.
|
||||
pub const MAX_EXPONENT: u64 = 10_000;
|
||||
|
||||
/// Maximum number of bits (binary digits) allowed for the numerator and for the
|
||||
/// denominator of an exact intermediate or result. This bounds memory and CPU
|
||||
/// for hostile formulas such as `(2^10000)^10000`.
|
||||
pub const MAX_RESULT_BITS: u64 = 1_000_000;
|
||||
|
||||
/// A number during evaluation: either exact or an explicit `f64`
|
||||
/// approximation produced by an irrational operation.
|
||||
#[derive(Debug, Clone)]
|
||||
enum Numeric {
|
||||
Exact(Rational),
|
||||
Approx(f64),
|
||||
}
|
||||
|
||||
impl Numeric {
|
||||
fn exact_zero() -> Self {
|
||||
Numeric::Exact(Ratio::from_integer(BigInt::zero()))
|
||||
}
|
||||
|
||||
fn exact_one() -> Self {
|
||||
Numeric::Exact(Ratio::from_integer(BigInt::one()))
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
match self {
|
||||
Numeric::Exact(value) => value.is_zero(),
|
||||
Numeric::Approx(value) => *value == 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
fn neg(self) -> Result<Self, MathError> {
|
||||
match self {
|
||||
Numeric::Exact(value) => Ok(Numeric::Exact(-value)),
|
||||
Numeric::Approx(value) => Ok(Numeric::Approx(-value)),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_approx(&self) -> Result<f64, MathError> {
|
||||
match self {
|
||||
Numeric::Approx(value) => Ok(*value),
|
||||
Numeric::Exact(value) => ratio_to_f64(value).ok_or_else(|| {
|
||||
MathError::size_bound(
|
||||
"an exact intermediate result is too large to represent as a Float",
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn add(self, other: Numeric) -> Result<Self, MathError> {
|
||||
match (self, other) {
|
||||
(Numeric::Exact(left), Numeric::Exact(right)) => {
|
||||
Ok(Numeric::Exact(checked_ratio(left + right)?))
|
||||
}
|
||||
(left, right) => Ok(Numeric::Approx(left.to_approx()? + right.to_approx()?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn sub(self, other: Numeric) -> Result<Self, MathError> {
|
||||
match (self, other) {
|
||||
(Numeric::Exact(left), Numeric::Exact(right)) => {
|
||||
Ok(Numeric::Exact(checked_ratio(left - right)?))
|
||||
}
|
||||
(left, right) => Ok(Numeric::Approx(left.to_approx()? - right.to_approx()?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn mul(self, other: Numeric) -> Result<Self, MathError> {
|
||||
match (self, other) {
|
||||
(Numeric::Exact(left), Numeric::Exact(right)) => {
|
||||
Ok(Numeric::Exact(exact_mul(&left, &right)?))
|
||||
}
|
||||
(left, right) => Ok(Numeric::Approx(left.to_approx()? * right.to_approx()?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn div(self, other: Numeric) -> Result<Self, MathError> {
|
||||
if other.is_zero() {
|
||||
return Err(division_by_zero());
|
||||
}
|
||||
match (self, other) {
|
||||
(Numeric::Exact(left), Numeric::Exact(right)) => {
|
||||
Ok(Numeric::Exact(checked_ratio(left / right)?))
|
||||
}
|
||||
(left, right) => Ok(Numeric::Approx(left.to_approx()? / right.to_approx()?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn sqrt(self) -> Result<Self, MathError> {
|
||||
match self {
|
||||
Numeric::Exact(value) => {
|
||||
if value.is_negative() {
|
||||
return Err(MathError::type_error(
|
||||
"square root of a negative number is not supported by the real-valued subset",
|
||||
None,
|
||||
));
|
||||
}
|
||||
let numerator_root = integer_sqrt(value.numer());
|
||||
let denominator_root = integer_sqrt(value.denom());
|
||||
if &(&numerator_root * &numerator_root) == value.numer()
|
||||
&& &(&denominator_root * &denominator_root) == value.denom()
|
||||
{
|
||||
Ok(Numeric::Exact(Ratio::new(numerator_root, denominator_root)))
|
||||
} else {
|
||||
let approximate = ratio_to_f64(&value).ok_or_else(|| {
|
||||
MathError::size_bound(
|
||||
"square root operands are outside the representable Float range",
|
||||
)
|
||||
})?;
|
||||
Ok(Numeric::Approx(approximate.sqrt()))
|
||||
}
|
||||
}
|
||||
Numeric::Approx(value) => {
|
||||
if value < 0.0 {
|
||||
return Err(MathError::type_error(
|
||||
"square root of a negative number is not supported by the real-valued subset",
|
||||
None,
|
||||
));
|
||||
}
|
||||
Ok(Numeric::Approx(value.sqrt()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pow(self, exponent: BigInt) -> Result<Self, MathError> {
|
||||
let negative_exponent = exponent.sign() == Sign::Minus;
|
||||
let magnitude = exponent.magnitude();
|
||||
let bound = num_bigint::BigUint::from(MAX_EXPONENT);
|
||||
if *magnitude > bound {
|
||||
return Err(MathError::size_bound(format!(
|
||||
"exponent magnitude exceeds the supported bound of {MAX_EXPONENT}"
|
||||
)));
|
||||
}
|
||||
let exponent_u32 = magnitude
|
||||
.to_u32()
|
||||
.ok_or_else(|| MathError::size_bound("exponent is outside the supported bound"))?;
|
||||
|
||||
match self {
|
||||
Numeric::Exact(base) => {
|
||||
if exponent_u32 == 0 {
|
||||
if base.is_zero() {
|
||||
return Err(MathError::type_error(
|
||||
"0 ^ 0 is undefined in the native math subset",
|
||||
None,
|
||||
));
|
||||
}
|
||||
return Ok(Numeric::Exact(Ratio::from_integer(BigInt::one())));
|
||||
}
|
||||
if base.is_zero() && negative_exponent {
|
||||
return Err(division_by_zero());
|
||||
}
|
||||
let max_bits = base.numer().bits().max(base.denom().bits());
|
||||
let estimated_bits = max_bits.saturating_mul(u64::from(exponent_u32));
|
||||
if estimated_bits > MAX_RESULT_BITS {
|
||||
return Err(MathError::size_bound(format!(
|
||||
"exponentiation result would exceed the supported size bound of {MAX_RESULT_BITS} bits"
|
||||
)));
|
||||
}
|
||||
let numerator = base.numer().pow(exponent_u32);
|
||||
let denominator = base.denom().pow(exponent_u32);
|
||||
let result = if negative_exponent {
|
||||
Ratio::new(denominator, numerator)
|
||||
} else {
|
||||
Ratio::new(numerator, denominator)
|
||||
};
|
||||
Ok(Numeric::Exact(checked_ratio(result)?))
|
||||
}
|
||||
Numeric::Approx(base) => {
|
||||
if base == 0.0 && negative_exponent {
|
||||
return Err(division_by_zero());
|
||||
}
|
||||
let exponent = exponent_u32 as i32;
|
||||
Ok(Numeric::Approx(base.powi(exponent)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn into_value(self) -> Result<Value, MathError> {
|
||||
match self {
|
||||
Numeric::Exact(value) => {
|
||||
if value.is_integer() {
|
||||
Ok(Value::Int(value.to_integer()))
|
||||
} else {
|
||||
Ok(Value::Float(ratio_to_f64(&value).ok_or_else(|| {
|
||||
MathError::size_bound("exact result is too large to represent as a Float")
|
||||
})?))
|
||||
}
|
||||
}
|
||||
Numeric::Approx(value) => Ok(Value::Float(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn division_by_zero() -> MathError {
|
||||
MathError::type_error(
|
||||
"division by zero is not defined in the native math subset",
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn checked_ratio(value: Rational) -> Result<Rational, MathError> {
|
||||
if value.numer().bits() > MAX_RESULT_BITS || value.denom().bits() > MAX_RESULT_BITS {
|
||||
return Err(MathError::size_bound(format!(
|
||||
"intermediate result exceeds the supported size bound of {MAX_RESULT_BITS} bits"
|
||||
)));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Convert an exact rational to `f64` without going through `Ratio`'s
|
||||
/// `ToPrimitive` impl (which is bound to a different `num-bigint` version in
|
||||
/// `num-rational` 0.4). Returns `None` when the value is outside the finite
|
||||
/// `f64` range or would lose everything to underflow.
|
||||
fn ratio_to_f64(value: &Rational) -> Option<f64> {
|
||||
if value.is_zero() {
|
||||
return Some(0.0);
|
||||
}
|
||||
let negative = value.numer().sign() == Sign::Minus;
|
||||
let (numerator_mantissa, numerator_exponent) = scaled_f64_mantissa(&value.numer().abs());
|
||||
let (denominator_mantissa, denominator_exponent) = scaled_f64_mantissa(value.denom());
|
||||
let exponent = numerator_exponent - denominator_exponent;
|
||||
if exponent > 1023 {
|
||||
return None;
|
||||
}
|
||||
if exponent < -1075 {
|
||||
return Some(if negative { -0.0 } else { 0.0 });
|
||||
}
|
||||
let mut result = numerator_mantissa / denominator_mantissa;
|
||||
result *= 2_f64.powi(exponent as i32);
|
||||
if !result.is_finite() {
|
||||
return None;
|
||||
}
|
||||
Some(if negative { -result } else { result })
|
||||
}
|
||||
|
||||
/// Decompose a non-negative integer into `(mantissa, exponent)` such that the
|
||||
/// value is approximately `mantissa * 2^exponent` and `mantissa` fits in `f64`.
|
||||
fn scaled_f64_mantissa(value: &BigInt) -> (f64, i64) {
|
||||
debug_assert!(!value.is_negative());
|
||||
let bits = value.bits();
|
||||
if bits == 0 {
|
||||
return (0.0, 0);
|
||||
}
|
||||
// Keep the most significant 64 bits; the low bits only affect the last
|
||||
// ~11 bits of the f64 mantissa, which is below the documented precision.
|
||||
let shift = bits.saturating_sub(64);
|
||||
let top = (value >> shift).to_u64().unwrap_or(u64::MAX);
|
||||
(top as f64, shift as i64)
|
||||
}
|
||||
|
||||
fn exact_mul(left: &Rational, right: &Rational) -> Result<Rational, MathError> {
|
||||
// Pre-check before allocating so that multiplying two large operands cannot
|
||||
// allocate an out-of-bound intermediate.
|
||||
let numerator_bits = left.numer().bits().saturating_add(right.numer().bits());
|
||||
let denominator_bits = left.denom().bits().saturating_add(right.denom().bits());
|
||||
if numerator_bits > MAX_RESULT_BITS || denominator_bits > MAX_RESULT_BITS {
|
||||
return Err(MathError::size_bound(format!(
|
||||
"multiplication result would exceed the supported size bound of {MAX_RESULT_BITS} bits"
|
||||
)));
|
||||
}
|
||||
checked_ratio(left * right)
|
||||
}
|
||||
|
||||
/// Integer square root of a non-negative integer (Newton iteration).
|
||||
fn integer_sqrt(value: &BigInt) -> BigInt {
|
||||
debug_assert!(!value.is_negative());
|
||||
if value.is_zero() {
|
||||
return BigInt::zero();
|
||||
}
|
||||
let mut estimate = BigInt::one() << value.bits().div_ceil(2) as usize;
|
||||
loop {
|
||||
let next = (&estimate + value / &estimate) >> 1;
|
||||
if next >= estimate {
|
||||
return estimate;
|
||||
}
|
||||
estimate = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a finite `f64` into the exact binary rational it represents.
|
||||
fn float_to_ratio(value: f64) -> Option<Rational> {
|
||||
if !value.is_finite() {
|
||||
return None;
|
||||
}
|
||||
if value == 0.0 {
|
||||
return Some(Ratio::from_integer(BigInt::zero()));
|
||||
}
|
||||
let bits = value.to_bits();
|
||||
let negative = (bits >> 63) == 1;
|
||||
let exponent_bits = ((bits >> 52) & 0x7ff) as i32;
|
||||
let fraction = bits & ((1_u64 << 52) - 1);
|
||||
let (mantissa, exponent) = if exponent_bits == 0 {
|
||||
(fraction, -1074_i32)
|
||||
} else {
|
||||
(fraction | (1_u64 << 52), exponent_bits - 1075)
|
||||
};
|
||||
let mut numerator = BigInt::from(mantissa);
|
||||
let mut denominator = BigInt::one();
|
||||
if exponent >= 0 {
|
||||
numerator <<= exponent as usize;
|
||||
} else {
|
||||
denominator <<= (-exponent) as usize;
|
||||
}
|
||||
if negative {
|
||||
numerator = -numerator;
|
||||
}
|
||||
Some(Ratio::new(numerator, denominator))
|
||||
}
|
||||
|
||||
/// Convert a [`Value`] binding into a kernel number.
|
||||
fn binding_numeric(name: &str, bindings: &BTreeMap<String, Value>) -> Result<Numeric, MathError> {
|
||||
match bindings.get(name) {
|
||||
None => Err(MathError::undefined_variable(
|
||||
format!("variable `{name}` is not bound"),
|
||||
None,
|
||||
)),
|
||||
Some(Value::Int(value)) => Ok(Numeric::Exact(Ratio::from_integer(value.clone()))),
|
||||
Some(Value::Float(value)) => float_to_ratio(*value).map(Numeric::Exact).ok_or_else(|| {
|
||||
MathError::type_error(
|
||||
format!(
|
||||
"binding `{name}` is a non-finite Float; the math kernel only accepts finite numeric bindings"
|
||||
),
|
||||
None,
|
||||
)
|
||||
}),
|
||||
Some(other) => Err(MathError::type_error(
|
||||
format!(
|
||||
"binding `{name}` has type {} but the math kernel requires Int or Float",
|
||||
other.type_name()
|
||||
),
|
||||
None,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_expr(expr: &Expr, bindings: &BTreeMap<String, Value>) -> Result<Numeric, MathError> {
|
||||
match expr {
|
||||
Expr::Number(value) => Ok(Numeric::Exact(value.clone())),
|
||||
Expr::Variable(name) => binding_numeric(name, bindings),
|
||||
Expr::Constant(constant) => Ok(Numeric::Approx(constant.value())),
|
||||
Expr::Neg(inner) => eval_expr(inner, bindings)?.neg(),
|
||||
Expr::Add(left, right) => eval_expr(left, bindings)?.add(eval_expr(right, bindings)?),
|
||||
Expr::Sub(left, right) => eval_expr(left, bindings)?.sub(eval_expr(right, bindings)?),
|
||||
Expr::Mul(left, right) => eval_expr(left, bindings)?.mul(eval_expr(right, bindings)?),
|
||||
Expr::Div(left, right) => eval_expr(left, bindings)?.div(eval_expr(right, bindings)?),
|
||||
Expr::Pow(base, exponent) => {
|
||||
let base = eval_expr(base, bindings)?;
|
||||
base.pow(integer_exponent(&eval_expr(exponent, bindings)?)?)
|
||||
}
|
||||
Expr::Sqrt(inner) => eval_expr(inner, bindings)?.sqrt(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Require a numeric value to be an integer and return it.
|
||||
fn integer_exponent(value: &Numeric) -> Result<BigInt, MathError> {
|
||||
match value {
|
||||
Numeric::Exact(value) if value.is_integer() => Ok(value.to_integer()),
|
||||
_ => Err(MathError::type_error(
|
||||
"exponent must be an integer in the native math subset",
|
||||
None,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// A linear/affine form `coefficient * target + constant`.
|
||||
struct Affine {
|
||||
coefficient: Numeric,
|
||||
constant: Numeric,
|
||||
}
|
||||
|
||||
impl Affine {
|
||||
fn constant(value: Numeric) -> Self {
|
||||
Self {
|
||||
coefficient: Numeric::exact_zero(),
|
||||
constant: value,
|
||||
}
|
||||
}
|
||||
|
||||
fn target() -> Self {
|
||||
Self {
|
||||
coefficient: Numeric::exact_one(),
|
||||
constant: Numeric::exact_zero(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_target_free(&self) -> bool {
|
||||
self.coefficient.is_zero()
|
||||
}
|
||||
|
||||
fn neg(self) -> Result<Self, MathError> {
|
||||
Ok(Self {
|
||||
coefficient: self.coefficient.neg()?,
|
||||
constant: self.constant.neg()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn add(self, other: Self) -> Result<Self, MathError> {
|
||||
Ok(Self {
|
||||
coefficient: self.coefficient.add(other.coefficient)?,
|
||||
constant: self.constant.add(other.constant)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn sub(self, other: Self) -> Result<Self, MathError> {
|
||||
Ok(Self {
|
||||
coefficient: self.coefficient.sub(other.coefficient)?,
|
||||
constant: self.constant.sub(other.constant)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn scale(self, factor: Numeric) -> Result<Self, MathError> {
|
||||
let coefficient = self.coefficient.mul(factor.clone())?;
|
||||
let constant = self.constant.mul(factor)?;
|
||||
Ok(Self {
|
||||
coefficient,
|
||||
constant,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the affine form of `expr` with respect to `target`.
|
||||
///
|
||||
/// Variables other than `target` are resolved from `bindings`. Operations that
|
||||
/// are not affine in `target` are rejected as unsupported instead of being
|
||||
/// approximated or reported as a solution.
|
||||
fn to_affine(
|
||||
expr: &Expr,
|
||||
target: &str,
|
||||
bindings: &BTreeMap<String, Value>,
|
||||
) -> Result<Affine, MathError> {
|
||||
match expr {
|
||||
Expr::Number(value) => Ok(Affine::constant(Numeric::Exact(value.clone()))),
|
||||
Expr::Constant(constant) => Ok(Affine::constant(Numeric::Approx(constant.value()))),
|
||||
Expr::Variable(name) if name == target => Ok(Affine::target()),
|
||||
Expr::Variable(name) => Ok(Affine::constant(binding_numeric(name, bindings)?)),
|
||||
Expr::Neg(inner) => to_affine(inner, target, bindings)?.neg(),
|
||||
Expr::Add(left, right) => {
|
||||
let left = to_affine(left, target, bindings)?;
|
||||
let right = to_affine(right, target, bindings)?;
|
||||
left.add(right)
|
||||
}
|
||||
Expr::Sub(left, right) => {
|
||||
let left = to_affine(left, target, bindings)?;
|
||||
let right = to_affine(right, target, bindings)?;
|
||||
left.sub(right)
|
||||
}
|
||||
Expr::Mul(left, right) => {
|
||||
let left = to_affine(left, target, bindings)?;
|
||||
let right = to_affine(right, target, bindings)?;
|
||||
match (left.is_target_free(), right.is_target_free()) {
|
||||
(true, true) => Ok(Affine::constant(
|
||||
left.constant.mul(right.constant)?,
|
||||
)),
|
||||
(true, false) => right.scale(left.constant),
|
||||
(false, true) => left.scale(right.constant),
|
||||
(false, false) => Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the equation is not linear in the target (it appears in both factors of a product)"
|
||||
),
|
||||
None,
|
||||
)),
|
||||
}
|
||||
}
|
||||
Expr::Div(left, right) => {
|
||||
let left = to_affine(left, target, bindings)?;
|
||||
let right = to_affine(right, target, bindings)?;
|
||||
if !right.is_target_free() {
|
||||
return Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the target appears in a denominator, which is outside the supported linear forms"
|
||||
),
|
||||
None,
|
||||
));
|
||||
}
|
||||
if right.constant.is_zero() {
|
||||
return Err(division_by_zero());
|
||||
}
|
||||
let inverse = Numeric::exact_one().div(right.constant)?;
|
||||
left.scale(inverse)
|
||||
}
|
||||
Expr::Pow(base, exponent) => {
|
||||
if exponent.occurrences(target) > 0 {
|
||||
return Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the target appears in an exponent, which is not an affine form"
|
||||
),
|
||||
None,
|
||||
));
|
||||
}
|
||||
let exponent_value = eval_expr(exponent, bindings)?;
|
||||
let exponent = integer_exponent(&exponent_value)?;
|
||||
let base = to_affine(base, target, bindings)?;
|
||||
if base.is_target_free() {
|
||||
let value = base.constant.pow(exponent)?;
|
||||
return Ok(Affine::constant(value));
|
||||
}
|
||||
if exponent.is_zero() {
|
||||
return Ok(Affine::constant(Numeric::exact_one()));
|
||||
}
|
||||
if exponent.is_one() {
|
||||
return Ok(base);
|
||||
}
|
||||
Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the target appears in a power with exponent {exponent}, which is not an affine form"
|
||||
),
|
||||
None,
|
||||
))
|
||||
}
|
||||
Expr::Sqrt(inner) => {
|
||||
if inner.occurrences(target) > 0 {
|
||||
return Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the target appears under a square root, which is not an affine form"
|
||||
),
|
||||
None,
|
||||
));
|
||||
}
|
||||
Ok(Affine::constant(eval_expr(inner, bindings)?.sqrt()?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate `formula` with `bindings`.
|
||||
///
|
||||
/// For an expression the result is the exact value (or a [`Value::Float`] when
|
||||
/// the exact value is not integral or an irrational operation was involved).
|
||||
/// For an equation `target` selects the variable to solve for; it must occur
|
||||
/// exactly once and the equation must be linear in it.
|
||||
pub fn evaluate(
|
||||
formula: &Formula,
|
||||
bindings: &BTreeMap<String, Value>,
|
||||
target: Option<&str>,
|
||||
) -> Result<Value, MathError> {
|
||||
match (&formula.kind, target) {
|
||||
(FormulaKind::Expression(expr), None) => eval_expr(expr, bindings)?.into_value(),
|
||||
(FormulaKind::Expression(_), Some(_)) => Err(MathError::unsupported(
|
||||
"a target variable is only meaningful for an equation; the formula is a plain expression",
|
||||
None,
|
||||
)),
|
||||
(FormulaKind::Equation { .. }, None) => Err(MathError::unsupported(
|
||||
"evaluating an equation requires a target variable to solve for",
|
||||
None,
|
||||
)),
|
||||
(FormulaKind::Equation { left, right }, Some(target)) => {
|
||||
solve_equation(left, right, target, bindings)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_equation(
|
||||
left: &Expr,
|
||||
right: &Expr,
|
||||
target: &str,
|
||||
bindings: &BTreeMap<String, Value>,
|
||||
) -> Result<Value, MathError> {
|
||||
if target.is_empty() {
|
||||
return Err(MathError::unsupported(
|
||||
"target variable name must not be empty",
|
||||
None,
|
||||
));
|
||||
}
|
||||
if target == "pi" || target == "e" {
|
||||
return Err(MathError::unsupported(
|
||||
format!("`{target}` is a reserved constant, not a solvable variable"),
|
||||
None,
|
||||
));
|
||||
}
|
||||
let occurrences = left.occurrences(target) + right.occurrences(target);
|
||||
match occurrences {
|
||||
0 => {
|
||||
return Err(MathError::unsupported(
|
||||
format!("cannot solve for `{target}`: the variable does not occur in the equation"),
|
||||
None,
|
||||
));
|
||||
}
|
||||
1 => {}
|
||||
count => {
|
||||
return Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the variable occurs {count} times, but only a single occurrence in an affine equation is supported"
|
||||
),
|
||||
None,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let left = to_affine(left, target, bindings)?;
|
||||
let right = to_affine(right, target, bindings)?;
|
||||
let coefficient = left.coefficient.sub(right.coefficient)?;
|
||||
let constant = left.constant.sub(right.constant)?;
|
||||
if coefficient.is_zero() {
|
||||
return Err(MathError::unsupported(
|
||||
format!(
|
||||
"cannot solve for `{target}`: the equation does not uniquely determine the variable"
|
||||
),
|
||||
None,
|
||||
));
|
||||
}
|
||||
constant.neg()?.div(coefficient)?.into_value()
|
||||
}
|
||||
|
||||
/// Replace every bound variable in `formula` with its exact value.
|
||||
///
|
||||
/// Unbound variables stay symbolic. The returned formula gets a deterministic,
|
||||
/// re-parseable [`Formula::source`] rendering; `Float` bindings are inserted as
|
||||
/// the exact rational value of the `f64` (never as a rounded or lossy decimal).
|
||||
pub fn substitute(
|
||||
formula: &Formula,
|
||||
bindings: &BTreeMap<String, Value>,
|
||||
) -> Result<Formula, MathError> {
|
||||
let kind = match &formula.kind {
|
||||
FormulaKind::Expression(expr) => FormulaKind::Expression(substitute_expr(expr, bindings)?),
|
||||
FormulaKind::Equation { left, right } => FormulaKind::Equation {
|
||||
left: substitute_expr(left, bindings)?,
|
||||
right: substitute_expr(right, bindings)?,
|
||||
},
|
||||
};
|
||||
let substituted = Formula {
|
||||
source: String::new(),
|
||||
kind,
|
||||
};
|
||||
Ok(Formula {
|
||||
source: substituted.render(),
|
||||
kind: substituted.kind,
|
||||
})
|
||||
}
|
||||
|
||||
fn substitute_expr(expr: &Expr, bindings: &BTreeMap<String, Value>) -> Result<Expr, MathError> {
|
||||
match expr {
|
||||
Expr::Number(_) | Expr::Constant(_) => Ok(expr.clone()),
|
||||
Expr::Variable(name) => match bindings.get(name) {
|
||||
None => Ok(expr.clone()),
|
||||
Some(Value::Int(value)) => Ok(Expr::Number(Ratio::from_integer(value.clone()))),
|
||||
Some(Value::Float(value)) => match float_to_ratio(*value) {
|
||||
Some(value) => Ok(Expr::Number(value)),
|
||||
None => Err(MathError::type_error(
|
||||
format!(
|
||||
"binding `{name}` is a non-finite Float; the math kernel only accepts finite numeric bindings"
|
||||
),
|
||||
None,
|
||||
)),
|
||||
},
|
||||
Some(other) => Err(MathError::type_error(
|
||||
format!(
|
||||
"binding `{name}` has type {} but the math kernel requires Int or Float",
|
||||
other.type_name()
|
||||
),
|
||||
None,
|
||||
)),
|
||||
},
|
||||
Expr::Neg(inner) => Ok(Expr::Neg(Box::new(substitute_expr(inner, bindings)?))),
|
||||
Expr::Add(left, right) => Ok(Expr::Add(
|
||||
Box::new(substitute_expr(left, bindings)?),
|
||||
Box::new(substitute_expr(right, bindings)?),
|
||||
)),
|
||||
Expr::Sub(left, right) => Ok(Expr::Sub(
|
||||
Box::new(substitute_expr(left, bindings)?),
|
||||
Box::new(substitute_expr(right, bindings)?),
|
||||
)),
|
||||
Expr::Mul(left, right) => Ok(Expr::Mul(
|
||||
Box::new(substitute_expr(left, bindings)?),
|
||||
Box::new(substitute_expr(right, bindings)?),
|
||||
)),
|
||||
Expr::Div(left, right) => Ok(Expr::Div(
|
||||
Box::new(substitute_expr(left, bindings)?),
|
||||
Box::new(substitute_expr(right, bindings)?),
|
||||
)),
|
||||
Expr::Pow(base, exponent) => Ok(Expr::Pow(
|
||||
Box::new(substitute_expr(base, bindings)?),
|
||||
Box::new(substitute_expr(exponent, bindings)?),
|
||||
)),
|
||||
Expr::Sqrt(inner) => Ok(Expr::Sqrt(Box::new(substitute_expr(inner, bindings)?))),
|
||||
}
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
//! Native Formula kernel for a deliberately limited, documented math subset.
|
||||
//!
|
||||
//! This crate is the native math kernel of the Marqdo toolchain. A Markdown math
|
||||
//! node becomes an inert [`marqdo_value::FormulaValue`]; nothing is evaluated
|
||||
//! implicitly. The host layer calls [`parse`], [`substitute`] and [`evaluate`]
|
||||
//! explicitly, and evaluation is gated by the `math.*` capabilities.
|
||||
//!
|
||||
//! **The kernel never proves anything.** It computes exact or floating values
|
||||
//! and solves a restricted affine equation. It does not establish a theorem,
|
||||
//! does not simplify a formula into a proof obligation and must not be presented
|
||||
//! as a formal verifier. See the blueprint section 5 ("原生 Math") and
|
||||
//! `spec/evaluation.md`.
|
||||
//!
|
||||
//! # Guarantees
|
||||
//!
|
||||
//! * No panics on untrusted LaTeX. Malformed input, unsupported constructs,
|
||||
//! unbounded work and wrong value types all become a structured [`MathError`].
|
||||
//! * Exact rational arithmetic (`num-rational` `Ratio` over `num-bigint`) is used
|
||||
//! wherever possible; integers are arbitrary precision.
|
||||
//! * A formula is never silently simplified, guessed or partially accepted:
|
||||
//! anything outside the grammar below is an error.
|
||||
//!
|
||||
//! # Supported grammar
|
||||
//!
|
||||
//! The parser accepts exactly the following LaTeX subset (whitespace is
|
||||
//! insignificant; byte offsets are reported on parse errors):
|
||||
//!
|
||||
//! ```text
|
||||
//! formula = expr [ "=" expr ] ; single '=' only, never assignment
|
||||
//! expr = term { ("+" | "-") term } ;
|
||||
//! term = unary { ("*" | "/" | "·" | "×" | implicit) unary } ;
|
||||
//! unary = ("+" | "-") unary | power ;
|
||||
//! power = primary [ "^" unary ] ; right associative
|
||||
//! primary = number
|
||||
//! | identifier Unicode letter (plus digits/underscore)
|
||||
//! | "\pi" | "pi" | "\e" | "e" constants
|
||||
//! | "(" expr ")" | "\left(" expr "\right)"
|
||||
//! | "{" expr "}" grouping
|
||||
//! | "\frac{" expr "}{" expr "}" exact division
|
||||
//! | "\sqrt{" expr "}" real square root
|
||||
//! ;
|
||||
//! number = digits [ "." digits ] | "." digits ; decimal, no exponent form
|
||||
//! ```
|
||||
//!
|
||||
//! Notes on the grammar:
|
||||
//!
|
||||
//! * `\cdot` and `\times` are accepted as synonyms for `*`.
|
||||
//! * Implicit multiplication is accepted between adjacent factors where the
|
||||
//! meaning is unambiguous: `2x`, `\pi r^2`, `(a)(b)` and `m a`. Concatenated
|
||||
//! letters form one identifier (`ma` is the variable `ma`; write `m a` or
|
||||
//! `m * a` for a product). Two adjacent number literals are a parse error.
|
||||
//! * Unicode identifiers are supported (for example `面积`, `r`, `速度`). Plain
|
||||
//! multi-letter identifiers are allowed as variables, but the names of common
|
||||
//! elementary functions (`sin`, `cos`, `log`, `exp`, …) are reserved and
|
||||
//! rejected as unsupported so that `sin(x)` is not silently read as a product.
|
||||
//! * A single top-level `=` creates an equation (`F = m a`). It is **not**
|
||||
//! assignment and never writes a binding. A second `=` is a parse error.
|
||||
//!
|
||||
//! # Unsupported constructs
|
||||
//!
|
||||
//! Everything outside the grammar is a structured [`MathErrorKind::Unsupported`]
|
||||
//! or [`MathErrorKind::Parse`] error: matrices, sums/integrals, function calls
|
||||
//! (`\sin`, `\cos`, …), multi-letter commands, `\left[`/`\right]`, mismatched or
|
||||
//! missing braces, scientific notation (`1e3`), commas, arrays, subscripts,
|
||||
//! superscript commands other than the `^` operator, `\frac`/`\sqrt` without
|
||||
//! braced arguments, and so on. Labels, alignment characters and line breaks are
|
||||
//! likewise unsupported. Underscores are ordinary identifier characters rather
|
||||
//! than subscripts: `x_1` is the variable named `x_1`, not `x` subscript `1`.
|
||||
//!
|
||||
//! # Constants and ambiguity handling
|
||||
//!
|
||||
//! * `\pi` and `pi` denote the circle ratio; `\e` and `e` denote Euler's number.
|
||||
//! * These spellings are **reserved**: a binding with the key `pi` or `e` never
|
||||
//! overrides the constant, and they cannot be used as a solve target. Use a
|
||||
//! different variable name (for example `E`, `x`, `r`).
|
||||
//! * `2e` is parsed as `2 * e` (number times the constant); `2e3` is rejected as
|
||||
//! scientific notation rather than silently reinterpreted.
|
||||
//!
|
||||
//! # Numeric semantics
|
||||
//!
|
||||
//! * Decimal literals are exact rationals: `0.25` is `1/4`, not a binary float.
|
||||
//! * `Int` bindings become exact integers. `Float` bindings are interpreted as
|
||||
//! the exact binary rational value of the `f64` (no rounding is introduced);
|
||||
//! non-finite `Float` bindings are a [`MathErrorKind::Type`] error.
|
||||
//! * `+ - * / ^` are exact when both operands are exact. Division keeps a
|
||||
//! rational, so `\frac{1}{3} * 3` evaluates exactly to `1`.
|
||||
//! * The final value is [`marqdo_value::Value::Int`] exactly when the exact result is an
|
||||
//! integer, and [`marqdo_value::Value::Float`] otherwise. Consequently `1.5 + 2.5` returns
|
||||
//! `Int(4)`, while `1 / 2` returns `Float(0.5)`.
|
||||
//! * `\pi`, `\e` and a `\sqrt` of a non-perfect square introduce an `f64`
|
||||
//! approximation; any result derived from one of them is a [`marqdo_value::Value::Float`].
|
||||
//! Precision caveats: approximations follow IEEE-754 binary64, so only about
|
||||
//! 15–17 significant decimal digits are reliable and repeated operations may
|
||||
//! accumulate error. The kernel never claims an approximate result is exact.
|
||||
//! * `\sqrt` of a perfect square of an exact rational stays exact (`\sqrt{4}` is
|
||||
//! `Int(2)`, `\sqrt{1/4}` is `Float(0.5)`); a negative radicand is a type
|
||||
//! error because the subset is real-valued.
|
||||
//! * The exponent of `^` must evaluate to an integer. Its magnitude is bounded by
|
||||
//! [`MAX_EXPONENT`]. Integer powers with a negative exponent are exact
|
||||
//! reciprocals. `0 ^ 0` is a type error.
|
||||
//! * Exact intermediate/result numerators and denominators are bounded by
|
||||
//! [`MAX_RESULT_BITS`]; exceeding the bound returns a structured
|
||||
//! `Unsupported` error instead of hanging or exhausting memory.
|
||||
//! * Parsing is bounded by [`MAX_SOURCE_BYTES`], [`MAX_PARSE_DEPTH`] and
|
||||
//! [`MAX_AST_NODES`]. The node bound also bounds later recursion depth for
|
||||
//! long operator chains, so hostile input cannot overflow the stack during
|
||||
//! evaluation, rendering or substitution.
|
||||
//! * Division by zero, `0 ^ 0` and other undefined real operations are
|
||||
//! [`MathErrorKind::Type`] errors (the kernel has no dedicated division error
|
||||
//! kind, so the host maps the message/kind to `runtime.division_by_zero`).
|
||||
//!
|
||||
//! # Equations
|
||||
//!
|
||||
//! [`evaluate`] of an equation requires `target`. The target must occur exactly
|
||||
//! once and the equation must be affine in it. The kernel supports `+`, `-`,
|
||||
//! multiplication by a target-free side, division where the target is in the
|
||||
//! numerator, and inversion of those operations in either direction (`2x = 4`,
|
||||
//! `4 = 2x`, `x / 2 = 3`, `3 = x / 2`). Other variables are substituted from
|
||||
//! `bindings` first; missing bindings are [`MathErrorKind::UndefinedVariable`].
|
||||
//!
|
||||
//! Solving a target that occurs zero times or more than once, that appears in a
|
||||
//! denominator, under a root, in an exponent or in a non-affine product is a
|
||||
//! structured [`MathErrorKind::Unsupported`] error. A target passed for a plain
|
||||
//! expression, or an equation without a target, is likewise `Unsupported`.
|
||||
//!
|
||||
//! # Substitution
|
||||
//!
|
||||
//! [`substitute`] replaces bound variables with exact numeric literals and keeps
|
||||
//! unbound variables symbolic. The returned [`Formula::source`] is a
|
||||
//! deterministic canonical rendering that [`parse`] accepts again. Because the
|
||||
//! rendering is canonical, it is not byte-identical to the original formatting.
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod ast;
|
||||
mod error;
|
||||
mod eval;
|
||||
mod parse;
|
||||
|
||||
pub use ast::{render_expr, Constant, Expr, Formula, FormulaKind, Rational};
|
||||
pub use error::{MathError, MathErrorKind};
|
||||
pub use eval::{evaluate, substitute, MAX_EXPONENT, MAX_RESULT_BITS};
|
||||
pub use parse::{parse, MAX_AST_NODES, MAX_PARSE_DEPTH, MAX_SOURCE_BYTES};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use marqdo_value::Value;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// End-to-end smoke test for the API shape documented on the crate root.
|
||||
#[test]
|
||||
fn public_api_round_trip() {
|
||||
let formula = parse("A = \\pi r^2").unwrap();
|
||||
assert!(formula.is_equation());
|
||||
assert_eq!(formula.source, "A = \\pi r^2");
|
||||
|
||||
let mut bindings = BTreeMap::new();
|
||||
bindings.insert("r".to_string(), Value::int(2));
|
||||
let area = evaluate(&formula, &bindings, Some("A")).unwrap();
|
||||
let Value::Float(area) = area else {
|
||||
panic!("area should be an approximate Float");
|
||||
};
|
||||
assert!((area - std::f64::consts::PI * 4.0).abs() < 1e-12);
|
||||
|
||||
let substituted = substitute(&formula, &bindings).unwrap();
|
||||
assert_eq!(substituted.source, "A = \\pi * 2 ^ 2");
|
||||
assert!(substituted.is_equation());
|
||||
assert_eq!(substituted.kind, parse("A = \\pi * 2 ^ 2").unwrap().kind);
|
||||
}
|
||||
}
|
||||
@@ -1,837 +0,0 @@
|
||||
//! Lexer and recursive-descent parser for the documented math subset.
|
||||
//!
|
||||
//! The parser is intentionally strict: anything outside the subset becomes a
|
||||
//! structured [`MathError`] instead of being skipped, guessed or silently
|
||||
//! simplified. It never panics on untrusted input: recursion depth, source size
|
||||
//! and numeric literal size are all bounded before the corresponding work
|
||||
//! happens.
|
||||
|
||||
use std::iter::Peekable;
|
||||
use std::str::CharIndices;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use num_rational::Ratio;
|
||||
|
||||
use crate::ast::{Constant, Expr, Formula, FormulaKind, Rational};
|
||||
use crate::error::MathError;
|
||||
|
||||
/// Maximum accepted source length in bytes. Longer inputs are rejected before
|
||||
/// tokenization so that a hostile formula cannot consume unbounded memory.
|
||||
pub const MAX_SOURCE_BYTES: usize = 1_000_000;
|
||||
|
||||
/// Maximum parser recursion depth (parentheses, groups, unary chains, fractions
|
||||
/// and roots all consume one level). Deeper input is a parse error instead of a
|
||||
/// stack overflow.
|
||||
pub const MAX_PARSE_DEPTH: usize = 256;
|
||||
|
||||
/// Maximum number of AST nodes in one formula. This bounds evaluation,
|
||||
/// substitution and rendering recursion depth: a long left-associative chain
|
||||
/// such as `1 + 1 + ...` would otherwise build a very deep tree even though the
|
||||
/// parser loop itself is iterative. Deeper formulas are rejected with a parse
|
||||
/// error instead of risking a stack overflow later.
|
||||
pub const MAX_AST_NODES: usize = 512;
|
||||
|
||||
/// Maximum number of digits in one numeric literal.
|
||||
const MAX_LITERAL_DIGITS: usize = 100_000;
|
||||
|
||||
/// Plain identifiers that name elementary functions in ordinary LaTeX. They are
|
||||
/// reported as unsupported instead of being silently reinterpreted as variables
|
||||
/// or implicit multiplication.
|
||||
const FUNCTION_NAMES: &[&str] = &[
|
||||
"sin", "cos", "tan", "cot", "sec", "csc", "arcsin", "arccos", "arctan", "sinh", "cosh", "tanh",
|
||||
"log", "ln", "lg", "exp", "abs", "min", "max", "floor", "ceil", "round", "det", "dim", "gcd",
|
||||
"lcm", "lim", "sup", "inf", "arg", "Re", "Im",
|
||||
];
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
enum TokenKind {
|
||||
Number(Rational),
|
||||
Ident(String),
|
||||
Constant(Constant),
|
||||
Plus,
|
||||
Minus,
|
||||
Star,
|
||||
Slash,
|
||||
Caret,
|
||||
LParen,
|
||||
RParen,
|
||||
LBrace,
|
||||
RBrace,
|
||||
Equals,
|
||||
Frac,
|
||||
Sqrt,
|
||||
Eof,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Token {
|
||||
kind: TokenKind,
|
||||
/// Byte offset of the first byte of the token.
|
||||
start: usize,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
fn describe(&self) -> String {
|
||||
match &self.kind {
|
||||
TokenKind::Number(_) => "a number".to_string(),
|
||||
TokenKind::Ident(name) => format!("identifier `{name}`"),
|
||||
TokenKind::Constant(constant) => match constant {
|
||||
Constant::Pi => "`\\pi`".to_string(),
|
||||
Constant::E => "`\\e`".to_string(),
|
||||
},
|
||||
TokenKind::Plus => "`+`".to_string(),
|
||||
TokenKind::Minus => "`-`".to_string(),
|
||||
TokenKind::Star => "`*`".to_string(),
|
||||
TokenKind::Slash => "`/`".to_string(),
|
||||
TokenKind::Caret => "`^`".to_string(),
|
||||
TokenKind::LParen => "`(`".to_string(),
|
||||
TokenKind::RParen => "`)`".to_string(),
|
||||
TokenKind::LBrace => "`{`".to_string(),
|
||||
TokenKind::RBrace => "`}`".to_string(),
|
||||
TokenKind::Equals => "`=`".to_string(),
|
||||
TokenKind::Frac => "`\\frac`".to_string(),
|
||||
TokenKind::Sqrt => "`\\sqrt`".to_string(),
|
||||
TokenKind::Eof => "the end of the formula".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse `latex` into a [`Formula`].
|
||||
///
|
||||
/// The supported grammar is documented on the crate root. The returned formula
|
||||
/// keeps the original source in [`Formula::source`] and exposes the parsed
|
||||
/// [`FormulaKind`] for inspection.
|
||||
pub fn parse(latex: &str) -> Result<Formula, MathError> {
|
||||
if latex.len() > MAX_SOURCE_BYTES {
|
||||
return Err(MathError::parse(
|
||||
format!(
|
||||
"formula source is {} bytes; the supported limit is {MAX_SOURCE_BYTES} bytes",
|
||||
latex.len()
|
||||
),
|
||||
Some(0),
|
||||
));
|
||||
}
|
||||
let tokens = lex(latex)?;
|
||||
Parser {
|
||||
tokens,
|
||||
position: 0,
|
||||
depth: 0,
|
||||
nodes: 0,
|
||||
}
|
||||
.parse_formula(latex)
|
||||
}
|
||||
|
||||
fn lex(source: &str) -> Result<Vec<Token>, MathError> {
|
||||
Lexer {
|
||||
source,
|
||||
chars: source.char_indices().peekable(),
|
||||
tokens: Vec::new(),
|
||||
}
|
||||
.run()
|
||||
}
|
||||
|
||||
struct Lexer<'a> {
|
||||
source: &'a str,
|
||||
chars: Peekable<CharIndices<'a>>,
|
||||
tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
fn run(mut self) -> Result<Vec<Token>, MathError> {
|
||||
while let Some(&(start, ch)) = self.chars.peek() {
|
||||
if ch.is_whitespace() {
|
||||
self.chars.next();
|
||||
continue;
|
||||
}
|
||||
let kind = match ch {
|
||||
'+' => {
|
||||
self.chars.next();
|
||||
TokenKind::Plus
|
||||
}
|
||||
'-' => {
|
||||
self.chars.next();
|
||||
TokenKind::Minus
|
||||
}
|
||||
'*' => {
|
||||
self.chars.next();
|
||||
TokenKind::Star
|
||||
}
|
||||
'/' => {
|
||||
self.chars.next();
|
||||
TokenKind::Slash
|
||||
}
|
||||
'^' => {
|
||||
self.chars.next();
|
||||
TokenKind::Caret
|
||||
}
|
||||
'(' => {
|
||||
self.chars.next();
|
||||
TokenKind::LParen
|
||||
}
|
||||
')' => {
|
||||
self.chars.next();
|
||||
TokenKind::RParen
|
||||
}
|
||||
'{' => {
|
||||
self.chars.next();
|
||||
TokenKind::LBrace
|
||||
}
|
||||
'}' => {
|
||||
self.chars.next();
|
||||
TokenKind::RBrace
|
||||
}
|
||||
'=' => {
|
||||
self.chars.next();
|
||||
TokenKind::Equals
|
||||
}
|
||||
'\\' => self.lex_command()?,
|
||||
ch if ch.is_ascii_digit() || ch == '.' => self.lex_number()?,
|
||||
ch if is_ident_start(ch) => self.lex_identifier()?,
|
||||
other => {
|
||||
return Err(MathError::unsupported(
|
||||
format!("unsupported character `{other}` in a formula"),
|
||||
Some(start),
|
||||
));
|
||||
}
|
||||
};
|
||||
self.tokens.push(Token { kind, start });
|
||||
}
|
||||
self.tokens.push(Token {
|
||||
kind: TokenKind::Eof,
|
||||
start: self.source.len(),
|
||||
});
|
||||
Ok(self.tokens)
|
||||
}
|
||||
|
||||
fn lex_identifier(&mut self) -> Result<TokenKind, MathError> {
|
||||
let start = self.current_offset();
|
||||
let mut text = String::new();
|
||||
while let Some(&(_, ch)) = self.chars.peek() {
|
||||
if is_ident_continue(ch) {
|
||||
text.push(ch);
|
||||
self.chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
match text.as_str() {
|
||||
"pi" => Ok(TokenKind::Constant(Constant::Pi)),
|
||||
"e" => Ok(TokenKind::Constant(Constant::E)),
|
||||
_ if FUNCTION_NAMES.contains(&text.as_str()) => Err(MathError::unsupported(
|
||||
format!(
|
||||
"function `{text}` is not supported; the native math subset has no function calls"
|
||||
),
|
||||
Some(start),
|
||||
)),
|
||||
_ => Ok(TokenKind::Ident(text)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Byte offset of the next unconsumed character (or the source end).
|
||||
fn current_offset(&mut self) -> usize {
|
||||
match self.chars.peek() {
|
||||
Some(&(offset, _)) => offset,
|
||||
None => self.source.len(),
|
||||
}
|
||||
}
|
||||
|
||||
fn lex_number(&mut self) -> Result<TokenKind, MathError> {
|
||||
let start = self.current_offset();
|
||||
let mut integer_digits = String::new();
|
||||
while let Some(&(_, ch)) = self.chars.peek() {
|
||||
if ch.is_ascii_digit() {
|
||||
integer_digits.push(ch);
|
||||
self.chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut fraction_digits = String::new();
|
||||
if matches!(self.chars.peek(), Some(&(_, '.'))) {
|
||||
self.chars.next();
|
||||
while let Some(&(_, ch)) = self.chars.peek() {
|
||||
if ch.is_ascii_digit() {
|
||||
fraction_digits.push(ch);
|
||||
self.chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if fraction_digits.is_empty() {
|
||||
return Err(MathError::parse(
|
||||
"malformed decimal literal: expected digits after `.`",
|
||||
Some(start),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if integer_digits.is_empty() && fraction_digits.is_empty() {
|
||||
return Err(MathError::parse("malformed numeric literal", Some(start)));
|
||||
}
|
||||
if integer_digits.len() + fraction_digits.len() > MAX_LITERAL_DIGITS {
|
||||
return Err(MathError::parse(
|
||||
format!(
|
||||
"numeric literal has more than {MAX_LITERAL_DIGITS} digits, which is outside the supported resource bound"
|
||||
),
|
||||
Some(start),
|
||||
));
|
||||
}
|
||||
|
||||
// Reject scientific notation (`1e3`) explicitly: it is outside the
|
||||
// subset and would otherwise parse as `1 * e3`.
|
||||
if matches!(self.chars.peek(), Some(&(_, 'e' | 'E')))
|
||||
&& self.number_is_followed_by_exponent()
|
||||
{
|
||||
return Err(MathError::parse(
|
||||
"scientific notation is not supported; write an explicit product such as `2 * 10 ^ 3`",
|
||||
Some(start),
|
||||
));
|
||||
}
|
||||
|
||||
let integer_text = if integer_digits.is_empty() {
|
||||
"0"
|
||||
} else {
|
||||
&integer_digits
|
||||
};
|
||||
let numerator = parse_bigint(integer_text, start)?;
|
||||
if fraction_digits.is_empty() {
|
||||
return Ok(TokenKind::Number(Ratio::from_integer(numerator)));
|
||||
}
|
||||
let mut all_digits = String::with_capacity(integer_digits.len() + fraction_digits.len());
|
||||
all_digits.push_str(if integer_digits.is_empty() {
|
||||
"0"
|
||||
} else {
|
||||
&integer_digits
|
||||
});
|
||||
all_digits.push_str(&fraction_digits);
|
||||
let numerator = parse_bigint(&all_digits, start)?;
|
||||
let denominator = BigInt::from(10_u32).pow(fraction_digits.len() as u32);
|
||||
Ok(TokenKind::Number(Ratio::new(numerator, denominator)))
|
||||
}
|
||||
|
||||
/// Look past the current `e`/`E` to decide whether it introduces an
|
||||
/// exponent (`1e3`, `1e-3`) rather than the Euler constant (`2e`).
|
||||
fn number_is_followed_by_exponent(&self) -> bool {
|
||||
let mut chars = self.chars.clone();
|
||||
let _ = chars.next(); // the 'e' / 'E'
|
||||
match chars.next().map(|(_, ch)| ch) {
|
||||
Some(ch) if ch.is_ascii_digit() => true,
|
||||
Some('+' | '-') => {
|
||||
matches!(chars.next().map(|(_, ch)| ch), Some(ch) if ch.is_ascii_digit())
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn lex_command(&mut self) -> Result<TokenKind, MathError> {
|
||||
let start = self.current_offset();
|
||||
self.chars.next(); // consume the backslash
|
||||
let mut name = String::new();
|
||||
while let Some(&(_, ch)) = self.chars.peek() {
|
||||
if ch.is_ascii_alphabetic() {
|
||||
name.push(ch);
|
||||
self.chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
match name.as_str() {
|
||||
"pi" => Ok(TokenKind::Constant(Constant::Pi)),
|
||||
"e" => Ok(TokenKind::Constant(Constant::E)),
|
||||
"cdot" | "times" => Ok(TokenKind::Star),
|
||||
"frac" => Ok(TokenKind::Frac),
|
||||
"sqrt" => Ok(TokenKind::Sqrt),
|
||||
"left" => self.lex_left_right(start, '('),
|
||||
"right" => self.lex_left_right(start, ')'),
|
||||
"" => Err(MathError::unsupported(
|
||||
"unsupported LaTeX control symbol; only alphabetic commands such as `\\pi` are supported",
|
||||
Some(start),
|
||||
)),
|
||||
other => Err(MathError::unsupported(
|
||||
format!(
|
||||
"unsupported LaTeX command `\\{other}`; the native math subset only supports `\\frac`, `\\sqrt`, `\\cdot`, `\\times`, `\\left(`, `\\right)`, `\\pi` and `\\e`"
|
||||
),
|
||||
Some(start),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn lex_left_right(&mut self, start: usize, expected: char) -> Result<TokenKind, MathError> {
|
||||
while let Some(&(_, ch)) = self.chars.peek() {
|
||||
if ch.is_whitespace() {
|
||||
self.chars.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
match self.chars.next() {
|
||||
Some((_, ch)) if ch == expected => Ok(if expected == '(' {
|
||||
TokenKind::LParen
|
||||
} else {
|
||||
TokenKind::RParen
|
||||
}),
|
||||
_ => Err(MathError::unsupported(
|
||||
format!(
|
||||
"only the parenthesis forms `\\left(` and `\\right)` are supported, not `\\{}` followed by another delimiter",
|
||||
if expected == '(' { "left" } else { "right" }
|
||||
),
|
||||
Some(start),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bigint(digits: &str, position: usize) -> Result<BigInt, MathError> {
|
||||
digits
|
||||
.parse::<BigInt>()
|
||||
.map_err(|_| MathError::parse("numeric literal is too large to represent", Some(position)))
|
||||
}
|
||||
|
||||
fn is_ident_start(ch: char) -> bool {
|
||||
ch.is_alphabetic() || ch == '_'
|
||||
}
|
||||
|
||||
fn is_ident_continue(ch: char) -> bool {
|
||||
ch.is_alphanumeric() || ch == '_'
|
||||
}
|
||||
|
||||
struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
position: usize,
|
||||
depth: usize,
|
||||
nodes: usize,
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
fn peek(&self) -> &Token {
|
||||
// The lexer always appends an Eof token, so indexing is in bounds.
|
||||
&self.tokens[self.position.min(self.tokens.len() - 1)]
|
||||
}
|
||||
|
||||
fn advance(&mut self) -> Token {
|
||||
let token = self.peek().clone();
|
||||
if self.position < self.tokens.len() - 1 {
|
||||
self.position += 1;
|
||||
}
|
||||
token
|
||||
}
|
||||
|
||||
/// Record one constructed AST node, enforcing [`MAX_AST_NODES`].
|
||||
fn node(&mut self, expr: Expr) -> Result<Expr, MathError> {
|
||||
self.nodes += 1;
|
||||
if self.nodes > MAX_AST_NODES {
|
||||
return Err(MathError::parse(
|
||||
format!(
|
||||
"formula contains more than the supported limit of {MAX_AST_NODES} syntax nodes"
|
||||
),
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
fn enter_depth(&mut self) -> Result<(), MathError> {
|
||||
self.depth += 1;
|
||||
if self.depth > MAX_PARSE_DEPTH {
|
||||
return Err(MathError::parse(
|
||||
format!("formula nesting exceeds the supported limit of {MAX_PARSE_DEPTH} levels"),
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_formula(mut self, source: &str) -> Result<Formula, MathError> {
|
||||
if matches!(self.peek().kind, TokenKind::Eof) {
|
||||
return Err(MathError::parse("empty formula", Some(0)));
|
||||
}
|
||||
let left = self.parse_expr()?;
|
||||
let kind = if matches!(self.peek().kind, TokenKind::Equals) {
|
||||
let equals = self.advance();
|
||||
if matches!(self.peek().kind, TokenKind::Eof) {
|
||||
return Err(MathError::parse(
|
||||
"expected a right-hand side after `=`",
|
||||
Some(equals.start + 1),
|
||||
));
|
||||
}
|
||||
let right = self.parse_expr()?;
|
||||
if matches!(self.peek().kind, TokenKind::Equals) {
|
||||
return Err(MathError::parse(
|
||||
"more than one `=` in a formula; at most one equation is supported and `=` is never assignment",
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
FormulaKind::Equation { left, right }
|
||||
} else {
|
||||
FormulaKind::Expression(left)
|
||||
};
|
||||
if !matches!(self.peek().kind, TokenKind::Eof) {
|
||||
return Err(MathError::parse(
|
||||
format!("unexpected {} after the formula", self.peek().describe()),
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
Ok(Formula {
|
||||
source: source.to_string(),
|
||||
kind,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_expr(&mut self) -> Result<Expr, MathError> {
|
||||
let mut left = self.parse_term()?;
|
||||
loop {
|
||||
match self.peek().kind {
|
||||
TokenKind::Plus => {
|
||||
self.advance();
|
||||
let right = self.parse_term()?;
|
||||
left = self.node(Expr::Add(Box::new(left), Box::new(right)))?;
|
||||
}
|
||||
TokenKind::Minus => {
|
||||
self.advance();
|
||||
let right = self.parse_term()?;
|
||||
left = self.node(Expr::Sub(Box::new(left), Box::new(right)))?;
|
||||
}
|
||||
_ => return Ok(left),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_term(&mut self) -> Result<Expr, MathError> {
|
||||
let mut left = self.parse_unary()?;
|
||||
loop {
|
||||
if matches!(self.peek().kind, TokenKind::Star) {
|
||||
self.advance();
|
||||
let right = self.parse_unary()?;
|
||||
left = self.node(Expr::Mul(Box::new(left), Box::new(right)))?;
|
||||
} else if matches!(self.peek().kind, TokenKind::Slash) {
|
||||
self.advance();
|
||||
let right = self.parse_unary()?;
|
||||
left = self.node(Expr::Div(Box::new(left), Box::new(right)))?;
|
||||
} else if can_start_primary(&self.peek().kind) {
|
||||
if matches!(left, Expr::Number(_))
|
||||
&& matches!(self.peek().kind, TokenKind::Number(_))
|
||||
{
|
||||
return Err(MathError::parse(
|
||||
"two adjacent number literals are ambiguous; insert an operator",
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
// Implicit multiplication: `2x`, `\pi r^2`, `m a`.
|
||||
let right = self.parse_unary()?;
|
||||
left = self.node(Expr::Mul(Box::new(left), Box::new(right)))?;
|
||||
} else {
|
||||
return Ok(left);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_unary(&mut self) -> Result<Expr, MathError> {
|
||||
self.enter_depth()?;
|
||||
let result = self.parse_unary_inner();
|
||||
self.depth -= 1;
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_unary_inner(&mut self) -> Result<Expr, MathError> {
|
||||
match self.peek().kind {
|
||||
TokenKind::Minus => {
|
||||
self.advance();
|
||||
let inner = self.parse_unary()?;
|
||||
self.node(Expr::Neg(Box::new(inner)))
|
||||
}
|
||||
TokenKind::Plus => {
|
||||
self.advance();
|
||||
self.parse_unary()
|
||||
}
|
||||
_ => self.parse_power(),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_power(&mut self) -> Result<Expr, MathError> {
|
||||
let base = self.parse_primary()?;
|
||||
if matches!(self.peek().kind, TokenKind::Caret) {
|
||||
self.advance();
|
||||
let exponent = self.parse_unary()?;
|
||||
return self.node(Expr::Pow(Box::new(base), Box::new(exponent)));
|
||||
}
|
||||
Ok(base)
|
||||
}
|
||||
|
||||
fn parse_primary(&mut self) -> Result<Expr, MathError> {
|
||||
self.enter_depth()?;
|
||||
let result = self.parse_primary_inner();
|
||||
self.depth -= 1;
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_primary_inner(&mut self) -> Result<Expr, MathError> {
|
||||
let token = self.peek().clone();
|
||||
match token.kind {
|
||||
TokenKind::Number(value) => {
|
||||
self.advance();
|
||||
self.node(Expr::Number(value))
|
||||
}
|
||||
TokenKind::Ident(name) => {
|
||||
self.advance();
|
||||
self.node(Expr::Variable(name))
|
||||
}
|
||||
TokenKind::Constant(constant) => {
|
||||
self.advance();
|
||||
self.node(Expr::Constant(constant))
|
||||
}
|
||||
TokenKind::LParen => {
|
||||
self.advance();
|
||||
let inner = self.parse_expr()?;
|
||||
self.expect_closing(TokenKind::RParen, "`)`", token.start)?;
|
||||
Ok(inner)
|
||||
}
|
||||
TokenKind::LBrace => {
|
||||
self.advance();
|
||||
let inner = self.parse_expr()?;
|
||||
self.expect_closing(TokenKind::RBrace, "`}`", token.start)?;
|
||||
Ok(inner)
|
||||
}
|
||||
TokenKind::Frac => {
|
||||
self.advance();
|
||||
let numerator = self.parse_braced_argument("\\frac")?;
|
||||
let denominator = self.parse_braced_argument("\\frac")?;
|
||||
self.node(Expr::Div(Box::new(numerator), Box::new(denominator)))
|
||||
}
|
||||
TokenKind::Sqrt => {
|
||||
self.advance();
|
||||
let inner = self.parse_braced_argument("\\sqrt")?;
|
||||
self.node(Expr::Sqrt(Box::new(inner)))
|
||||
}
|
||||
TokenKind::Eof => Err(MathError::parse(
|
||||
"expected an expression, found the end of the formula",
|
||||
Some(token.start),
|
||||
)),
|
||||
other => Err(MathError::parse(
|
||||
format!(
|
||||
"expected an expression, found {}",
|
||||
Token {
|
||||
kind: other,
|
||||
start: token.start
|
||||
}
|
||||
.describe()
|
||||
),
|
||||
Some(token.start),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_braced_argument(&mut self, command: &str) -> Result<Expr, MathError> {
|
||||
if !matches!(self.peek().kind, TokenKind::LBrace) {
|
||||
return Err(MathError::parse(
|
||||
format!("`{command}` requires a braced argument such as `{command}{{x}}`"),
|
||||
Some(self.peek().start),
|
||||
));
|
||||
}
|
||||
let open = self.advance();
|
||||
let inner = self.parse_expr()?;
|
||||
self.expect_closing(TokenKind::RBrace, "`}`", open.start)?;
|
||||
Ok(inner)
|
||||
}
|
||||
|
||||
fn expect_closing(
|
||||
&mut self,
|
||||
expected: TokenKind,
|
||||
description: &str,
|
||||
open_position: usize,
|
||||
) -> Result<(), MathError> {
|
||||
if std::mem::discriminant(&self.peek().kind) == std::mem::discriminant(&expected) {
|
||||
self.advance();
|
||||
Ok(())
|
||||
} else {
|
||||
Err(MathError::parse(
|
||||
format!(
|
||||
"expected {description} to close the group opened at byte {open_position}, found {}",
|
||||
self.peek().describe()
|
||||
),
|
||||
Some(self.peek().start),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn can_start_primary(kind: &TokenKind) -> bool {
|
||||
matches!(
|
||||
kind,
|
||||
TokenKind::Number(_)
|
||||
| TokenKind::Ident(_)
|
||||
| TokenKind::Constant(_)
|
||||
| TokenKind::LParen
|
||||
| TokenKind::LBrace
|
||||
| TokenKind::Frac
|
||||
| TokenKind::Sqrt
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn parse_ok(source: &str) -> Formula {
|
||||
parse(source).unwrap_or_else(|err| panic!("expected {source:?} to parse: {err}"))
|
||||
}
|
||||
|
||||
fn parse_err(source: &str) -> MathError {
|
||||
parse(source).unwrap_err()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precedence_matches_standard_arithmetic() {
|
||||
let formula = parse_ok("1 + 2 * 3");
|
||||
assert_eq!(
|
||||
formula.kind,
|
||||
FormulaKind::Expression(Expr::Add(
|
||||
Box::new(Expr::Number(Ratio::from_integer(BigInt::from(1)))),
|
||||
Box::new(Expr::Mul(
|
||||
Box::new(Expr::Number(Ratio::from_integer(BigInt::from(2)))),
|
||||
Box::new(Expr::Number(Ratio::from_integer(BigInt::from(3)))),
|
||||
)),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unary_minus_binds_looser_than_power() {
|
||||
// -2^2 is -(2^2), not (-2)^2.
|
||||
let formula = parse_ok("-2^2");
|
||||
assert!(matches!(
|
||||
formula.kind,
|
||||
FormulaKind::Expression(Expr::Neg(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn power_is_right_associative() {
|
||||
let formula = parse_ok("2^3^2");
|
||||
let FormulaKind::Expression(Expr::Pow(_, exponent)) = formula.kind else {
|
||||
panic!("expected a power");
|
||||
};
|
||||
assert!(matches!(*exponent, Expr::Pow(_, _)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn implicit_multiplication_joins_adjacent_factors() {
|
||||
let formula = parse_ok("\\pi r^2");
|
||||
let FormulaKind::Expression(Expr::Mul(left, right)) = formula.kind else {
|
||||
panic!("expected multiplication");
|
||||
};
|
||||
assert!(matches!(*left, Expr::Constant(Constant::Pi)));
|
||||
assert!(matches!(*right, Expr::Pow(_, _)));
|
||||
|
||||
// `F = m a` is an equation whose right side is a product.
|
||||
let formula = parse_ok("F = m a");
|
||||
let FormulaKind::Equation { right, .. } = formula.kind else {
|
||||
panic!("expected an equation");
|
||||
};
|
||||
assert!(matches!(right, Expr::Mul(_, _)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_and_right_parentheses_are_accepted() {
|
||||
assert_eq!(
|
||||
parse_ok("\\left(1 + 2\\right) * 3").kind,
|
||||
parse_ok("(1 + 2) * 3").kind
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frac_and_sqrt_become_dedicated_nodes() {
|
||||
let formula = parse_ok("\\frac{1}{2} + \\sqrt{4}");
|
||||
let FormulaKind::Expression(Expr::Add(left, right)) = formula.kind else {
|
||||
panic!("expected addition");
|
||||
};
|
||||
assert!(matches!(*left, Expr::Div(_, _)));
|
||||
assert!(matches!(*right, Expr::Sqrt(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_equals_is_an_equation() {
|
||||
let formula = parse_ok("A = \\pi r^2");
|
||||
assert!(formula.is_equation());
|
||||
assert_eq!(formula.variables().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn more_than_one_equals_is_a_parse_error() {
|
||||
let err = parse_err("a = b = c");
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Parse);
|
||||
assert!(err.message.contains("more than one"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_and_malformed_input_is_a_parse_error() {
|
||||
assert_eq!(parse_err("").kind, crate::error::MathErrorKind::Parse);
|
||||
for source in ["1 +", "(1 + 2", "\\frac{1}{2", "{1 + 2", "1 + 2)", "= 3"] {
|
||||
let err = parse_err(source);
|
||||
assert_eq!(
|
||||
err.kind,
|
||||
crate::error::MathErrorKind::Parse,
|
||||
"source {source:?}"
|
||||
);
|
||||
assert!(err.position.is_some(), "source {source:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_commands_are_structured() {
|
||||
for source in [
|
||||
"\\begin{matrix}1&2\\end{matrix}",
|
||||
"\\int_0^1 x dx",
|
||||
"\\sin(x)",
|
||||
"\\sum_{i=1}^n i",
|
||||
"sin(1)",
|
||||
] {
|
||||
let err = parse_err(source);
|
||||
assert_eq!(
|
||||
err.kind,
|
||||
crate::error::MathErrorKind::Unsupported,
|
||||
"source {source:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_characters_are_structured() {
|
||||
let err = parse_err("1, 2");
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Unsupported);
|
||||
assert!(err.message.contains("unsupported character"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_nesting_is_rejected_without_stack_overflow() {
|
||||
let source = format!("{}1{}", "(".repeat(10_000), ")".repeat(10_000));
|
||||
let err = parse_err(&source);
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Parse);
|
||||
assert!(err.message.contains("nesting"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn excessive_node_count_is_rejected() {
|
||||
// A long left-associative chain is cheap to parse iteratively but would
|
||||
// build a very deep tree; the node budget rejects it first.
|
||||
let source = format!("{}1", "1 + ".repeat(MAX_AST_NODES));
|
||||
let err = parse_err(&source);
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Parse);
|
||||
assert!(err.message.contains("syntax nodes"), "{}", err.message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn very_long_chain_is_rejected_without_stack_overflow() {
|
||||
let source = format!("{}1", "1 + ".repeat(200_000));
|
||||
let err = parse_err(&source);
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Parse);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_literal_bounds_are_enforced() {
|
||||
let source = "9".repeat(MAX_LITERAL_DIGITS + 1);
|
||||
let err = parse_err(&source);
|
||||
assert_eq!(err.kind, crate::error::MathErrorKind::Parse);
|
||||
assert!(err.message.contains("digits"));
|
||||
}
|
||||
}
|
||||
@@ -1,596 +0,0 @@
|
||||
//! End-to-end tests for the native Formula kernel.
|
||||
//!
|
||||
//! These tests exercise the public contract used by the host layer:
|
||||
//! `parse`, `evaluate` and `substitute`. They cover the documented grammar,
|
||||
//! exact numeric semantics, equation solving, structured errors and resource
|
||||
//! bounds.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use marqdo_math::{evaluate, parse, substitute, MathError, MathErrorKind, MAX_EXPONENT};
|
||||
use marqdo_value::Value;
|
||||
use num_bigint::BigInt;
|
||||
|
||||
fn empty() -> BTreeMap<String, Value> {
|
||||
BTreeMap::new()
|
||||
}
|
||||
|
||||
fn bindings(entries: &[(&str, Value)]) -> BTreeMap<String, Value> {
|
||||
entries
|
||||
.iter()
|
||||
.map(|(name, value)| ((*name).to_string(), value.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn eval(source: &str, bindings: &BTreeMap<String, Value>) -> Result<Value, MathError> {
|
||||
evaluate(&parse(source).expect("fixture must parse"), bindings, None)
|
||||
}
|
||||
|
||||
fn solve(
|
||||
source: &str,
|
||||
bindings: &BTreeMap<String, Value>,
|
||||
target: &str,
|
||||
) -> Result<Value, MathError> {
|
||||
evaluate(
|
||||
&parse(source).expect("fixture must parse"),
|
||||
bindings,
|
||||
Some(target),
|
||||
)
|
||||
}
|
||||
|
||||
fn assert_int(value: &Value, expected: i64) {
|
||||
assert_eq!(value, &Value::int(expected), "value was {value:?}");
|
||||
}
|
||||
|
||||
fn assert_float_close(value: &Value, expected: f64) {
|
||||
let Value::Float(actual) = value else {
|
||||
panic!("expected a Float, got {value:?}");
|
||||
};
|
||||
assert!(
|
||||
(actual - expected).abs() <= 1e-12 * expected.abs().max(1.0),
|
||||
"expected {expected}, got {actual}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precedence_and_parentheses() {
|
||||
assert_int(&eval("1 + 2 * 3", &empty()).unwrap(), 7);
|
||||
assert_int(&eval("(1 + 2) * 3", &empty()).unwrap(), 9);
|
||||
assert_int(&eval("10 - 3 - 2", &empty()).unwrap(), 5);
|
||||
assert_int(&eval("8 / 4 / 2", &empty()).unwrap(), 1);
|
||||
assert_int(&eval("2 + 3 * 4 ^ 2", &empty()).unwrap(), 50);
|
||||
// Power is right associative, unary minus binds looser than power.
|
||||
assert_int(&eval("2 ^ 3 ^ 2", &empty()).unwrap(), 512);
|
||||
assert_int(&eval("-2 ^ 2", &empty()).unwrap(), -4);
|
||||
assert_int(&eval("(-2) ^ 2", &empty()).unwrap(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_big_integer_arithmetic() {
|
||||
let Value::Int(power) = eval("2 ^ 100", &empty()).unwrap() else {
|
||||
panic!("expected an Int");
|
||||
};
|
||||
assert_eq!(power.to_string(), "1267650600228229401496703205376");
|
||||
|
||||
let Value::Int(product) = eval(
|
||||
"123456789012345678901234567890 * 987654321098765432109876543210",
|
||||
&empty(),
|
||||
)
|
||||
.unwrap() else {
|
||||
panic!("expected an Int");
|
||||
};
|
||||
assert_eq!(
|
||||
product.to_string(),
|
||||
"121932631137021795226185032733622923332237463801111263526900"
|
||||
);
|
||||
|
||||
let Value::Int(sum) = eval("2 ^ 200 + 1", &empty()).unwrap() else {
|
||||
panic!("expected an Int");
|
||||
};
|
||||
assert_eq!(
|
||||
sum.to_string(),
|
||||
"1606938044258990275541962092341162602522202993782792835301377"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn division_stays_exact_when_it_reduces_to_an_integer() {
|
||||
// 1/3 alone is a Float approximation, but three of them are exactly 1.
|
||||
assert_int(&eval("(1 / 3) * 3", &empty()).unwrap(), 1);
|
||||
assert_int(&eval("(2 / 7) * 7", &empty()).unwrap(), 2);
|
||||
assert_int(&eval("100 / 4", &empty()).unwrap(), 25);
|
||||
|
||||
assert_float_close(&eval("10 / 4", &empty()).unwrap(), 2.5);
|
||||
assert_float_close(&eval("1 / 3", &empty()).unwrap(), 1.0 / 3.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_literals_are_exact_rationals() {
|
||||
// 1.5 + 2.5 is exactly 4, which is integral, so the result is an Int.
|
||||
assert_int(&eval("1.5 + 2.5", &empty()).unwrap(), 4);
|
||||
assert_int(&eval("0.25 * 4", &empty()).unwrap(), 1);
|
||||
assert_float_close(&eval("0.1 + 0.2", &empty()).unwrap(), 0.3);
|
||||
assert_float_close(&eval(".5 + 1", &empty()).unwrap(), 1.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pi_r_squared_area() {
|
||||
let mut bindings = empty();
|
||||
bindings.insert("r".to_string(), Value::int(2));
|
||||
let value = solve("A = \\pi r^2", &bindings, "A").unwrap();
|
||||
assert_float_close(&value, std::f64::consts::PI * 4.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perfect_and_non_perfect_square_roots() {
|
||||
assert_int(&eval("\\sqrt{16}", &empty()).unwrap(), 4);
|
||||
assert_int(&eval("\\sqrt{0}", &empty()).unwrap(), 0);
|
||||
// \sqrt{1/4} = 1/2: exact but not integral, so it is a Float value.
|
||||
assert_float_close(&eval("\\sqrt{\\frac{1}{4}}", &empty()).unwrap(), 0.5);
|
||||
// Non-perfect squares are explicitly approximate Floats.
|
||||
assert_float_close(
|
||||
&eval("\\sqrt{2}", &empty()).unwrap(),
|
||||
std::f64::consts::SQRT_2,
|
||||
);
|
||||
assert_float_close(&eval("\\sqrt{2} * \\sqrt{2}", &empty()).unwrap(), 2.0);
|
||||
let err = eval("\\sqrt{-4}", &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frac_is_exact_division() {
|
||||
assert_int(&eval("\\frac{6}{2}", &empty()).unwrap(), 3);
|
||||
assert_float_close(&eval("\\frac{6}{4}", &empty()).unwrap(), 1.5);
|
||||
assert_float_close(
|
||||
&eval("\\frac{\\frac{1}{2}}{\\frac{3}{4}}", &empty()).unwrap(),
|
||||
2.0 / 3.0,
|
||||
);
|
||||
assert_int(&eval("\\frac{1}{3} + \\frac{2}{3}", &empty()).unwrap(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_right_parentheses_and_nesting() {
|
||||
assert_int(
|
||||
&eval(
|
||||
"\\left(\\left(1 + 2\\right) * \\left(3 + 4\\right)\\right)",
|
||||
&empty(),
|
||||
)
|
||||
.unwrap(),
|
||||
21,
|
||||
);
|
||||
assert_int(&eval("\\left(1 + 2\\right) * 3", &empty()).unwrap(), 9);
|
||||
assert_int(&eval("((((1 + 2)))) * 3", &empty()).unwrap(), 9);
|
||||
assert_int(&eval("2 * {3 + 4}", &empty()).unwrap(), 14);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_identifiers() {
|
||||
let bindings = bindings(&[("半径", Value::int(3))]);
|
||||
assert_float_close(
|
||||
&solve("面积 = \\pi 半径^2", &bindings, "面积").unwrap(),
|
||||
std::f64::consts::PI * 9.0,
|
||||
);
|
||||
let substituted = substitute(&parse("半径 + 1").unwrap(), &bindings).unwrap();
|
||||
assert_eq!(substituted.source, "3 + 1");
|
||||
assert_int(&eval(&substituted.source, &empty()).unwrap(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constants_and_their_ambiguity_rules() {
|
||||
assert_float_close(&eval("\\pi", &empty()).unwrap(), std::f64::consts::PI);
|
||||
assert_float_close(&eval("pi", &empty()).unwrap(), std::f64::consts::PI);
|
||||
assert_float_close(&eval("\\e", &empty()).unwrap(), std::f64::consts::E);
|
||||
assert_float_close(&eval("e", &empty()).unwrap(), std::f64::consts::E);
|
||||
// `2e` is 2 times the constant, not scientific notation.
|
||||
assert_float_close(&eval("2e", &empty()).unwrap(), 2.0 * std::f64::consts::E);
|
||||
// `pi` and `e` are reserved: bindings do not shadow the constants.
|
||||
let bindings = bindings(&[("pi", Value::int(3)), ("e", Value::int(3))]);
|
||||
assert_float_close(&eval("pi", &bindings).unwrap(), std::f64::consts::PI);
|
||||
assert_float_close(&eval("e", &bindings).unwrap(), std::f64::consts::E);
|
||||
assert_eq!(
|
||||
solve("pi = 3", &empty(), "pi").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f_equals_m_a_solved_for_m() {
|
||||
let bindings = bindings(&[("F", Value::int(12)), ("a", Value::int(3))]);
|
||||
assert_int(&solve("F = m a", &bindings, "m").unwrap(), 4);
|
||||
// F = m a is an equation, not assignment: solving for m never rebinds F.
|
||||
assert_eq!(bindings.get("F"), Some(&Value::int(12)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f_equals_m_a_solved_for_a() {
|
||||
let bindings = bindings(&[("F", Value::int(12)), ("m", Value::int(3))]);
|
||||
assert_int(&solve("F = m a", &bindings, "a").unwrap(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f_equals_m_a_solved_for_f() {
|
||||
let bindings = bindings(&[("m", Value::int(3)), ("a", Value::int(4))]);
|
||||
assert_int(&solve("F = m a", &bindings, "F").unwrap(), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_equation_solving_both_directions() {
|
||||
// Target on the left and on the right; multiplication and division by a
|
||||
// target-free side are both inverted.
|
||||
assert_int(&solve("2 x = 4", &empty(), "x").unwrap(), 2);
|
||||
assert_int(&solve("4 = 2 x", &empty(), "x").unwrap(), 2);
|
||||
assert_int(&solve("x / 2 = 3", &empty(), "x").unwrap(), 6);
|
||||
assert_int(&solve("3 = x / 2", &empty(), "x").unwrap(), 6);
|
||||
assert_int(&solve("x + 2 = 5", &empty(), "x").unwrap(), 3);
|
||||
assert_int(&solve("2 + x = 5", &empty(), "x").unwrap(), 3);
|
||||
assert_int(&solve("2 x + 1 = 7", &empty(), "x").unwrap(), 3);
|
||||
assert_int(&solve("x = 4", &empty(), "x").unwrap(), 4);
|
||||
assert_int(&solve("x / 4 = 1 / 2", &empty(), "x").unwrap(), 2);
|
||||
assert_int(&solve("(x + 1) * 2 = 8", &empty(), "x").unwrap(), 3);
|
||||
|
||||
// Other variables are substituted first.
|
||||
let bindings = bindings(&[("y", Value::int(3))]);
|
||||
assert_int(&solve("x = 2 y + 1", &bindings, "x").unwrap(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equation_solving_requires_exactly_one_affine_occurrence() {
|
||||
assert_eq!(
|
||||
solve("x + x = 4", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("x * x = 4", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("x ^ 2 = 4", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("1 / x = 2", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("\\sqrt{x} = 2", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("2 = 4", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
solve("x * 0 = 1", &empty(), "x").unwrap_err().kind,
|
||||
MathErrorKind::Unsupported
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_errors_are_structured() {
|
||||
let err = evaluate(&parse("A = 2").unwrap(), &empty(), None).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
assert!(err.message.contains("target"));
|
||||
|
||||
let err = evaluate(&parse("1 + 2").unwrap(), &empty(), Some("x")).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
|
||||
let err = solve("A = 2", &empty(), "B").unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
assert!(err.message.contains("does not occur"));
|
||||
|
||||
let err = solve("A = 2", &empty(), "").unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_constructs_are_structured() {
|
||||
for source in [
|
||||
"\\begin{matrix} 1 & 2 \\\\ 3 & 4 \\end{matrix}",
|
||||
"\\int_0^1 x dx",
|
||||
"\\sin(x)",
|
||||
"\\cos(x) + 1",
|
||||
"\\sum_{i=1}^{n} i",
|
||||
"\\alpha + 1",
|
||||
"\\frac12",
|
||||
"1e3",
|
||||
"\\left[ 1, 2 \\right]",
|
||||
] {
|
||||
let err = parse(source).unwrap_err();
|
||||
assert!(
|
||||
matches!(err.kind, MathErrorKind::Unsupported | MathErrorKind::Parse),
|
||||
"source {source:?} produced {err:?}"
|
||||
);
|
||||
assert!(
|
||||
err.position.is_some(),
|
||||
"source {source:?} lost its position"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_latex_is_a_parse_error_with_position() {
|
||||
for source in [
|
||||
"",
|
||||
"1 +",
|
||||
"(1 + 2",
|
||||
"\\frac{1}{2",
|
||||
"\\frac{1}",
|
||||
"\\sqrt 2",
|
||||
"{1 + 2",
|
||||
"1 + 2}",
|
||||
"1 + 2)",
|
||||
"1.2.3",
|
||||
"1 2",
|
||||
"a = b = c",
|
||||
] {
|
||||
let err = parse(source).unwrap_err();
|
||||
assert_eq!(
|
||||
err.kind,
|
||||
MathErrorKind::Parse,
|
||||
"source {source:?} produced {err:?}"
|
||||
);
|
||||
assert!(err.position.is_some(), "source {source:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undefined_variables_are_reported() {
|
||||
let err = eval("1 + x", &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::UndefinedVariable);
|
||||
assert!(err.message.contains('x'));
|
||||
|
||||
let err = solve("A = x + 1", &empty(), "A").unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::UndefinedVariable);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_numeric_bindings_are_type_errors() {
|
||||
for value in [
|
||||
Value::Bool(true),
|
||||
Value::text("two"),
|
||||
Value::List(vec![Value::int(2)]),
|
||||
Value::None,
|
||||
Value::Float(f64::NAN),
|
||||
Value::Float(f64::INFINITY),
|
||||
] {
|
||||
let err = eval("x + 1", &bindings(&[("x", value)])).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type);
|
||||
}
|
||||
// Unused bindings of other types are irrelevant.
|
||||
assert_int(
|
||||
&eval("1 + 1", &bindings(&[("x", Value::text("unused"))])).unwrap(),
|
||||
2,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn float_bindings_are_exact_numeric_values() {
|
||||
let bindings = bindings(&[("x", Value::Float(2.5))]);
|
||||
assert_float_close(&eval("x", &bindings).unwrap(), 2.5);
|
||||
// 2.5 + 0.5 is exactly 3, so the exact-result rule yields an Int.
|
||||
assert_int(&eval("x + 0.5", &bindings).unwrap(), 3);
|
||||
assert_float_close(&eval("x / 2", &bindings).unwrap(), 1.25);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn division_by_zero_is_a_structured_error() {
|
||||
for source in ["1 / 0", "\\frac{1}{0}", "1 / (2 - 2)"] {
|
||||
let err = eval(source, &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type, "source {source:?}");
|
||||
assert!(
|
||||
err.message.contains("division by zero"),
|
||||
"source {source:?} produced {err:?}"
|
||||
);
|
||||
}
|
||||
let err = solve("x / 0 = 1", &empty(), "x").unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exponent_must_be_an_integer() {
|
||||
for source in ["2 ^ 0.5", "2 ^ \\frac{1}{2}", "2 ^ \\pi"] {
|
||||
let err = eval(source, &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type, "source {source:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_integer_exponents_are_exact_reciprocals() {
|
||||
assert_float_close(&eval("2 ^ -3", &empty()).unwrap(), 0.125);
|
||||
assert_int(&eval("(1 / 2) ^ -2", &empty()).unwrap(), 4);
|
||||
let err = eval("0 ^ -1", &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_exponents_are_bounded_not_hung() {
|
||||
let err = eval(&format!("2 ^ {}", MAX_EXPONENT + 1), &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
assert!(err.message.contains("exponent"));
|
||||
|
||||
let err = eval("2 ^ 1000000", &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
|
||||
// Each factor is under the exponent bound, but the product size is not.
|
||||
let err = eval("(2 ^ 10000) ^ 10000", &empty()).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Unsupported);
|
||||
|
||||
// At the documented exponent bound the result is still computed exactly.
|
||||
let Value::Int(bounded) = eval("2 ^ 10000", &empty()).unwrap() else {
|
||||
panic!("expected an Int");
|
||||
};
|
||||
assert_eq!(bounded.bits(), 10001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substitute_replaces_bindings_deterministically() {
|
||||
let formula = parse("A = \\pi r^2").unwrap();
|
||||
let bindings = bindings(&[("r", Value::int(2))]);
|
||||
let substituted = substitute(&formula, &bindings).unwrap();
|
||||
|
||||
// The rendering is canonical, re-parseable and deterministic.
|
||||
assert_eq!(substituted.source, "A = \\pi * 2 ^ 2");
|
||||
assert_eq!(substituted.render(), substituted.source);
|
||||
assert_eq!(parse(&substituted.source).unwrap().kind, substituted.kind);
|
||||
|
||||
let direct = evaluate(&formula, &bindings, Some("A")).unwrap();
|
||||
let via_substitution = evaluate(&substituted, &empty(), Some("A")).unwrap();
|
||||
assert_eq!(direct, via_substitution);
|
||||
|
||||
let second = substitute(&formula, &bindings).unwrap();
|
||||
assert_eq!(substituted.source, second.source);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substitute_leaves_unbound_variables_symbolic() {
|
||||
let formula = parse("x + y * 2").unwrap();
|
||||
let substituted = substitute(&formula, &bindings(&[("x", Value::int(3))])).unwrap();
|
||||
assert_eq!(substituted.source, "3 + y * 2");
|
||||
assert!(substituted.variables().contains("y"));
|
||||
assert!(!substituted.variables().contains("x"));
|
||||
|
||||
let value = evaluate(&substituted, &bindings(&[("y", Value::int(4))]), None).unwrap();
|
||||
assert_int(&value, 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substitute_rejects_unsupported_binding_types() {
|
||||
let formula = parse("x + 1").unwrap();
|
||||
let err = substitute(&formula, &bindings(&[("x", Value::text("two"))])).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scientific_notation_and_adjacent_numbers_are_rejected() {
|
||||
assert_eq!(parse("1e3").unwrap_err().kind, MathErrorKind::Parse);
|
||||
assert_eq!(parse("1E3").unwrap_err().kind, MathErrorKind::Parse);
|
||||
assert_eq!(parse("1e-3").unwrap_err().kind, MathErrorKind::Parse);
|
||||
assert_eq!(parse("1 2").unwrap_err().kind, MathErrorKind::Parse);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_display_includes_kind_and_position() {
|
||||
let err = parse("\\frac{1}{2").unwrap_err();
|
||||
let rendered = err.to_string();
|
||||
assert!(rendered.starts_with("parse at byte "), "{rendered}");
|
||||
assert!(rendered.contains("expected `}`"), "{rendered}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_inputs_never_panic() {
|
||||
// A small corpus of adversarial inputs: all must produce a structured error
|
||||
// (or a value), never a panic or an unbounded computation.
|
||||
let sources = [
|
||||
"\\",
|
||||
"\\\\",
|
||||
"\\\\\\\\",
|
||||
"\\frac",
|
||||
"\\frac{}{}",
|
||||
"\\sqrt{}",
|
||||
"{}",
|
||||
"((((",
|
||||
"))))",
|
||||
"^^^^",
|
||||
"----------------x",
|
||||
"1 + + + +",
|
||||
"1 // 2",
|
||||
"\\left(1",
|
||||
"\\right)",
|
||||
"\\left[1\\right]",
|
||||
"1..2",
|
||||
"..",
|
||||
"∞",
|
||||
"1 × 2",
|
||||
"1 · 2",
|
||||
"2 ^ 2 ^ 2 ^ 2",
|
||||
"\\pi 2 3",
|
||||
"x = ",
|
||||
"= x",
|
||||
"x = = y",
|
||||
"\\frac{1}{2} \\frac{3}{4}",
|
||||
];
|
||||
for source in sources {
|
||||
match parse(source) {
|
||||
Ok(formula) => {
|
||||
// If it parses, evaluation must be structured as well.
|
||||
let _ = evaluate(&formula, &empty(), None);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(
|
||||
!err.message.is_empty(),
|
||||
"structured error must have a message for {source:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dot_and_times_are_multiplication_operators() {
|
||||
assert_int(&eval("2 \\cdot 3", &empty()).unwrap(), 6);
|
||||
assert_int(&eval("2 \\times 3", &empty()).unwrap(), 6);
|
||||
assert_int(&eval("2 \\cdot 3 \\times 4", &empty()).unwrap(), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solve_target_on_the_left_of_a_product() {
|
||||
let bindings = bindings(&[("F", Value::int(20)), ("a", Value::int(5))]);
|
||||
assert_int(&solve("m a = F", &bindings, "m").unwrap(), 4);
|
||||
assert_int(&solve("(m + 1) a = F", &bindings, "m").unwrap(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substitution_with_float_and_negative_bindings_reparses() {
|
||||
// A Float binding is substituted as its exact numeric value, and the
|
||||
// canonical rendering must evaluate to the same value as the direct form.
|
||||
let formula = parse("x * 2").unwrap();
|
||||
let x_is_float = bindings(&[("x", Value::Float(2.5))]);
|
||||
let substituted = substitute(&formula, &x_is_float).unwrap();
|
||||
let direct = evaluate(&formula, &x_is_float, None).unwrap();
|
||||
let re_evaluated = evaluate(&parse(&substituted.source).unwrap(), &empty(), None).unwrap();
|
||||
assert_eq!(direct, re_evaluated);
|
||||
assert_int(&direct, 5);
|
||||
|
||||
// Negative substituted literals keep a parseable rendering.
|
||||
let formula = parse("x + 1").unwrap();
|
||||
let x_is_negative = bindings(&[("x", Value::int(-3))]);
|
||||
let substituted = substitute(&formula, &x_is_negative).unwrap();
|
||||
let value = evaluate(&parse(&substituted.source).unwrap(), &empty(), None).unwrap();
|
||||
assert_int(&value, -2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_exact_results_are_returned_as_integers() {
|
||||
let Value::Int(value) = eval("2 ^ 10000", &empty()).unwrap() else {
|
||||
panic!("expected an Int");
|
||||
};
|
||||
assert_eq!(value.bits(), 10001);
|
||||
// A non-perfect square of the same order remains representable as Float.
|
||||
assert!(matches!(
|
||||
eval("\\sqrt{2}", &empty()).unwrap(),
|
||||
Value::Float(_)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_operator_chains_within_the_node_budget_still_evaluate() {
|
||||
// Each `+`/`*` needs two leaf nodes plus one operator node, so 200
|
||||
// operators stay inside the documented 512-node budget.
|
||||
let source = format!("{}1", "1 + ".repeat(200));
|
||||
assert_int(&eval(&source, &empty()).unwrap(), 201);
|
||||
|
||||
let source = format!("{}1", "2 * ".repeat(200));
|
||||
assert_eq!(
|
||||
eval(&source, &empty()).unwrap(),
|
||||
Value::Int(BigInt::from(2).pow(200))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_formulas_are_rejected_with_a_structured_error() {
|
||||
// Well over the documented node budget, but not deep enough to be caught by
|
||||
// the nesting guard: this must be a parse error, not a stack overflow.
|
||||
let source = format!("{}1", "1 + ".repeat(200_000));
|
||||
let err = parse(&source).unwrap_err();
|
||||
assert_eq!(err.kind, MathErrorKind::Parse);
|
||||
}
|
||||
@@ -121,44 +121,6 @@ pub(crate) fn dispatch(
|
||||
};
|
||||
resolve_ref(interpreter, &module, &target, span)
|
||||
}
|
||||
"math.parse" => {
|
||||
let formula = take(&bound, "formula", builtin, span)?;
|
||||
let text = formula_text(formula, span)?;
|
||||
interpreter.host_invoke(HostOp::MathParse, vec![Value::Text(text)], span, &module)
|
||||
}
|
||||
"math.eval" => {
|
||||
let formula = take(&bound, "formula", builtin, span)?;
|
||||
let bindings = take(&bound, "bindings", builtin, span)?;
|
||||
let mut args = vec![formula, bindings];
|
||||
if let Some(target) = bound.get("target") {
|
||||
args.push(target.clone());
|
||||
}
|
||||
interpreter.host_invoke(HostOp::MathEval, args, span, &module)
|
||||
}
|
||||
"math.substitute" => {
|
||||
let formula = take(&bound, "formula", builtin, span)?;
|
||||
let bindings = take(&bound, "bindings", builtin, span)?;
|
||||
interpreter.host_invoke(
|
||||
HostOp::MathSubstitute,
|
||||
vec![formula, bindings],
|
||||
span,
|
||||
&module,
|
||||
)
|
||||
}
|
||||
"math.simplify" => {
|
||||
let formula = take(&bound, "formula", builtin, span)?;
|
||||
interpreter.host_invoke(HostOp::MathSimplify, vec![formula], span, &module)
|
||||
}
|
||||
"math.differentiate" => {
|
||||
let formula = take(&bound, "formula", builtin, span)?;
|
||||
let variable = take(&bound, "变量", builtin, span)?;
|
||||
interpreter.host_invoke(
|
||||
HostOp::MathDifferentiate,
|
||||
vec![formula, variable],
|
||||
span,
|
||||
&module,
|
||||
)
|
||||
}
|
||||
"foreign.run" => {
|
||||
let code = take(&bound, "ref", builtin, span)?;
|
||||
let mut args = vec![code];
|
||||
@@ -738,20 +700,6 @@ fn table_index(
|
||||
Ok(position)
|
||||
}
|
||||
|
||||
fn formula_text(value: Value, span: Option<Span>) -> Result<String, RuntimeError> {
|
||||
match value {
|
||||
Value::Formula(formula) => Ok(formula.latex),
|
||||
Value::Text(text) => Ok(text),
|
||||
other => Err(RuntimeError::type_error(
|
||||
span,
|
||||
format!(
|
||||
"formula argument must be Formula or Text, got {}",
|
||||
other.type_name()
|
||||
),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn table(header: Value, rows: Value, span: Option<Span>) -> Result<Value, RuntimeError> {
|
||||
let Value::List(header_values) = header else {
|
||||
return Err(RuntimeError::type_error(
|
||||
|
||||
Reference in new issue
Block a user