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:
cflmy committed 2026-11-21 12:26:46 +08:00
1 parent 49835424ee
commit e029853e32
375 files changed
+6591 -4790

No files matched your search

+112 -27
View File
@@ -1,7 +1,7 @@
# Marqdo conformance and integration test corpus
This directory holds the P0/P1 conformance corpus required by
`docs/DEVELOPMENT_BLUEPRINT.md` §13/§14. The Rust harnesses live in
RFC-LANG-0002 and `docs/DEVELOPMENT_BLUEPRINT.md` §13/§14. The Rust harnesses live in
`crates/cli/tests/` (they use the workspace libraries directly and the
`marqdo` binary via `CARGO_BIN_EXE_marqdo`); the fixtures are plain files here
so they can be rendered, diffed and reviewed like Markdown documents.
@@ -9,6 +9,20 @@ so they can be rendered, diffed and reviewed like Markdown documents.
Everything here is data. Nothing in this directory modifies the repository, and
no test commits, copies or rewrites files outside a throw-away temp directory.
> **Migration state (RFC-LANG-0002).** The source text in this corpus uses the
> current single syntax. The Rust frontend still implements the pre-RFC mapping,
> so the semantic, negative, property and CLI suites that execute Marqdo source
> are **expected to be red** until the frontend migration lands. `conformance_ast`
> is unaffected by the semantic change (the Markdown profile is unchanged).
> `tests/negative/warnings/01-inferred-signature` pins the optional
> `sem.inferred_signature` lint as enabled; the current frontend does not emit
> it. `sem.no_entry_function` is a `run` entry-selection diagnostic and cannot be
> pinned by `crates/cli/tests/negative.rs`, which only calls `analyze_source` /
> `marqdo_runtime::run`; that coverage needs a CLI integration test. Do not
> regenerate goldens or restore the old mapper to turn these suites green. See
> [`../docs/STATUS.md`](../docs/STATUS.md) and
> [`../docs/migration/README.md`](../docs/migration/README.md).
## Layout
```text
@@ -17,8 +31,8 @@ tests/
│ ├── ast/ 160 × *.mq.md + sibling *.ast.json goldens
│ └── semantics/ 73 × *.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
│ ├── *.mq.md 45 × compile-time hard-error fixtures + *.expected.json
│ ├── warnings/ 2 × warning-model/control fixtures + *.expected.json
│ └── runtime/ 21 × runtime-error fixtures + *.expected.json
└── README.md this file
```
@@ -29,7 +43,7 @@ Rust harnesses (all in `crates/cli/tests/`):
|---|---|
| `conformance_ast.rs` | The frozen `marqdo.md.v1` profile parses each fixture into exactly the checked-in AST (node kinds, attributes, literals, byte spans, order). |
| `conformance_semantics.rs` | Each semantic fixture compiles through `marqdo_frontend`, is assembled into `marqdo_ir::Program` and executed with `marqdo_runtime` + `marqdo_host::StdHost`; returned wire value and program stdout match. |
| `negative.rs` | Invalid executable shapes are **hard errors** (never prose fallbacks), exact sorted diagnostic-code lists; warnings and runtime error codes are asserted in their own suites. |
| `negative.rs` | Current-syntax diagnostics are **hard errors** (never prose fallbacks): exact sorted `sem.*`/`symbol.*`/`call.*`/`frontmatter.*` code lists, the `sem.inferred_signature` warning model with inert-knowledge controls, and runtime error codes — each asserted in its own suite. |
| `properties.rs` | Blueprint §14.2 prose invariance: inserting/removing pure narration paragraphs never changes the executable function IR (modulo source spans) and diagnostics stay error-free. |
| `integration_cli.rs` | Real CLI contract: `init`, `check`, `run`, `call`, `inspect`, `fmt`, multi-module imports/cycles, `lock`/`sync --locked`, plugin inspection and explicitly trusted plugin invocation. |
@@ -63,8 +77,11 @@ The power/control-flow slice is pinned by:
* semantic fixtures 37–44: right-associative power, unary-minus precedence,
parentheses, float negative exponents, Strong+CodeSpan assignment, branch,
loop and inert prose;
* negative fixtures 49–52: missing power/condition operands and executable
nodes swallowed by a condition paragraph;
* negative fixtures 18–23: malformed payload statements (missing condition,
trailing colon, `end if`, empty `return`, multi-statement payload, bad
`params` syntax); fixtures 24–29 pin control-stack pairing, including the
heading boundary that reports `sem.unclosed_control` and `sem.unmatched_end`
independently;
* runtime negatives 11–12: negative integer exponent and exponent budget;
* `crates/marqdo-frontend/src/payload.rs` span tests and
`crates/cli/tests/integration_cli.rs` CLI span/E2E assertions.
@@ -77,8 +94,8 @@ The addressable-memory slice (RFC-CM P1, ADR-0022) is pinned by:
* 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);
* `tests/negative/runtime/15-index-assign-non-memory.mq.md` also guards the
current 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;
@@ -277,8 +294,9 @@ RFC-CM CM-A06 resource streams (ADR-0025) are pinned by:
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
compile-time hard-error fixtures.
corpus shrinks below 100 / 25 fixtures; the warning and runtime suites fail if
their directories are empty; the negative suite fails below 30 compile-time
hard-error fixtures.
## AST goldens and `UPDATE_GOLDENS`
@@ -326,38 +344,103 @@ golden without verifying the value would defeat the purpose of the corpus.
## Negative fixtures
Compile-time (`tests/negative/*.mq.md`):
All three suites use the single current language contract
(`docs/architecture/RFC-LANG-0002-unified-semantics.md`,
`spec/mapping.md`, `spec/expression.ebnf`): `@exec`/`@knowledge` heading
modes, interchangeable `Strong`/`Emph`/inline-`Code` one-statement payloads,
and explicit `if`/`else`/`while`/`end`/`return`/`params`. Ordinary Markdown
lists, block quotes, tables and emphasis are **not** errors: they are inert
containers/knowledge with an empty execution projection. The v1
`markdown.invalid_construct` shape errors ("ordered list must have a
condition", "block quote must be a call", "Strong must not nest Emph", "empty
heading") were removed with the old mapping; `markdown.inert_executable`
warnings no longer exist either.
Compile-time (`tests/negative/*.mq.md`, 45 fixtures):
```json
{"codes": ["markdown.invalid_construct"]}
{"codes": ["sem.unclosed_control"]}
```
The expected list is the exact sorted multiset of **error** diagnostic codes
produced by `marqdo_frontend::analyze_source`. Warnings emitted by the same
document are ignored here. Every listed code is additionally asserted to have
`error` severity, and every fixture must produce at least one error — invalid
executable shapes never fall back to prose.
`error` severity, and every fixture must produce at least one error — an
invalid Exec payload never falls back to prose.
Warnings (`tests/negative/warnings/*.mq.md`):
Coverage by group:
| Group | Fixtures | Codes |
|---|---|---|
| heading modes and unit identity | 01–07, 44–45 | `sem.invalid_mode_marker`, `sem.empty_unit_name`, `sem.duplicate_unit_name`, `sem.exec_without_function` |
| payload extraction | 08–17 | `sem.payload_nested` (nested `Emph`/`Strong`/`Link`/math/strikethrough/line break), `sem.empty_payload` (whitespace-only `Strong`/`Emph`) |
| statement grammar | 18–23 | `sem.syntax` |
| control stack and heading boundary | 24–29 | `sem.unmatched_else`, `sem.duplicate_else`, `sem.unmatched_end`, `sem.unclosed_control` |
| signatures | 30–32 | `sem.signature_position` |
| version selection | 33–34 | `sem.unsupported_version_directive` |
| kept non-`sem` errors | 35–43 | `symbol.unknown`, `call.arity`, `symbol.uninitialized`, `frontmatter.invalid` |
Fixture 29 crosses a heading boundary on purpose: the opener's segment reports
`sem.unclosed_control` and the later `### @exec` segment reports
`sem.unmatched_end`; both are asserted, and neither is "repaired" across the
boundary (`spec/mapping.md` §7).
`sem.no_entry_function` is a `run` entry-selection diagnostic: it is produced
when the CLI cannot find an Exec H2 `main`. `crates/cli/tests/negative.rs` only
calls `marqdo_frontend::analyze_source` and `marqdo_runtime::run`, and the
latter reports an unknown *function* for a missing name; there is no CLI-entry
suite in that harness, so this corpus cannot pin the code without a harness
change. It is recorded here as a known gap rather than replaced by a weaker
assertion (see `spec/references.md` §7 and `spec/mapping.md` §8).
### v1 → current reclassification
| v1 fixtures | v1 expectation | Current disposition |
|---|---|---|
| 01–08 nested `Strong`/`Emph` children | `markdown.invalid_construct` | kept as 08–15 with `sem.payload_nested` |
| 09–12 empty/parse-error carriers | `markdown.invalid_construct`, `expr.parse` | empty carriers kept as 16–17 (`sem.empty_payload`); the unqualified expression-parse cases are replaced by the explicit `sem.syntax` statements in 18–23 |
| 13–24 list/quote shape errors | `markdown.invalid_construct`, `expr.parse` | **removed**: ordinary lists/quotes are valid inert Markdown now; the positive control is `warnings/02-knowledge-and-containers-are-inert` |
| 25–27 link-form calls (external/cross-module/anchor) | `ref.unresolved`, `module.unresolved` | **removed**: link-form calls no longer exist; `ref.unresolved`/`module.unresolved` are now `check --refs`/loader diagnostics outside this harness (`spec/references.md` §7) |
| 28–29 duplicate function/namespace names | `markdown.invalid_construct` | kept as 44–45 with `sem.duplicate_unit_name` (duplicate canonical name, `spec/references.md` §2.3) |
| 30–31 empty function/namespace headings | `markdown.invalid_construct` | replaced by 04–05 with `sem.empty_unit_name` on an actual mode declaration |
| 32–40 unknown/call/signature/frontmatter errors | `symbol.unknown`, `call.arity`, `symbol.uninitialized`, `expr.parse`, `frontmatter.invalid` | kept 35–43 (`frontmatter.*`, `symbol.*`, `call.arity`, `symbol.uninitialized`); malformed-signature cases moved to `sem.syntax`/`sem.signature_position` (23, 30–32) |
| 41–48 frontmatter/link/assignment/power shapes | `frontmatter.invalid`, `expr.parse` | frontmatter cases kept as 40–42; link/quote/assignment shapes removed with the old mapping; power/condition cases replaced by 18–29 |
| 49–52 power/condition operands swallowed by a branch condition | `expr.parse`, `markdown.invalid_construct` | **removed**: executable nodes inside list items are now independent payloads. Replacement negatives are the `sem.syntax` (18–23) and control-stack (24–29) fixtures |
Warnings (`tests/negative/warnings/*.mq.md`, 2 fixtures):
```json
{"severity": "warning", "codes": ["markdown.inert_executable"]}
{"severity": "warning", "codes": ["sem.inferred_signature"]}
```
These documents must compile **without errors**; the sorted multiset of warning
codes must match exactly. `return`/`strong`/lists/block quotes outside a
function body are inert content (warning), not hard errors.
codes must match exactly. Fixture 01 pins the `sem.inferred_signature` lint for
an Exec H2 that relies on free-variable inference. Fixture 02 is a
zero-diagnostic control: it proves that ordinary ordered lists, bullet lists
and block quotes are valid inert Markdown even inside an `@exec` section, and
that an explicit `params(...)` declaration (allowed after prose) suppresses the
inferred-signature warning. The lint is specified as **optional**
(`spec/mapping.md` §6, "may be enabled"); these fixtures pin it enabled, so if
an implementation leaves it off the warning suite — not the hard-error corpus —
is what must be revisited.
Runtime (`tests/negative/runtime/*.mq.md`):
Runtime (`tests/negative/runtime/*.mq.md`, 21 fixtures):
```json
{"function": "main", "args": {}, "codes": ["runtime.division_by_zero"]}
```
These documents compile, then must fail at runtime with the expected stable
error code. Optional keys: `"grants": ["foreign"]` and `"stack_limit": 24`
(used for the recursion-depth fixture so the budget — not the host process
stack — is what fails).
error code. Sources were migrated to the current syntax (explicit `## @exec`
functions, `params(...)` where a function has arguments, payload calls and
explicit `if`/`end`), but the genuine runtime expectations were preserved:
arithmetic type/zero-division, index/field/memory bounds, `call.not_callable`,
`call.arity` through a `ref` function value, non-Bool branch condition,
capability (`permission.denied` for a foreign kernel in pure mode), unsupported
math subset, recursion budget and the addressable-memory/collection errors.
Optional keys: `"grants": ["foreign"]` and `"stack_limit": 24` (used for the
recursion-depth fixture so the budget — not the host process stack — is what
fails).
## CLI integration notes
@@ -374,13 +457,15 @@ stack — is what fails).
## Notes discovered while authoring (all since fixed)
The fixtures initially pinned three observations that were reported and then
fixed in the implementation; the goldens below reflect the fixed behaviour:
Two observations were reported and then fixed in the implementation; the
goldens reflect the fixed behaviour:
* wire floats round-trip exactly (`serde_json/float_roundtrip` is enabled), so
Python `math.pi * 9` keeps `28.274333882308138`;
* the default interpreter stack budget is 128 frames so a deeply recursive
document returns a structured `resource.exhausted` instead of exhausting the
native debug-build stack (`--stack` can raise it deliberately);
* an external-link call form (`> [x](https://example.com)()`) is rejected with
`ref.unresolved`, the same code used for other unresolvable call targets.
native debug-build stack (`--stack` can raise it deliberately).
The old link-form block-quote call (`> [x](https://example.com)()`) no longer
exists: a Markdown link is a static reference, so a call is now an ordinary
expression payload (`spec/references.md` §6.2).
@@ -1,3 +1,3 @@
## main
## @exec main
*1 + 2*
*return 1 + 2*
@@ -1,3 +1,3 @@
## main
## @exec main
*"你好,Marqdo"*
*return "你好,Marqdo"*
@@ -1,3 +1,3 @@
## main
## @exec main
*1.5 * 2.0*
`return 1.5 * 2.0`
@@ -1,3 +1,3 @@
## main
## @exec main
*123456789012345678901234567890 * 10*
`return 123456789012345678901234567890 * 10`
@@ -1,13 +1,15 @@
# 数学
# @knowledge 数学
## 阶乘
## @exec 阶乘
1. `n <= 1`
`if n <= 1`
*1*
`return 1`
2. `n > 1`
`else`
**结果 = n * 阶乘(n=n-1)**
`结果 = n * 阶乘(n=n-1)`
*结果*
`return 结果`
`end`
@@ -1,11 +1,13 @@
## 斐波那契
## @exec 斐波那契
1. `n < 2`
`if n < 2`
*n*
`return n`
2. `n >= 2`
`else`
**结果 = 斐波那契(n=n-1) + 斐波那契(n=n-2)**
`结果 = 斐波那契(n=n-1) + 斐波那契(n=n-2)`
*结果*
`return 结果`
`end`
@@ -1,15 +1,17 @@
## 最大公约数
## @exec 最大公约数
**a = 甲**
`a = 甲`
**b = 乙**
`b = 乙`
- `b > 0`
`while b > 0`
**t = b**
`t = b`
**b = a % b**
`b = a % b`
**a = t**
`a = t`
*a*
`end`
`return a`
@@ -1,13 +1,15 @@
## 求和
## @exec 求和
**i = 1**
`i = 1`
**total = 0**
`total = 0`
- `i <= n`
`while i <= n`
**total = total + i**
`total = total + i`
**i = i + 1**
`i = i + 1`
*total*
`end`
`return total`
@@ -1,19 +1,23 @@
## 嵌套
## @exec 嵌套
**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*
**total = 0**
`end`
*return total*
@@ -1,9 +1,9 @@
## 加法
## @exec 加法
`(a: Int, b: Int)`
`params(a: Int, b: Int)`
*a + b*
`return a + b`
## main
## @exec main
*加法(2, 3)*
`return 加法(2, 3)`
@@ -1,9 +1,9 @@
## 默认
## @exec 默认
`(x: Int, y: Int = 10)`
`params(x: Int, y: Int = 10)`
*x + y*
`return x + y`
## main
## @exec main
*默认(x=2)*
`return 默认(x=2)`
@@ -1,9 +1,9 @@
## 减法
## @exec 减法
`(a: Int, b: Int)`
`params(a: Int, b: Int)`
*a - b*
`return a - b`
## main
## @exec main
*减法(b=3, a=10)*
`return 减法(b=3, a=10)`
@@ -1,11 +1,11 @@
## 加一
## @exec 加一
**n = n + 1**
`n = n + 1`
*n*
`return n`
## main
## @exec main
**f = ref("#加一")**
`f = ref("#加一")`
*f(n=41)*
`return f(n=41)`
@@ -1,9 +1,9 @@
## 加一
## @exec 加一
**n = n + 1**
`n = n + 1`
*n*
`return n`
## main
## @exec main
*ref("#加一")*
`return ref("#加一")`
@@ -1,3 +1,3 @@
## main
## @exec main
*{"长度": 长度("你好"), "文本": 文本(42)}*
`return {"长度": 长度("你好"), "文本": 文本(42)}`
@@ -1,3 +1,3 @@
## main
## @exec main
*整数("40") + 整数(2.0)*
`return 整数("40") + 整数(2.0)`
@@ -1,3 +1,3 @@
## main
## @exec main
*小数("1.5") * 小数("2")*
`return 小数("1.5") * 小数("2")`
@@ -1,3 +1,3 @@
## main
## @exec main
*表(["名称", "数量"], [["苹果", 42], ["梨", 7]])*
`return 表(["名称", "数量"], [["苹果", 42], ["梨", 7]])`
@@ -1,3 +1,3 @@
## main
## @exec main
*{"a": 类型(1), "b": 类型("x"), "c": 类型(None), "d": 类型(1.5)}*
`return {"a": 类型(1), "b": 类型("x"), "c": 类型(None), "d": 类型(1.5)}`
@@ -1,9 +1,9 @@
## 公式
## @knowledge 公式
$$
A = \pi r^2
$$
## main
## @exec main
*math.eval(ref("#公式"), {"r": 2}, target="A")*
`return math.eval(ref("#公式"), {"r": 2}, target="A")`
@@ -1,9 +1,9 @@
## 公式
## @knowledge 公式
$$
A = r + 1
$$
## main
## @exec main
*math.eval(ref("#公式"), {"r": 2}, target="A")*
`return math.eval(ref("#公式"), {"r": 2}, target="A")`
@@ -1,9 +1,9 @@
## main
## @exec main
> 打印("你好")
> **打印("你好")**
> 打印(42)
> **打印(42)**
> 打印(2.5)
> **打印(2.5)**
*None*
`return None`
@@ -1,5 +1,5 @@
## main
## @exec main
**x = 1**
`x = 1`
> 打印(x)
`打印(x)`
@@ -1,3 +1,3 @@
## main
## @exec main
*None*
*return None*
@@ -1,3 +1,3 @@
## main
## @exec main
*(true and false) or (not false)*
`return (true and false) or (not false)`
@@ -1,3 +1,3 @@
## main
## @exec main
*(1 < 1.5) and (2.0 == 2) and not (1 != 1)*
`return (1 < 1.5) and (2.0 == 2) and not (1 != 1)`
@@ -1,3 +1,3 @@
## main
## @exec main
*7 // 2 * 10 + 7 % 2*
`return 7 // 2 * 10 + 7 % 2`
@@ -1,3 +1,3 @@
## main
## @exec main
*{"first": [10, 20, 30][0], "last": [10, 20, 30][-1], "text": "你好"[1], "member": {"a": 42}.a, "key": {"b": 2}["b"]}*
`return {"first": [10, 20, 30][0], "last": [10, 20, 30][-1], "text": "你好"[1], "member": {"a": 42}.a, "key": {"b": 2}["b"]}`
@@ -1,3 +1,3 @@
## main
## @exec main
*{"a": 1, "b": [true, None]}*
`return {"a": 1, "b": [true, None]}`
@@ -1,7 +1,7 @@
## main
## @exec main
> 打印([1, 2, 3])
> **打印([1, 2, 3])**
> 打印({"a": 1})
> **打印({"a": 1})**
*None*
`return None`
@@ -1,3 +1,3 @@
## main
## @exec main
*"你好" + "," + "世界"*
`return "你好" + "," + "世界"`
@@ -1,11 +1,13 @@
## 递归和
## @exec 递归和
1. `n <= 0`
`if n <= 0`
*0*
`return 0`
2. `n > 0`
`else`
**结果 = n + 递归和(n=n-1)**
`结果 = n + 递归和(n=n-1)`
*结果*
`return 结果`
`end`
@@ -1,9 +1,11 @@
## 找平方
## @exec 找平方
**i = 0**
`i = 0`
- `i * i < target`
`while i * i < target`
**i = i + 1**
`i = i + 1`
*i * i*
`end`
`return i * i`
@@ -1,13 +1,23 @@
## 符号
## @exec 符号
1. `n < 0`
`if n < 0`
*-1*
`return -1`
2. `n == 0`
`else`
*0*
`if n == 0`
3. `n > 0`
`return 0`
*1*
`else`
`if n > 0`
`return 1`
`end`
`end`
`end`
@@ -1,9 +1,9 @@
## 混合
## @exec 混合
`(x: Int, y: Int = 5, z: Int = 1)`
`params(x: Int, y: Int = 5, z: Int = 1)`
*x * y + z*
`return x * y + z`
## main
## @exec main
*混合(x=2, z=3)*
`return 混合(x=2, z=3)`
@@ -1,4 +1,4 @@
## Python 校验
## @knowledge Python 校验
```python
import json, math, sys
@@ -9,8 +9,8 @@ area = math.pi * r ** 2
print(json.dumps({"type": "map", "entries": {"area": {"type": "float", "value": area}}}))
```
## main
## @exec main
**结果 = foreign.call(ref("#python-校验"), {"r": 3})**
`结果 = foreign.call(ref("#python-校验"), {"r": 3})`
*结果.area*
`return 结果.area`
@@ -1,3 +1,3 @@
## main
## @exec main
*`2 ** 3 ** 2`*
`return 2 ** 3 ** 2`
@@ -1,3 +1,3 @@
## main
## @exec main
*`-2 ** 2`*
`return -2 ** 2`
@@ -1,3 +1,3 @@
## main
## @exec main
*`(-2) ** 2`*
`return (-2) ** 2`
@@ -1,3 +1,3 @@
## main
## @exec main
*`2.0 ** -1`*
`return 2.0 ** -1`
@@ -1,5 +1,5 @@
## main
## @exec main
**`n = 2 ** 3`**
*n*
*return n*
@@ -1,11 +1,17 @@
## main
## @exec main
**n = 2**
`n = 2`
1. `n ** 2 == 4`,正确分支的自然语言说明。
`if n ** 2 == 4`
> print("乘方正确")
正确分支的自然语言说明。
2. `n ** 2 != 4`,另一分支的说明。
`print("乘方正确")`
> print("乘方错误")
`else`
另一分支的说明。
`print("乘方错误")`
`end`
@@ -1,11 +1,13 @@
## main
## @exec main
**n = 0**
`n = 0`
**limit = 100**
`limit = 100`
- `n ** 2 < limit`
`while n ** 2 < limit`
**n = n + 1**
`n = n + 1`
*n*
`end`
`return n`
@@ -1,11 +1,11 @@
## main
## @exec main
正文里的 `2 ** 3` 不会执行。
正文里的 2 ** 3 不会执行。
```text
2 ** 3
```
**x = 1**
`x = 1`
*x*
`return x`
@@ -3,7 +3,7 @@
"function": "main",
"stdout": "",
"value": {
"type": "int",
"decimal": "141"
"decimal": "141",
"type": "int"
}
}
@@ -1,4 +1,4 @@
## main
## @exec main
This function demonstrates an addressable byte region next to ordinary prose.
A copied name shares the same region identity.
@@ -11,4 +11,4 @@ A copied name shares the same region identity.
**`另一个名字[9] = 99`**
*区域[8] + 区域[9]*
*return 区域[8] + 区域[9]*
@@ -3,12 +3,24 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "0"},
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "9"},
{"type": "int", "decimal": "0"}
]
{
"decimal": "0",
"type": "int"
},
{
"decimal": "1",
"type": "int"
},
{
"decimal": "9",
"type": "int"
},
{
"decimal": "0",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,14 +1,14 @@
## main
## @exec 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.new(0, 2)`
**旧页数 = memory.grow(内存, 1)**
`旧页数 = memory.grow(内存, 1)`
**内存[0] = 9**
`内存[0] = 9`
**页数 = memory.size(内存)**
`页数 = memory.size(内存)`
*[旧页数, 页数, 内存[0], 内存[65535]]*
`return [旧页数, 页数, 内存[0], 内存[65535]]`
@@ -3,13 +3,28 @@
"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}
]
{
"decimal": "5",
"type": "int"
},
{
"decimal": "6",
"type": "int"
},
{
"decimal": "7",
"type": "int"
},
{
"type": "bool",
"value": true
},
{
"type": "bool",
"value": false
}
],
"type": "list"
}
}
@@ -1,4 +1,4 @@
## main
## @exec 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.
@@ -15,4 +15,4 @@ the region identity, and equality compares that identity, not the bytes.
**`别名[1] = 7`**
*[甲[0], 乙[0], 甲[1], 甲 == 别名, 甲 == 乙]*
*return [甲[0], 乙[0], 甲[1], 甲 == 别名, 甲 == 乙]*
@@ -3,13 +3,28 @@
"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"}
]
{
"decimal": "4",
"type": "int"
},
{
"decimal": "12",
"type": "int"
},
{
"decimal": "28",
"type": "int"
},
{
"decimal": "36",
"type": "int"
},
{
"decimal": "255",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,72 +1,74 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
把非负 Int 的低 32 位按大端字节写入区域。
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
同样按大端读取四个字节。
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 分配
## @exec 分配
`(内存, 容量, 大小)`
`params(内存: Memory, 容量: Int, 大小: Int)`
区域头四个字节保存下一个空闲偏移。这是顺序分配器:free 只把块交还给调用方,
不在这里实现复用。
**当前位置 = 读32(内存, 0)**
`当前位置 = 读32(内存, 0)`
1. `当前位置 + 大小 <= 容量`
`if 当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
`写32(内存, 0, 当前位置 + 大小)`
*当前位置*
`return 当前位置`
2. `true`
`else`
*None*
`return None`
## main
`end`
**内存 = memory.new(1, 1)**
## @exec main
**`内存[0] = 0`**
`内存 = memory.new(1, 1)`
**`内存[1] = 0`**
`内存[0] = 0`
**`内存[2] = 0`**
`内存[1] = 0`
**`内存[3] = 4`**
`内存[2] = 0`
**第一个 = 分配(内存, 65536, 8)**
`内存[3] = 4`
**第二个 = 分配(内存, 65536, 16)**
`第一个 = 分配(内存, 65536, 8)`
**第三个 = 分配(内存, 65536, 8)**
`第二个 = 分配(内存, 65536, 16)`
**`内存[第一个] = 255`**
`第三个 = 分配(内存, 65536, 8)`
*[第一个, 第二个, 第三个, 读32(内存, 0), 内存[第一个]]*
`内存[第一个] = 255`
`return [第一个, 第二个, 第三个, 读32(内存, 0), 内存[第一个]]`
@@ -3,12 +3,24 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "12"},
{"type": "int", "decimal": "20"},
{"type": "int", "decimal": "60"}
]
{
"decimal": "4",
"type": "int"
},
{
"decimal": "12",
"type": "int"
},
{
"decimal": "20",
"type": "int"
},
{
"decimal": "60",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,93 +1,97 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 分配
## @exec 分配
`(内存, 容量, 大小)`
`params(内存: Memory, 容量: Int, 大小: Int)`
**当前位置 = 读32(内存, 0)**
`当前位置 = 读32(内存, 0)`
1. `当前位置 + 大小 <= 容量`
`if 当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
`写32(内存, 0, 当前位置 + 大小)`
*当前位置*
`return 当前位置`
2. `true`
`else`
*None*
`return None`
## 节点
`end`
`(内存, 值, 下一个)`
## @exec 节点
`params(内存: Memory, 值: Int, 下一个: Int)`
节点是 8 字节块:偏移 0 存一个字节的值,偏移 1..4 存 next 偏移;0 表示空。
**地址 = 分配(内存, 65536, 8)**
`地址 = 分配(内存, 65536, 8)`
**`内存[地址] = 值`**
`内存[地址] = 值`
> 写32(内存, 地址 + 1, 下一个)
`写32(内存, 地址 + 1, 下一个)`
*地址*
`return 地址`
## 遍历求和
## @exec 遍历求和
`(内存, 头)`
`params(内存: Memory, 头: Int)`
**当前 = 头**
`当前 = 头`
**总和 = 0**
`总和 = 0`
- `当前 != 0`
`while 当前 != 0`
**总和 = 总和 + 内存[当前]**
`总和 = 总和 + 内存[当前]`
**当前 = 读32(内存, 当前 + 1)**
`当前 = 读32(内存, 当前 + 1)`
*总和*
`end`
## main
`return 总和`
**内存 = memory.new(1, 1)**
## @exec main
**`内存[0] = 0`**
`内存 = memory.new(1, 1)`
**`内存[1] = 0`**
`内存[0] = 0`
**`内存[2] = 0`**
`内存[1] = 0`
**`内存[3] = 4`**
`内存[2] = 0`
**甲 = 节点(内存, 10, 0)**
`内存[3] = 4`
**乙 = 节点(内存, 20, 甲)**
`甲 = 节点(内存, 10, 0)`
**丙 = 节点(内存, 30, 乙)**
`乙 = 节点(内存, 20, 甲)`
*[甲, 乙, 丙, 遍历求和(内存, 丙)]*
`丙 = 节点(内存, 30, 乙)`
`return [甲, 乙, 丙, 遍历求和(内存, 丙)]`
@@ -3,12 +3,23 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{"type": "none"},
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "4"},
{"type": "int", "decimal": "65536"}
]
{
"type": "none"
},
{
"decimal": "4",
"type": "int"
},
{
"decimal": "4",
"type": "int"
},
{
"decimal": "65536",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,67 +1,69 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 分配
## @exec 分配
`(内存, 容量, 大小)`
`params(内存: Memory, 容量: Int, 大小: Int)`
**当前位置 = 读32(内存, 0)**
`当前位置 = 读32(内存, 0)`
1. `当前位置 + 大小 <= 容量`
`if 当前位置 + 大小 <= 容量`
> 写32(内存, 0, 当前位置 + 大小)
`写32(内存, 0, 当前位置 + 大小)`
*当前位置*
`return 当前位置`
2. `true`
`else`
*None*
`return None`
## main
`end`
## @exec main
分配失败必须保持区域头不变,之后的分配仍从原位置继续。
**内存 = memory.new(1, 1)**
`内存 = memory.new(1, 1)`
**`内存[0] = 0`**
`内存[0] = 0`
**`内存[1] = 0`**
`内存[1] = 0`
**`内存[2] = 0`**
`内存[2] = 0`
**`内存[3] = 4`**
`内存[3] = 4`
**失败 = 分配(内存, 65536, 65536)**
`失败 = 分配(内存, 65536, 65536)`
**失败后位置 = 读32(内存, 0)**
`失败后位置 = 读32(内存, 0)`
**成功 = 分配(内存, 65536, 65532)**
`成功 = 分配(内存, 65536, 65532)`
**最终位置 = 读32(内存, 0)**
`最终位置 = 读32(内存, 0)`
*[失败, 失败后位置, 成功, 最终位置]*
`return [失败, 失败后位置, 成功, 最终位置]`
@@ -3,43 +3,87 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"}
]
{
"decimal": "1",
"type": "int"
},
{
"decimal": "2",
"type": "int"
}
],
"type": "list"
},
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "3"}
]
{
"decimal": "1",
"type": "int"
},
{
"decimal": "2",
"type": "int"
},
{
"decimal": "3",
"type": "int"
}
],
"type": "list"
},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "2"},
{
"type": "list",
"items": [
{"type": "text", "value": "b"},
{"type": "text", "value": "c"}
]
"decimal": "2",
"type": "int"
},
{
"decimal": "2",
"type": "int"
},
{
"decimal": "2",
"type": "int"
},
{"type": "bool", "value": false},
{"type": "none"},
{
"type": "list",
"items": [
{"type": "int", "decimal": "1"},
{"type": "int", "decimal": "2"},
{"type": "int", "decimal": "3"}
]
{
"type": "text",
"value": "b"
},
{
"type": "text",
"value": "c"
}
],
"type": "list"
},
{
"type": "bool",
"value": false
},
{
"type": "none"
},
{
"items": [
{
"decimal": "1",
"type": "int"
},
{
"decimal": "2",
"type": "int"
},
{
"decimal": "3",
"type": "int"
}
],
"type": "list"
}
]
],
"type": "list"
}
}
@@ -1,18 +1,18 @@
## main
## @exec main
Lists and maps keep value semantics: constructors return new values and the
inputs are unchanged.
**原列表 = [1, 2]**
`原列表 = [1, 2]`
**新列表 = list.append(原列表, 3)**
`新列表 = list.append(原列表, 3)`
**原映射 = {a: 1, b: 2}**
`原映射 = {a: 1, b: 2}`
**新映射 = map.set(原映射, "c", 3)**
`新映射 = map.set(原映射, "c", 3)`
**删除后 = map.remove(新映射, "a")**
`删除后 = map.remove(新映射, "a")`
**键 = map.keys(删除后)**
`键 = map.keys(删除后)`
*[原列表, 新列表, 原映射.b, 新映射.b, 删除后.b, 键, map.has(原映射, "c"), map.get(原映射, "z"), list.concat([1], [2, 3])]*
`return [原列表, 新列表, 原映射.b, 新映射.b, 删除后.b, 键, map.has(原映射, "c"), map.get(原映射, "z"), list.concat([1], [2, 3])]`
@@ -3,20 +3,39 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "int", "decimal": "97"},
{"type": "int", "decimal": "233"},
{"type": "int", "decimal": "20013"}
]
{
"decimal": "97",
"type": "int"
},
{
"decimal": "233",
"type": "int"
},
{
"decimal": "20013",
"type": "int"
}
],
"type": "list"
},
{"type": "text", "value": "中"},
{"type": "none"},
{"type": "none"},
{"type": "int", "decimal": "1"}
]
{
"type": "text",
"value": "中"
},
{
"type": "none"
},
{
"type": "none"
},
{
"decimal": "1",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,7 +1,7 @@
## main
## @exec main
**点 = text.codepoints("aé中")**
`点 = text.codepoints("aé中")`
**回 = text.from_codepoint(20013)**
`回 = text.from_codepoint(20013)`
*[点, 回, text.from_codepoint(55296), text.from_codepoint(1114112), 长度(回)]*
`return [点, 回, text.from_codepoint(55296), text.from_codepoint(1114112), 长度(回)]`
@@ -3,24 +3,42 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "list",
"items": [
{"type": "text", "value": "名称"},
{"type": "text", "value": "分数"}
]
{
"type": "text",
"value": "名称"
},
{
"type": "text",
"value": "分数"
}
],
"type": "list"
},
{
"type": "list",
"items": [
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
]
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
],
"type": "list"
},
{"type": "int", "decimal": "87"},
{"type": "int", "decimal": "2"}
]
{
"decimal": "87",
"type": "int"
},
{
"decimal": "2",
"type": "int"
}
],
"type": "list"
}
}
@@ -1,5 +1,5 @@
## main
## @exec main
**数据 = 表(["名称", "分数"], [["甲", 87], ["乙", 92]])**
`数据 = 表(["名称", "分数"], [["甲", 87], ["乙", 92]])`
*[table.columns(数据), table.row(数据, 1), table.cell(数据, 0, 1), 长度(数据)]*
`return [table.columns(数据), table.row(数据, 1), table.cell(数据, 0, 1), 长度(数据)]`
@@ -3,40 +3,76 @@
"function": "main",
"stdout": "",
"value": {
"type": "list",
"items": [
{
"type": "table",
"header": ["名称", "分数"],
"header": [
"名称",
"分数"
],
"rows": [
[
{"type": "text", "value": "甲"},
{"type": "int", "decimal": "87"}
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
}
],
[
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
],
[
{"type": "text", "value": "丙"},
{"type": "int", "decimal": "71"}
{
"type": "text",
"value": "丙"
},
{
"decimal": "71",
"type": "int"
}
]
]
],
"type": "table"
},
{
"type": "table",
"header": ["名称", "分数"],
"header": [
"名称",
"分数"
],
"rows": [
[
{"type": "text", "value": "甲"},
{"type": "int", "decimal": "87"}
{
"type": "text",
"value": "甲"
},
{
"decimal": "87",
"type": "int"
}
],
[
{"type": "text", "value": "乙"},
{"type": "int", "decimal": "92"}
{
"type": "text",
"value": "乙"
},
{
"decimal": "92",
"type": "int"
}
]
]
],
"type": "table"
}
]
],
"type": "list"
}
}
@@ -1,31 +1,35 @@
## 过滤
## @exec 过滤
`(数据, 阈值)`
`params(数据: Table, 阈值: Int)`
只用 table 观察原语和 list 构造原语重建一个新表;输入表不被修改。
**列 = table.columns(数据)**
`列 = table.columns(数据)`
**结果行 = []**
`结果行 = []`
**行号 = 0**
`行号 = 0`
- `行号 < 长度(数据)`
`while 行号 < 长度(数据)`
**分数 = table.cell(数据, 行号, 1)**
`分数 = table.cell(数据, 行号, 1)`
1. `分数 > 阈值`
`if 分数 > 阈值`
**结果行 = list.append(结果行, table.row(数据, 行号))**
`结果行 = list.append(结果行, table.row(数据, 行号))`
**行号 = 行号 + 1**
`end`
*表(列, 结果行)*
`行号 = 行号 + 1`
## main
`end`
**原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
`return 表(列, 结果行)`
**结果 = 过滤(原表, 80)**
## @exec main
*[原表, 结果]*
`原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])`
`结果 = 过滤(原表, 80)`
`return [原表, 结果]`
@@ -1,338 +1,368 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 成功
## @exec 成功
`(值)`
`params(值)`
*{ok: true, value: 值}*
`return {ok: true, value: 值}`
## 失败
## @exec 失败
`(代码, 消息)`
`params(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
`return {ok: false, code: 代码, message: 消息}`
## 初始化
## @exec 初始化
`(内存)`
`params(内存: Memory)`
区域头:偏移 0 是空闲链表头,偏移 4 是 bump 指针;首个块从 8 开始。
> 写32(内存, 0, 0)
`写32(内存, 0, 0)`
> 写32(内存, 4, 8)
`写32(内存, 4, 8)`
*成功(None)*
`return 成功(None)`
## 查找
## @exec 查找
`(内存, 需求)`
`params(内存: Memory, 需求: Int)`
返回 `[前一个空闲块, 当前空闲块, 当前块大小]`;没有合适块时返回零值。
返回 [前一个空闲块, 当前空闲块, 当前块大小];没有合适块时返回零值。
**前一个 = 0**
`前一个 = 0`
**当前 = 读32(内存, 0)**
`当前 = 读32(内存, 0)`
- `当前 != 0`
`while 当前 != 0`
**块大小 = 读32(内存, 当前)**
`块大小 = 读32(内存, 当前)`
1. `块大小 >= 需求`
`if 块大小 >= 需求`
*[前一个, 当前, 块大小]*
`return [前一个, 当前, 块大小]`
**前一个 = 当前**
`end`
**当前 = 读32(内存, 当前 + 4)**
`前一个 = 当前`
*[0, 0, 0]*
`当前 = 读32(内存, 当前 + 4)`
## 分配
`end`
`(内存, 容量, 请求大小)`
`return [0, 0, 0]`
## @exec 分配
`params(内存: Memory, 容量: Int, 请求大小: Int)`
first-fit:先查空闲链表,必要时切分;否则从 bump 指针处取新块。
块头 8 字节:偏移 0 是总块大小,偏移 4 是空闲链表 next。返回 payload 偏移。
**需求 = (请求大小 + 8 + 7) // 8 * 8**
`需求 = (请求大小 + 8 + 7) // 8 * 8`
**位置 = 查找(内存, 需求)**
`位置 = 查找(内存, 需求)`
**前一个 = 位置[0]**
`前一个 = 位置[0]`
**当前 = 位置[1]**
`当前 = 位置[1]`
**块大小 = 位置[2]**
`块大小 = 位置[2]`
1. `当前 != 0`
`if 当前 != 0`
**余量 = 块大小 - 需求**
`余量 = 块大小 - 需求`
**下一个 = 读32(内存, 当前 + 4)**
`下一个 = 读32(内存, 当前 + 4)`
1. `余量 >= 16`
`if 余量 >= 16`
**余块 = 当前 + 需求**
`余块 = 当前 + 需求`
> 写32(内存, 余块, 余量)
`写32(内存, 余块, 余量)`
> 写32(内存, 余块 + 4, 下一个)
`写32(内存, 余块 + 4, 下一个)`
1. `前一个 == 0`
`if 前一个 == 0`
> 写32(内存, 0, 余块)
`写32(内存, 0, 余块)`
2. `true`
`else`
> 写32(内存, 前一个 + 4, 余块)
`写32(内存, 前一个 + 4, 余块)`
> 写32(内存, 当前, 需求)
`end`
*成功(当前 + 8)*
`写32(内存, 当前, 需求)`
2. `true`
`return 成功(当前 + 8)`
1. `前一个 == 0`
`else`
> 写32(内存, 0, 下一个)
`if 前一个 == 0`
2. `true`
`写32(内存, 0, 下一个)`
> 写32(内存, 前一个 + 4, 下一个)
`else`
*成功(当前 + 8)*
`写32(内存, 前一个 + 4, 下一个)`
2. `true`
`end`
**下一个空位 = 读32(内存, 4)**
`return 成功(当前 + 8)`
1. `下一个空位 + 需求 <= 容量`
`end`
> 写32(内存, 下一个空位, 需求)
`else`
> 写32(内存, 4, 下一个空位 + 需求)
`下一个空位 = 读32(内存, 4)`
*成功(下一个空位 + 8)*
`if 下一个空位 + 需求 <= 容量`
2. `true`
`写32(内存, 下一个空位, 需求)`
*失败("alloc.no_space", "没有足够的空闲空间")*
`写32(内存, 4, 下一个空位 + 需求)`
## 插入空闲
`return 成功(下一个空位 + 8)`
`(内存, 块)`
`else`
`return 失败("alloc.no_space", "没有足够的空闲空间")`
`end`
`end`
## @exec 插入空闲
`params(内存: Memory, 块: Int)`
空闲链表按地址升序;插入后先与后继合并,再与前去合并。
**前一个 = 0**
`前一个 = 0`
**当前 = 读32(内存, 0)**
`当前 = 读32(内存, 0)`
- `当前 != 0 and 当前 < 块`
`while 当前 != 0 and 当前 < 块`
**前一个 = 当前**
`前一个 = 当前`
**当前 = 读32(内存, 当前 + 4)**
`当前 = 读32(内存, 当前 + 4)`
> 写32(内存, 块 + 4, 当前)
`end`
1. `前一个 == 0`
`写32(内存, 块 + 4, 当前)`
> 写32(内存, 0, 块)
`if 前一个 == 0`
2. `true`
`写32(内存, 0, 块)`
> 写32(内存, 前一个 + 4, 块)
`else`
> 合并(内存, 前一个, 块, 当前)
`写32(内存, 前一个 + 4, 块)`
*成功(None)*
`end`
## 合并
`合并(内存, 前一个, 块, 当前)`
`(内存, 前一个, 块, 当前)`
`return 成功(None)`
## @exec 合并
`params(内存: Memory, 前一个: Int, 块: Int, 当前: Int)`
相邻块只有在地址连续时才合并,避免产生虚假的大块。
**块大小 = 读32(内存, 块)**
`块大小 = 读32(内存, 块)`
1. `当前 != 0 and 块 + 块大小 == 当前`
`if 当前 != 0 and 块 + 块大小 == 当前`
**合并大小 = 块大小 + 读32(内存, 当前)**
`合并大小 = 块大小 + 读32(内存, 当前)`
> 写32(内存, 块, 合并大小)
`写32(内存, 块, 合并大小)`
> 写32(内存, 块 + 4, 读32(内存, 当前 + 4))
`写32(内存, 块 + 4, 读32(内存, 当前 + 4))`
2. `true`
`else`
**忽略后继合并 = 0**
`忽略后继合并 = 0`
**前一个大小 = 0**
`end`
1. `前一个 != 0`
`前一个大小 = 0`
**前一个大小 = 读32(内存, 前一个)**
`if 前一个 != 0`
2. `true`
`前一个大小 = 读32(内存, 前一个)`
**忽略前去读取 = 0**
`else`
`忽略前去读取 = 0`
`end`
后继合并完成后再处理前驱;两个判断必须分开执行。
1. `前一个 != 0 and 前一个 + 前一个大小 == 块`
`if 前一个 != 0 and 前一个 + 前一个大小 == 块`
**前一个新大小 = 前一个大小 + 读32(内存, 块)**
`前一个新大小 = 前一个大小 + 读32(内存, 块)`
> 写32(内存, 前一个, 前一个新大小)
`写32(内存, 前一个, 前一个新大小)`
> 写32(内存, 前一个 + 4, 读32(内存, 块 + 4))
`写32(内存, 前一个 + 4, 读32(内存, 块 + 4))`
2. `true`
`else`
**忽略前去合并 = 0**
`忽略前去合并 = 0`
*成功(None)*
`end`
## 已在空闲表
`return 成功(None)`
`(内存, 块)`
## @exec 已在空闲表
`params(内存: Memory, 块: Int)`
线性扫描空闲链表,检测重复释放。
**当前 = 读32(内存, 0)**
`当前 = 读32(内存, 0)`
**结果 = false**
`结果 = false`
- `当前 != 0 and not 结果`
`while 当前 != 0 and not 结果`
1. `当前 == 块`
`if 当前 == 块`
**结果 = true**
`结果 = true`
2. `true`
`else`
**当前 = 读32(内存, 当前 + 4)**
`当前 = 读32(内存, 当前 + 4)`
*结果*
`end`
## 释放
`end`
`(内存, 地址)`
`return 结果`
## @exec 释放
`params(内存: Memory, 地址: Int)`
把 payload 偏移换算回块头;重复释放返回可恢复错误,正常释放按地址插入并由“合并”处理相邻块。
**块 = 地址 - 8**
`块 = 地址 - 8`
1. `已在空闲表(内存, 块)`
`if 已在空闲表(内存, 块)`
*失败("alloc.double_free", "块已在空闲链表")*
`return 失败("alloc.double_free", "块已在空闲链表")`
> 插入空闲(内存, 块)
`end`
*成功(None)*
`插入空闲(内存, 块)`
## main
`return 成功(None)`
**内存 = memory.new(1, 1)**
## @exec main
> 初始化(内存)
`内存 = memory.new(1, 1)`
**甲 = 分配(内存, 65536, 8)**
`初始化(内存)`
**乙 = 分配(内存, 65536, 16)**
`甲 = 分配(内存, 65536, 8)`
**`内存[甲.value] = 11`**
`乙 = 分配(内存, 65536, 16)`
**`内存[乙.value] = 22`**
`内存[甲.value] = 11`
> 释放(内存, 甲.value)
`内存[乙.value] = 22`
**丙 = 分配(内存, 65536, 8)**
`释放(内存, 甲.value)`
**`内存[丙.value] = 33`**
`丙 = 分配(内存, 65536, 8)`
> 释放(内存, 乙.value)
`内存[丙.value] = 33`
> 释放(内存, 丙.value)
`释放(内存, 乙.value)`
**丁 = 分配(内存, 65536, 4)**
`释放(内存, 丙.value)`
**戊 = 分配(内存, 65536, 16)**
`丁 = 分配(内存, 65536, 4)`
**己 = 分配(内存, 65536, 64)**
`戊 = 分配(内存, 65536, 16)`
> 释放(内存, 己.value)
`己 = 分配(内存, 65536, 64)`
**庚 = 分配(内存, 65536, 8)**
`释放(内存, 己.value)`
**辛 = 分配(内存, 65536, 16)**
`庚 = 分配(内存, 65536, 8)`
**壬 = 分配(内存, 65536, 8)**
`辛 = 分配(内存, 65536, 16)`
**癸 = 分配(内存, 65536, 8)**
`壬 = 分配(内存, 65536, 8)`
**`内存[庚.value] = 44`**
`癸 = 分配(内存, 65536, 8)`
**`内存[辛.value] = 55`**
`内存[庚.value] = 44`
**`内存[壬.value] = 66`**
`内存[辛.value] = 55`
**`内存[癸.value] = 77`**
`内存[壬.value] = 66`
**失败结果 = 分配(内存, 65536, 100000)**
`内存[癸.value] = 77`
**内存2 = memory.new(1, 1)**
`失败结果 = 分配(内存, 65536, 100000)`
> 初始化(内存2)
`内存2 = memory.new(1, 1)`
**甲2 = 分配(内存2, 65536, 8)**
`初始化(内存2)`
**乙2 = 分配(内存2, 65536, 8)**
`甲2 = 分配(内存2, 65536, 8)`
**丙2 = 分配(内存2, 65536, 8)**
`乙2 = 分配(内存2, 65536, 8)`
> 分配(内存2, 65536, 65472)
`丙2 = 分配(内存2, 65536, 8)`
> 释放(内存2, 甲2.value)
`分配(内存2, 65536, 65472)`
> 释放(内存2, 乙2.value)
`释放(内存2, 甲2.value)`
> 释放(内存2, 丙2.value)
`释放(内存2, 乙2.value)`
**丁2 = 分配(内存2, 65536, 40)**
`释放(内存2, 丙2.value)`
**戊2 = 分配(内存2, 65536, 8)**
`丁2 = 分配(内存2, 65536, 40)`
**释放一次 = 释放(内存2, 丁2.value)**
`戊2 = 分配(内存2, 65536, 8)`
**重复释放 = 释放(内存2, 丁2.value)**
`释放一次 = 释放(内存2, 丁2.value)`
*[甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸, 失败结果, 甲2, 乙2, 丙2, 丁2, 戊2, 释放一次, 重复释放, 内存[甲.value], 内存[乙.value], 内存[丙.value], 内存[丁.value], 内存[戊.value], 内存[庚.value], 内存[辛.value], 内存[壬.value], 内存[癸.value]]*
`重复释放 = 释放(内存2, 丁2.value)`
`return [甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸, 失败结果, 甲2, 乙2, 丙2, 丁2, 戊2, 释放一次, 重复释放, 内存[甲.value], 内存[乙.value], 内存[丙.value], 内存[丁.value], 内存[戊.value], 内存[庚.value], 内存[辛.value], 内存[壬.value], 内存[癸.value]]`
@@ -1,112 +1,122 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 新节点
## @exec 新节点
`(内存, 值)`
`params(内存: Memory, 值: Int)`
每个节点 16 字节:偏移 0 存一个字节的值,偏移 4 存左子,偏移 8 存右子;0 表示空。
节点值必须落在 0..255。
**位置 = 读32(内存, 0)**
`位置 = 读32(内存, 0)`
**`内存[位置] = 值`**
`内存[位置] = 值`
> 写32(内存, 位置 + 4, 0)
`写32(内存, 位置 + 4, 0)`
> 写32(内存, 位置 + 8, 0)
`写32(内存, 位置 + 8, 0)`
> 写32(内存, 0, 位置 + 16)
`写32(内存, 0, 位置 + 16)`
*位置*
`return 位置`
## 插入
## @exec 插入
`(内存, 根, 值)`
`params(内存: Memory, 根: Int, 值: Int)`
1. `根 == 0`
`if 根 == 0`
*新节点(内存, 值)*
`return 新节点(内存, 值)`
2. `值 < 内存[根]`
`else`
**左 = 插入(内存, 读32(内存, 根 + 4), 值)**
`if 值 < 内存[根]`
> 写32(内存, 根 + 4, 左)
`左 = 插入(内存, 读32(内存, 根 + 4), 值)`
*根*
`写32(内存, 根 + 4, 左)`
3. `true`
`return 根`
**右 = 插入(内存, 读32(内存, 根 + 8), 值)**
`else`
> 写32(内存, 根 + 8, 右)
`右 = 插入(内存, 读32(内存, 根 + 8), 值)`
*根*
`写32(内存, 根 + 8, 右)`
## 遍历
`return 根`
`(内存, 根)`
`end`
`end`
## @exec 遍历
`params(内存: Memory, 根: Int)`
中序遍历,返回从小到大的值列表。
1. `根 == 0`
`if 根 == 0`
*[]*
`return []`
**左 = 遍历(内存, 读32(内存, 根 + 4))**
`end`
**右 = 遍历(内存, 读32(内存, 根 + 8))**
`左 = 遍历(内存, 读32(内存, 根 + 4))`
*list.concat(list.concat(左, [内存[根]]), 右)*
`右 = 遍历(内存, 读32(内存, 根 + 8))`
## main
`return list.concat(list.concat(左, [内存[根]]), 右)`
**内存 = memory.new(1, 1)**
## @exec main
**`内存[0] = 0`**
`内存 = memory.new(1, 1)`
**`内存[1] = 0`**
`内存[0] = 0`
**`内存[2] = 0`**
`内存[1] = 0`
**`内存[3] = 8`**
`内存[2] = 0`
**根 = 0**
`内存[3] = 8`
**值表 = [5, 3, 8, 1, 4, 7, 9]**
`根 = 0`
**下标 = 0**
`值表 = [5, 3, 8, 1, 4, 7, 9]`
- `下标 < 长度(值表)`
`下标 = 0`
**根 = 插入(内存, 根, 值表[下标])**
`while 下标 < 长度(值表)`
**下标 = 下标 + 1**
`根 = 插入(内存, 根, 值表[下标])`
*[根, 遍历(内存, 根), 读32(内存, 根 + 4), 读32(内存, 根 + 8)]*
`下标 = 下标 + 1`
`end`
`return [根, 遍历(内存, 根), 读32(内存, 根 + 4), 读32(内存, 根 + 8)]`
@@ -1,35 +1,39 @@
## 分割
## @exec 分割
`(文本)`
`params(文本: Text)`
这是 CSV 单行拆分的最小实现:只处理逗号分隔,不支持引号或转义;空字段保留。
**结果 = []**
`结果 = []`
**当前 = ""**
`当前 = ""`
**点 = text.codepoints(文本)**
`点 = text.codepoints(文本)`
**下标 = 0**
`下标 = 0`
- `下标 < 长度(点)`
`while 下标 < 长度(点)`
**字符 = text.from_codepoint(点[下标])**
`字符 = text.from_codepoint(点[下标])`
1. `字符 == ","`
`if 字符 == ","`
**结果 = list.append(结果, 当前)**
`结果 = list.append(结果, 当前)`
**当前 = ""**
`当前 = ""`
2. `true`
`else`
**当前 = 当前 + 字符**
`当前 = 当前 + 字符`
**下标 = 下标 + 1**
`end`
*list.concat(结果, [当前])*
`下标 = 下标 + 1`
## main
`end`
*[分割("甲,乙,,丙"), 分割(""), 分割("a,b")]*
`return list.concat(结果, [当前])`
## @exec main
`return [分割("甲,乙,,丙"), 分割(""), 分割("a,b")]`
@@ -1,164 +1,182 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 成功
## @exec 成功
`(值)`
`params(值)`
*{ok: true, value: 值}*
`return {ok: true, value: 值}`
## 失败
## @exec 失败
`(代码, 消息)`
`params(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
`return {ok: false, code: 代码, message: 消息}`
## 初始化
## @exec 初始化
`(内存, 容量)`
`params(内存: Memory, 容量: Int)`
头部 8 字节:0..3 是元素数,4..7 是槽数。槽从偏移 8 开始,每槽 8 字节:
键 u32 + 值 u32。键 0 表示空槽,因此业务键必须从 1 开始。
> 写32(内存, 0, 0)
`写32(内存, 0, 0)`
> 写32(内存, 4, 容量)
`写32(内存, 4, 容量)`
*成功(None)*
`return 成功(None)`
## 插入
## @exec 插入
`(内存, 键, 值)`
`params(内存: Memory, 键: Int, 值: Int)`
线性探测;键已存在时只更新值,不增加元素数。
**容量 = 读32(内存, 4)**
`容量 = 读32(内存, 4)`
**位置 = 键 % 容量**
`位置 = 键 % 容量`
**尝试 = 0**
`尝试 = 0`
**完成 = false**
`完成 = false`
- `尝试 < 容量 and not 完成`
`while 尝试 < 容量 and not 完成`
**槽 = 8 + (位置 + 尝试) % 容量 * 8**
`槽 = 8 + (位置 + 尝试) % 容量 * 8`
**槽键 = 读32(内存, 槽)**
`槽键 = 读32(内存, 槽)`
1. `槽键 == 0`
`if 槽键 == 0`
> 写32(内存, 槽, 键)
`写32(内存, 槽, 键)`
> 写32(内存, 槽 + 4, 值)
`写32(内存, 槽 + 4, 值)`
> 写32(内存, 0, 读32(内存, 0) + 1)
`写32(内存, 0, 读32(内存, 0) + 1)`
**完成 = true**
`完成 = true`
2. `槽键 == 键`
`else`
> 写32(内存, 槽 + 4, 值)
`if 槽键 == 键`
**完成 = true**
`写32(内存, 槽 + 4, 值)`
3. `true`
`完成 = true`
**尝试 = 尝试 + 1**
`else`
1. `完成`
`尝试 = 尝试 + 1`
*成功(None)*
`end`
2. `true`
`end`
*失败("hash.full", "哈希表已满")*
`end`
## 查找
`if 完成`
`(内存, 键)`
`return 成功(None)`
`else`
`return 失败("hash.full", "哈希表已满")`
`end`
## @exec 查找
`params(内存: Memory, 键: Int)`
遇到空槽即停止;找不到返回 None。
**容量 = 读32(内存, 4)**
`容量 = 读32(内存, 4)`
**位置 = 键 % 容量**
`位置 = 键 % 容量`
**尝试 = 0**
`尝试 = 0`
**结果 = None**
`结果 = None`
- `尝试 < 容量`
`while 尝试 < 容量`
**槽 = 8 + (位置 + 尝试) % 容量 * 8**
`槽 = 8 + (位置 + 尝试) % 容量 * 8`
**槽键 = 读32(内存, 槽)**
`槽键 = 读32(内存, 槽)`
1. `槽键 == 键`
`if 槽键 == 键`
**结果 = 读32(内存, 槽 + 4)**
`结果 = 读32(内存, 槽 + 4)`
**尝试 = 容量**
`尝试 = 容量`
2. `槽键 == 0`
`else`
**尝试 = 容量**
`if 槽键 == 0`
3. `true`
`尝试 = 容量`
**尝试 = 尝试 + 1**
`else`
*结果*
`尝试 = 尝试 + 1`
## main
`end`
`end`
`end`
`return 结果`
## @exec main
业务键从 1 开始。37 与 5 在 32 个槽中同余,用来验证线性探测。
**内存 = memory.new(1, 1)**
`内存 = memory.new(1, 1)`
> 初始化(内存, 32)
`初始化(内存, 32)`
**插入5 = 插入(内存, 5, 50)**
`插入5 = 插入(内存, 5, 50)`
> 插入(内存, 37, 370)
`插入(内存, 37, 370)`
> 插入(内存, 12, 120)
`插入(内存, 12, 120)`
> 插入(内存, 5, 55)
`插入(内存, 5, 55)`
**内存2 = memory.new(1, 1)**
`内存2 = memory.new(1, 1)`
> 初始化(内存2, 2)
`初始化(内存2, 2)`
> 插入(内存2, 1, 10)
`插入(内存2, 1, 10)`
> 插入(内存2, 2, 20)
`插入(内存2, 2, 20)`
**表满 = 插入(内存2, 4, 40)**
`表满 = 插入(内存2, 4, 40)`
*[插入5, 查找(内存, 5), 查找(内存, 37), 查找(内存, 12), 查找(内存, 99), 读32(内存, 0), 表满, 查找(内存2, 1), 查找(内存2, 2)]*
`return [插入5, 查找(内存, 5), 查找(内存, 37), 查找(内存, 12), 查找(内存, 99), 读32(内存, 0), 表满, 查找(内存2, 1), 查找(内存2, 2)]`
@@ -1,74 +1,88 @@
## 编码
## @exec 编码
`(文本, 内存, 基址)`
`params(文本: Text, 内存: Memory, 基址: Int)`
把 Unicode 标量序列按 UTF-8 编码写入 `Memory`,返回字节长度。
这是 `.mq.md` 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
把 Unicode 标量序列按 UTF-8 编码写入 Memory,返回字节长度。
这是 .mq.md 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
**点 = text.codepoints(文本)**
`点 = text.codepoints(文本)`
**下标 = 0**
`下标 = 0`
**偏移 = 0**
`偏移 = 0`
- `下标 < 长度(点)`
`while 下标 < 长度(点)`
**码点 = 点[下标]**
`码点 = 点[下标]`
1. `码点 < 128`
`if 码点 < 128`
**`内存[基址 + 偏移] = 码点`**
`内存[基址 + 偏移] = 码点`
**偏移 = 偏移 + 1**
`偏移 = 偏移 + 1`
2. `码点 < 2048`
`else`
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
`if 码点 < 2048`
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
`内存[基址 + 偏移] = 192 + 码点 // 64`
**偏移 = 偏移 + 2**
`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`
3. `码点 < 65536`
`偏移 = 偏移 + 2`
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
`else`
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
`if 码点 < 65536`
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
`内存[基址 + 偏移] = 224 + 码点 // 4096`
**偏移 = 偏移 + 3**
`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`
4. `true`
`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
`偏移 = 偏移 + 3`
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
`else`
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
`内存[基址 + 偏移] = 240 + 码点 // 262144`
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`
**偏移 = 偏移 + 4**
`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`
**下标 = 下标 + 1**
`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`
*偏移*
`偏移 = 偏移 + 4`
## main
`end`
**内存 = memory.new(1, 1)**
`end`
**长度 = 编码("Aé中𝄞", 内存, 0)**
`end`
**字节 = []**
`下标 = 下标 + 1`
**下标 = 0**
`end`
- `下标 < 长度`
`return 偏移`
**字节 = list.append(字节, 内存[下标])**
## @exec main
**下标 = 下标 + 1**
`内存 = memory.new(1, 1)`
*[长度, 字节]*
`长度 = 编码("Aé中𝄞", 内存, 0)`
`字节 = []`
`下标 = 0`
`while 下标 < 长度`
`字节 = list.append(字节, 内存[下标])`
`下标 = 下标 + 1`
`end`
`return [长度, 字节]`
+135 -93
View File
@@ -1,185 +1,227 @@
## 成功
## @exec 成功
`(值)`
`params(值)`
*{ok: true, value: 值}*
`return {ok: true, value: 值}`
## 失败
## @exec 失败
`(代码, 消息)`
`params(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
`return {ok: false, code: 代码, message: 消息}`
## 续字节
## @exec 续字节
`(字节)`
`params(字节: Int)`
*字节 >= 128 and 字节 < 192*
`return 字节 >= 128 and 字节 < 192`
## 解码2
## @exec 解码2
`(内存, 基址, 长度, 偏移, 文本)`
`params(内存: Memory, 基址: Int, 长度: Int, 偏移: Int, 文本: Text)`
1. `偏移 + 1 >= 长度`
`if 偏移 + 1 >= 长度`
*[文本, 偏移, "utf8.truncated"]*
`return [文本, 偏移, "utf8.truncated"]`
**b1 = 内存[基址 + 偏移 + 1]**
`end`
1. `not 续字节(b1)`
`b1 = 内存[基址 + 偏移 + 1]`
*[文本, 偏移, "utf8.invalid_continuation"]*
`if not 续字节(b1)`
**码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)**
`return [文本, 偏移, "utf8.invalid_continuation"]`
1. `码点 < 128`
`end`
*[文本, 偏移, "utf8.invalid_scalar"]*
`码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)`
*[文本 + text.from_codepoint(码点), 偏移 + 2, ""]*
`if 码点 < 128`
## 解码3
`return [文本, 偏移, "utf8.invalid_scalar"]`
`(内存, 基址, 长度, 偏移, 文本)`
`end`
1. `偏移 + 2 >= 长度`
`return [文本 + text.from_codepoint(码点), 偏移 + 2, ""]`
*[文本, 偏移, "utf8.truncated"]*
## @exec 解码3
**b1 = 内存[基址 + 偏移 + 1]**
`params(内存: Memory, 基址: Int, 长度: Int, 偏移: Int, 文本: Text)`
**b2 = 内存[基址 + 偏移 + 2]**
`if 偏移 + 2 >= 长度`
1. `not (续字节(b1) and 续字节(b2))`
`return [文本, 偏移, "utf8.truncated"]`
*[文本, 偏移, "utf8.invalid_continuation"]*
`end`
**码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)**
`b1 = 内存[基址 + 偏移 + 1]`
1. `码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
`b2 = 内存[基址 + 偏移 + 2]`
*[文本, 偏移, "utf8.invalid_scalar"]*
`if not (续字节(b1) and 续字节(b2))`
*[文本 + text.from_codepoint(码点), 偏移 + 3, ""]*
`return [文本, 偏移, "utf8.invalid_continuation"]`
## 解码4
`end`
`(内存, 基址, 长度, 偏移, 文本)`
`码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)`
1. `偏移 + 3 >= 长度`
`if 码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
*[文本, 偏移, "utf8.truncated"]*
`return [文本, 偏移, "utf8.invalid_scalar"]`
**b1 = 内存[基址 + 偏移 + 1]**
`end`
**b2 = 内存[基址 + 偏移 + 2]**
`return [文本 + text.from_codepoint(码点), 偏移 + 3, ""]`
**b3 = 内存[基址 + 偏移 + 3]**
## @exec 解码4
1. `not (续字节(b1) and 续字节(b2) and 续字节(b3))`
`params(内存: Memory, 基址: Int, 长度: Int, 偏移: Int, 文本: Text)`
*[文本, 偏移, "utf8.invalid_continuation"]*
`if 偏移 + 3 >= 长度`
**码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)**
`return [文本, 偏移, "utf8.truncated"]`
1. `码点 < 65536 or 码点 > 1114111`
`end`
*[文本, 偏移, "utf8.invalid_scalar"]*
`b1 = 内存[基址 + 偏移 + 1]`
*[文本 + text.from_codepoint(码点), 偏移 + 4, ""]*
`b2 = 内存[基址 + 偏移 + 2]`
## 解码
`b3 = 内存[基址 + 偏移 + 3]`
`(内存, 基址, 长度)`
`if not (续字节(b1) and 续字节(b2) and 续字节(b3))`
**偏移 = 0**
`return [文本, 偏移, "utf8.invalid_continuation"]`
**文本 = ""**
`end`
**错误 = ""**
`码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)`
- `偏移 < 长度 and 错误 == ""`
`if 码点 < 65536 or 码点 > 1114111`
**首 = 内存[基址 + 偏移]**
`return [文本, 偏移, "utf8.invalid_scalar"]`
1. `首 < 128`
`end`
**文本 = 文本 + text.from_codepoint(首)**
`return [文本 + text.from_codepoint(码点), 偏移 + 4, ""]`
**偏移 = 偏移 + 1**
## @exec 解码
2. `首 < 194`
`params(内存: Memory, 基址: Int, 长度: Int)`
**错误 = "utf8.invalid_lead"**
`偏移 = 0`
3. `首 < 224`
`文本 = ""`
**结果 = 解码2(内存, 基址, 长度, 偏移, 文本)**
`错误 = ""`
**文本 = 结果[0]**
`while 偏移 < 长度 and 错误 == ""`
**偏移 = 结果[1]**
`首 = 内存[基址 + 偏移]`
**错误 = 结果[2]**
`if 首 < 128`
4. `首 < 240`
`文本 = 文本 + text.from_codepoint(首)`
**结果 = 解码3(内存, 基址, 长度, 偏移, 文本)**
`偏移 = 偏移 + 1`
**文本 = 结果[0]**
`else`
**偏移 = 结果[1]**
`if 首 < 194`
**错误 = 结果[2]**
`错误 = "utf8.invalid_lead"`
5. `首 < 245`
`else`
**结果 = 解码4(内存, 基址, 长度, 偏移, 文本)**
`if 首 < 224`
**文本 = 结果[0]**
`结果 = 解码2(内存, 基址, 长度, 偏移, 文本)`
**偏移 = 结果[1]**
`文本 = 结果[0]`
**错误 = 结果[2]**
`偏移 = 结果[1]`
6. `true`
`错误 = 结果[2]`
**错误 = "utf8.invalid_lead"**
`else`
1. `错误 == ""`
`if 首 < 240`
*成功(文本)*
`结果 = 解码3(内存, 基址, 长度, 偏移, 文本)`
2. `true`
`文本 = 结果[0]`
*失败(错误, "UTF-8 解码失败")*
`偏移 = 结果[1]`
## 写字节
`错误 = 结果[2]`
`(内存, 基址, 字节)`
`else`
**下标 = 0**
`if 首 < 245`
- `下标 < 长度(字节)`
`结果 = 解码4(内存, 基址, 长度, 偏移, 文本)`
**`内存[基址 + 下标] = 字节[下标]`**
`文本 = 结果[0]`
**下标 = 下标 + 1**
`偏移 = 结果[1]`
*None*
`错误 = 结果[2]`
## main
`else`
**内存 = memory.new(1, 1)**
`错误 = "utf8.invalid_lead"`
> 写字节(内存, 0, [65, 195, 169, 228, 184, 173])
`end`
> 写字节(内存, 16, [228, 184])
`end`
> 写字节(内存, 32, [228, 65, 173])
`end`
> 写字节(内存, 48, [237, 160, 128])
`end`
> 写字节(内存, 64, [240, 157, 132, 158])
`end`
*[解码(内存, 0, 6), 解码(内存, 16, 2), 解码(内存, 32, 3), 解码(内存, 48, 3), 解码(内存, 64, 4)]*
`end`
`if 错误 == ""`
`return 成功(文本)`
`else`
`return 失败(错误, "UTF-8 解码失败")`
`end`
## @exec 写字节
`params(内存: Memory, 基址: Int, 字节: List)`
`下标 = 0`
`while 下标 < 长度(字节)`
`内存[基址 + 下标] = 字节[下标]`
`下标 = 下标 + 1`
`end`
`return None`
## @exec 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])`
`return [解码(内存, 0, 6), 解码(内存, 16, 2), 解码(内存, 32, 3), 解码(内存, 48, 3), 解码(内存, 64, 4)]`
@@ -1,113 +1,115 @@
## 写32
## @exec 写32
`(内存, 基址, 值)`
`params(内存: Memory, 基址: Int, 值: Int)`
**字节3 = 值 % 256**
`字节3 = 值 % 256`
**字节2 = 值 // 256 % 256**
`字节2 = 值 // 256 % 256`
**字节1 = 值 // 65536 % 256**
`字节1 = 值 // 65536 % 256`
**字节0 = 值 // 16777216 % 256**
`字节0 = 值 // 16777216 % 256`
**`内存[基址] = 字节0`**
`内存[基址] = 字节0`
**`内存[基址 + 1] = 字节1`**
`内存[基址 + 1] = 字节1`
**`内存[基址 + 2] = 字节2`**
`内存[基址 + 2] = 字节2`
**`内存[基址 + 3] = 字节3`**
`内存[基址 + 3] = 字节3`
*None*
`return None`
## 读32
## @exec 读32
`(内存, 基址)`
`params(内存: Memory, 基址: Int)`
*内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]*
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## 分配
## @exec 分配
`(内存, 大小)`
`params(内存: Memory, 大小: Int)`
简单 bump 分配;调用方保证容量足够。
**位置 = 读32(内存, 0)**
`位置 = 读32(内存, 0)`
> 写32(内存, 0, 位置 + 大小)
`写32(内存, 0, 位置 + 大小)`
*位置*
`return 位置`
## 新节点
## @exec 新节点
`(内存, 值)`
`params(内存: Memory, 值: Int)`
节点 16 字节:偏移 0 存一个字节的值,偏移 4 存边链表头;0 表示空。
**位置 = 分配(内存, 16)**
`位置 = 分配(内存, 16)`
**`内存[位置] = 值`**
`内存[位置] = 值`
> 写32(内存, 位置 + 4, 0)
`写32(内存, 位置 + 4, 0)`
*位置*
`return 位置`
## 加边
## @exec 加边
`(内存, 源, 目标)`
`params(内存: Memory, 源: Int, 目标: Int)`
边 8 字节:目标 u32 + 下一条边 u32。头插到源节点的边链表。
**边 = 分配(内存, 8)**
`边 = 分配(内存, 8)`
> 写32(内存, 边, 目标)
`写32(内存, 边, 目标)`
> 写32(内存, 边 + 4, 读32(内存, 源 + 4))
`写32(内存, 边 + 4, 读32(内存, 源 + 4))`
> 写32(内存, 源 + 4, 边)
`写32(内存, 源 + 4, 边)`
*None*
`return None`
## 邻接
## @exec 邻接
`(内存, 节点)`
`params(内存: Memory, 节点: Int)`
返回节点值列表;顺序是头插顺序(后加的边在前)。
**当前 = 读32(内存, 节点 + 4)**
`当前 = 读32(内存, 节点 + 4)`
**结果 = []**
`结果 = []`
- `当前 != 0`
`while 当前 != 0`
**结果 = list.append(结果, 读32(内存, 当前))**
`结果 = list.append(结果, 读32(内存, 当前))`
**当前 = 读32(内存, 当前 + 4)**
`当前 = 读32(内存, 当前 + 4)`
*结果*
`end`
## main
`return 结果`
**内存 = memory.new(1, 1)**
## @exec main
**`内存[0] = 0`**
`内存 = memory.new(1, 1)`
**`内存[1] = 0`**
`内存[0] = 0`
**`内存[2] = 0`**
`内存[1] = 0`
**`内存[3] = 8`**
`内存[2] = 0`
**甲 = 新节点(内存, 1)**
`内存[3] = 8`
**乙 = 新节点(内存, 2)**
`甲 = 新节点(内存, 1)`
**丙 = 新节点(内存, 3)**
`乙 = 新节点(内存, 2)`
> 加边(内存, 甲, 乙)
`丙 = 新节点(内存, 3)`
> 加边(内存, 甲, 丙)
`加边(内存, 甲, 乙)`
> 加边(内存, 乙, 丙)
`加边(内存, 甲, 丙)`
*[甲, 乙, 丙, 邻接(内存, 甲), 邻接(内存, 乙), 邻接(内存, 丙)]*
`加边(内存, 乙, 丙)`
`return [甲, 乙, 丙, 邻接(内存, 甲), 邻接(内存, 乙), 邻接(内存, 丙)]`
File diff suppressed because it is too large. Load diff
@@ -1,130 +1,164 @@
## 成功
## @exec 成功
`(值)`
`params(值)`
*{ok: true, value: 值}*
`return {ok: true, value: 值}`
## 失败
## @exec 失败
`(代码, 消息)`
`params(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
`return {ok: false, code: 代码, message: 消息}`
## 写字节
## @exec 写字节
`(内存, 基址, 字节)`
`params(内存: Memory, 基址: Int, 字节: List)`
**下标 = 0**
`下标 = 0`
- `下标 < 长度(字节)`
`while 下标 < 长度(字节)`
**`内存[基址 + 下标] = 字节[下标]`**
`内存[基址 + 下标] = 字节[下标]`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
*None*
`end`
## 运行
`return None`
`(内存, 程序长度, 栈基址)`
## @exec 运行
程序从偏移 0 开始,栈从 `栈基址` 开始。指令集:
`params(内存: Memory, 程序长度: Int, 栈基址: Int)`
程序从偏移 0 开始,栈从 栈基址 开始。指令集:
1 = PUSH 立即数,2 = ADD,3 = MUL,5 = JMP,6 = JZ,7 = HALT。
**指令指针 = 0**
`指令指针 = 0`
**栈指针 = 0**
`栈指针 = 0`
**运行中 = true**
`运行中 = true`
**错误 = ""**
`错误 = ""`
- `指令指针 < 程序长度 and 运行中 and 错误 == ""`
`while 指令指针 < 程序长度 and 运行中 and 错误 == ""`
**操作码 = 内存[指令指针]**
`操作码 = 内存[指令指针]`
1. `操作码 == 1`
`if 操作码 == 1`
**`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`**
`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`
**栈指针 = 栈指针 + 1**
`栈指针 = 栈指针 + 1`
**指令指针 = 指令指针 + 2**
`指令指针 = 指令指针 + 2`
2. `操作码 == 2`
`else`
**右 = 内存[栈基址 + 栈指针 - 1]**
`if 操作码 == 2`
**左 = 内存[栈基址 + 栈指针 - 2]**
`右 = 内存[栈基址 + 栈指针 - 1]`
**`内存[栈基址 + 栈指针 - 2] = 左 + 右`**
`左 = 内存[栈基址 + 栈指针 - 2]`
**栈指针 = 栈指针 - 1**
`内存[栈基址 + 栈指针 - 2] = 左 + 右`
**指令指针 = 指令指针 + 1**
`栈指针 = 栈指针 - 1`
3. `操作码 == 3`
`指令指针 = 指令指针 + 1`
**右 = 内存[栈基址 + 栈指针 - 1]**
`else`
**左 = 内存[栈基址 + 栈指针 - 2]**
`if 操作码 == 3`
**`内存[栈基址 + 栈指针 - 2] = 左 * 右`**
`右 = 内存[栈基址 + 栈指针 - 1]`
**栈指针 = 栈指针 - 1**
`左 = 内存[栈基址 + 栈指针 - 2]`
**指令指针 = 指令指针 + 1**
`内存[栈基址 + 栈指针 - 2] = 左 * 右`
4. `操作码 == 5`
`栈指针 = 栈指针 - 1`
**指令指针 = 内存[指令指针 + 1]**
`指令指针 = 指令指针 + 1`
5. `操作码 == 6`
`else`
**条件 = 内存[栈基址 + 栈指针 - 1]**
`if 操作码 == 5`
**栈指针 = 栈指针 - 1**
`指令指针 = 内存[指令指针 + 1]`
1. `条件 == 0`
`else`
**指令指针 = 内存[指令指针 + 1]**
`if 操作码 == 6`
2. `true`
`条件 = 内存[栈基址 + 栈指针 - 1]`
**指令指针 = 指令指针 + 2**
`栈指针 = 栈指针 - 1`
6. `操作码 == 7`
`if 条件 == 0`
**运行中 = false**
`指令指针 = 内存[指令指针 + 1]`
7. `true`
`else`
**错误 = "vm.invalid_opcode"**
`指令指针 = 指令指针 + 2`
1. `错误 != ""`
`end`
*失败("vm.invalid_opcode", "未知指令")*
`else`
1. `栈指针 == 0`
`if 操作码 == 7`
*失败("vm.empty_stack", "没有结果")*
`运行中 = false`
*成功(内存[栈基址 + 栈指针 - 1])*
`else`
## main
`错误 = "vm.invalid_opcode"`
**内存 = memory.new(1, 1)**
`end`
第一段程序计算 `(5 + 7) * 3`。
`end`
> 写字节(内存, 0, [1, 5, 1, 7, 2, 1, 3, 3, 7])
`end`
**算术结果 = 运行(内存, 9, 1024)**
`end`
`end`
`end`
`end`
`if 错误 != ""`
`return 失败("vm.invalid_opcode", "未知指令")`
`else`
`if 栈指针 == 0`
`return 失败("vm.empty_stack", "没有结果")`
`end`
`end`
`return 成功(内存[栈基址 + 栈指针 - 1])`
## @exec 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])
`写字节(内存, 0, [1, 0, 6, 8, 1, 99, 7, 0, 1, 42, 7])`
**分支结果 = 运行(内存, 11, 2048)**
`分支结果 = 运行(内存, 11, 2048)`
*[算术结果, 分支结果]*
`return [算术结果, 分支结果]`
@@ -1,108 +1,118 @@
## 写大端
## @exec 写大端
`(内存, 基址, 值, 宽度)`
`params(内存: Memory, 基址: Int, 值: Int, 宽度: Int)`
从最低字节开始写,放到 `基址 + 宽度 - 1 - 下标`,因此最终是大端序。
调用方保证 `值` 能放进 `宽度` 字节。
从最低字节开始写,放到 基址 + 宽度 - 1 - 下标,因此最终是大端序。
调用方保证 值 能放进 宽度 字节。
**当前 = 值**
`当前 = 值`
**下标 = 0**
`下标 = 0`
- `下标 < 宽度`
`while 下标 < 宽度`
**`内存[基址 + 宽度 - 1 - 下标] = 当前 % 256`**
`内存[基址 + 宽度 - 1 - 下标] = 当前 % 256`
**当前 = 当前 // 256**
`当前 = 当前 // 256`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
*None*
`end`
## 读大端
`return None`
`(内存, 基址, 宽度)`
## @exec 读大端
**值 = 0**
`params(内存: Memory, 基址: Int, 宽度: Int)`
**下标 = 0**
`值 = 0`
- `下标 < 宽度`
`下标 = 0`
**值 = 值 * 256 + 内存[基址 + 下标]**
`while 下标 < 宽度`
**下标 = 下标 + 1**
`值 = 值 * 256 + 内存[基址 + 下标]`
*值*
`下标 = 下标 + 1`
## 写变长
`end`
`(内存, 基址, 值)`
`return 值`
## @exec 写变长
`params(内存: Memory, 基址: Int, 值: Int)`
LEB128:每字节低 7 位,最高位表示是否还有后续;返回写入字节数。
**当前 = 值**
`当前 = 值`
**偏移 = 0**
`偏移 = 0`
- `当前 >= 128`
`while 当前 >= 128`
**`内存[基址 + 偏移] = 当前 % 128 + 128`**
`内存[基址 + 偏移] = 当前 % 128 + 128`
**当前 = 当前 // 128**
`当前 = 当前 // 128`
**偏移 = 偏移 + 1**
`偏移 = 偏移 + 1`
**`内存[基址 + 偏移] = 当前`**
`end`
*偏移 + 1*
`内存[基址 + 偏移] = 当前`
## 读变长
`return 偏移 + 1`
`(内存, 基址)`
## @exec 读变长
返回 `[值, 消耗字节数]`。
`params(内存: Memory, 基址: Int)`
**值 = 0**
返回 [值, 消耗字节数]。
**倍数 = 1**
`值 = 0`
**偏移 = 0**
`倍数 = 1`
**继续 = true**
`偏移 = 0`
- `继续`
`继续 = true`
**字节 = 内存[基址 + 偏移]**
`while 继续`
**值 = 值 + 字节 % 128 * 倍数**
`字节 = 内存[基址 + 偏移]`
1. `字节 < 128`
`值 = 值 + 字节 % 128 * 倍数`
**继续 = false**
`if 字节 < 128`
2. `true`
`继续 = false`
**倍数 = 倍数 * 128**
`else`
**偏移 = 偏移 + 1**
`倍数 = 倍数 * 128`
*[值, 偏移 + 1]*
`偏移 = 偏移 + 1`
## main
`end`
**内存 = memory.new(1, 1)**
`end`
> 写大端(内存, 0, 4660, 2)
`return [值, 偏移 + 1]`
> 写大端(内存, 16, 305419896, 4)
## @exec main
> 写大端(内存, 32, 1311768467463790320, 8)
`内存 = memory.new(1, 1)`
> 写大端(内存, 48, 18446744073709551615, 8)
`写大端(内存, 0, 4660, 2)`
**变长长度 = 写变长(内存, 128, 123456789)**
`写大端(内存, 16, 305419896, 4)`
**变长 = 读变长(内存, 128)**
`写大端(内存, 32, 1311768467463790320, 8)`
*[读大端(内存, 0, 2), 读大端(内存, 16, 4), 读大端(内存, 32, 8), 读大端(内存, 48, 8), 变长长度, 变长[0], 变长[1], 内存[48], 内存[55]]*
`写大端(内存, 48, 18446744073709551615, 8)`
`变长长度 = 写变长(内存, 128, 123456789)`
`变长 = 读变长(内存, 128)`
`return [读大端(内存, 0, 2), 读大端(内存, 16, 4), 读大端(内存, 32, 8), 读大端(内存, 48, 8), 变长长度, 变长[0], 变长[1], 内存[48], 内存[55]]`
@@ -1,69 +1,77 @@
## 插入有序
## @exec 插入有序
`(已排序, 行, 列)`
`params(已排序: List, 行: List, 列: Int)`
把一行插入到已按 `列` 升序排列的行列表中;返回新列表,不修改输入。
把一行插入到已按 列 升序排列的行列表中;返回新列表,不修改输入。
**新列表 = []**
`新列表 = []`
**已插入 = false**
`已插入 = false`
**下标 = 0**
`下标 = 0`
- `下标 < 长度(已排序)`
`while 下标 < 长度(已排序)`
**当前行 = 已排序[下标]**
`当前行 = 已排序[下标]`
1. `not 已插入 and 当前行[列] > 行[列]`
`if not 已插入 and 当前行[列] > 行[列]`
**新列表 = list.append(新列表, 行)**
`新列表 = list.append(新列表, 行)`
**新列表 = list.append(新列表, 当前行)**
`新列表 = list.append(新列表, 当前行)`
**已插入 = true**
`已插入 = true`
2. `true`
`else`
**新列表 = list.append(新列表, 当前行)**
`新列表 = list.append(新列表, 当前行)`
**下标 = 下标 + 1**
`end`
1. `not 已插入`
`下标 = 下标 + 1`
**新列表 = list.append(新列表, 行)**
`end`
2. `true`
`if not 已插入`
**忽略 = 0**
`新列表 = list.append(新列表, 行)`
*新列表*
`else`
## 排序
`忽略 = 0`
`(数据, 列)`
`end`
`return 新列表`
## @exec 排序
`params(数据: Table, 列: Int)`
按列号升序排序;输入表不变,输出新表。
**列名 = table.columns(数据)**
`列名 = table.columns(数据)`
**行数 = 长度(数据)**
`行数 = 长度(数据)`
**已排序 = []**
`已排序 = []`
**下标 = 0**
`下标 = 0`
- `下标 < 行数`
`while 下标 < 行数`
**已排序 = 插入有序(已排序, table.row(数据, 下标), 列)**
`已排序 = 插入有序(已排序, table.row(数据, 下标), 列)`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
*表(列名, 已排序)*
`end`
## main
`return 表(列名, 已排序)`
**原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
## @exec main
**升序 = 排序(原表, 1)**
`原表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])`
*[原表, 升序]*
`升序 = 排序(原表, 1)`
`return [原表, 升序]`
@@ -1,69 +1,81 @@
## 编码
## @exec 编码
`(文本, 内存, 基址)`
`params(文本: Text, 内存: Memory, 基址: Int)`
**点 = text.codepoints(文本)**
`点 = text.codepoints(文本)`
**下标 = 0**
`下标 = 0`
**偏移 = 0**
`偏移 = 0`
- `下标 < 长度(点)`
`while 下标 < 长度(点)`
**码点 = 点[下标]**
`码点 = 点[下标]`
1. `码点 < 128`
`if 码点 < 128`
**`内存[基址 + 偏移] = 码点`**
`内存[基址 + 偏移] = 码点`
**偏移 = 偏移 + 1**
`偏移 = 偏移 + 1`
2. `码点 < 2048`
`else`
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
`if 码点 < 2048`
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
`内存[基址 + 偏移] = 192 + 码点 // 64`
**偏移 = 偏移 + 2**
`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`
3. `码点 < 65536`
`偏移 = 偏移 + 2`
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
`else`
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
`if 码点 < 65536`
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
`内存[基址 + 偏移] = 224 + 码点 // 4096`
**偏移 = 偏移 + 3**
`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`
4. `true`
`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
`偏移 = 偏移 + 3`
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
`else`
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
`内存[基址 + 偏移] = 240 + 码点 // 262144`
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`
**偏移 = 偏移 + 4**
`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`
**下标 = 下标 + 1**
`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`
*偏移*
`偏移 = 偏移 + 4`
## main
`end`
**内存 = memory.new(1, 1)**
`end`
**长度 = 编码("你好,资源!\n", 内存, 0)**
`end`
**标准输出 = resource.open("stdio", "stdout", ["write"])**
`下标 = 下标 + 1`
**写入 = resource.write(标准输出, 内存, 0, 长度)**
`end`
**关闭 = resource.close(标准输出)**
`return 偏移`
**再次读取 = resource.read(标准输出, 内存, 0, 1)**
## @exec main
*[类型(标准输出), 写入, 关闭, 再次读取]*
`内存 = memory.new(1, 1)`
`长度 = 编码("你好,资源!\n", 内存, 0)`
`标准输出 = resource.open("stdio", "stdout", ["write"])`
`写入 = resource.write(标准输出, 内存, 0, 长度)`
`关闭 = resource.close(标准输出)`
`再次读取 = resource.read(标准输出, 内存, 0, 1)`
`return [类型(标准输出), 写入, 关闭, 再次读取]`
@@ -1,45 +1,51 @@
## 连接
## @exec 连接
`(左, 右, 左列, 右列)`
`params(左: Table, 右: Table, 左列: Int, 右列: Int)`
按 equality 连接两张表;输出表头是左表头接右表头,原表不变。
**表头 = list.concat(table.columns(左), table.columns(右))**
`表头 = list.concat(table.columns(左), table.columns(右))`
**结果行 = []**
`结果行 = []`
**左行号 = 0**
`左行号 = 0`
- `左行号 < 长度(左)`
`while 左行号 < 长度(左)`
**左行 = table.row(左, 左行号)**
`左行 = table.row(左, 左行号)`
**键 = 左行[左列]**
`键 = 左行[左列]`
**右行号 = 0**
`右行号 = 0`
- `右行号 < 长度(右)`
`while 右行号 < 长度(右)`
**右行 = table.row(右, 右行号)**
`右行 = table.row(右, 右行号)`
1. `右行[右列] == 键`
`if 右行[右列] == 键`
**结果行 = list.append(结果行, list.concat(左行, 右行))**
`结果行 = list.append(结果行, list.concat(左行, 右行))`
2. `true`
`else`
**继续 = 0**
`继续 = 0`
**右行号 = 右行号 + 1**
`end`
**左行号 = 左行号 + 1**
`右行号 = 右行号 + 1`
*表(表头, 结果行)*
`end`
## main
`左行号 = 左行号 + 1`
**左表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
`end`
**右表 = 表(["名称", "等级"], [["乙", "A"], ["甲", "B"], ["丁", "C"]])**
`return 表(表头, 结果行)`
*[左表, 右表, 连接(左表, 右表, 0, 0)]*
## @exec main
`左表 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])`
`右表 = 表(["名称", "等级"], [["乙", "A"], ["甲", "B"], ["丁", "C"]])`
`return [左表, 右表, 连接(左表, 右表, 0, 0)]`
@@ -1,66 +1,84 @@
## 解析
## @exec 解析
`(文本)`
`params(文本: Text)`
单行 CSV:逗号分隔、双引号包裹、`""` 表示一个字面双引号。
单行 CSV:逗号分隔、双引号包裹、"" 表示一个字面双引号。
不处理换行、多行字段或首尾空白。
**点 = text.codepoints(文本)**
`点 = text.codepoints(文本)`
**下标 = 0**
`下标 = 0`
**字段 = ""**
`字段 = ""`
**字段表 = []**
`字段表 = []`
**引号内 = false**
`引号内 = false`
- `下标 < 长度(点)`
`while 下标 < 长度(点)`
**字符 = text.from_codepoint(点[下标])**
`字符 = text.from_codepoint(点[下标])`
1. `引号内 and 字符 == "\""`
`if 引号内 and 字符 == "\""`
1. `下标 + 1 < 长度(点) and text.from_codepoint(点[下标 + 1]) == "\""`
`if 下标 + 1 < 长度(点) and text.from_codepoint(点[下标 + 1]) == "\""`
**`字段 = 字段 + "\""`**
`字段 = 字段 + "\""`
**下标 = 下标 + 2**
`下标 = 下标 + 2`
2. `true`
`else`
**引号内 = false**
`引号内 = false`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
2. `引号内`
`end`
**字段 = 字段 + 字符**
`else`
**下标 = 下标 + 1**
`if 引号内`
3. `字符 == "\""`
`字段 = 字段 + 字符`
**引号内 = true**
`下标 = 下标 + 1`
**下标 = 下标 + 1**
`else`
4. `字符 == ","`
`if 字符 == "\""`
**字段表 = list.append(字段表, 字段)**
`引号内 = true`
**字段 = ""**
`下标 = 下标 + 1`
**下标 = 下标 + 1**
`else`
5. `true`
`if 字符 == ","`
**字段 = 字段 + 字符**
`字段表 = list.append(字段表, 字段)`
**下标 = 下标 + 1**
`字段 = ""`
*list.concat(字段表, [字段])*
`下标 = 下标 + 1`
## main
`else`
*`[解析("甲,\"乙,丙\",\"说\"\"好\""), 解析("a,,c"), 解析("\"\""), 解析("\"a\"\"b\",c")]`*
`字段 = 字段 + 字符`
`下标 = 下标 + 1`
`end`
`end`
`end`
`end`
`end`
`return list.concat(字段表, [字段])`
## @exec main
`return [解析("甲,\"乙,丙\",\"说\"\"好\""), 解析("a,,c"), 解析("\"\""), 解析("\"a\"\"b\",c")]`
@@ -1,41 +1,45 @@
## 分组
## @exec 分组
`(数据, 键列, 值列)`
`params(数据: Table, 键列: Int, 值列: Int)`
按文本键列分组,把值列聚合成 Map:键 -> List。输入表不变。
**结果 = {}**
`结果 = {}`
**行号 = 0**
`行号 = 0`
- `行号 < 长度(数据)`
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
`行 = table.row(数据, 行号)`
**键 = 行[键列]**
`键 = 行[键列]`
**当前 = map.get(结果, 键)**
`当前 = map.get(结果, 键)`
**列表 = []**
`列表 = []`
1. `map.has(结果, 键)`
`if map.has(结果, 键)`
**列表 = 当前**
`列表 = 当前`
2. `true`
`else`
**忽略 = 0**
`忽略 = 0`
**列表 = list.append(列表, 行[值列])**
`end`
**结果 = map.set(结果, 键, 列表)**
`列表 = list.append(列表, 行[值列])`
**行号 = 行号 + 1**
`结果 = map.set(结果, 键, 列表)`
*结果*
`行号 = 行号 + 1`
## main
`end`
**数据 = 表(["类别", "值"], [["a", 1], ["b", 2], ["a", 3], ["b", 4], ["a", 5]])**
`return 结果`
*[数据, 分组(数据, 0, 1)]*
## @exec main
`数据 = 表(["类别", "值"], [["a", 1], ["b", 2], ["a", 3], ["b", 4], ["a", 5]])`
`return [数据, 分组(数据, 0, 1)]`
@@ -1,47 +1,53 @@
## 矩阵乘
## @exec 矩阵乘
`(左, 右)`
`params(左: List, 右: List)`
List-of-List 行主序矩阵乘法;调用方保证维度匹配。使用 Int 精确算术。
**结果 = []**
`结果 = []`
**行号 = 0**
`行号 = 0`
- `行号 < 长度(左)`
`while 行号 < 长度(左)`
**左行 = 左[行号]**
`左行 = 左[行号]`
**新行 = []**
`新行 = []`
**列号 = 0**
`列号 = 0`
- `列号 < 长度(右[0])`
`while 列号 < 长度(右[0])`
**总和 = 0**
`总和 = 0`
**内号 = 0**
`内号 = 0`
- `内号 < 长度(左行)`
`while 内号 < 长度(左行)`
**总和 = 总和 + 左行[内号] * 右[内号][列号]**
`总和 = 总和 + 左行[内号] * 右[内号][列号]`
**内号 = 内号 + 1**
`内号 = 内号 + 1`
**新行 = list.append(新行, 总和)**
`end`
**列号 = 列号 + 1**
`新行 = list.append(新行, 总和)`
**结果 = list.append(结果, 新行)**
`列号 = 列号 + 1`
**行号 = 行号 + 1**
`end`
*结果*
`结果 = list.append(结果, 新行)`
## main
`行号 = 行号 + 1`
**左 = [[1, 2, 3], [4, 5, 6]]**
`end`
**右 = [[7, 8], [9, 10], [11, 12]]**
`return 结果`
*[左, 右, 矩阵乘(左, 右)]*
## @exec main
`左 = [[1, 2, 3], [4, 5, 6]]`
`右 = [[7, 8], [9, 10], [11, 12]]`
`return [左, 右, 矩阵乘(左, 右)]`
+166 -125
View File
@@ -1,250 +1,291 @@
## 切片
## @exec 切片
`(点, 起, 止)`
`params(点: List, 起: Int, 止: Int)`
**结果 = ""**
`结果 = ""`
**下标 = 起**
`下标 = 起`
- `下标 < 止`
`while 下标 < 止`
**结果 = 结果 + text.from_codepoint(点[下标])**
`结果 = 结果 + text.from_codepoint(点[下标])`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
*结果*
`end`
## 找子串
`return 结果`
`(点, 子点, 起点)`
## @exec 找子串
**结果 = 0 - 1**
`params(点: List, 子点: List, 起点: Int)`
**下标 = 起点**
`结果 = 0 - 1`
- `下标 + 长度(子点) <= 长度(点) and 结果 < 0`
`下标 = 起点`
**偏移 = 0**
`while 下标 + 长度(子点) <= 长度(点) and 结果 < 0`
**匹配 = true**
`偏移 = 0`
- `偏移 < 长度(子点) and 匹配`
`匹配 = true`
1. `点[下标 + 偏移] != 子点[偏移]`
`while 偏移 < 长度(子点) and 匹配`
**匹配 = false**
`if 点[下标 + 偏移] != 子点[偏移]`
2. `true`
`匹配 = false`
**忽略 = 0**
`else`
**偏移 = 偏移 + 1**
`忽略 = 0`
1. `匹配`
`end`
**结果 = 下标**
`偏移 = 偏移 + 1`
2. `true`
`end`
**忽略 = 0**
`if 匹配`
**下标 = 下标 + 1**
`结果 = 下标`
*结果*
`else`
## 找字符
`忽略 = 0`
`(点, 码点, 起点)`
`end`
**结果 = 0 - 1**
`下标 = 下标 + 1`
**下标 = 起点**
`end`
- `下标 < 长度(点) and 结果 < 0`
`return 结果`
1. `点[下标] == 码点`
## @exec 找字符
**结果 = 下标**
`params(点: List, 码点: Int, 起点: Int)`
2. `true`
`结果 = 0 - 1`
**忽略 = 0**
`下标 = 起点`
**下标 = 下标 + 1**
`while 下标 < 长度(点) and 结果 < 0`
*结果*
`if 点[下标] == 码点`
## 十六进制值
`结果 = 下标`
`(点)`
`else`
**数值 = 0**
`忽略 = 0`
**下标 = 0**
`end`
- `下标 < 长度(点)`
`下标 = 下标 + 1`
**码点 = 点[下标]**
`end`
1. `码点 >= 48 and 码点 <= 57`
`return 结果`
**数值 = 数值 * 16 + 码点 - 48**
## @exec 十六进制值
2. `码点 >= 65 and 码点 <= 70`
`params(点: List)`
**数值 = 数值 * 16 + 码点 - 55**
`数值 = 0`
3. `码点 >= 97 and 码点 <= 102`
`下标 = 0`
**数值 = 数值 * 16 + 码点 - 87**
`while 下标 < 长度(点)`
4. `true`
`码点 = 点[下标]`
**忽略 = 0**
`if 码点 >= 48 and 码点 <= 57`
**下标 = 下标 + 1**
`数值 = 数值 * 16 + 码点 - 48`
*数值*
`else`
## 解码分块
`if 码点 >= 65 and 码点 <= 70`
`(体)`
`数值 = 数值 * 16 + 码点 - 55`
**点 = text.codepoints(体)**
`else`
**结果 = ""**
`if 码点 >= 97 and 码点 <= 102`
**位置 = 0**
`数值 = 数值 * 16 + 码点 - 87`
- `位置 < 长度(点)`
`else`
**行尾 = 找子串(点, text.codepoints("\r\n"), 位置)**
`忽略 = 0`
- `行尾 < 0`
`end`
*""*
`end`
**大小文本 = 切片(点, 位置, 行尾)**
`end`
**大小 = 十六进制值(text.codepoints(大小文本))**
`下标 = 下标 + 1`
1. `大小 == 0`
`end`
**位置 = 长度(点)**
`return 数值`
2. `true`
## @exec 解码分块
**结果 = 结果 + 切片(点, 行尾 + 2, 行尾 + 2 + 大小)**
`params(体: Text)`
**位置 = 行尾 + 2 + 大小 + 2**
`点 = text.codepoints(体)`
*结果*
`结果 = ""`
## 解析响应
`位置 = 0`
`(文本)`
`while 位置 < 长度(点)`
**点 = text.codepoints(文本)**
`行尾 = 找子串(点, text.codepoints("\r\n"), 位置)`
**分隔 = text.codepoints("\r\n\r\n")**
`while 行尾 < 0`
**头部结束 = 找子串(点, 分隔, 0)**
`return ""`
**头部 = 切片(点, 0, 头部结束)**
`end`
**体 = 切片(点, 头部结束 + 4, 长度(点))**
`大小文本 = 切片(点, 位置, 行尾)`
**头部点 = text.codepoints(头部)**
`大小 = 十六进制值(text.codepoints(大小文本))`
**行列表 = []**
`if 大小 == 0`
**行起 = 0**
`位置 = 长度(点)`
**下一行 = 找子串(头部点, text.codepoints("\r\n"), 0)**
`else`
- `下一行 >= 0`
`结果 = 结果 + 切片(点, 行尾 + 2, 行尾 + 2 + 大小)`
**行列表 = list.append(行列表, 切片(头部点, 行起, 下一行))**
`位置 = 行尾 + 2 + 大小 + 2`
**行起 = 下一行 + 2**
`end`
**下一行 = 找子串(头部点, text.codepoints("\r\n"), 行起)**
`end`
**行列表 = list.append(行列表, 切片(头部点, 行起, 长度(头部点)))**
`return 结果`
**状态行 = 行列表[0]**
## @exec 解析响应
**状态点 = text.codepoints(状态行)**
`params(文本: Text)`
**第一空格 = 找字符(状态点, 32, 0)**
`点 = text.codepoints(文本)`
**第二空格 = 找字符(状态点, 32, 第一空格 + 1)**
`分隔 = text.codepoints("\r\n\r\n")`
**状态 = 整数(切片(状态点, 第一空格 + 1, 第二空格))**
`头部结束 = 找子串(点, 分隔, 0)`
**原因 = 切片(状态点, 第二空格 + 1, 长度(状态点))**
`头部 = 切片(点, 0, 头部结束)`
**头 = {}**
`体 = 切片(点, 头部结束 + 4, 长度(点))`
**行号 = 1**
`头部点 = text.codepoints(头部)`
- `行号 < 长度(行列表)`
`行列表 = []`
**行点 = text.codepoints(行列表[行号])**
`行起 = 0`
**冒号 = 找字符(行点, 58, 0)**
`下一行 = 找子串(头部点, text.codepoints("\r\n"), 0)`
1. `冒号 >= 0`
`while 下一行 >= 0`
**名 = 切片(行点, 0, 冒号)**
`行列表 = list.append(行列表, 切片(头部点, 行起, 下一行))`
**值 = 切片(行点, 冒号 + 2, 长度(行点))**
`行起 = 下一行 + 2`
**头 = map.set(头, 名, 值)**
`下一行 = 找子串(头部点, text.codepoints("\r\n"), 行起)`
2. `true`
`end`
**忽略 = 0**
`行列表 = list.append(行列表, 切片(头部点, 行起, 长度(头部点)))`
**行号 = 行号 + 1**
`状态行 = 行列表[0]`
**传输 = map.get(头, "Transfer-Encoding")**
`状态点 = text.codepoints(状态行)`
**解码后 = 体**
`第一空格 = 找字符(状态点, 32, 0)`
1. `传输 == "chunked"`
`第二空格 = 找字符(状态点, 32, 第一空格 + 1)`
**解码后 = 解码分块(体)**
`状态 = 整数(切片(状态点, 第一空格 + 1, 第二空格))`
2. `true`
`原因 = 切片(状态点, 第二空格 + 1, 长度(状态点))`
**解码后 = 体**
`头 = {}`
*{状态: 状态, 原因: 原因, 头: 头, 体: 解码后}*
`行号 = 1`
## 构建请求
`while 行号 < 长度(行列表)`
`(方法, 路径, 头列表, 体)`
`行点 = text.codepoints(行列表[行号])`
**请求 = 方法 + " " + 路径 + " HTTP/1.1\r\n"**
`冒号 = 找字符(行点, 58, 0)`
**行号 = 0**
`if 冒号 >= 0`
- `行号 < 长度(头列表)`
`名 = 切片(行点, 0, 冒号)`
**请求 = 请求 + 头列表[行号][0] + ": " + 头列表[行号][1] + "\r\n"**
`值 = 切片(行点, 冒号 + 2, 长度(行点))`
**行号 = 行号 + 1**
`头 = map.set(头, 名, 值)`
*请求 + "\r\n" + 体*
`else`
## main
`忽略 = 0`
**响应文本 = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"**
`end`
**响应 = 解析响应(响应文本)**
`行号 = 行号 + 1`
**请求 = 构建请求("GET", "/health", [["Accept", "text/plain"], ["Host", "example.test"]], "")**
`end`
*[响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求]*
`传输 = map.get(头, "Transfer-Encoding")`
`解码后 = 体`
`if 传输 == "chunked"`
`解码后 = 解码分块(体)`
`else`
`解码后 = 体`
`end`
`return {状态: 状态, 原因: 原因, 头: 头, 体: 解码后}`
## @exec 构建请求
`params(方法: Text, 路径: Text, 头列表: List, 体: Text)`
`请求 = 方法 + " " + 路径 + " HTTP/1.1\r\n"`
`行号 = 0`
`while 行号 < 长度(头列表)`
`请求 = 请求 + 头列表[行号][0] + ": " + 头列表[行号][1] + "\r\n"`
`行号 = 行号 + 1`
`end`
`return 请求 + "\r\n" + 体`
## @exec main
`响应文本 = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"`
`响应 = 解析响应(响应文本)`
`请求 = 构建请求("GET", "/health", [["Accept", "text/plain"], ["Host", "example.test"]], "")`
`return [响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求]`
@@ -3,236 +3,274 @@ import:
数据表: "std:data/table"
---
## 转表
## @exec 转表
`(列存)`
`params(列存: Map)`
**列 = 列存["列"]**
`列 = 列存["列"]`
**数据 = 列存["数据"]**
`数据 = 列存["数据"]`
**行数 = 0**
`行数 = 0`
1. `长度(数据) > 0`
`if 长度(数据) > 0`
**行数 = 长度(数据[0])**
`行数 = 长度(数据[0])`
2. `true`
`else`
**忽略 = 0**
`忽略 = 0`
**行列表 = []**
`end`
**行号 = 0**
`行列表 = []`
- `行号 < 行数`
`行号 = 0`
**行 = []**
`while 行号 < 行数`
**列号 = 0**
`行 = []`
- `列号 < 长度(列)`
`列号 = 0`
**行 = list.append(行, 数据[列号][行号])**
`while 列号 < 长度(列)`
**列号 = 列号 + 1**
`行 = list.append(行, 数据[列号][行号])`
**行列表 = list.append(行列表, 行)**
`列号 = 列号 + 1`
**行号 = 行号 + 1**
`end`
*表(列, 行列表)*
`行列表 = list.append(行列表, 行)`
## 列存排序
`行号 = 行号 + 1`
`(列存, 目标列)`
`end`
**列 = 列存["列"]**
`return 表(列, 行列表)`
**数据 = 列存["数据"]**
## @exec 列存排序
**行数 = 长度(数据[目标列])**
`params(列存: Map, 目标列: Int)`
**顺序 = []**
`列 = 列存["列"]`
**行号 = 0**
`数据 = 列存["数据"]`
- `行号 < 行数`
`行数 = 长度(数据[目标列])`
**顺序 = list.append(顺序, 行号)**
`顺序 = []`
**行号 = 行号 + 1**
`行号 = 0`
**下标 = 1**
`while 行号 < 行数`
- `下标 < 行数`
`顺序 = list.append(顺序, 行号)`
**当前 = 下标**
`行号 = 行号 + 1`
**新顺序 = []**
`end`
**已插入 = false**
`下标 = 1`
**位 = 0**
`while 下标 < 行数`
- `位 < 长度(顺序)`
`当前 = 下标`
**已有 = 顺序[位]**
`新顺序 = []`
1. `not 已插入 and 数据[目标列][已有] > 数据[目标列][当前]`
`已插入 = false`
**新顺序 = list.append(新顺序, 当前)**
`位 = 0`
**新顺序 = list.append(新顺序, 已有)**
`while 位 < 长度(顺序)`
**已插入 = true**
`已有 = 顺序[位]`
2. `true`
`if not 已插入 and 数据[目标列][已有] > 数据[目标列][当前]`
**新顺序 = list.append(新顺序, 已有)**
`新顺序 = list.append(新顺序, 当前)`
**位 = 位 + 1**
`新顺序 = list.append(新顺序, 已有)`
1. `not 已插入`
`已插入 = true`
**新顺序 = list.append(新顺序, 当前)**
`else`
2. `true`
`新顺序 = list.append(新顺序, 已有)`
**忽略 = 0**
`end`
**顺序 = 新顺序**
`位 = 位 + 1`
**下标 = 下标 + 1**
`end`
**新数据 = []**
`if not 已插入`
**列号 = 0**
`新顺序 = list.append(新顺序, 当前)`
- `列号 < 长度(列)`
`else`
**新列 = []**
`忽略 = 0`
**位 = 0**
`end`
- `位 < 行数`
`顺序 = 新顺序`
**新列 = list.append(新列, 数据[列号][顺序[位]])**
`下标 = 下标 + 1`
**位 = 位 + 1**
`end`
**新数据 = list.append(新数据, 新列)**
`新数据 = []`
**列号 = 列号 + 1**
`列号 = 0`
*{列: 列, 数据: 新数据}*
`while 列号 < 长度(列)`
## 列存过滤
`新列 = []`
`(列存, 目标列, 阈值)`
`位 = 0`
**列 = 列存["列"]**
`while 位 < 行数`
**数据 = 列存["数据"]**
`新列 = list.append(新列, 数据[列号][顺序[位]])`
**行数 = 长度(数据[目标列])**
`位 = 位 + 1`
**保留 = []**
`end`
**行号 = 0**
`新数据 = list.append(新数据, 新列)`
- `行号 < 行数`
`列号 = 列号 + 1`
1. `数据[目标列][行号] > 阈值`
`end`
**保留 = list.append(保留, 行号)**
`return {列: 列, 数据: 新数据}`
2. `true`
## @exec 列存过滤
**忽略 = 0**
`params(列存: Map, 目标列: Int, 阈值: Int)`
**行号 = 行号 + 1**
`列 = 列存["列"]`
**新数据 = []**
`数据 = 列存["数据"]`
**列号 = 0**
`行数 = 长度(数据[目标列])`
- `列号 < 长度(列)`
`保留 = []`
**新列 = []**
`行号 = 0`
**位 = 0**
`while 行号 < 行数`
- `位 < 长度(保留)`
`if 数据[目标列][行号] > 阈值`
**新列 = list.append(新列, 数据[列号][保留[位]])**
`保留 = list.append(保留, 行号)`
**位 = 位 + 1**
`else`
**新数据 = list.append(新数据, 新列)**
`忽略 = 0`
**列号 = 列号 + 1**
`end`
*{列: 列, 数据: 新数据}*
`行号 = 行号 + 1`
## 表相等
`end`
`(左, 右)`
`新数据 = []`
**左列 = table.columns(左)**
`列号 = 0`
**右列 = table.columns(右)**
`while 列号 < 长度(列)`
**相等 = 长度(左列) == 长度(右列) and 长度(左) == 长度(右)**
`新列 = []`
**列号 = 0**
`位 = 0`
- `列号 < 长度(左列) and 相等`
`while 位 < 长度(保留)`
1. `左列[列号] != 右列[列号]`
`新列 = list.append(新列, 数据[列号][保留[位]])`
**相等 = false**
`位 = 位 + 1`
2. `true`
`end`
**忽略 = 0**
`新数据 = list.append(新数据, 新列)`
**列号 = 列号 + 1**
`列号 = 列号 + 1`
**行号 = 0**
`end`
- `行号 < 长度(左) and 相等`
`return {列: 列, 数据: 新数据}`
**列号 = 0**
## @exec 表相等
- `列号 < 长度(左列) and 相等`
`params(左: Table, 右: Table)`
1. `table.cell(左, 行号, 列号) != table.cell(右, 行号, 列号)`
`左列 = table.columns(左)`
**相等 = false**
`右列 = table.columns(右)`
2. `true`
`相等 = 长度(左列) == 长度(右列) and 长度(左) == 长度(右)`
**忽略 = 0**
`列号 = 0`
**列号 = 列号 + 1**
`while 列号 < 长度(左列) and 相等`
**行号 = 行号 + 1**
`if 左列[列号] != 右列[列号]`
*相等*
`相等 = false`
## main
`else`
**列存 = {列: ["名称", "分数"], 数据: [["甲", "乙", "丙"], [87, 92, 71]]}**
`忽略 = 0`
**行表 = 转表(列存)**
`end`
**排序行 = 数据表.排序(行表, 1)**
`列号 = 列号 + 1`
**排序列 = 转表(列存排序(列存, 1))**
`end`
**过滤行 = 数据表.过滤数值大于(行表, 1, 80)**
`行号 = 0`
**过滤列 = 转表(列存过滤(列存, 1, 80))**
`while 行号 < 长度(左) and 相等`
*[表相等(转表(列存), 行表), 表相等(排序行, 排序列), 表相等(过滤行, 过滤列)]*
`列号 = 0`
`while 列号 < 长度(左列) and 相等`
`if table.cell(左, 行号, 列号) != table.cell(右, 行号, 列号)`
`相等 = false`
`else`
`忽略 = 0`
`end`
`列号 = 列号 + 1`
`end`
`行号 = 行号 + 1`
`end`
`return 相等`
## @exec main
`列存 = {列: ["名称", "分数"], 数据: [["甲", "乙", "丙"], [87, 92, 71]]}`
`行表 = 转表(列存)`
`排序行 = 数据表.排序(行表, 1)`
`排序列 = 转表(列存排序(列存, 1))`
`过滤行 = 数据表.过滤数值大于(行表, 1, 80)`
`过滤列 = 转表(列存过滤(列存, 1, 80))`
`return [表相等(转表(列存), 行表), 表相等(排序行, 排序列), 表相等(过滤行, 过滤列)]`
@@ -1,99 +1,115 @@
## 查值
## @exec 查值
`(三元组, 行, 列)`
`params(三元组: List, 行: Int, 列: Int)`
**值 = 0**
`值 = 0`
**下标 = 0**
`下标 = 0`
- `下标 < 长度(三元组)`
`while 下标 < 长度(三元组)`
1. `三元组[下标][0] == 行 and 三元组[下标][1] == 列`
`if 三元组[下标][0] == 行 and 三元组[下标][1] == 列`
**值 = 三元组[下标][2]**
`值 = 三元组[下标][2]`
2. `true`
`else`
**忽略 = 0**
`忽略 = 0`
**下标 = 下标 + 1**
`end`
*值*
`下标 = 下标 + 1`
## 三列转稠密
`end`
`(三元组, 行数, 列数)`
`return 值`
**矩阵 = []**
## @exec 三列转稠密
**行号 = 0**
`params(三元组: List, 行数: Int, 列数: Int)`
- `行号 < 行数`
`矩阵 = []`
**行 = []**
`行号 = 0`
**列号 = 0**
`while 行号 < 行数`
- `列号 < 列数`
`行 = []`
**行 = list.append(行, 查值(三元组, 行号, 列号))**
`列号 = 0`
**列号 = 列号 + 1**
`while 列号 < 列数`
**矩阵 = list.append(矩阵, 行)**
`行 = list.append(行, 查值(三元组, 行号, 列号))`
**行号 = 行号 + 1**
`列号 = 列号 + 1`
*矩阵*
`end`
## 稀疏乘法
`矩阵 = list.append(矩阵, 行)`
`(左, 右, 行数, 内维, 列数)`
`行号 = 行号 + 1`
**结果 = []**
`end`
**行号 = 0**
`return 矩阵`
- `行号 < 行数`
## @exec 稀疏乘法
**列号 = 0**
`params(左: List, 右: List, 行数: Int, 内维: Int, 列数: Int)`
- `列号 < 列数`
`结果 = []`
**和 = 0**
`行号 = 0`
**内号 = 0**
`while 行号 < 行数`
- `内号 < 内维`
`列号 = 0`
**左值 = 查值(左, 行号, 内号)**
`while 列号 < 列数`
**右值 = 查值(右, 内号, 列号)**
`和 = 0`
**和 = 和 + 左值 * 右值**
`内号 = 0`
**内号 = 内号 + 1**
`while 内号 < 内维`
1. `和 != 0`
`左值 = 查值(左, 行号, 内号)`
**结果 = list.append(结果, [行号, 列号, 和])**
`右值 = 查值(右, 内号, 列号)`
2. `true`
`和 = 和 + 左值 * 右值`
**忽略 = 0**
`内号 = 内号 + 1`
**列号 = 列号 + 1**
`end`
**行号 = 行号 + 1**
`if 和 != 0`
*结果*
`结果 = list.append(结果, [行号, 列号, 和])`
## main
`else`
**左 = [[0, 0, 1], [0, 2, 2], [1, 1, 3]]**
`忽略 = 0`
**右 = [[0, 0, 4], [1, 1, 5], [2, 0, 6]]**
`end`
**乘积 = 稀疏乘法(左, 右, 2, 3, 2)**
`列号 = 列号 + 1`
*[乘积, 三列转稠密(乘积, 2, 2)]*
`end`
`行号 = 行号 + 1`
`end`
`return 结果`
## @exec main
`左 = [[0, 0, 1], [0, 2, 2], [1, 1, 3]]`
`右 = [[0, 0, 4], [1, 1, 5], [2, 0, 6]]`
`乘积 = 稀疏乘法(左, 右, 2, 3, 2)`
`return [乘积, 三列转稠密(乘积, 2, 2)]`
@@ -0,0 +1,5 @@
{
"codes": [
"sem.invalid_mode_marker"
]
}
@@ -0,0 +1,5 @@
# @exec 01-invalid-mode-marker-case
## @Exec main
`return 1`
@@ -1,5 +0,0 @@
{
"codes": [
"markdown.invalid_construct"
]
}
@@ -0,0 +1,5 @@
{
"codes": [
"sem.invalid_mode_marker"
]
}
@@ -0,0 +1,5 @@
# @exec 02-invalid-mode-marker-misspelled
## @knowledg 说明
普通知识文字。
@@ -1,5 +0,0 @@
{
"codes": [
"markdown.invalid_construct"
]
}
@@ -0,0 +1,5 @@
{
"codes": [
"sem.invalid_mode_marker"
]
}
@@ -0,0 +1,3 @@
## @exec @knowledge main
`return 1`
@@ -1,5 +0,0 @@
{
"codes": [
"markdown.invalid_construct"
]
}
@@ -0,0 +1,5 @@
{
"codes": [
"sem.empty_unit_name"
]
}
@@ -0,0 +1 @@
## @exec
@@ -1,5 +0,0 @@
{
"codes": [
"markdown.invalid_construct"
]
}
@@ -0,0 +1,5 @@
{
"codes": [
"sem.empty_unit_name"
]
}
@@ -0,0 +1 @@
# @knowledge
@@ -1,5 +0,0 @@
{
"codes": [
"markdown.invalid_construct"
]
}
@@ -0,0 +1,5 @@
{
"codes": [
"sem.exec_without_function"
]
}
Loaded 100 of 375 files, more files were not shown because too many files have changed in this diff. Show more