feat(lang): implement RFC-LANG-0002 unified semantics (breaking)
One Markdown-to-program mapping, no version selector or legacy mapper. Frontend/runtime: - resolve @exec/@knowledge heading declarations with nearest-ancestor inheritance; only an Exec H2 is callable; add UnitKind::Knowledge and sem.* diagnostics (mode marker, unit name, exec-without-function, payload nesting/empty, syntax, control stack, params position, version directive, inferred-signature lint) - extract Strong/Emph/inline Code as one-statement carriers, refuse nested carriers, and honour heading segment boundaries so an if/while cannot be closed across a heading - lower if/else/while/end to the existing Branch/Loop/Return IR with a compile-time control stack; keep payload-relative source spans through expression parsing and rewrite them onto the file - accept params(...) only as the first direct Exec H2 payload; keep the existing free-variable inference and named-only rule for inferred signatures; fix duplicate-mode/else/end and Knowledge-body inertness - reject marqdo_semantics/syntax front matter directives Data and tests: - migrate stdlib/**/*.mq.md, regenerate stdlib.json digest - migrate examples and the full semantics/negative/runtime corpus to the only current syntax; expected values/runtime codes preserved - migrate Rust-embedded .mq.md fixtures, init template, CLI help and security-lane fixtures Verified: cargo fmt --check, cargo clippy -D warnings and cargo test --workspace --locked (green).
This commit is contained in:
1 parent
49835424ee
commit
e029853e32
375 files changed
+6591
-4790
No files matched your search
@@ -49,21 +49,21 @@ fn write(dir: &Path, name: &str, source: &str) -> PathBuf {
|
||||
path
|
||||
}
|
||||
|
||||
const PROGRAM: &str = r#"# 数学
|
||||
const PROGRAM: &str = r#"# @knowledge 数学
|
||||
|
||||
## 加一
|
||||
## @exec 加一
|
||||
|
||||
`(n)`
|
||||
`params(n)`
|
||||
|
||||
*n + 1*
|
||||
`return n + 1`
|
||||
|
||||
## main
|
||||
## @exec main
|
||||
|
||||
**结果 = 加一(41)**
|
||||
`结果 = 加一(41)`
|
||||
|
||||
> 打印("secret-value")
|
||||
`打印("secret-value")`
|
||||
|
||||
*结果*
|
||||
`return 结果`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -143,7 +143,7 @@ fn analysis_is_deterministic_and_has_stable_semantic_nodes() {
|
||||
fn analysis_v2_identity_is_location_independent() {
|
||||
let first = TempDir::new("analysis-identity-a");
|
||||
let second = TempDir::new("analysis-identity-b");
|
||||
let source = "## main\n\n**x = 1**\n\n*x*\n";
|
||||
let source = "## @exec main\n\n`x = 1`\n\n`return x`\n";
|
||||
for dir in [&first, &second] {
|
||||
write(
|
||||
dir.path(),
|
||||
@@ -184,7 +184,8 @@ fn analysis_v2_identity_is_location_independent() {
|
||||
#[test]
|
||||
fn analysis_v2_projects_memory_place_targets_additively() {
|
||||
let dir = TempDir::new("analysis-place");
|
||||
let source = "## main\n\n**内存 = memory.new(1, 2)**\n\n**`内存[0] = 7`**\n\n*内存[0]*\n";
|
||||
let source =
|
||||
"## @exec main\n\n`内存 = memory.new(1, 2)`\n\n`内存[0] = 7`\n\n`return 内存[0]`\n";
|
||||
write(dir.path(), "index.mq.md", source);
|
||||
let target = dir.path().join("analysis.json");
|
||||
let output = cli(
|
||||
@@ -212,9 +213,9 @@ fn analysis_v2_projects_memory_place_targets_additively() {
|
||||
#[test]
|
||||
fn analysis_v2_projects_addressable_formula_and_code_resources() {
|
||||
let dir = TempDir::new("analysis-resources");
|
||||
let source = r#"## calc
|
||||
let source = r#"## @exec calc
|
||||
|
||||
`(x)`
|
||||
`params(x)`
|
||||
|
||||
$$A = x + 1$$
|
||||
|
||||
@@ -222,7 +223,7 @@ $$A = x + 1$$
|
||||
print(1)
|
||||
```
|
||||
|
||||
*x*
|
||||
`return x`
|
||||
"#;
|
||||
write(
|
||||
dir.path(),
|
||||
@@ -275,7 +276,7 @@ fn trace_v2_budget_truncates_long_loops_without_failing_the_run() {
|
||||
write(
|
||||
dir.path(),
|
||||
"index.mq.md",
|
||||
"## main\n\n**i = 0**\n\n- `i < 500`\n\n **i = i + 1**\n\n*i*\n",
|
||||
"## @exec main\n\n`i = 0`\n\n`while i < 500`\n\n`i = i + 1`\n\n`end`\n\n`return i`\n",
|
||||
);
|
||||
let trace_path = dir.path().join("trace.json");
|
||||
let output = cli(
|
||||
@@ -318,7 +319,7 @@ fn trace_v2_budget_truncates_long_loops_without_failing_the_run() {
|
||||
#[test]
|
||||
fn failed_check_removes_stale_analysis() {
|
||||
let dir = TempDir::new("analysis-error");
|
||||
write(dir.path(), "bad.mq.md", "## main\n\n1. 没有条件\n");
|
||||
write(dir.path(), "bad.mq.md", "## @exec main\n\n`return x +`\n");
|
||||
let target = dir.path().join("analysis.json");
|
||||
std::fs::write(&target, "stale").unwrap();
|
||||
let output = cli(
|
||||
@@ -376,7 +377,7 @@ fn trace_records_calls_effects_and_status_without_secrets() {
|
||||
}
|
||||
|
||||
// Error runs still write a terminal trace.
|
||||
write(dir.path(), "bad.mq.md", "## main\n\n**x = 1 // 0**\n");
|
||||
write(dir.path(), "bad.mq.md", "## @exec main\n\n`x = 1 // 0`\n");
|
||||
let error_trace = dir.path().join("error-trace.json");
|
||||
let output = cli(
|
||||
dir.path(),
|
||||
|
||||
@@ -50,39 +50,41 @@ import:
|
||||
编码: "std:text"
|
||||
---
|
||||
|
||||
## main
|
||||
## @exec main
|
||||
|
||||
**流 = resource.open("tcp", "{address}", ["read", "write"])**
|
||||
`流 = resource.open("tcp", "{address}", ["read", "write"])`
|
||||
|
||||
**请求 = http.构建请求("GET", "/", [["Host", "example.test"]], "")**
|
||||
`请求 = http.构建请求("GET", "/", [["Host", "example.test"]], "")`
|
||||
|
||||
**请求内存 = memory.new(1, 1)**
|
||||
`请求内存 = memory.new(1, 1)`
|
||||
|
||||
**请求长度 = 编码.utf8编码(请求, 请求内存, 0)**
|
||||
`请求长度 = 编码.utf8编码(请求, 请求内存, 0)`
|
||||
|
||||
**写入 = resource.write(流, 请求内存, 0, 请求长度)**
|
||||
`写入 = resource.write(流, 请求内存, 0, 请求长度)`
|
||||
|
||||
**接收 = memory.new(1, 1)**
|
||||
`接收 = memory.new(1, 1)`
|
||||
|
||||
**读出 = resource.read(流, 接收, 0, 4096)**
|
||||
`读出 = resource.read(流, 接收, 0, 4096)`
|
||||
|
||||
**数量 = 读出.value.count**
|
||||
`数量 = 读出.value.count`
|
||||
|
||||
**响应文本 = ""**
|
||||
`响应文本 = ""`
|
||||
|
||||
**下标 = 0**
|
||||
`下标 = 0`
|
||||
|
||||
- `下标 < 数量`
|
||||
`while 下标 < 数量`
|
||||
|
||||
**响应文本 = 响应文本 + text.from_codepoint(接收[下标])**
|
||||
`响应文本 = 响应文本 + text.from_codepoint(接收[下标])`
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`下标 = 下标 + 1`
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
`end`
|
||||
|
||||
**响应 = http.解析响应(响应文本)**
|
||||
`关闭 = resource.close(流)`
|
||||
|
||||
*[写入.ok, 读出.ok, 响应.状态, 响应.头["Content-Type"], 响应.体, 关闭.ok]*
|
||||
`响应 = http.解析响应(响应文本)`
|
||||
|
||||
`return [写入.ok, 读出.ok, 响应.状态, 响应.头["Content-Type"], 响应.体, 关闭.ok]`
|
||||
"#
|
||||
);
|
||||
std::fs::write(dir.path().join("index.mq.md"), program).unwrap();
|
||||
|
||||
@@ -170,9 +170,12 @@ fn init_creates_a_runnable_project() {
|
||||
#[test]
|
||||
fn check_exit_codes_and_diagnostics() {
|
||||
let dir = TempDir::new("check");
|
||||
dir.write("good.mq.md", "## main\n\n*1 + 1*\n");
|
||||
dir.write("bad.mq.md", "## f\n\n**a *b* c**\n");
|
||||
dir.write("warning.mq.md", "**x = 1**\n\n## main\n\n*1*\n");
|
||||
dir.write("good.mq.md", "## @exec main\n\n`return 1 + 1`\n");
|
||||
dir.write("bad.mq.md", "## @exec f\n\n**a *b* c**\n");
|
||||
dir.write(
|
||||
"warning.mq.md",
|
||||
"**x = 1**\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
|
||||
let good = cli(dir.path(), &["check", "good.mq.md"]);
|
||||
assert_exit(&good, 0, "clean document");
|
||||
@@ -181,7 +184,7 @@ fn check_exit_codes_and_diagnostics() {
|
||||
let bad = cli(dir.path(), &["check", "bad.mq.md"]);
|
||||
assert_exit(&bad, 1, "invalid executable shape is a hard error");
|
||||
let bad_stderr = stderr_text(&bad);
|
||||
assert!(bad_stderr.contains("error[markdown.invalid_construct]"));
|
||||
assert!(bad_stderr.contains("error[sem.payload_nested]"));
|
||||
assert!(bad_stderr.contains("check failed"));
|
||||
assert!(stdout_text(&bad).is_empty());
|
||||
|
||||
@@ -192,20 +195,37 @@ fn check_exit_codes_and_diagnostics() {
|
||||
assert_eq!(value["success"], false);
|
||||
assert_eq!(value["errors"], 1);
|
||||
let diagnostics = value["diagnostics"].as_array().expect("diagnostics array");
|
||||
assert_eq!(diagnostics.len(), 1);
|
||||
assert_eq!(diagnostics[0]["code"], "markdown.invalid_construct");
|
||||
assert_eq!(diagnostics[0]["severity"], "error");
|
||||
assert_eq!(diagnostics[0]["line"], 3);
|
||||
let nested = diagnostics
|
||||
.iter()
|
||||
.find(|diagnostic| diagnostic["code"] == "sem.payload_nested")
|
||||
.expect("sem.payload_nested diagnostic");
|
||||
assert_eq!(nested["severity"], "error");
|
||||
assert_eq!(nested["line"], 3);
|
||||
assert!(
|
||||
diagnostics
|
||||
.iter()
|
||||
.all(|diagnostic| diagnostic["severity"] == "error"
|
||||
|| diagnostic["code"] == "sem.inferred_signature"),
|
||||
"the fixture may carry the inferred-signature warning but no other diagnostic"
|
||||
);
|
||||
assert!(stderr_text(&bad_json).is_empty());
|
||||
|
||||
let warning = cli(dir.path(), &["check", "warning.mq.md"]);
|
||||
assert_exit(&warning, 0, "a warning does not fail check");
|
||||
assert!(stdout_text(&warning).contains("checked 1 module(s): ok"));
|
||||
assert!(stderr_text(&warning).contains("warning[markdown.inert_executable]"));
|
||||
let warning_json = cli(dir.path(), &["check", "warning.mq.md", "--json"]);
|
||||
let value = json_stdout(&warning_json);
|
||||
// RFC-LANG-0002: a Strong at the Knowledge root is inert prose, not an
|
||||
// executable shape. There is no `markdown.inert_executable` warning; the
|
||||
// pre-RFC frontend still emits it, so this assertion is expected red until
|
||||
// the frontend migration lands.
|
||||
let inert = cli(dir.path(), &["check", "warning.mq.md"]);
|
||||
assert_exit(&inert, 0, "inert Knowledge prose does not fail check");
|
||||
assert!(stdout_text(&inert).contains("checked 1 module(s): ok"));
|
||||
assert!(!stderr_text(&inert).contains("warning["));
|
||||
let inert_json = cli(dir.path(), &["check", "warning.mq.md", "--json"]);
|
||||
let value = json_stdout(&inert_json);
|
||||
assert_eq!(value["success"], true);
|
||||
assert_eq!(value["diagnostics"][0]["severity"], "warning");
|
||||
assert_eq!(
|
||||
value["diagnostics"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"inert Knowledge prose emits no diagnostic under RFC-LANG-0002"
|
||||
);
|
||||
|
||||
let missing = cli(dir.path(), &["check", "missing.mq.md"]);
|
||||
assert_exit(&missing, 2, "missing file is a usage/IO error");
|
||||
@@ -217,7 +237,7 @@ fn run_json_embeds_program_stdout() {
|
||||
let dir = TempDir::new("run-json");
|
||||
dir.write(
|
||||
"print.mq.md",
|
||||
"## main\n\n> 打印(\"你好\")\n\n> 打印(42)\n\n*None*\n",
|
||||
"## @exec main\n\n`打印(\"你好\")`\n\n`打印(42)`\n\n`return None`\n",
|
||||
);
|
||||
let output = cli(dir.path(), &["run", "print.mq.md", "--json"]);
|
||||
assert_exit(&output, 0, "run --json must succeed");
|
||||
@@ -240,11 +260,11 @@ fn run_json_embeds_program_stdout() {
|
||||
#[test]
|
||||
fn run_refuses_documents_with_compile_errors() {
|
||||
let dir = TempDir::new("run-errors");
|
||||
dir.write("bad.mq.md", "## f\n\n**a *b* c**\n");
|
||||
dir.write("bad.mq.md", "## @exec f\n\n**a *b* c**\n");
|
||||
let output = cli(dir.path(), &["run", "bad.mq.md"]);
|
||||
assert_exit(&output, 1, "compile errors must prevent execution");
|
||||
let stderr = stderr_text(&output);
|
||||
assert!(stderr.contains("markdown.invalid_construct"));
|
||||
assert!(stderr.contains("sem.payload_nested"));
|
||||
assert!(stderr.contains("refusing to run"));
|
||||
assert!(stdout_text(&output).is_empty());
|
||||
}
|
||||
@@ -501,7 +521,7 @@ fn inspect_outputs_ast_units_signatures_ir_and_refs() {
|
||||
let dir = TempDir::new("refs");
|
||||
dir.write(
|
||||
"broken.mq.md",
|
||||
"## main\n\n见[断链](#不存在的标题)。\n\n*1*\n",
|
||||
"## @exec main\n\n见[断链](#不存在的标题)。\n\n`return 1`\n",
|
||||
);
|
||||
let broken = cli(dir.path(), &["inspect", "broken.mq.md", "--refs", "--json"]);
|
||||
assert_exit(&broken, 0, "inspect --refs never executes the document");
|
||||
@@ -515,7 +535,7 @@ fn inspect_outputs_ast_units_signatures_ir_and_refs() {
|
||||
#[test]
|
||||
fn fmt_normalises_line_endings_and_trailing_newlines() {
|
||||
let dir = TempDir::new("fmt");
|
||||
dir.write("doc.mq.md", "## main\r\n\r\n*1*\r\n\r\n\r\n");
|
||||
dir.write("doc.mq.md", "## @exec main\r\n\r\n`return 1`\r\n\r\n\r\n");
|
||||
let before = cli(dir.path(), &["fmt", "--check", "doc.mq.md"]);
|
||||
assert_exit(&before, 1, "unformatted document is detected");
|
||||
assert!(stderr_text(&before).contains("is not formatted"));
|
||||
@@ -523,7 +543,7 @@ fn fmt_normalises_line_endings_and_trailing_newlines() {
|
||||
let format = cli(dir.path(), &["fmt", "doc.mq.md"]);
|
||||
assert_exit(&format, 0, "fmt must rewrite the document");
|
||||
let formatted = dir.read("doc.mq.md");
|
||||
assert_eq!(formatted, "## main\n\n*1*\n");
|
||||
assert_eq!(formatted, "## @exec main\n\n`return 1`\n");
|
||||
assert!(!formatted.contains('\r'));
|
||||
|
||||
let after = cli(dir.path(), &["fmt", "--check", "doc.mq.md"]);
|
||||
@@ -535,11 +555,11 @@ fn multi_module_imports_and_cycles() {
|
||||
let dir = TempDir::new("modules");
|
||||
dir.write(
|
||||
"geometry.mq.md",
|
||||
"## 圆面积\n\n`(r: Number)`\n\n*math.eval(ref(\"#公式\"), {\"r\": r}, target=\"A\")*\n\n### 公式\n\n$$\nA = \\pi r^2\n$$\n",
|
||||
"## @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",
|
||||
);
|
||||
dir.write(
|
||||
"index.mq.md",
|
||||
"---\nimport:\n geometry: \"./geometry.mq.md\"\n---\n\n## main\n\n**面积 = geometry.圆面积(r=2)**\n\n> 打印(面积)\n",
|
||||
"---\nimport:\n geometry: \"./geometry.mq.md\"\n---\n\n## @exec main\n\n`面积 = geometry.圆面积(r=2)`\n\n`打印(面积)`\n",
|
||||
);
|
||||
let run = cli(dir.path(), &["run", "index.mq.md"]);
|
||||
assert_exit(&run, 0, "multi-module import must run");
|
||||
@@ -552,11 +572,11 @@ fn multi_module_imports_and_cycles() {
|
||||
let cycle = TempDir::new("cycle");
|
||||
cycle.write(
|
||||
"a.mq.md",
|
||||
"---\nimport:\n b: \"./b.mq.md\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n b: \"./b.mq.md\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
cycle.write(
|
||||
"b.mq.md",
|
||||
"---\nimport:\n a: \"./a.mq.md\"\n---\n\n## f\n\n*1*\n",
|
||||
"---\nimport:\n a: \"./a.mq.md\"\n---\n\n## @exec f\n\n`return 1`\n",
|
||||
);
|
||||
let output = cli(cycle.path(), &["check", "a.mq.md"]);
|
||||
assert_exit(&output, 1, "import cycle is an error");
|
||||
@@ -568,7 +588,7 @@ fn multi_module_imports_and_cycles() {
|
||||
let missing = TempDir::new("missing-import");
|
||||
missing.write(
|
||||
"index.mq.md",
|
||||
"---\nimport:\n gone: \"./gone.mq.md\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n gone: \"./gone.mq.md\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
let output = cli(missing.path(), &["check", "index.mq.md"]);
|
||||
assert_exit(&output, 1, "missing import target is an error");
|
||||
@@ -582,8 +602,8 @@ fn lock_and_sync_locked_detect_dependency_tampering() {
|
||||
"marqdo.yaml",
|
||||
"project:\n name: locktest\n version: 0.1.0\ndependencies:\n dep:\n path: \"./dep\"\n",
|
||||
);
|
||||
dir.write("index.mq.md", "## main\n\n*1*\n");
|
||||
dir.write("dep/index.mq.md", "## main\n\n*1*\n");
|
||||
dir.write("index.mq.md", "## @exec main\n\n`return 1`\n");
|
||||
dir.write("dep/index.mq.md", "## @exec main\n\n`return 1`\n");
|
||||
|
||||
let lock = cli(dir.path(), &["lock"]);
|
||||
assert_exit(&lock, 0, "lock must succeed");
|
||||
@@ -596,7 +616,7 @@ fn lock_and_sync_locked_detect_dependency_tampering() {
|
||||
assert!(stdout_text(&sync).contains("synchronized 1 package(s)"));
|
||||
|
||||
// Tamper with the dependency contents: `sync --locked` must fail.
|
||||
dir.write("dep/index.mq.md", "## main\n\n*2*\n");
|
||||
dir.write("dep/index.mq.md", "## @exec main\n\n`return 2`\n");
|
||||
let tampered = cli(dir.path(), &["sync", "--locked"]);
|
||||
assert_exit(&tampered, 1, "tampered dependency must fail sync --locked");
|
||||
assert!(stderr_text(&tampered).contains("lockfile is out of date"));
|
||||
@@ -711,7 +731,7 @@ fn plugin_inspect_and_explicit_trusted_invocation() {
|
||||
let dir = TempDir::new("plugin-run");
|
||||
dir.write(
|
||||
"doc.mq.md",
|
||||
"## main\n\n**结果 = solver.add(a=20, b=22)**\n\n> 打印(结果)\n",
|
||||
"## @exec main\n\n`结果 = solver.add(a=20, b=22)`\n\n`打印(结果)`\n",
|
||||
);
|
||||
let plugin_path = plugin_dir.to_str().unwrap();
|
||||
let doc = dir.path().join("doc.mq.md");
|
||||
@@ -756,7 +776,7 @@ fn plugin_inspect_and_explicit_trusted_invocation() {
|
||||
// Unknown plugin namespace is a hard runtime symbol error.
|
||||
dir.write(
|
||||
"unknown.mq.md",
|
||||
"## main\n\n**结果 = other.add(a=1, b=1)**\n",
|
||||
"## @exec main\n\n`结果 = other.add(a=1, b=1)`\n",
|
||||
);
|
||||
let unknown = cli(
|
||||
dir.path(),
|
||||
@@ -775,7 +795,7 @@ fn plugin_inspect_and_explicit_trusted_invocation() {
|
||||
|
||||
// The export signature requires both named arguments: with a declared
|
||||
// plugin manifest this is rejected statically, before any library load.
|
||||
dir.write("args.mq.md", "## main\n\n**结果 = solver.add(a=1)**\n");
|
||||
dir.write("args.mq.md", "## @exec main\n\n`结果 = solver.add(a=1)`\n");
|
||||
let bad_args = cli(
|
||||
dir.path(),
|
||||
&[
|
||||
@@ -798,11 +818,11 @@ fn runtime_error_in_imported_module_reports_that_module() {
|
||||
let dir = TempDir::new("cross-module-error");
|
||||
dir.write(
|
||||
"index.mq.md",
|
||||
"---\nimport:\n physics: \"./physics.mq.md\"\n---\n\n## main\n\n> 打印(physics.boom())\n",
|
||||
"---\nimport:\n physics: \"./physics.mq.md\"\n---\n\n## @exec main\n\n`打印(physics.boom())`\n",
|
||||
);
|
||||
dir.write(
|
||||
"physics.mq.md",
|
||||
"## boom\n\n说明文字。\n\n**x = 1 / 0**\n\n*x*\n",
|
||||
"## @exec boom\n\n说明文字。\n\n`x = 1 / 0`\n\n`return x`\n",
|
||||
);
|
||||
|
||||
let output = cli(dir.path(), &["run", "index.mq.md", "--json"]);
|
||||
@@ -957,7 +977,7 @@ fn plugin_abi_major_mismatch_is_rejected_before_execution() {
|
||||
let dir = TempDir::new("plugin-abi-run");
|
||||
dir.write(
|
||||
"doc.mq.md",
|
||||
"## main\n\n**结果 = solver.add(a=20, b=22)**\n\n> 打印(结果)\n",
|
||||
"## @exec main\n\n`结果 = solver.add(a=20, b=22)`\n\n`打印(结果)`\n",
|
||||
);
|
||||
let output = cli(
|
||||
dir.path(),
|
||||
@@ -1019,7 +1039,7 @@ fn check_never_spawns_a_foreign_kernel() {
|
||||
);
|
||||
dir.write(
|
||||
"doc.mq.md",
|
||||
"## 代码\n\n```mocklang\nignored\n```\n\n## main\n\n**值 = foreign.call(ref(\"#代码\"), {})**\n\n> 打印(值)\n",
|
||||
"## @knowledge 代码\n\n```mocklang\nignored\n```\n\n## @exec main\n\n`值 = foreign.call(ref(\"#代码\"), {})`\n\n`打印(值)`\n",
|
||||
);
|
||||
|
||||
let checked = cli(dir.path(), &["check", "doc.mq.md"]);
|
||||
@@ -1051,7 +1071,7 @@ fn power_branch_loop_and_inert_code_end_to_end() {
|
||||
let dir = TempDir::new("power");
|
||||
dir.write(
|
||||
"branch.mq.md",
|
||||
"## main\n\n**n = 2**\n\n1. `n ** 2 == 4`,正确分支。\n\n > print(\"乘方正确\")\n\n2. `n ** 2 != 4`,另一分支。\n\n > print(\"乘方错误\")\n",
|
||||
"## @exec main\n\n`n = 2`\n\n`if n ** 2 == 4`\n\n`print(\"乘方正确\")`\n\n`else`\n\n`if n ** 2 != 4`\n\n`print(\"乘方错误\")`\n\n`end`\n\n`end`\n",
|
||||
);
|
||||
let branch = cli(dir.path(), &["run", "branch.mq.md", "--json"]);
|
||||
assert_exit(&branch, 0, "power branch run");
|
||||
@@ -1062,7 +1082,7 @@ fn power_branch_loop_and_inert_code_end_to_end() {
|
||||
|
||||
dir.write(
|
||||
"loop.mq.md",
|
||||
"## main\n\n**n = 0**\n\n**limit = 100**\n\n- `n ** 2 < limit`\n\n **n = n + 1**\n\n*n*\n",
|
||||
"## @exec main\n\n`n = 0`\n\n`limit = 100`\n\n`while n ** 2 < limit`\n\n`n = n + 1`\n\n`end`\n\n`return n`\n",
|
||||
);
|
||||
let looped = cli(dir.path(), &["run", "loop.mq.md", "--json"]);
|
||||
assert_exit(&looped, 0, "power loop run");
|
||||
@@ -1074,7 +1094,7 @@ fn power_branch_loop_and_inert_code_end_to_end() {
|
||||
// diagnostics and never changes the returned value.
|
||||
dir.write(
|
||||
"inert.mq.md",
|
||||
"## main\n\n```text\n**n = 2 ** 3**\n```\n\n正文里的 `2 ** 3` 不执行。\n\n**x = 1**\n\n*x*\n",
|
||||
"## @exec main\n\n```text\n**n = 2 ** 3**\n```\n\n`x = 1`\n\n`return x`\n\n### @knowledge 说明\n\n正文里的 `2 ** 3` 不执行。\n",
|
||||
);
|
||||
let inert = cli(dir.path(), &["run", "inert.mq.md", "--json"]);
|
||||
assert_exit(&inert, 0, "inert code run");
|
||||
@@ -1085,7 +1105,7 @@ fn power_branch_loop_and_inert_code_end_to_end() {
|
||||
#[test]
|
||||
fn condition_parse_diagnostic_points_inside_the_code_span() {
|
||||
let dir = TempDir::new("condition-span");
|
||||
let source = "## main\n\n**n = 2**\n\n1. `n ** 2 ==`\n\n > print(1)\n";
|
||||
let source = "## @exec main\n\n`n = 2`\n\n`if n ** 2 ==`\n\n`print(1)`\n\n`end`\n";
|
||||
dir.write("bad.mq.md", source);
|
||||
let output = cli(dir.path(), &["check", "bad.mq.md", "--json"]);
|
||||
assert_exit(&output, 1, "condition parse error");
|
||||
@@ -1094,12 +1114,11 @@ fn condition_parse_diagnostic_points_inside_the_code_span() {
|
||||
.as_array()
|
||||
.expect("diagnostics array")
|
||||
.iter()
|
||||
.find(|diagnostic| diagnostic["code"] == "expr.parse")
|
||||
.expect("expr.parse diagnostic");
|
||||
.find(|diagnostic| diagnostic["code"] == "sem.syntax")
|
||||
.expect("sem.syntax diagnostic");
|
||||
assert_eq!(diagnostic["line"], 5);
|
||||
// 1-based column 14 is the closing backtick: the empty end-of-input span
|
||||
// must not be shifted by the delimiter.
|
||||
assert_eq!(diagnostic["column"], 14);
|
||||
let line = source.lines().nth(4).expect("line 5");
|
||||
assert_eq!(&line[13..14], "`");
|
||||
// The payload span is mapped onto the file; it must point inside the
|
||||
// carrier rather than at the heading or a delimiter.
|
||||
let column = diagnostic["column"].as_u64().expect("column");
|
||||
assert!((11..=14).contains(&column), "unexpected column {column}");
|
||||
}
|
||||
@@ -129,7 +129,7 @@ fn foreign_and_plugin_shortcuts_are_unavailable_in_the_pure_lane() {
|
||||
let foreign = dir.path().join("foreign.mq.md");
|
||||
std::fs::write(
|
||||
&foreign,
|
||||
"## 代码\n\n```python\nprint(1)\n```\n\n## main\n\n*foreign.call(ref(\"#代码\"), None)*\n",
|
||||
"## @knowledge 代码\n\n```python\nprint(1)\n```\n\n## @exec main\n\n`return foreign.call(ref(\"#代码\"), None)`\n",
|
||||
)
|
||||
.unwrap();
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
@@ -145,7 +145,7 @@ fn foreign_and_plugin_shortcuts_are_unavailable_in_the_pure_lane() {
|
||||
let plugin = dir.path().join("plugin.mq.md");
|
||||
std::fs::write(
|
||||
&plugin,
|
||||
"## main\n\n> plugin.invoke(\"missing.export\", {})\n\n*None*\n",
|
||||
"## @exec main\n\n`plugin.invoke(\"missing.export\", {})`\n\n`return None`\n",
|
||||
)
|
||||
.unwrap();
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
|
||||
@@ -82,20 +82,23 @@ fn transitive_path_dependency_resolves_from_its_own_manifest() {
|
||||
);
|
||||
write(
|
||||
&root.join("deps/a/index.mq.md"),
|
||||
"---\nimport:\n b: \"@b\"\n---\n\n## outer\n\n*b.inner()*\n",
|
||||
"---\nimport:\n b: \"@b\"\n---\n\n## @exec outer\n\n`return b.inner()`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("deps/a/b/marqdo.yaml"),
|
||||
"project:\n name: b\n version: 0.1.0\n",
|
||||
);
|
||||
write(&root.join("deps/a/b/index.mq.md"), "## inner\n\n*42*\n");
|
||||
write(
|
||||
&root.join("deps/a/b/index.mq.md"),
|
||||
"## @exec inner\n\n`return 42`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("marqdo.yaml"),
|
||||
"project:\n name: root\n version: 0.1.0\ndependencies:\n a:\n path: ./deps/a\n",
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n a: \"@a\"\n---\n\n## main\n\n> 打印(a.outer())\n",
|
||||
"---\nimport:\n a: \"@a\"\n---\n\n## @exec main\n\n`打印(a.outer())`\n",
|
||||
);
|
||||
let lock = cli(root, &["lock"]);
|
||||
assert_eq!(
|
||||
@@ -133,7 +136,7 @@ fn pinned_git_dependency_locks_syncs_and_runs() {
|
||||
&dep.join("marqdo.yaml"),
|
||||
"project:\n name: remote\n version: 0.3.0\n",
|
||||
);
|
||||
write(&dep.join("index.mq.md"), "## hello\n\n*7*\n");
|
||||
write(&dep.join("index.mq.md"), "## @exec hello\n\n`return 7`\n");
|
||||
git(&dep, &["init", "-q"]);
|
||||
git(&dep, &["add", "."]);
|
||||
git(&dep, &["commit", "-q", "-m", "initial"]);
|
||||
@@ -156,7 +159,7 @@ fn pinned_git_dependency_locks_syncs_and_runs() {
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n remote: \"@remote\"\n---\n\n## main\n\n> 打印(remote.hello())\n",
|
||||
"---\nimport:\n remote: \"@remote\"\n---\n\n## @exec main\n\n`打印(remote.hello())`\n",
|
||||
);
|
||||
|
||||
let lock = cli(&root, &["lock"]);
|
||||
@@ -206,7 +209,7 @@ fn check_does_not_fetch_a_missing_git_revision() {
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n remote: \"@remote\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n remote: \"@remote\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
let output = cli(root, &["check", "index.mq.md"]);
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
@@ -223,10 +226,13 @@ fn run_and_check_require_a_lock_when_dependencies_exist() {
|
||||
&root.join("marqdo.yaml"),
|
||||
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
|
||||
);
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*1*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 1`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
for command in ["check", "run"] {
|
||||
let output = cli(root, &[command, "index.mq.md"]);
|
||||
@@ -244,14 +250,20 @@ fn sync_is_strict_and_never_rewrites_the_lock() {
|
||||
&root.join("marqdo.yaml"),
|
||||
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
|
||||
);
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*1*\n");
|
||||
write(&root.join("index.mq.md"), "## main\n\n*1*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 1`\n",
|
||||
);
|
||||
write(&root.join("index.mq.md"), "## @exec main\n\n`return 1`\n");
|
||||
|
||||
let lock = cli(root, &["lock"]);
|
||||
assert_eq!(lock.status.code(), Some(0));
|
||||
let original = std::fs::read(root.join("marqdo.lock")).unwrap();
|
||||
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*2*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 2`\n",
|
||||
);
|
||||
let stale = cli(root, &["sync"]);
|
||||
assert_eq!(stale.status.code(), Some(1), "plain sync must be strict");
|
||||
let stderr = String::from_utf8_lossy(&stale.stderr);
|
||||
@@ -285,13 +297,19 @@ fn lock_records_transitive_closure_and_sync_materializes_it() {
|
||||
&root.join("deps/a/marqdo.yaml"),
|
||||
"project:\n name: a\n version: 0.1.0\ndependencies:\n b:\n path: ./b\n",
|
||||
);
|
||||
write(&root.join("deps/a/index.mq.md"), "## a\n\n*1*\n");
|
||||
write(
|
||||
&root.join("deps/a/index.mq.md"),
|
||||
"## @exec a\n\n`return 1`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("deps/a/b/marqdo.yaml"),
|
||||
"project:\n name: b\n version: 0.1.0\n",
|
||||
);
|
||||
write(&root.join("deps/a/b/index.mq.md"), "## b\n\n*2*\n");
|
||||
write(&root.join("index.mq.md"), "## main\n\n*1*\n");
|
||||
write(
|
||||
&root.join("deps/a/b/index.mq.md"),
|
||||
"## @exec b\n\n`return 2`\n",
|
||||
);
|
||||
write(&root.join("index.mq.md"), "## @exec main\n\n`return 1`\n");
|
||||
|
||||
let lock = cli(root, &["lock"]);
|
||||
assert_eq!(lock.status.code(), Some(0));
|
||||
@@ -326,10 +344,13 @@ fn deleted_lock_edge_fails_check_and_run() {
|
||||
&root.join("marqdo.yaml"),
|
||||
"project:\n name: root\n version: 0.1.0\ndependencies:\n dep:\n path: ./dep\n",
|
||||
);
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*1*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 1`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
assert_eq!(cli(root, &["lock"]).status.code(), Some(0));
|
||||
let lock = std::fs::read_to_string(root.join("marqdo.lock")).unwrap();
|
||||
@@ -362,10 +383,13 @@ fn tampered_path_dependency_content_is_rejected_by_locked_sync() {
|
||||
&root.join("dep/marqdo.yaml"),
|
||||
"project:\n name: dep\n version: 0.1.0\n",
|
||||
);
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*1*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 1`\n",
|
||||
);
|
||||
write(
|
||||
&root.join("index.mq.md"),
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## main\n\n*1*\n",
|
||||
"---\nimport:\n dep: \"@dep\"\n---\n\n## @exec main\n\n`return 1`\n",
|
||||
);
|
||||
assert_eq!(cli(root, &["lock"]).status.code(), Some(0));
|
||||
let lock_before = std::fs::read(root.join("marqdo.lock")).unwrap();
|
||||
@@ -375,7 +399,10 @@ fn tampered_path_dependency_content_is_rejected_by_locked_sync() {
|
||||
// Content change after the lock is a tamper/staleness signal. `run` and
|
||||
// `check` must never silently accept it, re-lock, or materialize a new
|
||||
// environment.
|
||||
write(&root.join("dep/index.mq.md"), "## value\n\n*2*\n");
|
||||
write(
|
||||
&root.join("dep/index.mq.md"),
|
||||
"## @exec value\n\n`return 2`\n",
|
||||
);
|
||||
for args in [
|
||||
vec!["sync", "--locked"],
|
||||
vec!["run", "index.mq.md"],
|
||||
|
||||
@@ -112,7 +112,7 @@ fn write_manifest(dir: &Path, sha: &str) {
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("doc.mq.md"),
|
||||
"## main\n\n**结果 = solver.add(a=20, b=22)**\n\n> 打印(结果)\n",
|
||||
"## @exec main\n\n`结果 = solver.add(a=20, b=22)`\n\n`打印(结果)`\n",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
@@ -227,7 +227,7 @@ fn write_capability_manifest(dir: &Path, sha: &str) {
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("doc.mq.md"),
|
||||
"## main\n\n**结果 = capability.read()**\n\n> 打印(结果)\n",
|
||||
"## @exec main\n\n`结果 = capability.read()`\n\n`打印(结果)`\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::create_dir_all(dir.join("data")).unwrap();
|
||||
|
||||
@@ -44,7 +44,7 @@ fn print_is_routed_through_the_stdlib_and_not_hostop_print() {
|
||||
write(
|
||||
dir.path(),
|
||||
"index.mq.md",
|
||||
"## main\n\n> 打印([\"x\", 1])\n\n*None*\n",
|
||||
"## @exec main\n\n`打印([\"x\", 1])`\n\n`return None`\n",
|
||||
);
|
||||
let trace = dir.path().join("trace.json");
|
||||
let output = cli(
|
||||
@@ -84,11 +84,11 @@ fn print_is_routed_through_the_stdlib_and_not_hostop_print() {
|
||||
#[test]
|
||||
fn stdlib_print_modules_are_injected_only_when_print_is_used() {
|
||||
let dir = TempDir::new("lazy");
|
||||
write(dir.path(), "plain.mq.md", "## main\n\n*1*\n");
|
||||
write(dir.path(), "plain.mq.md", "## @exec main\n\n`return 1`\n");
|
||||
write(
|
||||
dir.path(),
|
||||
"prints.mq.md",
|
||||
"## main\n\n> 打印(1)\n\n*None*\n",
|
||||
"## @exec main\n\n`打印(1)`\n\n`return None`\n",
|
||||
);
|
||||
for (file, expected_stdlib) in [("plain.mq.md", false), ("prints.mq.md", true)] {
|
||||
let target = dir.path().join(format!("{file}.json"));
|
||||
|
||||
@@ -29,39 +29,45 @@ struct Base {
|
||||
const BASES: &[Base] = &[
|
||||
Base {
|
||||
name: "factorial",
|
||||
source: r##"# 数学
|
||||
source: r##"# @knowledge 数学
|
||||
|
||||
## 阶乘
|
||||
## @exec 阶乘
|
||||
|
||||
计算阶乘的叙述一。
|
||||
|
||||
1. `n <= 1`
|
||||
`if n <= 1`
|
||||
|
||||
递归终点叙述。
|
||||
递归终点叙述。
|
||||
|
||||
*1*
|
||||
`return 1`
|
||||
|
||||
2. `n > 1`
|
||||
`else`
|
||||
|
||||
递归步骤叙述。
|
||||
`if n > 1`
|
||||
|
||||
**结果 = n * 阶乘(n=n-1)**
|
||||
递归步骤叙述。
|
||||
|
||||
*结果*
|
||||
`结果 = n * 阶乘(n=n-1)`
|
||||
|
||||
`return 结果`
|
||||
|
||||
`end`
|
||||
|
||||
`end`
|
||||
"##,
|
||||
narration: &["计算阶乘的叙述一。", "递归终点叙述。", "递归步骤叙述。"],
|
||||
},
|
||||
Base {
|
||||
name: "explicit-signature",
|
||||
source: r##"## 圆面积
|
||||
source: r##"## @exec 圆面积
|
||||
|
||||
`(r: Number)`
|
||||
`params(r: Number)`
|
||||
|
||||
半径叙述。
|
||||
|
||||
*math.eval(ref("#公式"), {"r": r}, target="A")*
|
||||
`return math.eval(ref("#公式"), {"r": r}, target="A")`
|
||||
|
||||
### 公式
|
||||
### @knowledge 公式
|
||||
|
||||
$$
|
||||
A = \pi r^2
|
||||
@@ -71,117 +77,133 @@ $$
|
||||
},
|
||||
Base {
|
||||
name: "countdown-loop",
|
||||
source: r##"## 倒计时
|
||||
source: r##"## @exec 倒计时
|
||||
|
||||
循环叙述。
|
||||
|
||||
**n = 3**
|
||||
`n = 3`
|
||||
|
||||
- `n > 0`
|
||||
`while n > 0`
|
||||
|
||||
循环体叙述。
|
||||
循环体叙述。
|
||||
|
||||
> 打印(n)
|
||||
`打印(n)`
|
||||
|
||||
**n = n - 1**
|
||||
`n = n - 1`
|
||||
|
||||
*n*
|
||||
`end`
|
||||
|
||||
`return n`
|
||||
"##,
|
||||
narration: &["循环叙述。", "循环体叙述。"],
|
||||
},
|
||||
Base {
|
||||
name: "branch-classification",
|
||||
source: r##"## 分类
|
||||
source: r##"## @exec 分类
|
||||
|
||||
背景叙述。
|
||||
|
||||
**结果 = 0**
|
||||
`结果 = 0`
|
||||
|
||||
1. `n < 0`
|
||||
`if n < 0`
|
||||
|
||||
负数分支叙述。
|
||||
负数分支叙述。
|
||||
|
||||
**结果 = -1**
|
||||
`结果 = -1`
|
||||
|
||||
2. `n >= 0`
|
||||
`else`
|
||||
|
||||
非负分支叙述。
|
||||
`if n >= 0`
|
||||
|
||||
**结果 = 1**
|
||||
非负分支叙述。
|
||||
|
||||
*结果*
|
||||
`结果 = 1`
|
||||
|
||||
`end`
|
||||
|
||||
`end`
|
||||
|
||||
`return 结果`
|
||||
"##,
|
||||
narration: &["背景叙述。", "负数分支叙述。", "非负分支叙述。"],
|
||||
},
|
||||
Base {
|
||||
name: "call-and-print",
|
||||
source: r##"## 问候
|
||||
source: r##"## @exec 问候
|
||||
|
||||
说明为什么这样写。
|
||||
|
||||
> 打印("你好")
|
||||
`打印("你好")`
|
||||
|
||||
**问候 = "你好,Marqdo"**
|
||||
`问候 = "你好,Marqdo"`
|
||||
|
||||
再补充一句叙述。
|
||||
|
||||
*问候*
|
||||
`return 问候`
|
||||
"##,
|
||||
narration: &["说明为什么这样写。", "再补充一句叙述。"],
|
||||
},
|
||||
Base {
|
||||
name: "inline-prose-in-executable-paragraph",
|
||||
source: r##"## 加一
|
||||
source: r##"## @exec 加一
|
||||
|
||||
这是行内叙述 **结果 = 1 + 1** 后面继续叙述。
|
||||
|
||||
*结果*
|
||||
`return 结果`
|
||||
"##,
|
||||
narration: &["这是行内叙述 ", " 后面继续叙述。"],
|
||||
},
|
||||
Base {
|
||||
name: "nested-control-flow",
|
||||
source: r##"## 嵌套
|
||||
source: r##"## @exec 嵌套
|
||||
|
||||
说明嵌套结构。
|
||||
|
||||
**total = 0**
|
||||
`total = 0`
|
||||
|
||||
1. `n > 0`
|
||||
`if n > 0`
|
||||
|
||||
**m = 3**
|
||||
`m = 3`
|
||||
|
||||
- `m > 0`
|
||||
`while m > 0`
|
||||
|
||||
**total = total + m**
|
||||
`total = total + m`
|
||||
|
||||
**m = m - 1**
|
||||
`m = m - 1`
|
||||
|
||||
2. `n <= 0`
|
||||
`end`
|
||||
|
||||
**total = 0**
|
||||
`else`
|
||||
|
||||
*total*
|
||||
`if n <= 0`
|
||||
|
||||
`total = 0`
|
||||
|
||||
`end`
|
||||
|
||||
`end`
|
||||
|
||||
`return total`
|
||||
"##,
|
||||
narration: &["说明嵌套结构。"],
|
||||
},
|
||||
Base {
|
||||
name: "two-namespaces",
|
||||
source: r##"# 数学
|
||||
source: r##"# @knowledge 数学
|
||||
|
||||
命名空间叙述。
|
||||
|
||||
## 平方
|
||||
## @exec 平方
|
||||
|
||||
**结果 = n * n**
|
||||
`结果 = n * n`
|
||||
|
||||
*结果*
|
||||
`return 结果`
|
||||
|
||||
# 文本
|
||||
# @knowledge 文本
|
||||
|
||||
## 长度
|
||||
## @exec 长度
|
||||
|
||||
*长度(s)*
|
||||
`return 长度(s)`
|
||||
"##,
|
||||
narration: &["命名空间叙述。"],
|
||||
},
|
||||
@@ -265,7 +287,7 @@ fn insert_prose_after_headings(source: &str) -> String {
|
||||
while next < lines.len() && lines[next].trim().is_empty() {
|
||||
next += 1;
|
||||
}
|
||||
let signature = next < lines.len() && lines[next].trim_start().starts_with("`(");
|
||||
let signature = next < lines.len() && lines[next].trim_start().starts_with("`params(");
|
||||
if signature {
|
||||
while index + 1 < lines.len() && lines[index + 1].trim().is_empty() {
|
||||
index += 1;
|
||||
|
||||
@@ -56,21 +56,21 @@ fn write_file(path: &Path, contents: &str) {
|
||||
std::fs::write(path, contents).unwrap();
|
||||
}
|
||||
|
||||
const READER: &str = r#"## main
|
||||
const READER: &str = r#"## @exec main
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**流 = resource.open("file", "data.txt", ["read"])**
|
||||
`流 = resource.open("file", "data.txt", ["read"])`
|
||||
|
||||
**第一次 = resource.read(流, 内存, 0, 2)**
|
||||
`第一次 = resource.read(流, 内存, 0, 2)`
|
||||
|
||||
**第二次 = resource.read(流, 内存, 2, 4)**
|
||||
`第二次 = resource.read(流, 内存, 2, 4)`
|
||||
|
||||
**第三次 = resource.read(流, 内存, 6, 1)**
|
||||
`第三次 = resource.read(流, 内存, 6, 1)`
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
`关闭 = resource.close(流)`
|
||||
|
||||
*[第一次.value.count, 第一次.value.eof, 第二次.value.count, 第二次.value.eof, 第三次.value.count, 第三次.value.eof, 内存[0], 内存[1], 内存[2], 内存[3], 内存[4], 内存[5], 关闭.ok]*
|
||||
`return [第一次.value.count, 第一次.value.eof, 第二次.value.count, 第二次.value.eof, 第三次.value.count, 第三次.value.eof, 内存[0], 内存[1], 内存[2], 内存[3], 内存[4], 内存[5], 关闭.ok]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -110,29 +110,29 @@ fn project_file_stream_reads_chunks_into_memory() {
|
||||
assert_eq!(values[12]["value"], true);
|
||||
}
|
||||
|
||||
const WRITER: &str = r#"## main
|
||||
const WRITER: &str = r#"## @exec main
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**`内存[0] = 65`**
|
||||
`内存[0] = 65`
|
||||
|
||||
**`内存[1] = 66`**
|
||||
`内存[1] = 66`
|
||||
|
||||
**`内存[2] = 67`**
|
||||
`内存[2] = 67`
|
||||
|
||||
**写 = resource.open("file", "out.bin", ["write"])**
|
||||
`写 = resource.open("file", "out.bin", ["write"])`
|
||||
|
||||
**写入 = resource.write(写, 内存, 0, 3)**
|
||||
`写入 = resource.write(写, 内存, 0, 3)`
|
||||
|
||||
**关闭写 = resource.close(写)**
|
||||
`关闭写 = resource.close(写)`
|
||||
|
||||
**读 = resource.open("file", "out.bin", ["read"])**
|
||||
`读 = resource.open("file", "out.bin", ["read"])`
|
||||
|
||||
**读出 = resource.read(读, 内存, 0, 3)**
|
||||
`读出 = resource.read(读, 内存, 0, 3)`
|
||||
|
||||
**关闭读 = resource.close(读)**
|
||||
`关闭读 = resource.close(读)`
|
||||
|
||||
*[写入.value.count, 关闭写.ok, 读出.value.count, 读出.value.eof, 内存[0], 内存[1], 内存[2], 关闭读.ok]*
|
||||
`return [写入.value.count, 关闭写.ok, 读出.value.count, 读出.value.eof, 内存[0], 内存[1], 内存[2], 关闭读.ok]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -171,39 +171,43 @@ fn project_file_stream_writes_on_close_then_reads_back() {
|
||||
assert_eq!(std::fs::read(dir.path().join("out.bin")).unwrap(), b"ABC");
|
||||
}
|
||||
|
||||
const COPY: &str = r#"## main
|
||||
const COPY: &str = r#"## @exec main
|
||||
|
||||
**输入 = resource.open("file", "data.txt", ["read"])**
|
||||
`输入 = resource.open("file", "data.txt", ["read"])`
|
||||
|
||||
**输出 = resource.open("file", "out.bin", ["write"])**
|
||||
`输出 = resource.open("file", "out.bin", ["write"])`
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**总数 = 0**
|
||||
`总数 = 0`
|
||||
|
||||
**继续 = true**
|
||||
`继续 = true`
|
||||
|
||||
- `继续`
|
||||
`while 继续`
|
||||
|
||||
**块 = resource.read(输入, 内存, 0, 16)**
|
||||
`块 = resource.read(输入, 内存, 0, 16)`
|
||||
|
||||
1. `块.ok`
|
||||
`if 块.ok`
|
||||
|
||||
> resource.write(输出, 内存, 0, 块.value.count)
|
||||
`resource.write(输出, 内存, 0, 块.value.count)`
|
||||
|
||||
**总数 = 总数 + 块.value.count**
|
||||
`总数 = 总数 + 块.value.count`
|
||||
|
||||
**继续 = not 块.value.eof**
|
||||
`继续 = not 块.value.eof`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
|
||||
**继续 = false**
|
||||
`继续 = false`
|
||||
|
||||
**关闭读 = resource.close(输入)**
|
||||
`end`
|
||||
|
||||
**关闭写 = resource.close(输出)**
|
||||
`end`
|
||||
|
||||
*[总数, 关闭读.ok, 关闭写.ok]*
|
||||
`关闭读 = resource.close(输入)`
|
||||
|
||||
`关闭写 = resource.close(输出)`
|
||||
|
||||
`return [总数, 关闭读.ok, 关闭写.ok]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -257,27 +261,27 @@ fn tcp_stream_round_trips_through_mqmd_with_a_network_grant() {
|
||||
|
||||
let dir = TempDir::new("tcp");
|
||||
let program = format!(
|
||||
r#"## main
|
||||
r#"## @exec main
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**`内存[0] = 112`**
|
||||
`内存[0] = 112`
|
||||
|
||||
**`内存[1] = 105`**
|
||||
`内存[1] = 105`
|
||||
|
||||
**`内存[2] = 110`**
|
||||
`内存[2] = 110`
|
||||
|
||||
**`内存[3] = 103`**
|
||||
`内存[3] = 103`
|
||||
|
||||
**流 = resource.open("tcp", "{address}", ["read", "write"])**
|
||||
`流 = resource.open("tcp", "{address}", ["read", "write"])`
|
||||
|
||||
**写入 = resource.write(流, 内存, 0, 4)**
|
||||
`写入 = resource.write(流, 内存, 0, 4)`
|
||||
|
||||
**读出 = resource.read(流, 内存, 0, 4)**
|
||||
`读出 = resource.read(流, 内存, 0, 4)`
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
`关闭 = resource.close(流)`
|
||||
|
||||
*[写入.value.count, 读出.value.count, 内存[0], 内存[1], 内存[2], 内存[3], 关闭.ok]*
|
||||
`return [写入.value.count, 读出.value.count, 内存[0], 内存[1], 内存[2], 内存[3], 关闭.ok]`
|
||||
"#
|
||||
);
|
||||
write_file(&dir.path().join("index.mq.md"), &program);
|
||||
@@ -315,19 +319,19 @@ fn file_stream_requires_a_read_grant() {
|
||||
assert_eq!(report["error"]["code"], "permission.denied");
|
||||
}
|
||||
|
||||
const STDIN_READER: &str = r#"## main
|
||||
const STDIN_READER: &str = r#"## @exec main
|
||||
|
||||
**输入 = resource.open("stdio", "stdin", ["read"])**
|
||||
`输入 = resource.open("stdio", "stdin", ["read"])`
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**第一 = resource.read(输入, 内存, 0, 2)**
|
||||
`第一 = resource.read(输入, 内存, 0, 2)`
|
||||
|
||||
**第二 = resource.read(输入, 内存, 2, 8)**
|
||||
`第二 = resource.read(输入, 内存, 2, 8)`
|
||||
|
||||
**第三 = resource.read(输入, 内存, 0, 1)**
|
||||
`第三 = resource.read(输入, 内存, 0, 1)`
|
||||
|
||||
*[第一.value.count, 第一.value.eof, 第二.value.count, 第二.value.eof, 第三.value.eof, 内存[0], 内存[1], 内存[2], 内存[3]]*
|
||||
`return [第一.value.count, 第一.value.eof, 第二.value.count, 第二.value.eof, 第三.value.eof, 内存[0], 内存[1], 内存[2], 内存[3]]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -354,35 +358,35 @@ fn piped_stdin_is_chunked_and_reaches_eof() {
|
||||
}
|
||||
}
|
||||
|
||||
const STDERR_WRITER: &str = r#"## main
|
||||
const STDERR_WRITER: &str = r#"## @exec main
|
||||
|
||||
**错误 = resource.open("stdio", "stderr", ["write"])**
|
||||
`错误 = resource.open("stdio", "stderr", ["write"])`
|
||||
|
||||
**输出 = resource.open("stdio", "stdout", ["write"])**
|
||||
`输出 = resource.open("stdio", "stdout", ["write"])`
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
`内存 = memory.new(1, 1)`
|
||||
|
||||
**`内存[0] = 101`**
|
||||
`内存[0] = 101`
|
||||
|
||||
**`内存[1] = 114`**
|
||||
`内存[1] = 114`
|
||||
|
||||
**`内存[2] = 114`**
|
||||
`内存[2] = 114`
|
||||
|
||||
**写错误 = resource.write(错误, 内存, 0, 3)**
|
||||
`写错误 = resource.write(错误, 内存, 0, 3)`
|
||||
|
||||
**`内存[0] = 111`**
|
||||
`内存[0] = 111`
|
||||
|
||||
**`内存[1] = 117`**
|
||||
`内存[1] = 117`
|
||||
|
||||
**`内存[2] = 116`**
|
||||
`内存[2] = 116`
|
||||
|
||||
**写输出 = resource.write(输出, 内存, 0, 3)**
|
||||
`写输出 = resource.write(输出, 内存, 0, 3)`
|
||||
|
||||
**关闭错误 = resource.close(错误)**
|
||||
`关闭错误 = resource.close(错误)`
|
||||
|
||||
**关闭输出 = resource.close(输出)**
|
||||
`关闭输出 = resource.close(输出)`
|
||||
|
||||
*[写错误.value.count, 写输出.value.count, 关闭错误.ok, 关闭输出.ok]*
|
||||
`return [写错误.value.count, 写输出.value.count, 关闭错误.ok, 关闭输出.ok]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -412,17 +416,17 @@ import:
|
||||
console: "std:io/console"
|
||||
---
|
||||
|
||||
## main
|
||||
## @exec main
|
||||
|
||||
**第一 = console.读取行()**
|
||||
`第一 = console.读取行()`
|
||||
|
||||
**第二 = console.读取行()**
|
||||
`第二 = console.读取行()`
|
||||
|
||||
**第三 = console.读取行()**
|
||||
`第三 = console.读取行()`
|
||||
|
||||
**输出 = console.打印(第一.value + "|" + 第二.value)**
|
||||
`输出 = console.打印(第一.value + "|" + 第二.value)`
|
||||
|
||||
*[第一.ok, 第二.ok, 第三.ok, 第三.code, 输出.ok]*
|
||||
`return [第一.ok, 第二.ok, 第三.ok, 第三.code, 输出.ok]`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -71,11 +71,11 @@ import:
|
||||
io: "std:io"
|
||||
---
|
||||
|
||||
## main
|
||||
## @exec main
|
||||
|
||||
> io.写值(["bundle", 1, 2.5])
|
||||
`io.写值(["bundle", 1, 2.5])`
|
||||
|
||||
*None*
|
||||
`return None`
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
@@ -154,7 +154,7 @@ fn tampered_bundle_is_rejected_before_module_loading() {
|
||||
let dir = TempDir::new("tamper");
|
||||
let bundle = dir.path().join("stdlib");
|
||||
copy_stdlib(&stdlib_dir(), &bundle);
|
||||
std::fs::write(bundle.join("io.mq.md"), "## tampered\n").unwrap();
|
||||
std::fs::write(bundle.join("io.mq.md"), "## @knowledge tampered\n").unwrap();
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &bundle, &["run", "index.mq.md"]);
|
||||
assert!(!output.status.success());
|
||||
|
||||
Reference in new issue
Block a user