feat(rfc-cm): baseline P0-P3 compute machine, stdlib and resource slices

Implements the first vertical slices of RFC-CM-0001/0002 on top of a113d89:

- P1: opaque Memory regions (64 KiB pages, budgets, grow-only), byte
  read/write, Memory-index place assignment, wire/host-boundary rejection.
- CM-A04/A05: value-semantics List/Map/Text/Table primitives and the
  recoverable Result map convention.
- CM-A06: opaque Resource handles, resource.open/read/write/close, stdout
  write, granted project-file read and buffered write, strict fail-closed.
- CM-A07 Compat-A: explicit .mq.md format/utf8/io chain matching builtin
  print for the covered value types; builtin remains default.
- P2 fixtures: allocators, list/tree/graph/hash, integer codecs, UTF-8
  encode/decode, CSV/JSON subsets, table filter/sort/join, bytecode VM.
- ADRs 0022-0027, spec updates, evidence reports and 68 semantic goldens.

Gate: cargo fmt --check, clippy -D warnings, 407 tests passed.
This commit is contained in:
cflmy committed 2026-11-20 21:16:54 +08:00
1 parent a113d89496
commit 92bfbf2568
127 files changed
+11351 -141

No files matched your search

+126 -2
View File
@@ -15,11 +15,11 @@ no test commits, copies or rewrites files outside a throw-away temp directory.
tests/
├── conformance/
│ ├── ast/ 160 × *.mq.md + sibling *.ast.json goldens
│ └── semantics/ 44 × *.mq.md + sibling *.expected.json goldens
│ └── semantics/ 68 × *.mq.md + sibling *.expected.json goldens
├── negative/
│ ├── *.mq.md 52 × compile-time hard-error fixtures + *.expected.json
│ ├── warnings/ 6 × inert-executable warning fixtures + *.expected.json
│ └── runtime/ 12 × runtime-error fixtures + *.expected.json
│ └── runtime/ 21 × runtime-error fixtures + *.expected.json
└── README.md this file
```
@@ -69,6 +69,130 @@ The power/control-flow slice is pinned by:
* `crates/marqdo-frontend/src/payload.rs` span tests and
`crates/cli/tests/integration_cli.rs` CLI span/E2E assertions.
The addressable-memory slice (RFC-CM P1, ADR-0022) is pinned by:
* semantic fixtures 45–47: sharing a region by handle, per-region isolation,
`memory.size`, `memory.grow` returning the old count, preserved old bytes and
zero-filled new pages;
* runtime negatives 13–18: out-of-bounds and huge offsets, byte values outside
`0..=255`, index assignment on a `List`, growth beyond the declared maximum
and negative page counts;
* `tests/negative/runtime/15-index-assign-non-memory.mq.md` also guards the v1
value semantics of `List` (it must not become shared mutable memory);
* `crates/marqdo-runtime/src/memory.rs` unit tests (aliasing, atomic failed
growth, forged/stale handles, page arithmetic) and
`crates/marqdo-runtime/tests/interpreter.rs` host-boundary/budget tests;
* `crates/marqdo-value/src/wire.rs` proves live handles are not transferable;
* `examples/memory-buffer.mq.md` and the `examples_r6` CLI test prove the
`.mq.md` surface end to end.
Partial RFC-CM P2 (compute closure) is demonstrated, not certified, by:
* semantic fixtures 48–50: a big-endian u32 codec and bump allocator, three
allocated nodes linked and traversed, and a failed allocation that leaves
the allocator header unchanged. These documents use only language builtins
and control flow; no Rust algorithm is added for them.
* The full P2 gate also needs a free-list allocator with reuse/coalescing,
tree/graph structures, a recoverable `Result` model and an independent
stdlib repository. Those are **not** implemented.
CM-A04 structure primitives (ADR-0023) are pinned by:
* semantic fixtures 51–54: value semantics of `list.append`/`map.set`/
`map.remove` (inputs unchanged, keys sorted, `map.has`/`map.get`),
Unicode scalar round-trip through `text.codepoints`/
`text.from_codepoint` (including invalid scalars → `None`),
`table.columns`/`table.row`/`table.cell` projection, and a `.mq.md`
`过滤` function that rebuilds a filtered GFM-style table without mutating
the input or adding a Rust table algorithm;
* runtime negatives 19–21: `list.append` on a Map, table row out of bounds,
and a non-Text `map.get` key;
* `crates/marqdo-runtime/src/builtins_impl.rs`; no algorithm builtin such as
sort/filter/join/JSON/CSV is added.
CM-A05 recoverable results and the first-fit allocator (ADR-0024) are pinned
by:
* semantic fixture 55: a `.mq.md` first-fit/free-list allocator with 8-byte
block headers, free-block reuse, block splitting, address-adjacent
coalescing (a second region fills its bump space, so only the merged block
can satisfy a 40-byte request), a too-large request returning
`{ok:false, code:"alloc.no_space", ...}`, and payload writes that show the
reused blocks do not overlap;
* the `Result` shape is an ordinary Map; no `Result` value type, exception,
`try`/`catch` or propagation syntax is added. The ADR records the
success/failure field contract and the fatal-vs-recoverable boundary;
* semantic fixture 56: a `Memory`-backed binary search tree with 16-byte nodes,
recursive insertion and recursive in-order traversal built from
`list.concat`; the returned root/left/right offsets and sorted values pin
node layout and traversal;
* semantic fixture 57: a `.mq.md` CSV single-row splitter using
`text.codepoints`, `text.from_codepoint`, `list.append` and `list.concat`;
it preserves empty fields and does **not** pretend to support quoted or
escaped CSV;
* semantic fixture 58: an open-addressing hash table with linear probing,
collision handling (keys 5 and 37), value update, not-found `None`, element
count, and a full-table `hash.full` Result in a second region;
* semantic fixture 59: a `.mq.md` UTF-8 encoder that writes 1–4 byte sequences
into `Memory` from `text.codepoints`, returning the byte length and bytes;
* semantic fixture 60: a validating `.mq.md` UTF-8 decoder over `Memory` with
explicit `utf8.truncated`, `utf8.invalid_continuation`, `utf8.invalid_scalar`
and `utf8.invalid_lead` failures; valid ASCII, 2/3/4-byte and mixed text is
decoded;
* semantic fixture 61: a `Memory`-backed graph adjacency list with 16-byte nodes
and 8-byte edges, head-inserted edges and a neighbor traversal; no Rust graph
container or new `Value` type;
* semantic fixture 62: a recursive `.mq.md` JSON subset parser for nested
arrays and objects, strings with standard escapes including `\uXXXX`,
integers (including negative), `true`/`false`/`null`, whitespace, `next`
positions and `Result` errors (`json.unexpected_eof`, `json.invalid_array`,
`json.invalid_object`, `json.invalid_string`, `json.invalid_number`,
`json.invalid_literal`, `json.unexpected_token`). Floats are explicitly out
of scope;
* semantic fixture 63: a `Memory`-backed stack bytecode VM with instruction
decode, PUSH/ADD/MUL/JMP/JZ/HALT, a stack region and two programs (arithmetic
and conditional branch); no Rust VM or interpreter loop was added;
* semantic fixture 64: `.mq.md` big-endian 16/32/64-bit encoders/decoders and
LEB128 varint encode/decode, including the `u64` maximum and byte-level
round-trips;
* semantic fixture 65: a `.mq.md` table sort built on `table.row`,
`table.columns`, `table.cell`, `表` and `list.append`; insertion sort
rebuilds a new table and leaves the input table unchanged;
* semantic fixture 67: a `.mq.md` table equality join over two tables, using
`table.row/columns/cell`, `list.concat`, `list.append` and `表`; both input
tables remain unchanged;
* semantic fixture 68: a quoted/escaped CSV single-row parser using Unicode
scalar primitives; `""` is a literal quote and commas inside quotes are
preserved. Multi-line fields and whitespace trimming are out of scope.
RFC-CM CM-A07 Compat-A (ADR-0027) is pinned by:
* `examples/stdlib/{format,text,io}.mq.md` and `examples/stdlib/demo.mq.md`;
* `crates/cli/tests/examples_r6.rs::mqmd_stdlib_print_output_matches_builtin_print`,
which runs the demo and asserts `io.写值` and builtin `打印` lines are
byte-identical for a nested List/Map/None value and a Table;
* the demo covers Unit/None/Bool? (Bool appears inside the nested list),
Int, Float, Text, List, Map and Table. Formula/Code/FunctionRef/PluginHandle
are explicitly not covered; `打印` is not yet routed to the stdlib.
RFC-CM CM-A06 resource streams (ADR-0025) are pinned by:
* semantic fixture 66: `.mq.md` UTF-8 text encoded into `Memory`, written to a
`stdio/stdout` resource, closed, then re-used to prove
`{ok:false,code:"resource.closed"}`; the expected `stdout` is the decoded
text;
* `crates/marqdo-host/tests/resource_streams.rs` (9): pure-mode stdout write,
output-budget atomicity, strict refusal, project-file chunking/EOF, missing
grant, traversal rejection and unsupported rights/kinds;
* `crates/cli/tests/resource_streams.rs`: real `marqdo run` with
`--allow-read data.txt`, three chunked reads into `Memory` plus EOF, the same
document failing with `permission.denied` without the grant, and a
write-then-read program with `--allow-write/--allow-read out.bin` proving the
file is created only at `resource.close`, and a chunked `.mq.md` file copy
(`resource.read` → `resource.write`) that reproduces `data.txt` in `out.bin`;
* `crates/marqdo-host/src/resource.rs` unit tests for forged/closed handles,
stdout read/file write unsupported, and chunked EOF.
Fixture discovery is directory-based and sorted by file name, so the order is
deterministic on every platform. The AST and semantic suites fail loudly if the
corpus shrinks below 100 / 25 fixtures, and the negative suite fails below 30
@@ -0,0 +1,9 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "int",
"decimal": "141"
}
}
@@ -0,0 +1,14 @@
## main
This function demonstrates an addressable byte region next to ordinary prose.
A copied name shares the same region identity.
**区域 = memory.new(1, 4)**
**`区域[8] = 42`**
**另一个名字 = 区域**
**`另一个名字[9] = 99`**
*区域[8] + 区域[9]*
@@ -0,0 +1,14 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "0"},
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "9"},
{"type": "int", "decimal": "0"}
]
}
}
@@ -0,0 +1,14 @@
## main
A zero-page region is legal. Growing it returns the previous page count,
preserves old bytes and zero-fills every new byte.
**内存 = memory.new(0, 2)**
**旧页数 = memory.grow(内存, 1)**
**内存[0] = 9**
**页数 = memory.size(内存)**
*[旧页数, 页数, 内存[0], 内存[65535]]*
@@ -0,0 +1,15 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "5"},
{"type": "int", "decimal": "6"},
{"type": "int", "decimal": "7"},
{"type": "bool", "value": true},
{"type": "bool", "value": false}
]
}
}
@@ -0,0 +1,18 @@
## main
Two regions never alias even at the same offset. A copied name does share
the region identity, and equality compares that identity, not the bytes.
**甲 = memory.new(1, 1)**
**乙 = memory.new(1, 1)**
**`甲[0] = 5`**
**`乙[0] = 6`**
**别名 = 甲**
**`别名[1] = 7`**
*[甲[0], 乙[0], 甲[1], 甲 == 别名, 甲 == 乙]*
@@ -0,0 +1,15 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "12"},
{"type": "int", "decimal": "28"},
{"type": "int", "decimal": "36"},
{"type": "int", "decimal": "255"}
]
}
}
@@ -0,0 +1,72 @@
## 写32
`(内存, 基址, 值)`
把非负 Int 的低 32 位按大端字节写入区域。
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
同样按大端读取四个字节。
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 分配
`(内存, 容量, 大小)`
区域头四个字节保存下一个空闲偏移。这是顺序分配器:free 只把块交还给调用方,
不在这里实现复用。
**当前位置 = 读32(内存, 0)**
1. `当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
*当前位置*
2. `true`
*None*
## main
**内存 = memory.new(1, 1)**
**`内存[0] = 0`**
**`内存[1] = 0`**
**`内存[2] = 0`**
**`内存[3] = 4`**
**第一个 = 分配(内存, 65536, 8)**
**第二个 = 分配(内存, 65536, 16)**
**第三个 = 分配(内存, 65536, 8)**
**`内存[第一个] = 255`**
*[第一个, 第二个, 第三个, 读32(内存, 0), 内存[第一个]]*
@@ -0,0 +1,14 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "12"},
{"type": "int", "decimal": "20"},
{"type": "int", "decimal": "60"}
]
}
}
@@ -0,0 +1,93 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 分配
`(内存, 容量, 大小)`
**当前位置 = 读32(内存, 0)**
1. `当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
*当前位置*
2. `true`
*None*
## 节点
`(内存, 值, 下一个)`
节点是 8 字节块:偏移 0 存一个字节的值,偏移 1..4 存 next 偏移;0 表示空。
**地址 = 分配(内存, 65536, 8)**
**`内存[地址] = 值`**
> 写32(内存, 地址 + 1, 下一个)
*地址*
## 遍历求和
`(内存, 头)`
**当前 = 头**
**总和 = 0**
- `当前 != 0`
**总和 = 总和 + 内存[当前]**
**当前 = 读32(内存, 当前 + 1)**
*总和*
## main
**内存 = memory.new(1, 1)**
**`内存[0] = 0`**
**`内存[1] = 0`**
**`内存[2] = 0`**
**`内存[3] = 4`**
**甲 = 节点(内存, 10, 0)**
**乙 = 节点(内存, 20, 甲)**
**丙 = 节点(内存, 30, 乙)**
*[甲, 乙, 丙, 遍历求和(内存, 丙)]*
@@ -0,0 +1,14 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "none"},
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "65536"}
]
}
}
@@ -0,0 +1,67 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 分配
`(内存, 容量, 大小)`
**当前位置 = 读32(内存, 0)**
1. `当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
*当前位置*
2. `true`
*None*
## main
分配失败必须保持区域头不变,之后的分配仍从原位置继续。
**内存 = memory.new(1, 1)**
**`内存[0] = 0`**
**`内存[1] = 0`**
**`内存[2] = 0`**
**`内存[3] = 4`**
**失败 = 分配(内存, 65536, 65536)**
**失败后位置 = 读32(内存, 0)**
**成功 = 分配(内存, 65536, 65532)**
**最终位置 = 读32(内存, 0)**
*[失败, 失败后位置, 成功, 最终位置]*
@@ -0,0 +1,45 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"}
]
},
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "3"}
]
},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "2"},
{
"type": "list",
"items": [
{"type": "text", "value": "b"},
{"type": "text", "value": "c"}
]
},
{"type": "bool", "value": false},
{"type": "none"},
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "3"}
]
}
]
}
}
@@ -0,0 +1,18 @@
## main
Lists and maps keep value semantics: constructors return new values and the
inputs are unchanged.
**原列表 = [1, 2]**
**新列表 = list.append(原列表, 3)**
**原映射 = {a: 1, b: 2}**
**新映射 = map.set(原映射, "c", 3)**
**删除后 = map.remove(新映射, "a")**
**键 = map.keys(删除后)**
*[原列表, 新列表, 原映射.b, 新映射.b, 删除后.b, 键, map.has(原映射, "c"), map.get(原映射, "z"), list.concat([1], [2, 3])]*
@@ -0,0 +1,22 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "int", "decimal": "97"},
{"type": "int", "decimal": "233"},
{"type": "int", "decimal": "20013"}
]
},
{"type": "text", "value": "中"},
{"type": "none"},
{"type": "none"},
{"type": "int", "decimal": "1"}
]
}
}
@@ -0,0 +1,7 @@
## main
**点 = text.codepoints("aé中")**
**回 = text.from_codepoint(20013)**
*[点, 回, text.from_codepoint(55296), text.from_codepoint(1114112), 长度(回)]*
@@ -0,0 +1,26 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "text", "value": "名称"},
{"type": "text", "value": "分数"}
]
},
{
"type": "list",
"items": [
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
]
},
{"type": "int", "decimal": "87"},
{"type": "int", "decimal": "2"}
]
}
}
@@ -0,0 +1,5 @@
## main
**数据 = 表(["名称", "分数"], [["甲", 87], ["乙", 92]])**
*[table.columns(数据), table.row(数据, 1), table.cell(数据, 0, 1), 长度(数据)]*
@@ -0,0 +1,42 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "table",
"header": ["名称", "分数"],
"rows": [
[
{"type": "text", "value": "甲"},
{"type": "int", "decimal": "87"}
],
[
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
],
[
{"type": "text", "value": "丙"},
{"type": "int", "decimal": "71"}
]
]
},
{
"type": "table",
"header": ["名称", "分数"],
"rows": [
[
{"type": "text", "value": "甲"},
{"type": "int", "decimal": "87"}
],
[
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
]
]
}
]
}
}
@@ -0,0 +1,31 @@
## 过滤
`(数据, 阈值)`
只用 table 观察原语和 list 构造原语重建一个新表;输入表不被修改。
**列 = table.columns(数据)**
**结果行 = []**
**行号 = 0**
- `行号 < 长度(数据)`
**分数 = table.cell(数据, 行号, 1)**
1. `分数 > 阈值`
**结果行 = list.append(结果行, table.row(数据, 行号))**
**行号 = 行号 + 1**
*表(列, 结果行)*
## main
**原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
**结果 = 过滤(原表, 80)**
*[原表, 结果]*
@@ -0,0 +1,262 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "16",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "32",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "16",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "16",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "32",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "56",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "56",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "72",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "96",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "112",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "alloc.no_space"
},
"message": {
"type": "text",
"value": "没有足够的空闲空间"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "16",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "32",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "48",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "16",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "alloc.no_space"
},
"message": {
"type": "text",
"value": "没有足够的空闲空间"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"decimal": "33",
"type": "int"
},
{
"decimal": "22",
"type": "int"
},
{
"decimal": "33",
"type": "int"
},
{
"decimal": "33",
"type": "int"
},
{
"decimal": "22",
"type": "int"
},
{
"decimal": "44",
"type": "int"
},
{
"decimal": "55",
"type": "int"
},
{
"decimal": "66",
"type": "int"
},
{
"decimal": "77",
"type": "int"
}
],
"type": "list"
}
}
@@ -0,0 +1,308 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 成功
`(值)`
*{ok: true, value: 值}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 初始化
`(内存)`
区域头:偏移 0 是空闲链表头,偏移 4 是 bump 指针;首个块从 8 开始。
> 写32(内存, 0, 0)
> 写32(内存, 4, 8)
*成功(None)*
## 查找
`(内存, 需求)`
返回 `[前一个空闲块, 当前空闲块, 当前块大小]`;没有合适块时返回零值。
**前一个 = 0**
**当前 = 读32(内存, 0)**
- `当前 != 0`
**块大小 = 读32(内存, 当前)**
1. `块大小 >= 需求`
*[前一个, 当前, 块大小]*
**前一个 = 当前**
**当前 = 读32(内存, 当前 + 4)**
*[0, 0, 0]*
## 分配
`(内存, 容量, 请求大小)`
first-fit:先查空闲链表,必要时切分;否则从 bump 指针处取新块。
块头 8 字节:偏移 0 是总块大小,偏移 4 是空闲链表 next。返回 payload 偏移。
**需求 = (请求大小 + 8 + 7) // 8 * 8**
**位置 = 查找(内存, 需求)**
**前一个 = 位置[0]**
**当前 = 位置[1]**
**块大小 = 位置[2]**
1. `当前 != 0`
**余量 = 块大小 - 需求**
**下一个 = 读32(内存, 当前 + 4)**
1. `余量 >= 16`
**余块 = 当前 + 需求**
> 写32(内存, 余块, 余量)
> 写32(内存, 余块 + 4, 下一个)
1. `前一个 == 0`
> 写32(内存, 0, 余块)
2. `true`
> 写32(内存, 前一个 + 4, 余块)
> 写32(内存, 当前, 需求)
*成功(当前 + 8)*
2. `true`
1. `前一个 == 0`
> 写32(内存, 0, 下一个)
2. `true`
> 写32(内存, 前一个 + 4, 下一个)
*成功(当前 + 8)*
2. `true`
**下一个空位 = 读32(内存, 4)**
1. `下一个空位 + 需求 <= 容量`
> 写32(内存, 下一个空位, 需求)
> 写32(内存, 4, 下一个空位 + 需求)
*成功(下一个空位 + 8)*
2. `true`
*失败("alloc.no_space", "没有足够的空闲空间")*
## 插入空闲
`(内存, 块)`
空闲链表按地址升序;插入后先与后继合并,再与前去合并。
**前一个 = 0**
**当前 = 读32(内存, 0)**
- `当前 != 0 and 当前 < 块`
**前一个 = 当前**
**当前 = 读32(内存, 当前 + 4)**
> 写32(内存, 块 + 4, 当前)
1. `前一个 == 0`
> 写32(内存, 0, 块)
2. `true`
> 写32(内存, 前一个 + 4, 块)
> 合并(内存, 前一个, 块, 当前)
*成功(None)*
## 合并
`(内存, 前一个, 块, 当前)`
相邻块只有在地址连续时才合并,避免产生虚假的大块。
**块大小 = 读32(内存, 块)**
1. `当前 != 0 and 块 + 块大小 == 当前`
**合并大小 = 块大小 + 读32(内存, 当前)**
> 写32(内存, 块, 合并大小)
> 写32(内存, 块 + 4, 读32(内存, 当前 + 4))
2. `true`
**忽略后继合并 = 0**
**前一个大小 = 0**
1. `前一个 != 0`
**前一个大小 = 读32(内存, 前一个)**
2. `true`
**忽略前去读取 = 0**
后继合并完成后再处理前驱;两个判断必须分开执行。
1. `前一个 != 0 and 前一个 + 前一个大小 == 块`
**前一个新大小 = 前一个大小 + 读32(内存, 块)**
> 写32(内存, 前一个, 前一个新大小)
> 写32(内存, 前一个 + 4, 读32(内存, 块 + 4))
2. `true`
**忽略前去合并 = 0**
*成功(None)*
## 释放
`(内存, 地址)`
把 payload 偏移换算回块头,按地址插入空闲链表并由“合并”处理相邻块。
**块 = 地址 - 8**
> 插入空闲(内存, 块)
*成功(None)*
## main
**内存 = memory.new(1, 1)**
> 初始化(内存)
**甲 = 分配(内存, 65536, 8)**
**乙 = 分配(内存, 65536, 16)**
**`内存[甲.value] = 11`**
**`内存[乙.value] = 22`**
> 释放(内存, 甲.value)
**丙 = 分配(内存, 65536, 8)**
**`内存[丙.value] = 33`**
> 释放(内存, 乙.value)
> 释放(内存, 丙.value)
**丁 = 分配(内存, 65536, 4)**
**戊 = 分配(内存, 65536, 16)**
**己 = 分配(内存, 65536, 64)**
> 释放(内存, 己.value)
**庚 = 分配(内存, 65536, 8)**
**辛 = 分配(内存, 65536, 16)**
**壬 = 分配(内存, 65536, 8)**
**癸 = 分配(内存, 65536, 8)**
**`内存[庚.value] = 44`**
**`内存[辛.value] = 55`**
**`内存[壬.value] = 66`**
**`内存[癸.value] = 77`**
**失败结果 = 分配(内存, 65536, 100000)**
**内存2 = memory.new(1, 1)**
> 初始化(内存2)
**甲2 = 分配(内存2, 65536, 8)**
**乙2 = 分配(内存2, 65536, 8)**
**丙2 = 分配(内存2, 65536, 8)**
> 分配(内存2, 65536, 65472)
> 释放(内存2, 甲2.value)
> 释放(内存2, 乙2.value)
> 释放(内存2, 丙2.value)
**丁2 = 分配(内存2, 65536, 40)**
**戊2 = 分配(内存2, 65536, 8)**
*[甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸, 失败结果, 甲2, 乙2, 丙2, 丁2, 戊2, 内存[甲.value], 内存[乙.value], 内存[丙.value], 内存[丁.value], 内存[戊.value], 内存[庚.value], 内存[辛.value], 内存[壬.value], 内存[癸.value]]*
@@ -0,0 +1,55 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"decimal": "8",
"type": "int"
},
{
"items": [
{
"decimal": "1",
"type": "int"
},
{
"decimal": "3",
"type": "int"
},
{
"decimal": "4",
"type": "int"
},
{
"decimal": "5",
"type": "int"
},
{
"decimal": "7",
"type": "int"
},
{
"decimal": "8",
"type": "int"
},
{
"decimal": "9",
"type": "int"
}
],
"type": "list"
},
{
"decimal": "24",
"type": "int"
},
{
"decimal": "40",
"type": "int"
}
],
"type": "list"
}
}
@@ -0,0 +1,112 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 新节点
`(内存, 值)`
每个节点 16 字节:偏移 0 存一个字节的值,偏移 4 存左子,偏移 8 存右子;0 表示空。
节点值必须落在 0..255。
**位置 = 读32(内存, 0)**
**`内存[位置] = 值`**
> 写32(内存, 位置 + 4, 0)
> 写32(内存, 位置 + 8, 0)
> 写32(内存, 0, 位置 + 16)
*位置*
## 插入
`(内存, 根, 值)`
1. `根 == 0`
*新节点(内存, 值)*
2. `值 < 内存[根]`
**左 = 插入(内存, 读32(内存, 根 + 4), 值)**
> 写32(内存, 根 + 4, 左)
*根*
3. `true`
**右 = 插入(内存, 读32(内存, 根 + 8), 值)**
> 写32(内存, 根 + 8, 右)
*根*
## 遍历
`(内存, 根)`
中序遍历,返回从小到大的值列表。
1. `根 == 0`
*[]*
**左 = 遍历(内存, 读32(内存, 根 + 4))**
**右 = 遍历(内存, 读32(内存, 根 + 8))**
*list.concat(list.concat(左, [内存[根]]), 右)*
## main
**内存 = memory.new(1, 1)**
**`内存[0] = 0`**
**`内存[1] = 0`**
**`内存[2] = 0`**
**`内存[3] = 8`**
**根 = 0**
**值表 = [5, 3, 8, 1, 4, 7, 9]**
**下标 = 0**
- `下标 < 长度(值表)`
**根 = 插入(内存, 根, 值表[下标])**
**下标 = 下标 + 1**
*[根, 遍历(内存, 根), 读32(内存, 根 + 4), 读32(内存, 根 + 8)]*
@@ -0,0 +1,53 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"items": [
{
"type": "text",
"value": "甲"
},
{
"type": "text",
"value": "乙"
},
{
"type": "text",
"value": ""
},
{
"type": "text",
"value": "丙"
}
],
"type": "list"
},
{
"items": [
{
"type": "text",
"value": ""
}
],
"type": "list"
},
{
"items": [
{
"type": "text",
"value": "a"
},
{
"type": "text",
"value": "b"
}
],
"type": "list"
}
],
"type": "list"
}
}
@@ -0,0 +1,35 @@
## 分割
`(文本)`
这是 CSV 单行拆分的最小实现:只处理逗号分隔,不支持引号或转义;空字段保留。
**结果 = []**
**当前 = ""**
**点 = text.codepoints(文本)**
**下标 = 0**
- `下标 < 长度(点)`
**字符 = text.from_codepoint(点[下标])**
1. `字符 == ","`
**结果 = list.append(结果, 当前)**
**当前 = ""**
2. `true`
**当前 = 当前 + 字符**
**下标 = 下标 + 1**
*list.concat(结果, [当前])*
## main
*[分割("甲,乙,,丙"), 分割(""), 分割("a,b")]*
@@ -0,0 +1,66 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"type": "none"
}
},
"type": "map"
},
{
"decimal": "55",
"type": "int"
},
{
"decimal": "370",
"type": "int"
},
{
"decimal": "120",
"type": "int"
},
{
"type": "none"
},
{
"decimal": "3",
"type": "int"
},
{
"entries": {
"code": {
"type": "text",
"value": "hash.full"
},
"message": {
"type": "text",
"value": "哈希表已满"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"decimal": "10",
"type": "int"
},
{
"decimal": "20",
"type": "int"
}
],
"type": "list"
}
}
@@ -0,0 +1,164 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 成功
`(值)`
*{ok: true, value: 值}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 初始化
`(内存, 容量)`
头部 8 字节:0..3 是元素数,4..7 是槽数。槽从偏移 8 开始,每槽 8 字节:
键 u32 + 值 u32。键 0 表示空槽,因此业务键必须从 1 开始。
> 写32(内存, 0, 0)
> 写32(内存, 4, 容量)
*成功(None)*
## 插入
`(内存, 键, 值)`
线性探测;键已存在时只更新值,不增加元素数。
**容量 = 读32(内存, 4)**
**位置 = 键 % 容量**
**尝试 = 0**
**完成 = false**
- `尝试 < 容量 and not 完成`
**槽 = 8 + (位置 + 尝试) % 容量 * 8**
**槽键 = 读32(内存, 槽)**
1. `槽键 == 0`
> 写32(内存, 槽, 键)
> 写32(内存, 槽 + 4, 值)
> 写32(内存, 0, 读32(内存, 0) + 1)
**完成 = true**
2. `槽键 == 键`
> 写32(内存, 槽 + 4, 值)
**完成 = true**
3. `true`
**尝试 = 尝试 + 1**
1. `完成`
*成功(None)*
2. `true`
*失败("hash.full", "哈希表已满")*
## 查找
`(内存, 键)`
遇到空槽即停止;找不到返回 None。
**容量 = 读32(内存, 4)**
**位置 = 键 % 容量**
**尝试 = 0**
**结果 = None**
- `尝试 < 容量`
**槽 = 8 + (位置 + 尝试) % 容量 * 8**
**槽键 = 读32(内存, 槽)**
1. `槽键 == 键`
**结果 = 读32(内存, 槽 + 4)**
**尝试 = 容量**
2. `槽键 == 0`
**尝试 = 容量**
3. `true`
**尝试 = 尝试 + 1**
*结果*
## main
业务键从 1 开始。37 与 5 在 32 个槽中同余,用来验证线性探测。
**内存 = memory.new(1, 1)**
> 初始化(内存, 32)
**插入5 = 插入(内存, 5, 50)**
> 插入(内存, 37, 370)
> 插入(内存, 12, 120)
> 插入(内存, 5, 55)
**内存2 = memory.new(1, 1)**
> 初始化(内存2, 2)
> 插入(内存2, 1, 10)
> 插入(内存2, 2, 20)
**表满 = 插入(内存2, 4, 40)**
*[插入5, 查找(内存, 5), 查找(内存, 37), 查找(内存, 12), 查找(内存, 99), 读32(内存, 0), 表满, 查找(内存2, 1), 查找(内存2, 2)]*
@@ -0,0 +1,59 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"decimal": "10",
"type": "int"
},
{
"items": [
{
"decimal": "65",
"type": "int"
},
{
"decimal": "195",
"type": "int"
},
{
"decimal": "169",
"type": "int"
},
{
"decimal": "228",
"type": "int"
},
{
"decimal": "184",
"type": "int"
},
{
"decimal": "173",
"type": "int"
},
{
"decimal": "240",
"type": "int"
},
{
"decimal": "157",
"type": "int"
},
{
"decimal": "132",
"type": "int"
},
{
"decimal": "158",
"type": "int"
}
],
"type": "list"
}
],
"type": "list"
}
}
@@ -0,0 +1,74 @@
## 编码
`(文本, 内存, 基址)`
把 Unicode 标量序列按 UTF-8 编码写入 `Memory`,返回字节长度。
这是 `.mq.md` 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
**点 = text.codepoints(文本)**
**下标 = 0**
**偏移 = 0**
- `下标 < 长度(点)`
**码点 = 点[下标]**
1. `码点 < 128`
**`内存[基址 + 偏移] = 码点`**
**偏移 = 偏移 + 1**
2. `码点 < 2048`
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
**偏移 = 偏移 + 2**
3. `码点 < 65536`
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
**偏移 = 偏移 + 3**
4. `true`
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
**偏移 = 偏移 + 4**
**下标 = 下标 + 1**
*偏移*
## main
**内存 = memory.new(1, 1)**
**长度 = 编码("Aé中𝄞", 内存, 0)**
**字节 = []**
**下标 = 0**
- `下标 < 长度`
**字节 = list.append(字节, 内存[下标])**
**下标 = 下标 + 1**
*[长度, 字节]*
@@ -0,0 +1,87 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"type": "text",
"value": "Aé中"
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "utf8.truncated"
},
"message": {
"type": "text",
"value": "UTF-8 解码失败"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "utf8.invalid_continuation"
},
"message": {
"type": "text",
"value": "UTF-8 解码失败"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "utf8.invalid_scalar"
},
"message": {
"type": "text",
"value": "UTF-8 解码失败"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"type": "text",
"value": "𝄞"
}
},
"type": "map"
}
],
"type": "list"
}
}
@@ -0,0 +1,185 @@
## 成功
`(值)`
*{ok: true, value: 值}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 续字节
`(字节)`
*字节 >= 128 and 字节 < 192*
## 解码2
`(内存, 基址, 长度, 偏移, 文本)`
1. `偏移 + 1 >= 长度`
*[文本, 偏移, "utf8.truncated"]*
**b1 = 内存[基址 + 偏移 + 1]**
1. `not 续字节(b1)`
*[文本, 偏移, "utf8.invalid_continuation"]*
**码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)**
1. `码点 < 128`
*[文本, 偏移, "utf8.invalid_scalar"]*
*[文本 + text.from_codepoint(码点), 偏移 + 2, ""]*
## 解码3
`(内存, 基址, 长度, 偏移, 文本)`
1. `偏移 + 2 >= 长度`
*[文本, 偏移, "utf8.truncated"]*
**b1 = 内存[基址 + 偏移 + 1]**
**b2 = 内存[基址 + 偏移 + 2]**
1. `not (续字节(b1) and 续字节(b2))`
*[文本, 偏移, "utf8.invalid_continuation"]*
**码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)**
1. `码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
*[文本, 偏移, "utf8.invalid_scalar"]*
*[文本 + text.from_codepoint(码点), 偏移 + 3, ""]*
## 解码4
`(内存, 基址, 长度, 偏移, 文本)`
1. `偏移 + 3 >= 长度`
*[文本, 偏移, "utf8.truncated"]*
**b1 = 内存[基址 + 偏移 + 1]**
**b2 = 内存[基址 + 偏移 + 2]**
**b3 = 内存[基址 + 偏移 + 3]**
1. `not (续字节(b1) and 续字节(b2) and 续字节(b3))`
*[文本, 偏移, "utf8.invalid_continuation"]*
**码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)**
1. `码点 < 65536 or 码点 > 1114111`
*[文本, 偏移, "utf8.invalid_scalar"]*
*[文本 + text.from_codepoint(码点), 偏移 + 4, ""]*
## 解码
`(内存, 基址, 长度)`
**偏移 = 0**
**文本 = ""**
**错误 = ""**
- `偏移 < 长度 and 错误 == ""`
**首 = 内存[基址 + 偏移]**
1. `首 < 128`
**文本 = 文本 + text.from_codepoint(首)**
**偏移 = 偏移 + 1**
2. `首 < 194`
**错误 = "utf8.invalid_lead"**
3. `首 < 224`
**结果 = 解码2(内存, 基址, 长度, 偏移, 文本)**
**文本 = 结果[0]**
**偏移 = 结果[1]**
**错误 = 结果[2]**
4. `首 < 240`
**结果 = 解码3(内存, 基址, 长度, 偏移, 文本)**
**文本 = 结果[0]**
**偏移 = 结果[1]**
**错误 = 结果[2]**
5. `首 < 245`
**结果 = 解码4(内存, 基址, 长度, 偏移, 文本)**
**文本 = 结果[0]**
**偏移 = 结果[1]**
**错误 = 结果[2]**
6. `true`
**错误 = "utf8.invalid_lead"**
1. `错误 == ""`
*成功(文本)*
2. `true`
*失败(错误, "UTF-8 解码失败")*
## 写字节
`(内存, 基址, 字节)`
**下标 = 0**
- `下标 < 长度(字节)`
**`内存[基址 + 下标] = 字节[下标]`**
**下标 = 下标 + 1**
*None*
## main
**内存 = memory.new(1, 1)**
> 写字节(内存, 0, [65, 195, 169, 228, 184, 173])
> 写字节(内存, 16, [228, 184])
> 写字节(内存, 32, [228, 65, 173])
> 写字节(内存, 48, [237, 160, 128])
> 写字节(内存, 64, [240, 157, 132, 158])
*[解码(内存, 0, 6), 解码(内存, 16, 2), 解码(内存, 32, 3), 解码(内存, 48, 3), 解码(内存, 64, 4)]*
@@ -0,0 +1,48 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"decimal": "8",
"type": "int"
},
{
"decimal": "24",
"type": "int"
},
{
"decimal": "40",
"type": "int"
},
{
"items": [
{
"decimal": "40",
"type": "int"
},
{
"decimal": "24",
"type": "int"
}
],
"type": "list"
},
{
"items": [
{
"decimal": "40",
"type": "int"
}
],
"type": "list"
},
{
"items": [],
"type": "list"
}
],
"type": "list"
}
}
@@ -0,0 +1,113 @@
## 写32
`(内存, 基址, 值)`
**字节3 = 值 % 256**
**字节2 = 值 // 256 % 256**
**字节1 = 值 // 65536 % 256**
**字节0 = 值 // 16777216 % 256**
**`内存[基址] = 字节0`**
**`内存[基址 + 1] = 字节1`**
**`内存[基址 + 2] = 字节2`**
**`内存[基址 + 3] = 字节3`**
*None*
## 读32
`(内存, 基址)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
## 分配
`(内存, 大小)`
简单 bump 分配;调用方保证容量足够。
**位置 = 读32(内存, 0)**
> 写32(内存, 0, 位置 + 大小)
*位置*
## 新节点
`(内存, 值)`
节点 16 字节:偏移 0 存一个字节的值,偏移 4 存边链表头;0 表示空。
**位置 = 分配(内存, 16)**
**`内存[位置] = 值`**
> 写32(内存, 位置 + 4, 0)
*位置*
## 加边
`(内存, 源, 目标)`
边 8 字节:目标 u32 + 下一条边 u32。头插到源节点的边链表。
**边 = 分配(内存, 8)**
> 写32(内存, 边, 目标)
> 写32(内存, 边 + 4, 读32(内存, 源 + 4))
> 写32(内存, 源 + 4, 边)
*None*
## 邻接
`(内存, 节点)`
返回节点值列表;顺序是头插顺序(后加的边在前)。
**当前 = 读32(内存, 节点 + 4)**
**结果 = []**
- `当前 != 0`
**结果 = list.append(结果, 读32(内存, 当前))**
**当前 = 读32(内存, 当前 + 4)**
*结果*
## main
**内存 = memory.new(1, 1)**
**`内存[0] = 0`**
**`内存[1] = 0`**
**`内存[2] = 0`**
**`内存[3] = 8`**
**甲 = 新节点(内存, 1)**
**乙 = 新节点(内存, 2)**
**丙 = 新节点(内存, 3)**
> 加边(内存, 甲, 乙)
> 加边(内存, 甲, 丙)
> 加边(内存, 乙, 丙)
*[甲, 乙, 丙, 邻接(内存, 甲), 邻接(内存, 乙), 邻接(内存, 丙)]*
@@ -0,0 +1,142 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"entries": {
"next": {
"decimal": "20",
"type": "int"
},
"ok": {
"type": "bool",
"value": true
},
"value": {
"items": [
{
"decimal": "1",
"type": "int"
},
{
"decimal": "23",
"type": "int"
},
{
"items": [
{
"decimal": "4",
"type": "int"
},
{
"decimal": "5",
"type": "int"
}
],
"type": "list"
},
{
"decimal": "-6",
"type": "int"
}
],
"type": "list"
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "json.invalid_array"
},
"message": {
"type": "text",
"value": "数组缺少逗号或 ]"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "json.unexpected_eof"
},
"message": {
"type": "text",
"value": "数组未闭合"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
},
{
"entries": {
"next": {
"decimal": "35",
"type": "int"
},
"ok": {
"type": "bool",
"value": true
},
"value": {
"entries": {
"a": {
"decimal": "1",
"type": "int"
},
"b": {
"items": [
{
"type": "bool",
"value": true
},
{
"type": "none"
},
{
"type": "text",
"value": "说\"好"
}
],
"type": "list"
}
},
"type": "map"
}
},
"type": "map"
},
{
"entries": {
"next": {
"decimal": "6",
"type": "int"
},
"ok": {
"type": "bool",
"value": true
},
"value": {
"type": "text",
"value": "a\nb"
}
},
"type": "map"
}
],
"type": "list"
}
}
@@ -0,0 +1,431 @@
## 成功值
`(值, 下一位置)`
*{ok: true, value: 值, next: 下一位置}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 是空白
`(码点)`
*码点 == 32 or 码点 == 9 or 码点 == 10 or 码点 == 13*
## 是数字
`(码点)`
*码点 >= 48 and 码点 <= 57*
## 跳过空白
`(点, 位置)`
**当前 = 位置**
- `当前 < 长度(点) and 是空白(点[当前])`
**当前 = 当前 + 1**
*当前*
## 匹配
`(点, 位置, 文本)`
**目标 = text.codepoints(文本)**
**下标 = 0**
**结果 = true**
- `下标 < 长度(目标) and 结果`
1. `位置 + 下标 >= 长度(点)`
**结果 = false**
2. `点[位置 + 下标] != 目标[下标]`
**结果 = false**
3. `true`
**继续 = 0**
**下标 = 下标 + 1**
*结果*
## 解析数字
`(点, 位置)`
**负数 = false**
**当前 = 位置**
1. `text.from_codepoint(点[当前]) == "-"`
**负数 = true**
**当前 = 当前 + 1**
2. `true`
**非负 = 0**
**值 = 0**
**开始 = 当前**
- `当前 < 长度(点) and 是数字(点[当前])`
**值 = 值 * 10 + 点[当前] - 48**
**当前 = 当前 + 1**
1. `当前 == 开始`
*失败("json.invalid_number", "数字缺少位数")*
1. `负数`
*成功值(0 - 值, 当前)*
*成功值(值, 当前)*
## 十六进制值
`(码点)`
1. `码点 >= 48 and 码点 <= 57`
*码点 - 48*
2. `码点 >= 65 and 码点 <= 70`
*码点 - 55*
3. `码点 >= 97 and 码点 <= 102`
*码点 - 87*
*0 - 1*
## 解析字符串
`(点, 位置)`
**当前 = 位置 + 1**
**结果 = ""**
**完成 = false**
**错误 = ""**
- `当前 < 长度(点) and not 完成 and 错误 == ""`
**码点 = 点[当前]**
1. `码点 == 34`
**完成 = true**
**当前 = 当前 + 1**
2. `码点 == 92`
1. `当前 + 1 >= 长度(点)`
**错误 = "json.invalid_escape"**
2. `true`
**转义码点 = 点[当前 + 1]**
1. `转义码点 == 34`
**结果 = 结果 + text.from_codepoint(34)**
**当前 = 当前 + 2**
2. `转义码点 == 92`
**结果 = 结果 + text.from_codepoint(92)**
**当前 = 当前 + 2**
3. `转义码点 == 47`
**结果 = 结果 + text.from_codepoint(47)**
**当前 = 当前 + 2**
4. `转义码点 == 98`
**结果 = 结果 + text.from_codepoint(8)**
**当前 = 当前 + 2**
5. `转义码点 == 102`
**结果 = 结果 + text.from_codepoint(12)**
**当前 = 当前 + 2**
6. `转义码点 == 110`
**结果 = 结果 + text.from_codepoint(10)**
**当前 = 当前 + 2**
7. `转义码点 == 114`
**结果 = 结果 + text.from_codepoint(13)**
**当前 = 当前 + 2**
8. `转义码点 == 116`
**结果 = 结果 + text.from_codepoint(9)**
**当前 = 当前 + 2**
9. `转义码点 == 117`
1. `当前 + 5 >= 长度(点)`
**错误 = "json.invalid_escape"**
2. `true`
**数字 = 十六进制值(点[当前 + 2]) * 4096 + 十六进制值(点[当前 + 3]) * 256 + 十六进制值(点[当前 + 4]) * 16 + 十六进制值(点[当前 + 5])**
1. `数字 < 0 or (数字 >= 55296 and 数字 <= 57343)`
**错误 = "json.invalid_escape"**
2. `true`
**结果 = 结果 + text.from_codepoint(数字)**
**当前 = 当前 + 6**
10. `true`
**错误 = "json.invalid_escape"**
3. `true`
**结果 = 结果 + text.from_codepoint(码点)**
**当前 = 当前 + 1**
1. `错误 != ""`
*失败("json.invalid_string", "字符串转义无效")*
1. `not 完成`
*失败("json.unexpected_eof", "字符串未闭合")*
*成功值(结果, 当前)*
## 解析对象
`(点, 位置)`
*解析对象项(点, 位置 + 1, {})*
## 解析对象项
`(点, 位置, 已解析)`
**位置 = 跳过空白(点, 位置)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "对象未闭合")*
**当前 = text.from_codepoint(点[位置])**
1. `当前 == "}"`
*成功值(已解析, 位置 + 1)*
**键结果 = 解析字符串(点, 位置)**
1. `not 键结果.ok`
*键结果*
**键 = 键结果.value**
**位置 = 跳过空白(点, 键结果.next)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "对象缺少冒号")*
1. `text.from_codepoint(点[位置]) != ":"`
*失败("json.invalid_object", "对象缺少冒号")*
**位置 = 跳过空白(点, 位置 + 1)**
**值结果 = 解析值(点, 位置)**
1. `not 值结果.ok`
*值结果*
**新的已解析 = map.set(已解析, 键, 值结果.value)**
**位置 = 跳过空白(点, 值结果.next)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "对象未闭合")*
**分隔 = text.from_codepoint(点[位置])**
1. `分隔 == ","`
*解析对象项(点, 位置 + 1, 新的已解析)*
2. `分隔 == "}"`
*成功值(新的已解析, 位置 + 1)*
3. `true`
*失败("json.invalid_object", "对象缺少逗号或 }")*
## 解析值
`(点, 位置)`
**位置 = 跳过空白(点, 位置)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "需要值")*
**首 = text.from_codepoint(点[位置])**
**首码点 = 点[位置]**
1. `首码点 == 34`
*解析字符串(点, 位置)*
2. `首码点 == 123`
*解析对象(点, 位置)*
3. `首 == "["`
*解析数组(点, 位置)*
4. `首 == "-" or 是数字(点[位置])`
*解析数字(点, 位置)*
5. `首 == "t"`
1. `匹配(点, 位置, "true")`
*成功值(true, 位置 + 4)*
2. `true`
*失败("json.invalid_literal", "无效字面量")*
6. `首 == "f"`
1. `匹配(点, 位置, "false")`
*成功值(false, 位置 + 5)*
2. `true`
*失败("json.invalid_literal", "无效字面量")*
7. `首 == "n"`
1. `匹配(点, 位置, "null")`
*成功值(None, 位置 + 4)*
2. `true`
*失败("json.invalid_literal", "无效字面量")*
8. `true`
*失败("json.unexpected_token", "不支持的值")*
## 解析数组
`(点, 位置)`
*解析数组项(点, 位置 + 1, [])*
## 解析数组项
`(点, 位置, 已解析)`
**位置 = 跳过空白(点, 位置)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "数组未闭合")*
**当前 = text.from_codepoint(点[位置])**
1. `当前 == "]"`
*成功值(已解析, 位置 + 1)*
**项 = 解析值(点, 位置)**
1. `not 项.ok`
*项*
**新的已解析 = list.append(已解析, 项.value)**
**位置 = 跳过空白(点, 项.next)**
1. `位置 >= 长度(点)`
*失败("json.unexpected_eof", "数组未闭合")*
**分隔 = text.from_codepoint(点[位置])**
1. `分隔 == ","`
*解析数组项(点, 位置 + 1, 新的已解析)*
2. `分隔 == "]"`
*成功值(新的已解析, 位置 + 1)*
3. `true`
*失败("json.invalid_array", "数组缺少逗号或 ]")*
## main
**`对象结果 = 解析值(text.codepoints("{\"a\": 1, \"b\": [true, null, \"说\\\"好\"]}"), 0)`**
**`字符串结果 = 解析值(text.codepoints("\"a\\nb\""), 0)`**
*[解析值(text.codepoints(" [1, 23, [4, 5], -6] "), 0), 解析值(text.codepoints("[1 2]"), 0), 解析值(text.codepoints("[1,"), 0), 对象结果, 字符串结果]*
@@ -0,0 +1,36 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "36",
"type": "int"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"decimal": "42",
"type": "int"
}
},
"type": "map"
}
],
"type": "list"
}
}
@@ -0,0 +1,130 @@
## 成功
`(值)`
*{ok: true, value: 值}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 写字节
`(内存, 基址, 字节)`
**下标 = 0**
- `下标 < 长度(字节)`
**`内存[基址 + 下标] = 字节[下标]`**
**下标 = 下标 + 1**
*None*
## 运行
`(内存, 程序长度, 栈基址)`
程序从偏移 0 开始,栈从 `栈基址` 开始。指令集:
1 = PUSH 立即数,2 = ADD,3 = MUL,5 = JMP,6 = JZ,7 = HALT。
**指令指针 = 0**
**栈指针 = 0**
**运行中 = true**
**错误 = ""**
- `指令指针 < 程序长度 and 运行中 and 错误 == ""`
**操作码 = 内存[指令指针]**
1. `操作码 == 1`
**`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`**
**栈指针 = 栈指针 + 1**
**指令指针 = 指令指针 + 2**
2. `操作码 == 2`
**右 = 内存[栈基址 + 栈指针 - 1]**
**左 = 内存[栈基址 + 栈指针 - 2]**
**`内存[栈基址 + 栈指针 - 2] = 左 + 右`**
**栈指针 = 栈指针 - 1**
**指令指针 = 指令指针 + 1**
3. `操作码 == 3`
**右 = 内存[栈基址 + 栈指针 - 1]**
**左 = 内存[栈基址 + 栈指针 - 2]**
**`内存[栈基址 + 栈指针 - 2] = 左 * 右`**
**栈指针 = 栈指针 - 1**
**指令指针 = 指令指针 + 1**
4. `操作码 == 5`
**指令指针 = 内存[指令指针 + 1]**
5. `操作码 == 6`
**条件 = 内存[栈基址 + 栈指针 - 1]**
**栈指针 = 栈指针 - 1**
1. `条件 == 0`
**指令指针 = 内存[指令指针 + 1]**
2. `true`
**指令指针 = 指令指针 + 2**
6. `操作码 == 7`
**运行中 = false**
7. `true`
**错误 = "vm.invalid_opcode"**
1. `错误 != ""`
*失败("vm.invalid_opcode", "未知指令")*
1. `栈指针 == 0`
*失败("vm.empty_stack", "没有结果")*
*成功(内存[栈基址 + 栈指针 - 1])*
## main
**内存 = memory.new(1, 1)**
第一段程序计算 `(5 + 7) * 3`。
> 写字节(内存, 0, [1, 5, 1, 7, 2, 1, 3, 3, 7])
**算术结果 = 运行(内存, 9, 1024)**
第二段程序在条件为 0 时跳到偏移 8,压入 42 后停机。
> 写字节(内存, 0, [1, 0, 6, 8, 1, 99, 7, 0, 1, 42, 7])
**分支结果 = 运行(内存, 11, 2048)**
*[算术结果, 分支结果]*
@@ -0,0 +1,46 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"decimal": "4660",
"type": "int"
},
{
"decimal": "305419896",
"type": "int"
},
{
"decimal": "1311768467463790320",
"type": "int"
},
{
"decimal": "18446744073709551615",
"type": "int"
},
{
"decimal": "4",
"type": "int"
},
{
"decimal": "123456789",
"type": "int"
},
{
"decimal": "4",
"type": "int"
},
{
"decimal": "255",
"type": "int"
},
{
"decimal": "255",
"type": "int"
}
],
"type": "list"
}
}
@@ -0,0 +1,108 @@
## 写大端
`(内存, 基址, 值, 宽度)`
从最低字节开始写,放到 `基址 + 宽度 - 1 - 下标`,因此最终是大端序。
调用方保证 `值` 能放进 `宽度` 字节。
**当前 = 值**
**下标 = 0**
- `下标 < 宽度`
**`内存[基址 + 宽度 - 1 - 下标] = 当前 % 256`**
**当前 = 当前 // 256**
**下标 = 下标 + 1**
*None*
## 读大端
`(内存, 基址, 宽度)`
**值 = 0**
**下标 = 0**
- `下标 < 宽度`
**值 = 值 * 256 + 内存[基址 + 下标]**
**下标 = 下标 + 1**
*值*
## 写变长
`(内存, 基址, 值)`
LEB128:每字节低 7 位,最高位表示是否还有后续;返回写入字节数。
**当前 = 值**
**偏移 = 0**
- `当前 >= 128`
**`内存[基址 + 偏移] = 当前 % 128 + 128`**
**当前 = 当前 // 128**
**偏移 = 偏移 + 1**
**`内存[基址 + 偏移] = 当前`**
*偏移 + 1*
## 读变长
`(内存, 基址)`
返回 `[值, 消耗字节数]`。
**值 = 0**
**倍数 = 1**
**偏移 = 0**
**继续 = true**
- `继续`
**字节 = 内存[基址 + 偏移]**
**值 = 值 + 字节 % 128 * 倍数**
1. `字节 < 128`
**继续 = false**
2. `true`
**倍数 = 倍数 * 128**
**偏移 = 偏移 + 1**
*[值, 偏移 + 1]*
## main
**内存 = memory.new(1, 1)**
> 写大端(内存, 0, 4660, 2)
> 写大端(内存, 16, 305419896, 4)
> 写大端(内存, 32, 1311768467463790320, 8)
> 写大端(内存, 48, 18446744073709551615, 8)
**变长长度 = 写变长(内存, 128, 123456789)**
**变长 = 读变长(内存, 128)**
*[读大端(内存, 0, 2), 读大端(内存, 16, 4), 读大端(内存, 32, 8), 读大端(内存, 48, 8), 变长长度, 变长[0], 变长[1], 内存[48], 内存[55]]*
@@ -0,0 +1,88 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"header": [
"名称",
"分数"
],
"rows": [
[
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
}
],
[
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
],
[
{
"type": "text",
"value": "丙"
},
{
"decimal": "71",
"type": "int"
}
]
],
"type": "table"
},
{
"header": [
"名称",
"分数"
],
"rows": [
[
{
"type": "text",
"value": "丙"
},
{
"decimal": "71",
"type": "int"
}
],
[
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
}
],
[
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
]
],
"type": "table"
}
],
"type": "list"
}
}
@@ -0,0 +1,69 @@
## 插入有序
`(已排序, 行, 列)`
把一行插入到已按 `列` 升序排列的行列表中;返回新列表,不修改输入。
**新列表 = []**
**已插入 = false**
**下标 = 0**
- `下标 < 长度(已排序)`
**当前行 = 已排序[下标]**
1. `not 已插入 and 当前行[列] > 行[列]`
**新列表 = list.append(新列表, 行)**
**新列表 = list.append(新列表, 当前行)**
**已插入 = true**
2. `true`
**新列表 = list.append(新列表, 当前行)**
**下标 = 下标 + 1**
1. `not 已插入`
**新列表 = list.append(新列表, 行)**
2. `true`
**忽略 = 0**
*新列表*
## 排序
`(数据, 列)`
按列号升序排序;输入表不变,输出新表。
**列名 = table.columns(数据)**
**行数 = 长度(数据)**
**已排序 = []**
**下标 = 0**
- `下标 < 行数`
**已排序 = 插入有序(已排序, table.row(数据, 下标), 列)**
**下标 = 下标 + 1**
*表(列名, 已排序)*
## main
**原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
**升序 = 排序(原表, 1)**
*[原表, 升序]*
@@ -0,0 +1,61 @@
{
"args": {},
"function": "main",
"stdout": "你好,资源!\n",
"value": {
"items": [
{
"type": "text",
"value": "Resource"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"entries": {
"count": {
"decimal": "19",
"type": "int"
}
},
"type": "map"
}
},
"type": "map"
},
{
"entries": {
"ok": {
"type": "bool",
"value": true
},
"value": {
"type": "none"
}
},
"type": "map"
},
{
"entries": {
"code": {
"type": "text",
"value": "resource.closed"
},
"message": {
"type": "text",
"value": "Resource handle is closed"
},
"ok": {
"type": "bool",
"value": false
}
},
"type": "map"
}
],
"type": "list"
}
}
@@ -0,0 +1,69 @@
## 编码
`(文本, 内存, 基址)`
**点 = text.codepoints(文本)**
**下标 = 0**
**偏移 = 0**
- `下标 < 长度(点)`
**码点 = 点[下标]**
1. `码点 < 128`
**`内存[基址 + 偏移] = 码点`**
**偏移 = 偏移 + 1**
2. `码点 < 2048`
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
**偏移 = 偏移 + 2**
3. `码点 < 65536`
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
**偏移 = 偏移 + 3**
4. `true`
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
**偏移 = 偏移 + 4**
**下标 = 下标 + 1**
*偏移*
## main
**内存 = memory.new(1, 1)**
**长度 = 编码("你好,资源!\n", 内存, 0)**
**标准输出 = resource.open("stdio", "stdout", ["write"])**
**写入 = resource.write(标准输出, 内存, 0, 长度)**
**关闭 = resource.close(标准输出)**
**再次读取 = resource.read(标准输出, 内存, 0, 1)**
*[类型(标准输出), 写入, 关闭, 再次读取]*
@@ -0,0 +1,135 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"header": [
"名称",
"分数"
],
"rows": [
[
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
}
],
[
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
],
[
{
"type": "text",
"value": "丙"
},
{
"decimal": "71",
"type": "int"
}
]
],
"type": "table"
},
{
"header": [
"名称",
"等级"
],
"rows": [
[
{
"type": "text",
"value": "乙"
},
{
"type": "text",
"value": "A"
}
],
[
{
"type": "text",
"value": "甲"
},
{
"type": "text",
"value": "B"
}
],
[
{
"type": "text",
"value": "丁"
},
{
"type": "text",
"value": "C"
}
]
],
"type": "table"
},
{
"header": [
"名称",
"分数",
"名称",
"等级"
],
"rows": [
[
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
},
{
"type": "text",
"value": "甲"
},
{
"type": "text",
"value": "B"
}
],
[
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
},
{
"type": "text",
"value": "乙"
},
{
"type": "text",
"value": "A"
}
]
],
"type": "table"
}
],
"type": "list"
}
}
@@ -0,0 +1,45 @@
## 连接
`(左, 右, 左列, 右列)`
按 equality 连接两张表;输出表头是左表头接右表头,原表不变。
**表头 = list.concat(table.columns(左), table.columns(右))**
**结果行 = []**
**左行号 = 0**
- `左行号 < 长度(左)`
**左行 = table.row(左, 左行号)**
**键 = 左行[左列]**
**右行号 = 0**
- `右行号 < 长度(右)`
**右行 = table.row(右, 右行号)**
1. `右行[右列] == 键`
**结果行 = list.append(结果行, list.concat(左行, 右行))**
2. `true`
**继续 = 0**
**右行号 = 右行号 + 1**
**左行号 = 左行号 + 1**
*表(表头, 结果行)*
## main
**左表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
**右表 = 表(["名称", "等级"], [["乙", "A"], ["甲", "B"], ["丁", "C"]])**
*[左表, 右表, 连接(左表, 右表, 0, 0)]*
@@ -0,0 +1,66 @@
{
"args": {},
"function": "main",
"stdout": "",
"value": {
"items": [
{
"items": [
{
"type": "text",
"value": "甲"
},
{
"type": "text",
"value": "乙,丙"
},
{
"type": "text",
"value": "说\"好"
}
],
"type": "list"
},
{
"items": [
{
"type": "text",
"value": "a"
},
{
"type": "text",
"value": ""
},
{
"type": "text",
"value": "c"
}
],
"type": "list"
},
{
"items": [
{
"type": "text",
"value": ""
}
],
"type": "list"
},
{
"items": [
{
"type": "text",
"value": "a\"b"
},
{
"type": "text",
"value": "c"
}
],
"type": "list"
}
],
"type": "list"
}
}
@@ -0,0 +1,66 @@
## 解析
`(文本)`
单行 CSV:逗号分隔、双引号包裹、`""` 表示一个字面双引号。
不处理换行、多行字段或首尾空白。
**点 = text.codepoints(文本)**
**下标 = 0**
**字段 = ""**
**字段表 = []**
**引号内 = false**
- `下标 < 长度(点)`
**字符 = text.from_codepoint(点[下标])**
1. `引号内 and 字符 == "\""`
1. `下标 + 1 < 长度(点) and text.from_codepoint(点[下标 + 1]) == "\""`
**`字段 = 字段 + "\""`**
**下标 = 下标 + 2**
2. `true`
**引号内 = false**
**下标 = 下标 + 1**
2. `引号内`
**字段 = 字段 + 字符**
**下标 = 下标 + 1**
3. `字符 == "\""`
**引号内 = true**
**下标 = 下标 + 1**
4. `字符 == ","`
**字段表 = list.append(字段表, 字段)**
**字段 = ""**
**下标 = 下标 + 1**
5. `true`
**字段 = 字段 + 字符**
**下标 = 下标 + 1**
*list.concat(字段表, [字段])*
## main
*`[解析("甲,\"乙,丙\",\"说\"\"好\""), 解析("a,,c"), 解析("\"\""), 解析("\"a\"\"b\",c")]`*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.index"
]
}
@@ -0,0 +1,5 @@
## main
**内存 = memory.new(1, 1)**
*内存[65536]*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.type"
]
}
@@ -0,0 +1,5 @@
## main
**内存 = memory.new(1, 1)**
**`内存[0] = 256`**
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.type"
]
}
@@ -0,0 +1,8 @@
## main
List values keep their v1 value semantics: index assignment is not defined
for them and must not silently turn a list into shared mutable memory.
**列表 = [1, 2]**
**`列表[0] = 9`**
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.index"
]
}
@@ -0,0 +1,5 @@
## main
**内存 = memory.new(1, 1)**
*内存[999999999999999999999999999999999999999]*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"resource.exhausted"
]
}
@@ -0,0 +1,5 @@
## main
**内存 = memory.new(1, 1)**
*memory.grow(内存, 1)*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.memory"
]
}
@@ -0,0 +1,3 @@
## main
*[memory.new(0 - 1, 1)]*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.type"
]
}
@@ -0,0 +1,3 @@
## main
*list.append({a: 1}, 2)*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.index"
]
}
@@ -0,0 +1,5 @@
## main
**数据 = 表(["a"], [[1]])**
*table.cell(数据, 1, 0)*
@@ -0,0 +1,7 @@
{
"function": "main",
"args": {},
"codes": [
"runtime.type"
]
}
@@ -0,0 +1,5 @@
## main
**数据 = {a: 1}**
*map.get(数据, 1)*