feat(lang): implement RFC-LANG-0002 unified semantics (breaking)
One Markdown-to-program mapping, no version selector or legacy mapper. Frontend/runtime: - resolve @exec/@knowledge heading declarations with nearest-ancestor inheritance; only an Exec H2 is callable; add UnitKind::Knowledge and sem.* diagnostics (mode marker, unit name, exec-without-function, payload nesting/empty, syntax, control stack, params position, version directive, inferred-signature lint) - extract Strong/Emph/inline Code as one-statement carriers, refuse nested carriers, and honour heading segment boundaries so an if/while cannot be closed across a heading - lower if/else/while/end to the existing Branch/Loop/Return IR with a compile-time control stack; keep payload-relative source spans through expression parsing and rewrite them onto the file - accept params(...) only as the first direct Exec H2 payload; keep the existing free-variable inference and named-only rule for inferred signatures; fix duplicate-mode/else/end and Knowledge-body inertness - reject marqdo_semantics/syntax front matter directives Data and tests: - migrate stdlib/**/*.mq.md, regenerate stdlib.json digest - migrate examples and the full semantics/negative/runtime corpus to the only current syntax; expected values/runtime codes preserved - migrate Rust-embedded .mq.md fixtures, init template, CLI help and security-lane fixtures Verified: cargo fmt --check, cargo clippy -D warnings and cargo test --workspace --locked (green).
This commit is contained in:
1 parent
49835424ee
commit
e029853e32
375 files changed
+6591
-4790
No files matched your search
+6
-4
@@ -1,8 +1,10 @@
|
||||
|
||||
# @exec clock
|
||||
## 现在
|
||||
|
||||
`()`
|
||||
`params()`
|
||||
|
||||
返回项目模式下的 UTC Unix 毫秒。`pure`/`strict` 返回
|
||||
`permission.denied`,与 `resource.request` 的其他操作一致。
|
||||
返回项目模式下的 UTC Unix 毫秒。pure/strict 返回
|
||||
permission.denied,与 resource.request 的其他操作一致。
|
||||
|
||||
*resource.request("clock.now", [])*
|
||||
`return resource.request("clock.now", [])`
|
||||
@@ -1,17 +1,19 @@
|
||||
|
||||
# @exec compress
|
||||
## 压缩
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
把 UTF-8 文本压缩为 gzip 字节列表(`List<Int>`,0–255)。输入上限
|
||||
把 UTF-8 文本压缩为 gzip 字节列表(List<Int>,0–255)。输入上限
|
||||
8 MiB;结果确定(不写入时间戳/文件名)。
|
||||
|
||||
*resource.request("compress.gzip", [文本])*
|
||||
`return resource.request("compress.gzip", [文本])`
|
||||
|
||||
## 解压
|
||||
|
||||
`(字节列表)`
|
||||
`params(字节列表)`
|
||||
|
||||
解压 gzip 字节列表并校验 UTF-8。输出上限 8 MiB;坏 gzip 返回
|
||||
`compress.invalid`,非 UTF-8 返回 `compress.not_utf8`。
|
||||
compress.invalid,非 UTF-8 返回 compress.not_utf8。
|
||||
|
||||
*resource.request("compress.gunzip", [字节列表])*
|
||||
`return resource.request("compress.gunzip", [字节列表])`
|
||||
+179
-116
@@ -1,20 +1,22 @@
|
||||
|
||||
# @exec csv
|
||||
## 成功
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
*{ok: true, value: 值}*
|
||||
`return {ok: true, value: 值}`
|
||||
|
||||
## 失败
|
||||
|
||||
`(代码, 消息)`
|
||||
`params(代码, 消息)`
|
||||
|
||||
*{ok: false, code: 代码, message: 消息}*
|
||||
`return {ok: false, code: 代码, message: 消息}`
|
||||
|
||||
## 解析行
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
解析单行 CSV:逗号分隔、双引号包裹、`""` 表示一个字面双引号。
|
||||
解析单行 CSV:逗号分隔、双引号包裹、"" 表示一个字面双引号。
|
||||
不处理换行、多行字段或首尾空白。未闭合引号和未加引号字段中的双引号
|
||||
返回可恢复错误。
|
||||
|
||||
@@ -30,71 +32,90 @@
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `下标 < 长度(点) and 错误 == ""`
|
||||
`while 下标 < 长度(点) and 错误 == ""`
|
||||
|
||||
**字符 = 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`
|
||||
`if true`
|
||||
|
||||
**引号内 = false**
|
||||
**引号内 = false**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
2. `引号内`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 引号内`
|
||||
|
||||
**字段 = 字段 + 字符**
|
||||
**字段 = 字段 + 字符**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
3. `字符 == "\""`
|
||||
`else`
|
||||
`if 字符 == "\""`
|
||||
|
||||
1. `字段 == ""`
|
||||
`if 字段 == ""`
|
||||
|
||||
**引号内 = true**
|
||||
**引号内 = true**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**错误 = "csv.invalid_quote"**
|
||||
**错误 = "csv.invalid_quote"**
|
||||
|
||||
4. `字符 == ","`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 字符 == ","`
|
||||
|
||||
**字段表 = list.append(字段表, 字段)**
|
||||
**字段表 = list.append(字段表, 字段)**
|
||||
|
||||
**字段 = ""**
|
||||
**字段 = ""**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
5. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**字段 = 字段 + 字符**
|
||||
**字段 = 字段 + 字符**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `错误 != ""`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 错误 != ""`
|
||||
|
||||
*失败(错误, "未加引号字段中出现双引号")*
|
||||
`return 失败(错误, "未加引号字段中出现双引号")`
|
||||
|
||||
1. `引号内`
|
||||
`else`
|
||||
`if 引号内`
|
||||
|
||||
*失败("csv.unterminated_quote", "引号未闭合")*
|
||||
`return 失败("csv.unterminated_quote", "引号未闭合")`
|
||||
|
||||
*成功(list.concat(字段表, [字段]))*
|
||||
`end`
|
||||
`end`
|
||||
`return 成功(list.concat(字段表, [字段]))`
|
||||
|
||||
## 格式化字段
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
含逗号、双引号、换行或回车的字段加引号,内部双引号写成 `""`。
|
||||
含逗号、双引号、换行或回车的字段加引号,内部双引号写成 ""。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -102,72 +123,85 @@
|
||||
|
||||
**需要引号 = false**
|
||||
|
||||
- `下标 < 长度(点) and not 需要引号`
|
||||
`while 下标 < 长度(点) and not 需要引号`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 == 34 or 码点 == 44 or 码点 == 10 or 码点 == 13`
|
||||
`if 码点 == 34 or 码点 == 44 or 码点 == 10 or 码点 == 13`
|
||||
|
||||
**需要引号 = true**
|
||||
**需要引号 = true**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `not 需要引号`
|
||||
`end`
|
||||
`if not 需要引号`
|
||||
|
||||
*文本*
|
||||
`return 文本`
|
||||
|
||||
`end`
|
||||
**`结果 = "\""`**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
1. `点[下标] == 34`
|
||||
`if 点[下标] == 34`
|
||||
|
||||
**`结果 = 结果 + "\"\""`**
|
||||
**`结果 = 结果 + "\"\""`**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*`结果 + "\""`*
|
||||
`end`
|
||||
``return 结果 + "\""``
|
||||
|
||||
## 格式化行
|
||||
|
||||
`(列表)`
|
||||
`params(列表)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表)`
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + ","**
|
||||
**结果 = 结果 + ","**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 格式化字段(文本(列表[下标]))**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 格式化字段(文本(列表[下标]))**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 解析
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
解析多行 CSV(逗号分隔、双引号、`""` 转义、CRLF/LF 行尾,引号内可含
|
||||
换行)。返回 `{ok:true, value:List<List<Text>>}`;未闭合引号和不合法
|
||||
解析多行 CSV(逗号分隔、双引号、"" 转义、CRLF/LF 行尾,引号内可含
|
||||
换行)。返回 {ok:true, value:List<List<Text>>};未闭合引号和不合法
|
||||
引号返回可恢复错误。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
@@ -184,108 +218,137 @@
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `下标 < 长度(点) and 错误 == ""`
|
||||
`while 下标 < 长度(点) and 错误 == ""`
|
||||
|
||||
**字符 = 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`
|
||||
`if true`
|
||||
|
||||
**引号内 = false**
|
||||
**引号内 = false**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
2. `引号内`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 引号内`
|
||||
|
||||
**字段 = 字段 + 字符**
|
||||
**字段 = 字段 + 字符**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
3. `字符 == "\""`
|
||||
`else`
|
||||
`if 字符 == "\""`
|
||||
|
||||
1. `字段 == ""`
|
||||
`if 字段 == ""`
|
||||
|
||||
**引号内 = true**
|
||||
**引号内 = true**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**错误 = "csv.invalid_quote"**
|
||||
**错误 = "csv.invalid_quote"**
|
||||
|
||||
4. `字符 == ","`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 字符 == ","`
|
||||
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
|
||||
**字段 = ""**
|
||||
**字段 = ""**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
5. `字符 == "\n"`
|
||||
`else`
|
||||
`if 字符 == "\n"`
|
||||
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
|
||||
**行 = list.append(行, 当前行)**
|
||||
**行 = list.append(行, 当前行)**
|
||||
|
||||
**当前行 = []**
|
||||
**当前行 = []**
|
||||
|
||||
**字段 = ""**
|
||||
**字段 = ""**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
6. `字符 == "\r"`
|
||||
`else`
|
||||
`if 字符 == "\r"`
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
7. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**字段 = 字段 + 字符**
|
||||
**字段 = 字段 + 字符**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `错误 != ""`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 错误 != ""`
|
||||
|
||||
*失败(错误, "未加引号字段中出现双引号")*
|
||||
`return 失败(错误, "未加引号字段中出现双引号")`
|
||||
|
||||
1. `引号内`
|
||||
`else`
|
||||
`if 引号内`
|
||||
|
||||
*失败("csv.unterminated_quote", "引号未闭合")*
|
||||
`return 失败("csv.unterminated_quote", "引号未闭合")`
|
||||
|
||||
1. `字段 != "" or 长度(当前行) > 0`
|
||||
`else`
|
||||
`if 字段 != "" or 长度(当前行) > 0`
|
||||
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
|
||||
**行 = list.append(行, 当前行)**
|
||||
**行 = list.append(行, 当前行)**
|
||||
|
||||
*成功(行)*
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`return 成功(行)`
|
||||
|
||||
## 格式化
|
||||
|
||||
`(行列表)`
|
||||
`params(行列表)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(行列表)`
|
||||
`while 行号 < 长度(行列表)`
|
||||
|
||||
1. `行号 > 0`
|
||||
`if 行号 > 0`
|
||||
|
||||
**结果 = 结果 + "\n"**
|
||||
**结果 = 结果 + "\n"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 格式化行(行列表[行号])**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 格式化行(行列表[行号])**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
+468
-292
File diff suppressed because it is too large.
Load diff
+181
-130
@@ -1,6 +1,8 @@
|
||||
|
||||
# @exec table
|
||||
## 选择列
|
||||
|
||||
`(数据, 列)`
|
||||
`params(数据, 列)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -8,19 +10,20 @@
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**行 = table.row(数据, 行号)**
|
||||
**行 = table.row(数据, 行号)**
|
||||
|
||||
**结果行 = list.append(结果行, [行[列]])**
|
||||
**结果行 = list.append(结果行, [行[列]])**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*表([列名[列]], 结果行)*
|
||||
`end`
|
||||
`return 表([列名[列]], 结果行)`
|
||||
|
||||
## 过滤数值大于
|
||||
|
||||
`(数据, 列, 阈值)`
|
||||
`params(数据, 列, 阈值)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -28,25 +31,29 @@
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**行 = table.row(数据, 行号)**
|
||||
**行 = table.row(数据, 行号)**
|
||||
|
||||
1. `行[列] > 阈值`
|
||||
`if 行[列] > 阈值`
|
||||
|
||||
**结果行 = list.append(结果行, 行)**
|
||||
**结果行 = list.append(结果行, 行)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*表(列名, 结果行)*
|
||||
`end`
|
||||
`return 表(列名, 结果行)`
|
||||
|
||||
## 插入有序
|
||||
|
||||
`(已排序, 行, 列)`
|
||||
`params(已排序, 行, 列)`
|
||||
|
||||
**新列表 = []**
|
||||
|
||||
@@ -54,37 +61,44 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(已排序)`
|
||||
`while 下标 < 长度(已排序)`
|
||||
|
||||
**当前行 = 已排序[下标]**
|
||||
**当前行 = 已排序[下标]**
|
||||
|
||||
1. `not 已插入 and 当前行[列] > 行[列]`
|
||||
`if not 已插入 and 当前行[列] > 行[列]`
|
||||
|
||||
**新列表 = list.append(新列表, 行)**
|
||||
**新列表 = list.append(新列表, 行)**
|
||||
|
||||
**新列表 = list.append(新列表, 当前行)**
|
||||
**新列表 = list.append(新列表, 当前行)**
|
||||
|
||||
**已插入 = true**
|
||||
**已插入 = true**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**新列表 = list.append(新列表, 当前行)**
|
||||
**新列表 = list.append(新列表, 当前行)**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `not 已插入`
|
||||
`end`
|
||||
`if not 已插入`
|
||||
|
||||
**新列表 = list.append(新列表, 行)**
|
||||
**新列表 = list.append(新列表, 行)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
*新列表*
|
||||
`end`
|
||||
`end`
|
||||
`return 新列表`
|
||||
|
||||
## 排序
|
||||
|
||||
`(数据, 列)`
|
||||
`params(数据, 列)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -94,17 +108,18 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 行数`
|
||||
`while 下标 < 行数`
|
||||
|
||||
**已排序 = 插入有序(已排序, table.row(数据, 下标), 列)**
|
||||
**已排序 = 插入有序(已排序, table.row(数据, 下标), 列)**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*表(列名, 已排序)*
|
||||
`end`
|
||||
`return 表(列名, 已排序)`
|
||||
|
||||
## 追加行
|
||||
|
||||
`(数据, 行)`
|
||||
`params(数据, 行)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -112,17 +127,18 @@
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**所有行 = list.append(所有行, table.row(数据, 行号))**
|
||||
**所有行 = list.append(所有行, table.row(数据, 行号))**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*表(列名, list.append(所有行, 行))*
|
||||
`end`
|
||||
`return 表(列名, list.append(所有行, 行))`
|
||||
|
||||
## 设置单元格
|
||||
|
||||
`(数据, 目标行, 目标列, 值)`
|
||||
`params(数据, 目标行, 目标列, 值)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -130,41 +146,49 @@
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**行 = table.row(数据, 行号)**
|
||||
**行 = table.row(数据, 行号)**
|
||||
|
||||
1. `行号 == 目标行`
|
||||
`if 行号 == 目标行`
|
||||
|
||||
**新行 = []**
|
||||
**新行 = []**
|
||||
|
||||
**列号 = 0**
|
||||
**列号 = 0**
|
||||
|
||||
- `列号 < 长度(行)`
|
||||
`while 列号 < 长度(行)`
|
||||
|
||||
1. `列号 == 目标列`
|
||||
`if 列号 == 目标列`
|
||||
|
||||
**新行 = list.append(新行, 值)**
|
||||
**新行 = list.append(新行, 值)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**新行 = list.append(新行, 行[列号])**
|
||||
**新行 = list.append(新行, 行[列号])**
|
||||
|
||||
**列号 = 列号 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**列号 = 列号 + 1**
|
||||
|
||||
**新行表 = list.append(新行表, 新行)**
|
||||
`end`
|
||||
**新行表 = list.append(新行表, 新行)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**新行表 = list.append(新行表, 行)**
|
||||
**新行表 = list.append(新行表, 行)**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*表(列名, 新行表)*
|
||||
`end`
|
||||
`return 表(列名, 新行表)`
|
||||
|
||||
## 连接
|
||||
|
||||
`(左, 右, 左列, 右列)`
|
||||
`params(左, 右, 左列, 右列)`
|
||||
|
||||
**表头 = list.concat(table.columns(左), table.columns(右))**
|
||||
|
||||
@@ -172,117 +196,134 @@
|
||||
|
||||
**左行号 = 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`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**右行号 = 右行号 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**右行号 = 右行号 + 1**
|
||||
|
||||
**左行号 = 左行号 + 1**
|
||||
`end`
|
||||
**左行号 = 左行号 + 1**
|
||||
|
||||
*表(表头, 结果行)*
|
||||
`end`
|
||||
`return 表(表头, 结果行)`
|
||||
|
||||
## 分组
|
||||
|
||||
`(数据, 键列, 值列)`
|
||||
`params(数据, 键列, 值列)`
|
||||
|
||||
**结果 = {}**
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**行 = table.row(数据, 行号)**
|
||||
**行 = table.row(数据, 行号)**
|
||||
|
||||
**键 = 行[键列]**
|
||||
**键 = 行[键列]**
|
||||
|
||||
**当前 = map.get(结果, 键)**
|
||||
**当前 = map.get(结果, 键)**
|
||||
|
||||
**列表 = []**
|
||||
**列表 = []**
|
||||
|
||||
1. `map.has(结果, 键)`
|
||||
`if map.has(结果, 键)`
|
||||
|
||||
**列表 = 当前**
|
||||
**列表 = 当前**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**列表 = list.append(列表, 行[值列])**
|
||||
`end`
|
||||
`end`
|
||||
**列表 = list.append(列表, 行[值列])**
|
||||
|
||||
**结果 = map.set(结果, 键, 列表)**
|
||||
**结果 = map.set(结果, 键, 列表)**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 连接片段
|
||||
|
||||
`(片段)`
|
||||
`params(片段)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(片段)`
|
||||
`while 下标 < 长度(片段)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + " | "**
|
||||
**结果 = 结果 + " | "**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 片段[下标]**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 片段[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 连接行
|
||||
|
||||
`(行列表)`
|
||||
`params(行列表)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(行列表)`
|
||||
`while 下标 < 长度(行列表)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + "\n"**
|
||||
**结果 = 结果 + "\n"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 行列表[下标]**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 行列表[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 转义单元格
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
**文本值 = 文本(值)**
|
||||
|
||||
@@ -292,27 +333,33 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
1. `点[下标] == 124`
|
||||
`if 点[下标] == 124`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(92) + "|"**
|
||||
**结果 = 结果 + text.from_codepoint(92) + "|"**
|
||||
|
||||
2. `点[下标] == 10 or 点[下标] == 13`
|
||||
`else`
|
||||
`if 点[下标] == 10 or 点[下标] == 13`
|
||||
|
||||
**结果 = 结果 + " "**
|
||||
**结果 = 结果 + " "**
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 转GFM
|
||||
|
||||
`(数据)`
|
||||
`params(数据)`
|
||||
|
||||
**列名 = table.columns(数据)**
|
||||
|
||||
@@ -320,12 +367,13 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列名)`
|
||||
`while 下标 < 长度(列名)`
|
||||
|
||||
**表头 = list.append(表头, 转义单元格(列名[下标]))**
|
||||
**表头 = list.append(表头, 转义单元格(列名[下标]))**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
**行列表 = []**
|
||||
|
||||
**行列表 = list.append(行列表, "| " + 连接片段(表头) + " |")**
|
||||
@@ -334,32 +382,35 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列名)`
|
||||
`while 下标 < 长度(列名)`
|
||||
|
||||
**分隔 = list.append(分隔, "---")**
|
||||
**分隔 = list.append(分隔, "---")**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
**行列表 = list.append(行列表, "| " + 连接片段(分隔) + " |")**
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(数据)`
|
||||
`while 行号 < 长度(数据)`
|
||||
|
||||
**行 = table.row(数据, 行号)**
|
||||
**行 = table.row(数据, 行号)**
|
||||
|
||||
**单元格 = []**
|
||||
**单元格 = []**
|
||||
|
||||
**列号 = 0**
|
||||
**列号 = 0**
|
||||
|
||||
- `列号 < 长度(行)`
|
||||
`while 列号 < 长度(行)`
|
||||
|
||||
**单元格 = list.append(单元格, 转义单元格(行[列号]))**
|
||||
**单元格 = list.append(单元格, 转义单元格(行[列号]))**
|
||||
|
||||
**列号 = 列号 + 1**
|
||||
**列号 = 列号 + 1**
|
||||
|
||||
**行列表 = list.append(行列表, "| " + 连接片段(单元格) + " |")**
|
||||
`end`
|
||||
**行列表 = list.append(行列表, "| " + 连接片段(单元格) + " |")**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*连接行(行列表)*
|
||||
`end`
|
||||
`return 连接行(行列表)`
|
||||
+131
-90
@@ -1,98 +1,115 @@
|
||||
|
||||
# @exec datetime
|
||||
## 失败
|
||||
|
||||
`(消息)`
|
||||
`params(消息)`
|
||||
|
||||
*{ok: false, code: "datetime.invalid", message: 消息}*
|
||||
`return {ok: false, code: "datetime.invalid", message: 消息}`
|
||||
|
||||
## 闰年
|
||||
|
||||
`(年)`
|
||||
`params(年)`
|
||||
|
||||
*年 % 4 == 0 and (年 % 100 != 0 or 年 % 400 == 0)*
|
||||
`return 年 % 4 == 0 and (年 % 100 != 0 or 年 % 400 == 0)`
|
||||
|
||||
## 每月天数
|
||||
|
||||
`(年, 月)`
|
||||
`params(年, 月)`
|
||||
|
||||
1. `月 == 2`
|
||||
`if 月 == 2`
|
||||
|
||||
1. `闰年(年)`
|
||||
`if 闰年(年)`
|
||||
|
||||
*29*
|
||||
`return 29`
|
||||
|
||||
*28*
|
||||
`end`
|
||||
`return 28`
|
||||
|
||||
2. `月 == 4 or 月 == 6 or 月 == 9 or 月 == 11`
|
||||
`else`
|
||||
`if 月 == 4 or 月 == 6 or 月 == 9 or 月 == 11`
|
||||
|
||||
*30*
|
||||
`return 30`
|
||||
|
||||
*31*
|
||||
`end`
|
||||
`end`
|
||||
`return 31`
|
||||
|
||||
## 补零2
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
1. `值 < 10`
|
||||
`if 值 < 10`
|
||||
|
||||
*"0" + 文本(值)*
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
*文本(值)*
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 补零3
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
1. `值 < 10`
|
||||
`if 值 < 10`
|
||||
|
||||
*"00" + 文本(值)*
|
||||
`return "00" + 文本(值)`
|
||||
|
||||
2. `值 < 100`
|
||||
`else`
|
||||
`if 值 < 100`
|
||||
|
||||
*"0" + 文本(值)*
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
*文本(值)*
|
||||
`end`
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 补零4
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
1. `值 < 10`
|
||||
`if 值 < 10`
|
||||
|
||||
*"000" + 文本(值)*
|
||||
`return "000" + 文本(值)`
|
||||
|
||||
2. `值 < 100`
|
||||
`else`
|
||||
`if 值 < 100`
|
||||
|
||||
*"00" + 文本(值)*
|
||||
`return "00" + 文本(值)`
|
||||
|
||||
3. `值 < 1000`
|
||||
`else`
|
||||
`if 值 < 1000`
|
||||
|
||||
*"0" + 文本(值)*
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
*文本(值)*
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 月份
|
||||
|
||||
`(mp)`
|
||||
`params(mp)`
|
||||
|
||||
1. `mp < 10`
|
||||
`if mp < 10`
|
||||
|
||||
*mp + 3*
|
||||
`return mp + 3`
|
||||
|
||||
*mp - 9*
|
||||
`end`
|
||||
`return mp - 9`
|
||||
|
||||
## 年份
|
||||
|
||||
`(年, 月)`
|
||||
`params(年, 月)`
|
||||
|
||||
1. `月 <= 2`
|
||||
`if 月 <= 2`
|
||||
|
||||
*年 + 1*
|
||||
`return 年 + 1`
|
||||
|
||||
*年*
|
||||
`end`
|
||||
`return 年`
|
||||
|
||||
## 日期部件
|
||||
|
||||
`(天)`
|
||||
`params(天)`
|
||||
|
||||
Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
@@ -114,45 +131,51 @@ Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**月 = 月份(mp)**
|
||||
|
||||
*{年: 年份(年, 月), 月: 月, 日: 日}*
|
||||
`return {年: 年份(年, 月), 月: 月, 日: 日}`
|
||||
|
||||
## 日期序号
|
||||
|
||||
`(年, 月, 日)`
|
||||
`params(年, 月, 日)`
|
||||
|
||||
**y = 年**
|
||||
|
||||
1. `月 <= 2`
|
||||
`if 月 <= 2`
|
||||
|
||||
**y = y - 1**
|
||||
**y = y - 1**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**era = y // 400**
|
||||
|
||||
**yoe = y - era * 400**
|
||||
|
||||
**m = 月**
|
||||
|
||||
1. `月 > 2`
|
||||
`if 月 > 2`
|
||||
|
||||
**m = 月 - 3**
|
||||
**m = 月 - 3**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**m = 月 + 9**
|
||||
**m = 月 + 9**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**doy = (153 * m + 2) // 5 + 日 - 1**
|
||||
|
||||
**doe = yoe * 365 + yoe // 4 - yoe // 100 + doy**
|
||||
|
||||
*era * 146097 + doe - 719468*
|
||||
`return era * 146097 + doe - 719468`
|
||||
|
||||
## 是数字串
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -160,42 +183,47 @@ Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点) and 结果`
|
||||
`while 下标 < 长度(点) and 结果`
|
||||
|
||||
1. `点[下标] < 48 or 点[下标] > 57`
|
||||
`if 点[下标] < 48 or 点[下标] > 57`
|
||||
|
||||
**结果 = false**
|
||||
**结果 = false**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 片段
|
||||
|
||||
`(点, 起点, 终点)`
|
||||
`params(点, 起点, 终点)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
- `下标 < 终点`
|
||||
`while 下标 < 终点`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 格式化
|
||||
|
||||
`(毫秒)`
|
||||
`params(毫秒)`
|
||||
|
||||
输出 UTC ISO-8601:`YYYY-MM-DDTHH:MM:SSZ`;毫秒非零时追加
|
||||
`.mmm`。负数毫秒按 1970 前的 UTC 时间处理。
|
||||
输出 UTC ISO-8601:YYYY-MM-DDTHH:MM:SSZ;毫秒非零时追加
|
||||
.mmm。负数毫秒按 1970 前的 UTC 时间处理。
|
||||
|
||||
**秒 = 毫秒 // 1000**
|
||||
|
||||
@@ -215,29 +243,33 @@ Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**基 = 补零4(日期.年) + "-" + 补零2(日期.月) + "-" + 补零2(日期.日) + "T" + 补零2(时) + ":" + 补零2(分) + ":" + 补零2(秒数)**
|
||||
|
||||
1. `毫 == 0`
|
||||
`if 毫 == 0`
|
||||
|
||||
*基 + "Z"*
|
||||
`return 基 + "Z"`
|
||||
|
||||
*基 + "." + 补零3(毫) + "Z"*
|
||||
`end`
|
||||
`return 基 + "." + 补零3(毫) + "Z"`
|
||||
|
||||
## 解析
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
解析 `YYYY-MM-DDTHH:MM:SSZ` 或带 `.mmm` 的 UTC 文本,返回 Unix 毫秒。
|
||||
范围、闰年和格式错误都是可恢复 `datetime.invalid`。
|
||||
解析 YYYY-MM-DDTHH:MM:SSZ 或带 .mmm 的 UTC 文本,返回 Unix 毫秒。
|
||||
范围、闰年和格式错误都是可恢复 datetime.invalid。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
1. `长度(点) < 20 or 长度(点) > 24`
|
||||
`if 长度(点) < 20 or 长度(点) > 24`
|
||||
|
||||
*失败("长度不是 20–24 个码点")*
|
||||
`return 失败("长度不是 20–24 个码点")`
|
||||
|
||||
1. `点[4] != 45 or 点[7] != 45 or 点[10] != 84 or 点[13] != 58 or 点[16] != 58`
|
||||
`else`
|
||||
`if 点[4] != 45 or 点[7] != 45 or 点[10] != 84 or 点[13] != 58 or 点[16] != 58`
|
||||
|
||||
*失败("分隔符不符合 YYYY-MM-DDTHH:MM:SS")*
|
||||
`return 失败("分隔符不符合 YYYY-MM-DDTHH:MM:SS")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**年文本 = 片段(点, 0, 4)**
|
||||
|
||||
**月文本 = 片段(点, 5, 7)**
|
||||
@@ -250,10 +282,11 @@ Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**秒文本 = 片段(点, 17, 19)**
|
||||
|
||||
1. `not 是数字串(年文本) or not 是数字串(月文本) or not 是数字串(日文本) or not 是数字串(时文本) or not 是数字串(分文本) or not 是数字串(秒文本)`
|
||||
`if not 是数字串(年文本) or not 是数字串(月文本) or not 是数字串(日文本) or not 是数字串(时文本) or not 是数字串(分文本) or not 是数字串(秒文本)`
|
||||
|
||||
*失败("日期时间分量必须是数字")*
|
||||
`return 失败("日期时间分量必须是数字")`
|
||||
|
||||
`end`
|
||||
**年 = 整数(年文本)**
|
||||
|
||||
**月 = 整数(月文本)**
|
||||
@@ -270,30 +303,38 @@ Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**结束 = 20**
|
||||
|
||||
1. `长度(点) >= 24 and 点[19] == 46 and 点[23] == 90`
|
||||
`if 长度(点) >= 24 and 点[19] == 46 and 点[23] == 90`
|
||||
|
||||
**毫文本 = 片段(点, 20, 23)**
|
||||
**毫文本 = 片段(点, 20, 23)**
|
||||
|
||||
**结束 = 24**
|
||||
**结束 = 24**
|
||||
|
||||
1. `长度(点) != 结束 or 点[结束 - 1] != 90`
|
||||
`else`
|
||||
`if 长度(点) != 结束 or 点[结束 - 1] != 90`
|
||||
|
||||
*失败("必须以 Z 结尾")*
|
||||
`return 失败("必须以 Z 结尾")`
|
||||
|
||||
1. `not 是数字串(毫文本)`
|
||||
`else`
|
||||
`if not 是数字串(毫文本)`
|
||||
|
||||
*失败("毫秒必须是数字")*
|
||||
`return 失败("毫秒必须是数字")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**毫 = 整数(毫文本)**
|
||||
|
||||
1. `年 < 1970 or 月 < 1 or 月 > 12 or 日 < 1 or 日 > 每月天数(年, 月)`
|
||||
`if 年 < 1970 or 月 < 1 or 月 > 12 or 日 < 1 or 日 > 每月天数(年, 月)`
|
||||
|
||||
*失败("日期超出范围")*
|
||||
`return 失败("日期超出范围")`
|
||||
|
||||
1. `时 < 0 or 时 > 23 or 分 < 0 or 分 > 59 or 秒 < 0 or 秒 > 59`
|
||||
`else`
|
||||
`if 时 < 0 or 时 > 23 or 分 < 0 or 分 > 59 or 秒 < 0 or 秒 > 59`
|
||||
|
||||
*失败("时间超出范围")*
|
||||
`return 失败("时间超出范围")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**毫秒 = 日期序号(年, 月, 日) * 86400000 + 时 * 3600000 + 分 * 60000 + 秒 * 1000 + 毫**
|
||||
|
||||
*{ok: true, value: 毫秒}*
|
||||
`return {ok: true, value: 毫秒}`
|
||||
+14
-12
@@ -3,38 +3,40 @@ import:
|
||||
io: "./io.mq.md"
|
||||
---
|
||||
|
||||
## 演示函数
|
||||
## @exec 演示函数
|
||||
|
||||
*1*
|
||||
`return 1`
|
||||
|
||||
## main
|
||||
## @exec main
|
||||
|
||||
### 公式
|
||||
### @knowledge 公式
|
||||
|
||||
$$a = 1$$
|
||||
|
||||
### 代码
|
||||
### @knowledge 代码
|
||||
|
||||
```text
|
||||
print(1)
|
||||
```
|
||||
|
||||
### @exec 主流程
|
||||
|
||||
**列表 = ["文本", 2, 1.5, [3, true], {a: 1, b: "x"}, None]**
|
||||
|
||||
> io.写值(列表)
|
||||
> **io.写值(列表)**
|
||||
|
||||
> 打印(列表)
|
||||
> **打印(列表)**
|
||||
|
||||
**表格 = 表(["名称", "分数"], [["甲", 87], ["乙", 92]])**
|
||||
|
||||
> io.写值(表格)
|
||||
> **io.写值(表格)**
|
||||
|
||||
> 打印(表格)
|
||||
> **打印(表格)**
|
||||
|
||||
**特殊 = [ref("#公式"), ref("#代码"), ref("#演示函数")]**
|
||||
|
||||
> io.写值(特殊)
|
||||
> **io.写值(特殊)**
|
||||
|
||||
> 打印(特殊)
|
||||
> **打印(特殊)**
|
||||
|
||||
*None*
|
||||
`return None`
|
||||
+15
-10
@@ -1,25 +1,30 @@
|
||||
|
||||
# @exec env
|
||||
## 读取
|
||||
|
||||
`(名称)`
|
||||
`params(名称)`
|
||||
|
||||
读取一个显式授权的环境变量。名称是精确匹配,不支持 glob;未授权是
|
||||
`permission.denied`,未设置是 `env.not_found`,非 UTF-8 是
|
||||
`env.not_unicode`。
|
||||
permission.denied,未设置是 env.not_found,非 UTF-8 是
|
||||
env.not_unicode。
|
||||
|
||||
*resource.request("env.get", [名称])*
|
||||
`return resource.request("env.get", [名称])`
|
||||
|
||||
## 存在
|
||||
|
||||
`(名称)`
|
||||
`params(名称)`
|
||||
|
||||
**结果 = 读取(名称)**
|
||||
|
||||
1. `结果.ok`
|
||||
`if 结果.ok`
|
||||
|
||||
*{ok: true, value: true}*
|
||||
`return {ok: true, value: true}`
|
||||
|
||||
1. `结果.code == "env.not_found"`
|
||||
`else`
|
||||
`if 结果.code == "env.not_found"`
|
||||
|
||||
*{ok: true, value: false}*
|
||||
`return {ok: true, value: false}`
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`end`
|
||||
`return 结果`
|
||||
+173
-106
@@ -1,6 +1,8 @@
|
||||
|
||||
# @exec format
|
||||
## 字节长度
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -8,119 +10,156 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 < 128`
|
||||
`if 码点 < 128`
|
||||
|
||||
**总字节 = 总字节 + 1**
|
||||
**总字节 = 总字节 + 1**
|
||||
|
||||
2. `码点 < 2048`
|
||||
`else`
|
||||
`if 码点 < 2048`
|
||||
|
||||
**总字节 = 总字节 + 2**
|
||||
**总字节 = 总字节 + 2**
|
||||
|
||||
3. `码点 < 65536`
|
||||
`else`
|
||||
`if 码点 < 65536`
|
||||
|
||||
**总字节 = 总字节 + 3**
|
||||
**总字节 = 总字节 + 3**
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**总字节 = 总字节 + 4**
|
||||
**总字节 = 总字节 + 4**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*总字节*
|
||||
`end`
|
||||
`return 总字节`
|
||||
|
||||
## 显示
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
按语言值语义格式化,用于与内置 `打印` 的输出对照。
|
||||
按语言值语义格式化,用于与内置 打印 的输出对照。
|
||||
覆盖 Unit/None/Bool/Int/Float/Text/List/Map/Table/Formula/Code/
|
||||
FunctionRef/PluginHandle/Memory/Resource。
|
||||
|
||||
**类别 = 类型(值)**
|
||||
|
||||
1. `类别 == "Unit"`
|
||||
`if 类别 == "Unit"`
|
||||
|
||||
*"Unit"*
|
||||
`return "Unit"`
|
||||
|
||||
2. `类别 == "None"`
|
||||
`else`
|
||||
`if 类别 == "None"`
|
||||
|
||||
*"None"*
|
||||
`return "None"`
|
||||
|
||||
3. `类别 == "Text"`
|
||||
`else`
|
||||
`if 类别 == "Text"`
|
||||
|
||||
*值*
|
||||
`return 值`
|
||||
|
||||
4. `类别 == "List"`
|
||||
`else`
|
||||
`if 类别 == "List"`
|
||||
|
||||
*列表显示(值)*
|
||||
`return 列表显示(值)`
|
||||
|
||||
5. `类别 == "Map"`
|
||||
`else`
|
||||
`if 类别 == "Map"`
|
||||
|
||||
*映射显示(值)*
|
||||
`return 映射显示(值)`
|
||||
|
||||
6. `类别 == "Table"`
|
||||
`else`
|
||||
`if 类别 == "Table"`
|
||||
|
||||
*表格显示(值)*
|
||||
`return 表格显示(值)`
|
||||
|
||||
7. `类别 == "Formula"`
|
||||
`else`
|
||||
`if 类别 == "Formula"`
|
||||
|
||||
*"Formula(" + 转义文本(值.latex) + ")"*
|
||||
`return "Formula(" + 转义文本(值.latex) + ")"`
|
||||
|
||||
8. `类别 == "Code"`
|
||||
`else`
|
||||
`if 类别 == "Code"`
|
||||
|
||||
*"Code(" + 转义文本(值.language) + ", " + 文本(字节长度(值.source)) + " bytes)"*
|
||||
`return "Code(" + 转义文本(值.language) + ", " + 文本(字节长度(值.source)) + " bytes)"`
|
||||
|
||||
9. `类别 == "FunctionRef"`
|
||||
`else`
|
||||
`if 类别 == "FunctionRef"`
|
||||
|
||||
*"FunctionRef(" + 值.module + "::" + 值.name + ")"*
|
||||
`return "FunctionRef(" + 值.module + "::" + 值.name + ")"`
|
||||
|
||||
10. `类别 == "PluginHandle"`
|
||||
`else`
|
||||
`if 类别 == "PluginHandle"`
|
||||
|
||||
*"PluginHandle(" + 值.plugin + "::" + 值.export + ")"*
|
||||
`return "PluginHandle(" + 值.plugin + "::" + 值.export + ")"`
|
||||
|
||||
11. `类别 == "Memory"`
|
||||
`else`
|
||||
`if 类别 == "Memory"`
|
||||
|
||||
*"Memory"*
|
||||
`return "Memory"`
|
||||
|
||||
12. `类别 == "Resource"`
|
||||
`else`
|
||||
`if 类别 == "Resource"`
|
||||
|
||||
*"Resource"*
|
||||
`return "Resource"`
|
||||
|
||||
13. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*文本(值)*
|
||||
`return 文本(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 列表显示
|
||||
|
||||
`(列表)`
|
||||
`params(列表)`
|
||||
|
||||
**结果 = "["**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表)`
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + ", "**
|
||||
**结果 = 结果 + ", "**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 值显示(列表[下标])**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 值显示(列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果 + "]"*
|
||||
`end`
|
||||
`return 结果 + "]"`
|
||||
|
||||
## 映射显示
|
||||
|
||||
`(映射)`
|
||||
`params(映射)`
|
||||
|
||||
**键 = map.keys(映射)**
|
||||
|
||||
@@ -128,27 +167,31 @@ FunctionRef/PluginHandle/Memory/Resource。
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(键)`
|
||||
`while 下标 < 长度(键)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + ", "**
|
||||
**结果 = 结果 + ", "**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**当前键 = 键[下标]**
|
||||
`end`
|
||||
`end`
|
||||
**当前键 = 键[下标]**
|
||||
|
||||
**结果 = 结果 + 当前键 + ": " + 值显示(map.get(映射, 当前键))**
|
||||
**结果 = 结果 + 当前键 + ": " + 值显示(map.get(映射, 当前键))**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果 + "}"*
|
||||
`end`
|
||||
`return 结果 + "}"`
|
||||
|
||||
## 表格显示
|
||||
|
||||
`(表格)`
|
||||
`params(表格)`
|
||||
|
||||
**列 = table.columns(表格)**
|
||||
|
||||
@@ -156,77 +199,89 @@ FunctionRef/PluginHandle/Memory/Resource。
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(表格)`
|
||||
`while 行号 < 长度(表格)`
|
||||
|
||||
1. `行号 > 0`
|
||||
`if 行号 > 0`
|
||||
|
||||
**结果 = 结果 + "; "**
|
||||
**结果 = 结果 + "; "**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**行 = table.row(表格, 行号)**
|
||||
`end`
|
||||
`end`
|
||||
**行 = table.row(表格, 行号)**
|
||||
|
||||
**单元格 = []**
|
||||
**单元格 = []**
|
||||
|
||||
**列号 = 0**
|
||||
**列号 = 0**
|
||||
|
||||
- `列号 < 长度(行)`
|
||||
`while 列号 < 长度(行)`
|
||||
|
||||
**单元格 = list.append(单元格, 显示(行[列号]))**
|
||||
**单元格 = list.append(单元格, 显示(行[列号]))**
|
||||
|
||||
**列号 = 列号 + 1**
|
||||
**列号 = 列号 + 1**
|
||||
|
||||
**结果 = 结果 + 连接文本(单元格, " | ")**
|
||||
`end`
|
||||
**结果 = 结果 + 连接文本(单元格, " | ")**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*结果 + "}"*
|
||||
`end`
|
||||
`return 结果 + "}"`
|
||||
|
||||
## 连接文本
|
||||
|
||||
`(列表, 分隔)`
|
||||
`params(列表, 分隔)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表)`
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
1. `下标 > 0`
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + 分隔**
|
||||
**结果 = 结果 + 分隔**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 列表[下标]**
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 列表[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 值显示
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
列表/映射/表格内部使用 repr 语义:Text 带引号并转义,其它值递归显示。
|
||||
|
||||
**类别 = 类型(值)**
|
||||
|
||||
1. `类别 == "Text"`
|
||||
`if 类别 == "Text"`
|
||||
|
||||
*转义文本(值)*
|
||||
`return 转义文本(值)`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*显示(值)*
|
||||
`return 显示(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 转义文本
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -234,34 +289,46 @@ FunctionRef/PluginHandle/Memory/Resource。
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 == 34`
|
||||
`if 码点 == 34`
|
||||
|
||||
**`结果 = 结果 + "\\\""`**
|
||||
**`结果 = 结果 + "\\\""`**
|
||||
|
||||
2. `码点 == 92`
|
||||
`else`
|
||||
`if 码点 == 92`
|
||||
|
||||
**`结果 = 结果 + "\\\\"`**
|
||||
**`结果 = 结果 + "\\\\"`**
|
||||
|
||||
3. `码点 == 10`
|
||||
`else`
|
||||
`if 码点 == 10`
|
||||
|
||||
**`结果 = 结果 + "\\n"`**
|
||||
**`结果 = 结果 + "\\n"`**
|
||||
|
||||
4. `码点 == 9`
|
||||
`else`
|
||||
`if 码点 == 9`
|
||||
|
||||
**`结果 = 结果 + "\\t"`**
|
||||
**`结果 = 结果 + "\\t"`**
|
||||
|
||||
5. `码点 == 13`
|
||||
`else`
|
||||
`if 码点 == 13`
|
||||
|
||||
**`结果 = 结果 + "\\r"`**
|
||||
**`结果 = 结果 + "\\r"`**
|
||||
|
||||
6. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(码点)**
|
||||
**结果 = 结果 + text.from_codepoint(码点)**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*`结果 + "\""`*
|
||||
`end`
|
||||
``return 结果 + "\""``
|
||||
+100
-69
@@ -3,12 +3,14 @@ import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec file
|
||||
|
||||
## 读文本
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
以 UTF-8 读取一个有界的项目文件。文件在打开时整文件载入,上限 8 MiB;
|
||||
结果使用可恢复 `Result` 形状。
|
||||
结果使用可恢复 Result 形状。
|
||||
|
||||
**流 = resource.open("file", 路径, ["read"])**
|
||||
|
||||
@@ -18,32 +20,40 @@ import:
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
`if not 读取.ok`
|
||||
|
||||
*读取*
|
||||
`return 读取`
|
||||
|
||||
2. `not 关闭.ok`
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
`return 关闭`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**文本 = utf8.解码(内存, 0, 读取.value.count)**
|
||||
**文本 = utf8.解码(内存, 0, 读取.value.count)**
|
||||
|
||||
1. `not 文本.ok`
|
||||
`if not 文本.ok`
|
||||
|
||||
*文本*
|
||||
`return 文本`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: 文本.value}*
|
||||
`return {ok: true, value: 文本.value}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`(路径, 文本)`
|
||||
`params(路径, 文本)`
|
||||
|
||||
把文本按 UTF-8 写入项目文件。路径必须已经通过 `fs_write` 授权;
|
||||
文件只在 `resource.close` 成功时创建或覆盖,写入缓冲上限 8 MiB。
|
||||
把文本按 UTF-8 写入项目文件。路径必须已经通过 fs_write 授权;
|
||||
文件只在 resource.close 成功时创建或覆盖,写入缓冲上限 8 MiB。
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
@@ -55,24 +65,29 @@ import:
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 写入.ok`
|
||||
`if not 写入.ok`
|
||||
|
||||
*写入*
|
||||
`return 写入`
|
||||
|
||||
2. `not 关闭.ok`
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
`return 关闭`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: {count: 写入.value.count}}*
|
||||
`return {ok: true, value: {count: 写入.value.count}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 复制文件
|
||||
|
||||
`(源, 目标)`
|
||||
`params(源, 目标)`
|
||||
|
||||
按字节复制一个有界的项目文件。源读取和目标写入分别需要
|
||||
`fs_read`/`fs_write` 授权,结果值与 [`写文本`](#写文本) 相同。
|
||||
fs_read/fs_write 授权,结果值与 [写文本](#写文本) 相同。
|
||||
|
||||
**输入 = resource.open("file", 源, ["read"])**
|
||||
|
||||
@@ -82,125 +97,141 @@ import:
|
||||
|
||||
**关闭输入 = resource.close(输入)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
`if not 读取.ok`
|
||||
|
||||
*读取*
|
||||
`return 读取`
|
||||
|
||||
2. `not 关闭输入.ok`
|
||||
`else`
|
||||
`if not 关闭输入.ok`
|
||||
|
||||
*关闭输入*
|
||||
`return 关闭输入`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**输出 = resource.open("file", 目标, ["write"])**
|
||||
**输出 = resource.open("file", 目标, ["write"])**
|
||||
|
||||
**写入 = resource.write(输出, 内存, 0, 读取.value.count)**
|
||||
**写入 = resource.write(输出, 内存, 0, 读取.value.count)**
|
||||
|
||||
**关闭输出 = resource.close(输出)**
|
||||
**关闭输出 = resource.close(输出)**
|
||||
|
||||
1. `not 写入.ok`
|
||||
`if not 写入.ok`
|
||||
|
||||
*写入*
|
||||
`return 写入`
|
||||
|
||||
2. `not 关闭输出.ok`
|
||||
`else`
|
||||
`if not 关闭输出.ok`
|
||||
|
||||
*关闭输出*
|
||||
`return 关闭输出`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: {count: 写入.value.count}}*
|
||||
`return {ok: true, value: {count: 写入.value.count}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 存在
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
*resource.request("fs.exists", [路径])*
|
||||
`return resource.request("fs.exists", [路径])`
|
||||
|
||||
## 元数据
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
*resource.request("fs.metadata", [路径])*
|
||||
`return resource.request("fs.metadata", [路径])`
|
||||
|
||||
## 是文件
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**元数据 = resource.request("fs.metadata", [路径])**
|
||||
|
||||
1. `not 元数据.ok`
|
||||
`if not 元数据.ok`
|
||||
|
||||
*元数据*
|
||||
`return 元数据`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: 元数据.value.kind == "file"}*
|
||||
`return {ok: true, value: 元数据.value.kind == "file"}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 是目录
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**元数据 = resource.request("fs.metadata", [路径])**
|
||||
|
||||
1. `not 元数据.ok`
|
||||
`if not 元数据.ok`
|
||||
|
||||
*元数据*
|
||||
`return 元数据`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: 元数据.value.kind == "directory"}*
|
||||
`return {ok: true, value: 元数据.value.kind == "directory"}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 列出目录
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
*resource.request("fs.list", [路径])*
|
||||
`return resource.request("fs.list", [路径])`
|
||||
|
||||
## 创建目录
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
*resource.request("fs.mkdir", [路径])*
|
||||
`return resource.request("fs.mkdir", [路径])`
|
||||
|
||||
## 删除
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
删除一个文件或空目录。非空目录返回 `fs.not_empty`,不递归删除。
|
||||
删除一个文件或空目录。非空目录返回 fs.not_empty,不递归删除。
|
||||
|
||||
*resource.request("fs.remove", [路径])*
|
||||
`return resource.request("fs.remove", [路径])`
|
||||
|
||||
## 重命名
|
||||
|
||||
`(源, 目标)`
|
||||
`params(源, 目标)`
|
||||
|
||||
目标已存在时返回 `fs.exists`,不覆盖。
|
||||
目标已存在时返回 fs.exists,不覆盖。
|
||||
|
||||
*resource.request("fs.rename", [源, 目标])*
|
||||
`return resource.request("fs.rename", [源, 目标])`
|
||||
|
||||
## 追加文本
|
||||
|
||||
`(路径, 文本)`
|
||||
`params(路径, 文本)`
|
||||
|
||||
把文本追加到项目文件;文件不存在时创建。仍受 8 MiB 单次读取上限和
|
||||
`fs_write` 授权约束。
|
||||
fs_write 授权约束。
|
||||
|
||||
*resource.request("fs.append", [路径, 文本])*
|
||||
`return resource.request("fs.append", [路径, 文本])`
|
||||
|
||||
## 读取片段
|
||||
|
||||
`(路径, 偏移, 长度)`
|
||||
`params(路径, 偏移, 长度)`
|
||||
|
||||
从偏移处读取最多 `长度` 字节,按 UTF-8 解码后返回文本。偏移/长度必须
|
||||
从偏移处读取最多 长度 字节,按 UTF-8 解码后返回文本。偏移/长度必须
|
||||
是非负整数,长度上限 8 MiB。
|
||||
|
||||
*resource.request("fs.read_at", [路径, 偏移, 长度])*
|
||||
`return resource.request("fs.read_at", [路径, 偏移, 长度])`
|
||||
|
||||
## 遍历
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
递归列出目录下的普通文件,返回相对项目根的路径列表。跳过符号链接,
|
||||
深度上限 64,条目上限 4096。
|
||||
|
||||
*resource.request("fs.walk", [路径])*
|
||||
`return resource.request("fs.walk", [路径])`
|
||||
+12
-10
@@ -5,34 +5,36 @@ import:
|
||||
console: "./io/console.mq.md"
|
||||
---
|
||||
|
||||
# @exec io
|
||||
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
*stream.写文本(文本)*
|
||||
`return stream.写文本(文本)`
|
||||
|
||||
## 写全部
|
||||
|
||||
`(流, 内存, 起点, 长度)`
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
*stream.写全部(流, 内存, 起点, 长度)*
|
||||
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||
|
||||
## 写值
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
按语言值语义格式化后输出,并追加换行。
|
||||
|
||||
*stream.写文本(format.显示(值) + "\n")*
|
||||
`return stream.写文本(format.显示(值) + "\n")`
|
||||
|
||||
## 读取行
|
||||
|
||||
`()`
|
||||
`params()`
|
||||
|
||||
*console.读取行()*
|
||||
`return console.读取行()`
|
||||
|
||||
## 打印
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
*console.打印(值)*
|
||||
`return console.打印(值)`
|
||||
+73
-46
@@ -5,12 +5,14 @@ import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec console
|
||||
|
||||
## 读取行
|
||||
|
||||
`()`
|
||||
`params()`
|
||||
|
||||
从已授权的 `stdio/stdin` 资源读取一行。按有界块读取并增量解码 UTF-8;
|
||||
EOF 返回 `{ok:false, code:"io.eof"}`,无效 UTF-8 返回可恢复错误。
|
||||
从已授权的 stdio/stdin 资源读取一行。按有界块读取并增量解码 UTF-8;
|
||||
EOF 返回 {ok:false, code:"io.eof"},无效 UTF-8 返回可恢复错误。
|
||||
|
||||
**流 = resource.open("stdio", "stdin", ["read"])**
|
||||
|
||||
@@ -22,98 +24,123 @@ EOF 返回 `{ok:false, code:"io.eof"}`,无效 UTF-8 返回可恢复错误。
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `not 结束`
|
||||
`while not 结束`
|
||||
|
||||
**读取 = resource.read(流, 内存, 0, 1)**
|
||||
**读取 = resource.read(流, 内存, 0, 1)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
`if not 读取.ok`
|
||||
|
||||
**结束 = true**
|
||||
**结束 = true**
|
||||
|
||||
**错误 = 读取.code**
|
||||
**错误 = 读取.code**
|
||||
|
||||
2. `读取.value.eof and 读取.value.count == 0`
|
||||
`else`
|
||||
`if 读取.value.eof and 读取.value.count == 0`
|
||||
|
||||
**结束 = true**
|
||||
**结束 = true**
|
||||
|
||||
3. `内存[0] == 10`
|
||||
`else`
|
||||
`if 内存[0] == 10`
|
||||
|
||||
**结束 = true**
|
||||
**结束 = true**
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**字节 = list.append(字节, 内存[0])**
|
||||
**字节 = list.append(字节, 内存[0])**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `错误 != ""`
|
||||
`if 错误 != ""`
|
||||
|
||||
*{ok: false, code: 错误, message: "stdin read failed"}*
|
||||
`return {ok: false, code: 错误, message: "stdin read failed"}`
|
||||
|
||||
2. `not 关闭.ok`
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
`return 关闭`
|
||||
|
||||
3. `长度(字节) == 0`
|
||||
`else`
|
||||
`if 长度(字节) == 0`
|
||||
|
||||
*{ok: false, code: "io.eof", message: "end of input"}*
|
||||
`return {ok: false, code: "io.eof", message: "end of input"}`
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**数据长度 = 长度(字节)**
|
||||
**数据长度 = 长度(字节)**
|
||||
|
||||
1. `数据长度 > 0 and 字节[数据长度 - 1] == 13`
|
||||
`if 数据长度 > 0 and 字节[数据长度 - 1] == 13`
|
||||
|
||||
**数据长度 = 数据长度 - 1**
|
||||
**数据长度 = 数据长度 - 1**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**页 = (数据长度 + 65535) // 65536**
|
||||
`end`
|
||||
`end`
|
||||
**页 = (数据长度 + 65535) // 65536**
|
||||
|
||||
**数据 = memory.new(页, 页)**
|
||||
**数据 = memory.new(页, 页)**
|
||||
|
||||
**下标 = 0**
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 数据长度`
|
||||
`while 下标 < 数据长度`
|
||||
|
||||
**`数据[下标] = 字节[下标]`**
|
||||
**`数据[下标] = 字节[下标]`**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
**解码 = utf8.解码(数据, 0, 数据长度)**
|
||||
`end`
|
||||
**解码 = utf8.解码(数据, 0, 数据长度)**
|
||||
|
||||
1. `not 解码.ok`
|
||||
`if not 解码.ok`
|
||||
|
||||
*解码*
|
||||
`return 解码`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: 解码.value}*
|
||||
`return {ok: true, value: 解码.value}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 打印
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
**结果 = stream.写文本(format.显示(值) + "\n")**
|
||||
|
||||
1. `not 结果.ok`
|
||||
`if not 结果.ok`
|
||||
|
||||
*结果*
|
||||
`return 结果`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: None}*
|
||||
`return {ok: true, value: None}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
*stream.写文本(文本)*
|
||||
`return stream.写文本(文本)`
|
||||
|
||||
## 写全部
|
||||
|
||||
`(流, 内存, 起点, 长度)`
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
*stream.写全部(流, 内存, 起点, 长度)*
|
||||
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||
+55
-34
@@ -4,26 +4,31 @@ import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec stream
|
||||
|
||||
## 页数
|
||||
|
||||
`(字节)`
|
||||
`params(字节)`
|
||||
|
||||
**页 = (字节 + 65535) // 65536**
|
||||
|
||||
1. `页 == 0`
|
||||
`if 页 == 0`
|
||||
|
||||
*1*
|
||||
`return 1`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*页*
|
||||
`return 页`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 写全部
|
||||
|
||||
`(流, 内存, 起点, 长度)`
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
把 `长度` 字节从 `内存` 写到资源流。短写只返回实际数量,调用方继续;
|
||||
零进度返回 `io.write_zero`,不静默吞掉失败。
|
||||
把 长度 字节从 内存 写到资源流。短写只返回实际数量,调用方继续;
|
||||
零进度返回 io.write_zero,不静默吞掉失败。
|
||||
|
||||
**已写 = 0**
|
||||
|
||||
@@ -33,48 +38,59 @@ import:
|
||||
|
||||
**错误结果 = {ok: false, code: "io.write_zero", message: "resource.write made no progress"}**
|
||||
|
||||
- `已写 < 长度`
|
||||
`while 已写 < 长度`
|
||||
|
||||
**剩余 = 长度 - 已写**
|
||||
**剩余 = 长度 - 已写**
|
||||
|
||||
**本次 = resource.write(流, 内存, 起点 + 已写, 剩余)**
|
||||
**本次 = resource.write(流, 内存, 起点 + 已写, 剩余)**
|
||||
|
||||
1. `not 本次.ok`
|
||||
`if not 本次.ok`
|
||||
|
||||
**失败 = true**
|
||||
**失败 = true**
|
||||
|
||||
**错误结果 = 本次**
|
||||
**错误结果 = 本次**
|
||||
|
||||
**已写 = 长度**
|
||||
**已写 = 长度**
|
||||
|
||||
2. `本次.value.count == 0`
|
||||
`else`
|
||||
`if 本次.value.count == 0`
|
||||
|
||||
**失败 = true**
|
||||
**失败 = true**
|
||||
|
||||
**已写 = 长度**
|
||||
**已写 = 长度**
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**已写 = 已写 + 本次.value.count**
|
||||
**已写 = 已写 + 本次.value.count**
|
||||
|
||||
1. `失败`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 失败`
|
||||
|
||||
*错误结果*
|
||||
`return 错误结果`
|
||||
|
||||
2. `已写 < 长度`
|
||||
`else`
|
||||
`if 已写 < 长度`
|
||||
|
||||
*错误结果*
|
||||
`return 错误结果`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: {count: 长度}}*
|
||||
`return {ok: true, value: {count: 长度}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。内存按实际字节长度增长,
|
||||
写入循环检查 `{ok,count}`;只有全部字节写完并关闭成功才返回成功。
|
||||
写入循环检查 {ok,count};只有全部字节写完并关闭成功才返回成功。
|
||||
|
||||
**字节数 = format.字节长度(文本)**
|
||||
|
||||
@@ -88,14 +104,19 @@ import:
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 写结果.ok`
|
||||
`if not 写结果.ok`
|
||||
|
||||
*写结果*
|
||||
`return 写结果`
|
||||
|
||||
2. `not 关闭.ok`
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
`return 关闭`
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*{ok: true, value: {count: 长度}}*
|
||||
`return {ok: true, value: {count: 长度}}`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
+184
-126
@@ -3,117 +3,140 @@ import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec http
|
||||
|
||||
## 切片字节
|
||||
|
||||
`(字节, 起, 止)`
|
||||
`params(字节, 起, 止)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 起**
|
||||
|
||||
- `下标 < 止 and 下标 < 长度(字节)`
|
||||
`while 下标 < 止 and 下标 < 长度(字节)`
|
||||
|
||||
**结果 = list.append(结果, 字节[下标])**
|
||||
**结果 = list.append(结果, 字节[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 找子串
|
||||
|
||||
`(字节, 子, 起点)`
|
||||
`params(字节, 子, 起点)`
|
||||
|
||||
**结果 = 0 - 1**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
- `下标 + 长度(子) <= 长度(字节) and 结果 < 0`
|
||||
`while 下标 + 长度(子) <= 长度(字节) and 结果 < 0`
|
||||
|
||||
**偏移 = 0**
|
||||
**偏移 = 0**
|
||||
|
||||
**匹配 = true**
|
||||
**匹配 = true**
|
||||
|
||||
- `偏移 < 长度(子) and 匹配`
|
||||
`while 偏移 < 长度(子) and 匹配`
|
||||
|
||||
1. `字节[下标 + 偏移] != 子[偏移]`
|
||||
`if 字节[下标 + 偏移] != 子[偏移]`
|
||||
|
||||
**匹配 = false**
|
||||
**匹配 = false**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
1. `匹配`
|
||||
`end`
|
||||
`if 匹配`
|
||||
|
||||
**结果 = 下标**
|
||||
**结果 = 下标**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 找字符
|
||||
|
||||
`(字节, 值, 起点)`
|
||||
`params(字节, 值, 起点)`
|
||||
|
||||
**结果 = 0 - 1**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
- `下标 < 长度(字节) and 结果 < 0`
|
||||
`while 下标 < 长度(字节) and 结果 < 0`
|
||||
|
||||
1. `字节[下标] == 值`
|
||||
`if 字节[下标] == 值`
|
||||
|
||||
**结果 = 下标**
|
||||
**结果 = 下标**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 十六进制值
|
||||
|
||||
`(字节)`
|
||||
`params(字节)`
|
||||
|
||||
**数值 = 0**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(字节)`
|
||||
`while 下标 < 长度(字节)`
|
||||
|
||||
**值 = 字节[下标]**
|
||||
**值 = 字节[下标]**
|
||||
|
||||
1. `值 >= 48 and 值 <= 57`
|
||||
`if 值 >= 48 and 值 <= 57`
|
||||
|
||||
**数值 = 数值 * 16 + 值 - 48**
|
||||
**数值 = 数值 * 16 + 值 - 48**
|
||||
|
||||
2. `值 >= 65 and 值 <= 70`
|
||||
`else`
|
||||
`if 值 >= 65 and 值 <= 70`
|
||||
|
||||
**数值 = 数值 * 16 + 值 - 55**
|
||||
**数值 = 数值 * 16 + 值 - 55**
|
||||
|
||||
3. `值 >= 97 and 值 <= 102`
|
||||
`else`
|
||||
`if 值 >= 97 and 值 <= 102`
|
||||
|
||||
**数值 = 数值 * 16 + 值 - 87**
|
||||
**数值 = 数值 * 16 + 值 - 87**
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*数值*
|
||||
`end`
|
||||
`return 数值`
|
||||
|
||||
## 文本字节
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
把 UTF-8 文本编码为字节列表,上限 8 MiB。
|
||||
|
||||
@@ -125,43 +148,46 @@ import:
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 字节数`
|
||||
`while 下标 < 字节数`
|
||||
|
||||
**结果 = list.append(结果, 内存[下标])**
|
||||
**结果 = list.append(结果, 内存[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 字节文本
|
||||
|
||||
`(字节)`
|
||||
`params(字节)`
|
||||
|
||||
把字节列表按 UTF-8 解码为文本,上限 8 MiB。
|
||||
|
||||
**长度值 = 长度(字节)**
|
||||
|
||||
1. `长度值 == 0`
|
||||
`if 长度值 == 0`
|
||||
|
||||
*{ok: true, value: ""}*
|
||||
`return {ok: true, value: ""}`
|
||||
|
||||
`end`
|
||||
**页 = (长度值 + 65535) // 65536**
|
||||
|
||||
**内存 = memory.new(页, 页)**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度值`
|
||||
`while 下标 < 长度值`
|
||||
|
||||
**内存[下标] = 字节[下标]**
|
||||
**内存[下标] = 字节[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*utf8.解码(内存, 0, 长度值)*
|
||||
`end`
|
||||
`return utf8.解码(内存, 0, 长度值)`
|
||||
|
||||
## 解码分块字节
|
||||
|
||||
`(体)`
|
||||
`params(体)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
@@ -169,51 +195,62 @@ import:
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `位置 < 长度(体) and 错误 == ""`
|
||||
`while 位置 < 长度(体) and 错误 == ""`
|
||||
|
||||
**行尾 = 找子串(体, [13, 10], 位置)**
|
||||
**行尾 = 找子串(体, [13, 10], 位置)**
|
||||
|
||||
1. `行尾 < 0`
|
||||
`if 行尾 < 0`
|
||||
|
||||
**错误 = "http.chunked"**
|
||||
**错误 = "http.chunked"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**大小 = 十六进制值(切片字节(体, 位置, 行尾))**
|
||||
**大小 = 十六进制值(切片字节(体, 位置, 行尾))**
|
||||
|
||||
1. `大小 == 0`
|
||||
`if 大小 == 0`
|
||||
|
||||
**位置 = 长度(体)**
|
||||
**位置 = 长度(体)**
|
||||
|
||||
2. `位置 + 大小 + 4 > 长度(体)`
|
||||
`else`
|
||||
`if 位置 + 大小 + 4 > 长度(体)`
|
||||
|
||||
**错误 = "http.chunked"**
|
||||
**错误 = "http.chunked"**
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = list.concat(结果, 切片字节(体, 行尾 + 2, 行尾 + 2 + 大小))**
|
||||
**结果 = list.concat(结果, 切片字节(体, 行尾 + 2, 行尾 + 2 + 大小))**
|
||||
|
||||
**位置 = 行尾 + 2 + 大小 + 2**
|
||||
**位置 = 行尾 + 2 + 大小 + 2**
|
||||
|
||||
1. `错误 != ""`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 错误 != ""`
|
||||
|
||||
*{ok: false, code: 错误, message: "分块长度无效"}*
|
||||
`return {ok: false, code: 错误, message: "分块长度无效"}`
|
||||
|
||||
*{ok: true, value: 结果}*
|
||||
`end`
|
||||
`return {ok: true, value: 结果}`
|
||||
|
||||
## 解析响应字节
|
||||
|
||||
`(字节)`
|
||||
`params(字节)`
|
||||
|
||||
解析 HTTP/1.1 响应字节。头部按 ASCII 解析,正文保持原始字节;chunked
|
||||
长度以字节计。成功返回 `{ok:true, value:{status, reason, headers, body}}`。
|
||||
长度以字节计。成功返回 {ok:true, value:{status, reason, headers, body}}。
|
||||
|
||||
**头部结束 = 找子串(字节, [13, 10, 13, 10], 0)**
|
||||
|
||||
1. `头部结束 < 0`
|
||||
`if 头部结束 < 0`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "缺少头部结束"}*
|
||||
`return {ok: false, code: "http.invalid", message: "缺少头部结束"}`
|
||||
|
||||
`end`
|
||||
**头部 = 切片字节(字节, 0, 头部结束)**
|
||||
|
||||
**体 = 切片字节(字节, 头部结束 + 4, 长度(字节))**
|
||||
@@ -224,123 +261,142 @@ import:
|
||||
|
||||
**下一行 = 找子串(头部, [13, 10], 0)**
|
||||
|
||||
- `下一行 >= 0`
|
||||
`while 下一行 >= 0`
|
||||
|
||||
**行列表 = list.append(行列表, 切片字节(头部, 行起, 下一行))**
|
||||
**行列表 = list.append(行列表, 切片字节(头部, 行起, 下一行))**
|
||||
|
||||
**行起 = 下一行 + 2**
|
||||
**行起 = 下一行 + 2**
|
||||
|
||||
**下一行 = 找子串(头部, [13, 10], 行起)**
|
||||
**下一行 = 找子串(头部, [13, 10], 行起)**
|
||||
|
||||
`end`
|
||||
**行列表 = list.append(行列表, 切片字节(头部, 行起, 长度(头部)))**
|
||||
|
||||
1. `长度(行列表) == 0`
|
||||
`if 长度(行列表) == 0`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "缺少状态行"}*
|
||||
`return {ok: false, code: "http.invalid", message: "缺少状态行"}`
|
||||
|
||||
`end`
|
||||
**状态行 = 字节文本(行列表[0])**
|
||||
|
||||
1. `not 状态行.ok`
|
||||
`if not 状态行.ok`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "状态行不是 ASCII"}*
|
||||
`return {ok: false, code: "http.invalid", message: "状态行不是 ASCII"}`
|
||||
|
||||
`end`
|
||||
**状态字节 = 行列表[0]**
|
||||
|
||||
**第一空格 = 找字符(状态字节, 32, 0)**
|
||||
|
||||
**第二空格 = 找字符(状态字节, 32, 第一空格 + 1)**
|
||||
|
||||
1. `第一空格 < 0 or 第二空格 < 0`
|
||||
`if 第一空格 < 0 or 第二空格 < 0`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "状态行格式无效"}*
|
||||
`return {ok: false, code: "http.invalid", message: "状态行格式无效"}`
|
||||
|
||||
`end`
|
||||
**状态文本 = 字节文本(切片字节(状态字节, 第一空格 + 1, 第二空格))**
|
||||
|
||||
1. `not 状态文本.ok`
|
||||
`if not 状态文本.ok`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "状态码不是 ASCII"}*
|
||||
`return {ok: false, code: "http.invalid", message: "状态码不是 ASCII"}`
|
||||
|
||||
`end`
|
||||
**状态 = 整数(状态文本.value)**
|
||||
|
||||
**原因 = 字节文本(切片字节(状态字节, 第二空格 + 1, 长度(状态字节)))**
|
||||
|
||||
1. `not 原因.ok`
|
||||
`if not 原因.ok`
|
||||
|
||||
*{ok: false, code: "http.invalid", message: "原因短语不是 ASCII"}*
|
||||
`return {ok: false, code: "http.invalid", message: "原因短语不是 ASCII"}`
|
||||
|
||||
`end`
|
||||
**头 = {}**
|
||||
|
||||
**行号 = 1**
|
||||
|
||||
- `行号 < 长度(行列表)`
|
||||
`while 行号 < 长度(行列表)`
|
||||
|
||||
**行 = 行列表[行号]**
|
||||
**行 = 行列表[行号]**
|
||||
|
||||
**冒号 = 找字符(行, 58, 0)**
|
||||
**冒号 = 找字符(行, 58, 0)**
|
||||
|
||||
1. `冒号 >= 0`
|
||||
`if 冒号 >= 0`
|
||||
|
||||
**名 = 字节文本(切片字节(行, 0, 冒号))**
|
||||
**名 = 字节文本(切片字节(行, 0, 冒号))**
|
||||
|
||||
**值 = 字节文本(切片字节(行, 冒号 + 2, 长度(行)))**
|
||||
**值 = 字节文本(切片字节(行, 冒号 + 2, 长度(行)))**
|
||||
|
||||
1. `名.ok and 值.ok`
|
||||
`if 名.ok and 值.ok`
|
||||
|
||||
**头 = map.set(头, 名.value, 值.value)**
|
||||
**头 = map.set(头, 名.value, 值.value)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
2. `true`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
`end`
|
||||
**传输 = map.get(头, "Transfer-Encoding")**
|
||||
|
||||
**解码后 = 体**
|
||||
|
||||
1. `传输 == "chunked"`
|
||||
`if 传输 == "chunked"`
|
||||
|
||||
**分块 = 解码分块字节(体)**
|
||||
**分块 = 解码分块字节(体)**
|
||||
|
||||
1. `not 分块.ok`
|
||||
`if not 分块.ok`
|
||||
|
||||
*分块*
|
||||
`return 分块`
|
||||
|
||||
**解码后 = 分块.value**
|
||||
`end`
|
||||
**解码后 = 分块.value**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
*{ok: true, value: {status: 状态, reason: 原因.value, headers: 头, body: 解码后}}*
|
||||
`end`
|
||||
`end`
|
||||
`return {ok: true, value: {status: 状态, reason: 原因.value, headers: 头, body: 解码后}}`
|
||||
|
||||
## 解析响应
|
||||
|
||||
`(文本)`
|
||||
`params(文本)`
|
||||
|
||||
兼容文本 API:按字节解析后,把正文按 UTF-8 解码。
|
||||
|
||||
**响应 = 解析响应字节(文本字节(文本))**
|
||||
|
||||
1. `not 响应.ok`
|
||||
`if not 响应.ok`
|
||||
|
||||
*{状态: 0, 原因: "", 头: {}, 体: ""}*
|
||||
`return {状态: 0, 原因: "", 头: {}, 体: ""}`
|
||||
|
||||
`end`
|
||||
**正文 = 字节文本(响应.value.body)**
|
||||
|
||||
1. `not 正文.ok`
|
||||
`if not 正文.ok`
|
||||
|
||||
*{状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: ""}*
|
||||
`return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: ""}`
|
||||
|
||||
*{状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: 正文.value}*
|
||||
`end`
|
||||
`return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: 正文.value}`
|
||||
|
||||
## 构建请求字节
|
||||
|
||||
`(方法, 路径, 头列表, 体字节)`
|
||||
`params(方法, 路径, 头列表, 体字节)`
|
||||
|
||||
构建 HTTP/1.1 请求字节。方法、路径、头名/值按 UTF-8 编码;正文使用
|
||||
传入的字节列表。
|
||||
@@ -349,21 +405,22 @@ import:
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(头列表)`
|
||||
`while 下标 < 长度(头列表)`
|
||||
|
||||
**头 = 头列表[下标]**
|
||||
**头 = 头列表[下标]**
|
||||
|
||||
**结果 = list.concat(结果, 文本字节(文本(头[0]) + ": " + 文本(头[1]) + "\r\n"))**
|
||||
**结果 = list.concat(结果, 文本字节(文本(头[0]) + ": " + 文本(头[1]) + "\r\n"))**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
**结果 = list.concat(结果, [13, 10])**
|
||||
|
||||
*list.concat(结果, 体字节)*
|
||||
`return list.concat(结果, 体字节)`
|
||||
|
||||
## 构建请求
|
||||
|
||||
`(方法, 路径, 头列表, 体文本)`
|
||||
`params(方法, 路径, 头列表, 体文本)`
|
||||
|
||||
兼容文本 API,正文按 UTF-8 编码。
|
||||
|
||||
@@ -371,8 +428,9 @@ import:
|
||||
|
||||
**文本结果 = 字节文本(请求)**
|
||||
|
||||
1. `not 文本结果.ok`
|
||||
`if not 文本结果.ok`
|
||||
|
||||
*""*
|
||||
`return ""`
|
||||
|
||||
*文本结果.value*
|
||||
`end`
|
||||
`return 文本结果.value`
|
||||
+95
-64
@@ -1,22 +1,27 @@
|
||||
|
||||
# @exec path
|
||||
## 是否绝对
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**点 = text.codepoints(路径)**
|
||||
|
||||
1. `长度(点) == 0`
|
||||
`if 长度(点) == 0`
|
||||
|
||||
*false*
|
||||
`return false`
|
||||
|
||||
2. `点[0] == 47`
|
||||
`else`
|
||||
`if 点[0] == 47`
|
||||
|
||||
*true*
|
||||
`return true`
|
||||
|
||||
*false*
|
||||
`end`
|
||||
`end`
|
||||
`return false`
|
||||
|
||||
## 分割
|
||||
|
||||
`(文本, 分隔码点)`
|
||||
`params(文本, 分隔码点)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -26,65 +31,76 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
1. `点[下标] == 分隔码点`
|
||||
`if 点[下标] == 分隔码点`
|
||||
|
||||
**结果 = list.append(结果, 当前)**
|
||||
**结果 = list.append(结果, 当前)**
|
||||
|
||||
**当前 = ""**
|
||||
**当前 = ""**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**当前 = 当前 + text.from_codepoint(点[下标])**
|
||||
**当前 = 当前 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*list.append(结果, 当前)*
|
||||
`end`
|
||||
`return list.append(结果, 当前)`
|
||||
|
||||
## 连接
|
||||
|
||||
`(片段列表)`
|
||||
`params(片段列表)`
|
||||
|
||||
空片段被跳过,片段之间只插入一个 `/`。
|
||||
空片段被跳过,片段之间只插入一个 /。
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(片段列表)`
|
||||
`while 下标 < 长度(片段列表)`
|
||||
|
||||
**片段 = 文本(片段列表[下标])**
|
||||
**片段 = 文本(片段列表[下标])**
|
||||
|
||||
1. `片段 != ""`
|
||||
`if 片段 != ""`
|
||||
|
||||
1. `结果 == ""`
|
||||
`if 结果 == ""`
|
||||
|
||||
**结果 = 片段**
|
||||
**结果 = 片段**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + "/" + 片段**
|
||||
**结果 = 结果 + "/" + 片段**
|
||||
|
||||
2. `true`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 文件名
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
*片段[长度(片段) - 1]*
|
||||
`return 片段[长度(片段) - 1]`
|
||||
|
||||
## 目录名
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
@@ -92,50 +108,53 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(片段) - 1`
|
||||
`while 下标 < 长度(片段) - 1`
|
||||
|
||||
**结果 = list.append(结果, 片段[下标])**
|
||||
**结果 = list.append(结果, 片段[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*连接(结果)*
|
||||
`end`
|
||||
`return 连接(结果)`
|
||||
|
||||
## 扩展名
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
**名字 = 文件名(路径)**
|
||||
|
||||
**点列表 = 分割(名字, 46)**
|
||||
|
||||
1. `长度(点列表) <= 1`
|
||||
`if 长度(点列表) <= 1`
|
||||
|
||||
*""*
|
||||
`return ""`
|
||||
|
||||
*点列表[长度(点列表) - 1]*
|
||||
`end`
|
||||
`return 点列表[长度(点列表) - 1]`
|
||||
|
||||
## 去尾
|
||||
|
||||
`(列表)`
|
||||
`params(列表)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表) - 1`
|
||||
`while 下标 < 长度(列表) - 1`
|
||||
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 规范
|
||||
|
||||
`(路径)`
|
||||
`params(路径)`
|
||||
|
||||
词法规范化:折叠 `.`、空片段和重复 `/`,用 `..` 消去前一片段;
|
||||
绝对路径保留前导 `/`。不访问文件系统。
|
||||
词法规范化:折叠 .、空片段和重复 /,用 .. 消去前一片段;
|
||||
绝对路径保留前导 /。不访问文件系统。
|
||||
|
||||
**片段 = 分割(路径, 47)**
|
||||
|
||||
@@ -145,36 +164,48 @@
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(片段)`
|
||||
`while 下标 < 长度(片段)`
|
||||
|
||||
**段 = 片段[下标]**
|
||||
**段 = 片段[下标]**
|
||||
|
||||
1. `段 == "" or 段 == "."`
|
||||
`if 段 == "" or 段 == "."`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
2. `段 == ".."`
|
||||
`else`
|
||||
`if 段 == ".."`
|
||||
|
||||
1. `长度(栈) > 0 and 栈[长度(栈) - 1] != ".."`
|
||||
`if 长度(栈) > 0 and 栈[长度(栈) - 1] != ".."`
|
||||
|
||||
**栈 = 去尾(栈)**
|
||||
**栈 = 去尾(栈)**
|
||||
|
||||
2. `not 绝对`
|
||||
`else`
|
||||
`if not 绝对`
|
||||
|
||||
**栈 = list.append(栈, 段)**
|
||||
**栈 = list.append(栈, 段)**
|
||||
|
||||
3. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略绝对根 = 0**
|
||||
**忽略绝对根 = 0**
|
||||
|
||||
3. `true`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**栈 = list.append(栈, 段)**
|
||||
**栈 = list.append(栈, 段)**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `绝对`
|
||||
`end`
|
||||
`if 绝对`
|
||||
|
||||
*"/" + 连接(栈)*
|
||||
`return "/" + 连接(栈)`
|
||||
|
||||
*连接(栈)*
|
||||
`end`
|
||||
`return 连接(栈)`
|
||||
@@ -1,10 +1,12 @@
|
||||
|
||||
# @exec process
|
||||
## 运行
|
||||
|
||||
`(程序, 参数列表)`
|
||||
`params(程序, 参数列表)`
|
||||
|
||||
在项目模式下运行一个显式授权的可执行文件。程序路径必须与
|
||||
`--allow-process PROGRAM` 精确匹配;不经过 shell,清空环境变量,
|
||||
--allow-process PROGRAM 精确匹配;不经过 shell,清空环境变量,
|
||||
工作目录是项目根,stdin 为空。5 秒超时、1 MiB stdout/stderr 上限,
|
||||
结果形状为 `{status, stdout, stderr}`。
|
||||
结果形状为 {status, stdout, stderr}。
|
||||
|
||||
*resource.request("process.run", [程序, 参数列表])*
|
||||
`return resource.request("process.run", [程序, 参数列表])`
|
||||
+19
-14
@@ -1,37 +1,42 @@
|
||||
|
||||
# @exec random
|
||||
## 字节
|
||||
|
||||
`(数量)`
|
||||
`params(数量)`
|
||||
|
||||
读取 `数量` 个 OS 熵字节,返回 `List<Int>`(每个 0–255)。仅项目模式,
|
||||
需要显式 `--allow-random`;`pure`/`strict` 拒绝。
|
||||
读取 数量 个 OS 熵字节,返回 List<Int>(每个 0–255)。仅项目模式,
|
||||
需要显式 --allow-random;pure/strict 拒绝。
|
||||
|
||||
*resource.request("random.bytes", [数量])*
|
||||
`return resource.request("random.bytes", [数量])`
|
||||
|
||||
## 整数
|
||||
|
||||
`(上限)`
|
||||
`params(上限)`
|
||||
|
||||
返回 `0 <= 值 < 上限` 的整数。`上限` 必须大于 0;使用 8 个熵字节
|
||||
返回 0 <= 值 < 上限 的整数。上限 必须大于 0;使用 8 个熵字节
|
||||
组合后取模(无偏拒绝采样尚未实现)。
|
||||
|
||||
1. `上限 <= 0`
|
||||
`if 上限 <= 0`
|
||||
|
||||
*{ok: false, code: "random.invalid", message: "上限必须大于 0"}*
|
||||
`return {ok: false, code: "random.invalid", message: "上限必须大于 0"}`
|
||||
|
||||
`end`
|
||||
**字节结果 = 字节(8)**
|
||||
|
||||
1. `not 字节结果.ok`
|
||||
`if not 字节结果.ok`
|
||||
|
||||
*字节结果*
|
||||
`return 字节结果`
|
||||
|
||||
`end`
|
||||
**值 = 0**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 8`
|
||||
`while 下标 < 8`
|
||||
|
||||
**值 = 值 * 256 + 字节结果.value[下标]**
|
||||
**值 = 值 * 256 + 字节结果.value[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*{ok: true, value: 值 % 上限}*
|
||||
`end`
|
||||
`return {ok: true, value: 值 % 上限}`
|
||||
+264
-181
@@ -1,224 +1,260 @@
|
||||
|
||||
# @exec regex
|
||||
## 成功
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
*{ok: true, value: 值}*
|
||||
`return {ok: true, value: 值}`
|
||||
|
||||
## 失败
|
||||
|
||||
`(代码, 消息)`
|
||||
`params(代码, 消息)`
|
||||
|
||||
*{ok: false, code: 代码, message: 消息}*
|
||||
`return {ok: false, code: 代码, message: 消息}`
|
||||
|
||||
## 包含
|
||||
|
||||
`(列表, 值)`
|
||||
`params(列表, 值)`
|
||||
|
||||
**结果 = false**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表) and not 结果`
|
||||
`while 下标 < 长度(列表) and not 结果`
|
||||
|
||||
1. `列表[下标] == 值`
|
||||
`if 列表[下标] == 值`
|
||||
|
||||
**结果 = true**
|
||||
**结果 = true**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 去头
|
||||
|
||||
`(列表)`
|
||||
`params(列表)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 1**
|
||||
|
||||
- `下标 < 长度(列表)`
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 设置最后
|
||||
|
||||
`(列表, 值)`
|
||||
`params(列表, 值)`
|
||||
|
||||
**结果 = []**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
- `下标 < 长度(列表)`
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
1. `下标 + 1 == 长度(列表)`
|
||||
`if 下标 + 1 == 长度(列表)`
|
||||
|
||||
**结果 = list.append(结果, 值)**
|
||||
**结果 = list.append(结果, 值)**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
**结果 = list.append(结果, 列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 片段
|
||||
|
||||
`(点, 起点, 终点)`
|
||||
`params(点, 起点, 终点)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
- `下标 < 终点`
|
||||
`while 下标 < 终点`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 是数字
|
||||
|
||||
`(码点)`
|
||||
`params(码点)`
|
||||
|
||||
*码点 >= 48 and 码点 <= 57*
|
||||
`return 码点 >= 48 and 码点 <= 57`
|
||||
|
||||
## 字面令牌
|
||||
|
||||
`(码点)`
|
||||
`params(码点)`
|
||||
|
||||
包含数量词,默认恰好一次。
|
||||
|
||||
*{kind: "literal", value: 码点, neg: false, min: 1, max: 1}*
|
||||
`return {kind: "literal", value: 码点, neg: false, min: 1, max: 1}`
|
||||
|
||||
## 任意令牌
|
||||
|
||||
`()`
|
||||
`params()`
|
||||
|
||||
*{kind: "any", value: 0, neg: false, min: 1, max: 1}*
|
||||
`return {kind: "any", value: 0, neg: false, min: 1, max: 1}`
|
||||
|
||||
## 类令牌
|
||||
|
||||
`(集合, 否定)`
|
||||
`params(集合, 否定)`
|
||||
|
||||
*{kind: "class", value: 集合, neg: 否定, min: 1, max: 1}*
|
||||
`return {kind: "class", value: 集合, neg: 否定, min: 1, max: 1}`
|
||||
|
||||
## 锚点令牌
|
||||
|
||||
`(方向)`
|
||||
`params(方向)`
|
||||
|
||||
*{kind: "anchor", value: 方向, neg: false, min: 1, max: 1}*
|
||||
`return {kind: "anchor", value: 方向, neg: false, min: 1, max: 1}`
|
||||
|
||||
## 设置量词
|
||||
|
||||
`(令牌, 码点)`
|
||||
`params(令牌, 码点)`
|
||||
|
||||
1. `码点 == 42`
|
||||
`if 码点 == 42`
|
||||
|
||||
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: -1}*
|
||||
`return {kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: -1}`
|
||||
|
||||
2. `码点 == 43`
|
||||
`else`
|
||||
`if 码点 == 43`
|
||||
|
||||
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 1, max: -1}*
|
||||
`return {kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 1, max: -1}`
|
||||
|
||||
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: 1}*
|
||||
`end`
|
||||
`end`
|
||||
`return {kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: 1}`
|
||||
|
||||
## 编译类
|
||||
|
||||
`(点, 位置)`
|
||||
`params(点, 位置)`
|
||||
|
||||
**当前 = 位置 + 1**
|
||||
|
||||
**否定 = false**
|
||||
|
||||
1. `当前 < 长度(点) and 点[当前] == 94`
|
||||
`if 当前 < 长度(点) and 点[当前] == 94`
|
||||
|
||||
**否定 = true**
|
||||
**否定 = true**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略否定 = 0**
|
||||
**忽略否定 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**集合 = []**
|
||||
|
||||
**完成 = false**
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `当前 < 长度(点) and not 完成 and 错误 == ""`
|
||||
`while 当前 < 长度(点) and not 完成 and 错误 == ""`
|
||||
|
||||
**码点 = 点[当前]**
|
||||
**码点 = 点[当前]**
|
||||
|
||||
1. `码点 == 93`
|
||||
`if 码点 == 93`
|
||||
|
||||
**完成 = true**
|
||||
**完成 = true**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
2. `码点 == 92`
|
||||
`else`
|
||||
`if 码点 == 92`
|
||||
|
||||
1. `当前 + 1 >= 长度(点)`
|
||||
`if 当前 + 1 >= 长度(点)`
|
||||
|
||||
**错误 = "regex.invalid"**
|
||||
**错误 = "regex.invalid"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**集合 = list.append(集合, 点[当前 + 1])**
|
||||
**集合 = list.append(集合, 点[当前 + 1])**
|
||||
|
||||
**当前 = 当前 + 2**
|
||||
**当前 = 当前 + 2**
|
||||
|
||||
3. `当前 + 2 < 长度(点) and 点[当前 + 1] == 45 and 点[当前 + 2] != 93`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 当前 + 2 < 长度(点) and 点[当前 + 1] == 45 and 点[当前 + 2] != 93`
|
||||
|
||||
**开始 = 码点**
|
||||
**开始 = 码点**
|
||||
|
||||
**结束 = 点[当前 + 2]**
|
||||
**结束 = 点[当前 + 2]**
|
||||
|
||||
1. `开始 > 结束`
|
||||
`if 开始 > 结束`
|
||||
|
||||
**错误 = "regex.invalid"**
|
||||
**错误 = "regex.invalid"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**值 = 开始**
|
||||
**值 = 开始**
|
||||
|
||||
- `值 <= 结束`
|
||||
`while 值 <= 结束`
|
||||
|
||||
**集合 = list.append(集合, 值)**
|
||||
**集合 = list.append(集合, 值)**
|
||||
|
||||
**值 = 值 + 1**
|
||||
**值 = 值 + 1**
|
||||
|
||||
**当前 = 当前 + 3**
|
||||
`end`
|
||||
**当前 = 当前 + 3**
|
||||
|
||||
4. `true`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**集合 = list.append(集合, 码点)**
|
||||
**集合 = list.append(集合, 码点)**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
1. `not 完成`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if not 完成`
|
||||
|
||||
*失败("regex.invalid", "字符类未闭合")*
|
||||
`return 失败("regex.invalid", "字符类未闭合")`
|
||||
|
||||
1. `错误 != ""`
|
||||
`else`
|
||||
`if 错误 != ""`
|
||||
|
||||
*失败(错误, "字符类范围无效")*
|
||||
`return 失败(错误, "字符类范围无效")`
|
||||
|
||||
*成功({set: 集合, neg: 否定, next: 当前})*
|
||||
`end`
|
||||
`end`
|
||||
`return 成功({set: 集合, neg: 否定, next: 当前})`
|
||||
|
||||
## 编译
|
||||
|
||||
`(点, 位置)`
|
||||
`params(点, 位置)`
|
||||
|
||||
**令牌表 = []**
|
||||
|
||||
@@ -228,189 +264,232 @@
|
||||
|
||||
**代码 = ""**
|
||||
|
||||
- `当前 < 长度(点) and 错误 == ""`
|
||||
`while 当前 < 长度(点) and 错误 == ""`
|
||||
|
||||
**码点 = 点[当前]**
|
||||
**码点 = 点[当前]**
|
||||
|
||||
1. `码点 == 94`
|
||||
`if 码点 == 94`
|
||||
|
||||
**令牌表 = list.append(令牌表, 锚点令牌("start"))**
|
||||
**令牌表 = list.append(令牌表, 锚点令牌("start"))**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
2. `码点 == 36`
|
||||
`else`
|
||||
`if 码点 == 36`
|
||||
|
||||
**令牌表 = list.append(令牌表, 锚点令牌("end"))**
|
||||
**令牌表 = list.append(令牌表, 锚点令牌("end"))**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
3. `码点 == 91`
|
||||
`else`
|
||||
`if 码点 == 91`
|
||||
|
||||
**类结果 = 编译类(点, 当前)**
|
||||
**类结果 = 编译类(点, 当前)**
|
||||
|
||||
1. `not 类结果.ok`
|
||||
`if not 类结果.ok`
|
||||
|
||||
**错误 = 类结果.code**
|
||||
**错误 = 类结果.code**
|
||||
|
||||
**代码 = 类结果.message**
|
||||
**代码 = 类结果.message**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**令牌表 = list.append(令牌表, 类令牌(类结果.value.set, 类结果.value.neg))**
|
||||
**令牌表 = list.append(令牌表, 类令牌(类结果.value.set, 类结果.value.neg))**
|
||||
|
||||
**当前 = 类结果.value.next**
|
||||
**当前 = 类结果.value.next**
|
||||
|
||||
4. `码点 == 92`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 码点 == 92`
|
||||
|
||||
1. `当前 + 1 >= 长度(点)`
|
||||
`if 当前 + 1 >= 长度(点)`
|
||||
|
||||
**错误 = "regex.invalid"**
|
||||
**错误 = "regex.invalid"**
|
||||
|
||||
**代码 = "转义缺少字符"**
|
||||
**代码 = "转义缺少字符"**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**令牌表 = list.append(令牌表, 字面令牌(点[当前 + 1]))**
|
||||
**令牌表 = list.append(令牌表, 字面令牌(点[当前 + 1]))**
|
||||
|
||||
**当前 = 当前 + 2**
|
||||
**当前 = 当前 + 2**
|
||||
|
||||
5. `码点 == 46`
|
||||
`end`
|
||||
`end`
|
||||
`else`
|
||||
`if 码点 == 46`
|
||||
|
||||
**令牌表 = list.append(令牌表, 任意令牌())**
|
||||
**令牌表 = list.append(令牌表, 任意令牌())**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
6. `码点 == 40 or 码点 == 41 or 码点 == 124`
|
||||
`else`
|
||||
`if 码点 == 40 or 码点 == 41 or 码点 == 124`
|
||||
|
||||
**错误 = "regex.unsupported"**
|
||||
**错误 = "regex.unsupported"**
|
||||
|
||||
**代码 = "不支持分组或选择"**
|
||||
**代码 = "不支持分组或选择"**
|
||||
|
||||
7. `码点 == 42 or 码点 == 43 or 码点 == 63`
|
||||
`else`
|
||||
`if 码点 == 42 or 码点 == 43 or 码点 == 63`
|
||||
|
||||
**错误 = "regex.invalid"**
|
||||
**错误 = "regex.invalid"**
|
||||
|
||||
**代码 = "量词没有前一项"**
|
||||
**代码 = "量词没有前一项"**
|
||||
|
||||
8. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**令牌表 = list.append(令牌表, 字面令牌(码点))**
|
||||
**令牌表 = list.append(令牌表, 字面令牌(码点))**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
- `错误 == "" and 当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`while 错误 == "" and 当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
|
||||
|
||||
**令牌表 = 设置最后(令牌表, 设置量词(令牌表[长度(令牌表) - 1], 点[当前]))**
|
||||
**令牌表 = 设置最后(令牌表, 设置量词(令牌表[长度(令牌表) - 1], 点[当前]))**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
- `当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
|
||||
`while 当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
|
||||
|
||||
**错误 = "regex.invalid"**
|
||||
**错误 = "regex.invalid"**
|
||||
|
||||
**代码 = "连续量词无效"**
|
||||
**代码 = "连续量词无效"**
|
||||
|
||||
1. `错误 != ""`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 错误 != ""`
|
||||
|
||||
*失败(错误, 代码)*
|
||||
`return 失败(错误, 代码)`
|
||||
|
||||
*成功(令牌表)*
|
||||
`end`
|
||||
`return 成功(令牌表)`
|
||||
|
||||
## 单字符匹配
|
||||
|
||||
`(令牌, 码点)`
|
||||
`params(令牌, 码点)`
|
||||
|
||||
**类别 = 令牌.kind**
|
||||
|
||||
1. `类别 == "any"`
|
||||
`if 类别 == "any"`
|
||||
|
||||
*true*
|
||||
`return true`
|
||||
|
||||
2. `类别 == "literal"`
|
||||
`else`
|
||||
`if 类别 == "literal"`
|
||||
|
||||
*令牌.value == 码点*
|
||||
`return 令牌.value == 码点`
|
||||
|
||||
3. `类别 == "class"`
|
||||
`else`
|
||||
`if 类别 == "class"`
|
||||
|
||||
*包含(令牌.value, 码点) != 令牌.neg*
|
||||
`return 包含(令牌.value, 码点) != 令牌.neg`
|
||||
|
||||
*false*
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`return false`
|
||||
|
||||
## 尝试匹配
|
||||
|
||||
`(令牌, 下标, 当前, 点, 位置, 次数, 要求结束)`
|
||||
`params(令牌, 下标, 当前, 点, 位置, 次数, 要求结束)`
|
||||
|
||||
**下限 = 当前.min**
|
||||
|
||||
**上限 = 当前.max**
|
||||
|
||||
1. `次数 >= 下限 and 匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)`
|
||||
`if 次数 >= 下限 and 匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)`
|
||||
|
||||
*true*
|
||||
`return true`
|
||||
|
||||
1. `上限 >= 0 and 次数 >= 上限`
|
||||
`else`
|
||||
`if 上限 >= 0 and 次数 >= 上限`
|
||||
|
||||
*false*
|
||||
`return false`
|
||||
|
||||
1. `位置 < 长度(点) and 单字符匹配(当前, 点[位置])`
|
||||
`else`
|
||||
`if 位置 < 长度(点) and 单字符匹配(当前, 点[位置])`
|
||||
|
||||
*尝试匹配(令牌, 下标, 当前, 点, 位置 + 1, 次数 + 1, 要求结束)*
|
||||
`return 尝试匹配(令牌, 下标, 当前, 点, 位置 + 1, 次数 + 1, 要求结束)`
|
||||
|
||||
*false*
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`return false`
|
||||
|
||||
## 匹配序列
|
||||
|
||||
`(令牌, 下标, 点, 位置, 要求结束)`
|
||||
`params(令牌, 下标, 点, 位置, 要求结束)`
|
||||
|
||||
1. `下标 >= 长度(令牌)`
|
||||
`if 下标 >= 长度(令牌)`
|
||||
|
||||
1. `要求结束`
|
||||
`if 要求结束`
|
||||
|
||||
*位置 == 长度(点)*
|
||||
`return 位置 == 长度(点)`
|
||||
|
||||
*true*
|
||||
`end`
|
||||
`return true`
|
||||
|
||||
`end`
|
||||
**当前 = 令牌[下标]**
|
||||
|
||||
1. `当前.kind == "anchor"`
|
||||
`if 当前.kind == "anchor"`
|
||||
|
||||
1. `当前.value == "start" and 位置 == 0`
|
||||
`if 当前.value == "start" and 位置 == 0`
|
||||
|
||||
*匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)*
|
||||
`return 匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)`
|
||||
|
||||
2. `当前.value == "end" and 位置 == 长度(点)`
|
||||
`else`
|
||||
`if 当前.value == "end" and 位置 == 长度(点)`
|
||||
|
||||
*匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)*
|
||||
`return 匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)`
|
||||
|
||||
*false*
|
||||
`end`
|
||||
`end`
|
||||
`return false`
|
||||
|
||||
*尝试匹配(令牌, 下标, 当前, 点, 位置, 0, 要求结束)*
|
||||
`end`
|
||||
`return 尝试匹配(令牌, 下标, 当前, 点, 位置, 0, 要求结束)`
|
||||
|
||||
## 匹配
|
||||
|
||||
`(模式, 文本)`
|
||||
`params(模式, 文本)`
|
||||
|
||||
编译后要求整个文本匹配。支持 `^`、`$`、`.`、`*`、`+`、`?`、
|
||||
`[...]` 和 `\\` 转义;不支持分组和选择。
|
||||
编译后要求整个文本匹配。支持 ^、$、.、*、+、?、
|
||||
[...] 和 \\ 转义;不支持分组和选择。
|
||||
|
||||
**编译结果 = 编译(text.codepoints(模式), 0)**
|
||||
|
||||
1. `not 编译结果.ok`
|
||||
`if not 编译结果.ok`
|
||||
|
||||
*编译结果*
|
||||
`return 编译结果`
|
||||
|
||||
*{ok: true, value: 匹配序列(编译结果.value, 0, text.codepoints(文本), 0, true)}*
|
||||
`end`
|
||||
`return {ok: true, value: 匹配序列(编译结果.value, 0, text.codepoints(文本), 0, true)}`
|
||||
|
||||
## 搜索
|
||||
|
||||
`(模式, 文本)`
|
||||
`params(模式, 文本)`
|
||||
|
||||
**编译结果 = 编译(text.codepoints(模式), 0)**
|
||||
|
||||
1. `not 编译结果.ok`
|
||||
`if not 编译结果.ok`
|
||||
|
||||
*编译结果*
|
||||
`return 编译结果`
|
||||
|
||||
`end`
|
||||
**令牌 = 编译结果.value**
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
@@ -419,40 +498,44 @@
|
||||
|
||||
**结果 = false**
|
||||
|
||||
- `下标 <= 长度(点) and not 结果`
|
||||
`while 下标 <= 长度(点) and not 结果`
|
||||
|
||||
1. `匹配序列(令牌, 0, 点, 下标, false)`
|
||||
`if 匹配序列(令牌, 0, 点, 下标, false)`
|
||||
|
||||
**结果 = true**
|
||||
**结果 = true**
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*成功(结果)*
|
||||
`end`
|
||||
`return 成功(结果)`
|
||||
|
||||
## 扩展匹配
|
||||
|
||||
`(模式, 文本)`
|
||||
`params(模式, 文本)`
|
||||
|
||||
使用宿主正则引擎(支持分组、选择、`\\d` 等)判断是否匹配。模式无效时
|
||||
返回 `regex.invalid`。仅项目模式;纯 `.mq.md` 子集仍由 `匹配`/`搜索` 提供。
|
||||
使用宿主正则引擎(支持分组、选择、\\d 等)判断是否匹配。模式无效时
|
||||
返回 regex.invalid。仅项目模式;纯 .mq.md 子集仍由 匹配/搜索 提供。
|
||||
|
||||
*resource.request("regex.match", [模式, 文本])*
|
||||
`return resource.request("regex.match", [模式, 文本])`
|
||||
|
||||
## 扩展搜索
|
||||
|
||||
`(模式, 文本)`
|
||||
`params(模式, 文本)`
|
||||
|
||||
*resource.request("regex.match", [模式, 文本])*
|
||||
`return resource.request("regex.match", [模式, 文本])`
|
||||
|
||||
## 捕获
|
||||
|
||||
`(模式, 文本)`
|
||||
`params(模式, 文本)`
|
||||
|
||||
返回 `{ok:true, value:{matched:Bool, groups:List<Text|None>}}`。下标 0 是
|
||||
完整匹配;未参与匹配的分组是 `None`。
|
||||
返回 {ok:true, value:{matched:Bool, groups:List<Text|None>}}。下标 0 是
|
||||
完整匹配;未参与匹配的分组是 None。
|
||||
|
||||
*resource.request("regex.captures", [模式, 文本])*
|
||||
`return resource.request("regex.captures", [模式, 文本])`
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "marqdo-stdlib",
|
||||
"version": "0.1.0",
|
||||
"digest": "sha256:9a921f5fc7a929cee1fa4b4d8f2b401a3bf69198d1a753b5dc8de5d28d46583f"
|
||||
"digest": "sha256:c3fca0cc3bd8f89f0f45d52418113613dd0613711cfdba76b514e5e6244409d2"
|
||||
}
|
||||
@@ -3,8 +3,10 @@ import:
|
||||
clock: "std:clock"
|
||||
---
|
||||
|
||||
# @exec clock
|
||||
|
||||
## main
|
||||
|
||||
**现在 = clock.现在()**
|
||||
|
||||
*[现在.ok, 类型(现在.value), 现在.value > 1600000000000]*
|
||||
`return [现在.ok, 类型(现在.value), 现在.value > 1600000000000]`
|
||||
@@ -3,10 +3,12 @@ import:
|
||||
compress: "std:compress"
|
||||
---
|
||||
|
||||
# @exec compress
|
||||
|
||||
## main
|
||||
|
||||
**压缩结果 = compress.压缩("hello 世界")**
|
||||
|
||||
**解压结果 = compress.解压(压缩结果.value)**
|
||||
|
||||
*[压缩结果.ok, 长度(压缩结果.value) > 0, 解压结果.ok, 解压结果.value]*
|
||||
`return [压缩结果.ok, 长度(压缩结果.value) > 0, 解压结果.ok, 解压结果.value]`
|
||||
@@ -3,8 +3,10 @@ import:
|
||||
console: "std:io/console"
|
||||
---
|
||||
|
||||
# @exec console
|
||||
|
||||
## main
|
||||
|
||||
**行 = console.读取行()**
|
||||
|
||||
*[行.ok, 行.value]*
|
||||
`return [行.ok, 行.value]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
csv: "std:data/csv"
|
||||
---
|
||||
|
||||
# @exec csv
|
||||
|
||||
## main
|
||||
|
||||
**`行 = csv.解析行("甲,\"乙,丙\",\"说\"\"好\"")`**
|
||||
@@ -17,4 +19,4 @@ import:
|
||||
|
||||
**格式化 = csv.格式化(多行.value)**
|
||||
|
||||
*[行.value, csv.格式化行(行.value), 空.value, 坏.code, 框.value, 多行.ok, 多行.value, 格式化]*
|
||||
`return [行.value, csv.格式化行(行.value), 空.value, 坏.code, 框.value, 多行.ok, 多行.value, 格式化]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
datetime: "std:datetime"
|
||||
---
|
||||
|
||||
# @exec datetime
|
||||
|
||||
## main
|
||||
|
||||
*[datetime.格式化(0), datetime.格式化(1700000000123), datetime.解析("1970-01-01T00:00:00Z").value, datetime.解析("2026-11-21T08:00:00.123Z").value, datetime.解析("2026-13-01T00:00:00Z").ok, datetime.解析("2026-02-29T00:00:00Z").ok, datetime.格式化(-1)]*
|
||||
`return [datetime.格式化(0), datetime.格式化(1700000000123), datetime.解析("1970-01-01T00:00:00Z").value, datetime.解析("2026-11-21T08:00:00.123Z").value, datetime.解析("2026-13-01T00:00:00Z").ok, datetime.解析("2026-02-29T00:00:00Z").ok, datetime.格式化(-1)]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
demo: "std:demo"
|
||||
---
|
||||
|
||||
# @exec demo
|
||||
|
||||
## main
|
||||
|
||||
*demo.演示函数()*
|
||||
`return demo.演示函数()`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
env: "std:env"
|
||||
---
|
||||
|
||||
# @exec env
|
||||
|
||||
## main
|
||||
|
||||
**存在 = env.存在("MARQDO_TEST_ENV")**
|
||||
@@ -11,4 +13,4 @@ import:
|
||||
|
||||
**缺失 = env.读取("MARQDO_MISSING_ENV")**
|
||||
|
||||
*[存在.ok, 存在.value, 读取.ok, 读取.value, 缺失.ok, 缺失.code]*
|
||||
`return [存在.ok, 存在.value, 读取.ok, 读取.value, 缺失.ok, 缺失.code]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
format: "std:format"
|
||||
---
|
||||
|
||||
# @exec format
|
||||
|
||||
## main
|
||||
|
||||
*[format.字节长度("Aé"), format.显示(["文本", 2, 1.5]), format.显示({a: 1}), format.列表显示(["a", 2]), format.显示(None)]*
|
||||
`return [format.字节长度("Aé"), format.显示(["文本", 2, 1.5]), format.显示({a: 1}), format.列表显示(["a", 2]), format.显示(None)]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
fs: "std:fs/file"
|
||||
---
|
||||
|
||||
# @exec fs
|
||||
|
||||
## main
|
||||
|
||||
**创建 = fs.创建目录("out")**
|
||||
@@ -33,4 +35,4 @@ import:
|
||||
|
||||
**删除目录 = fs.删除("out")**
|
||||
|
||||
*[创建.ok, 写入.value.count, 存在.value, 元数据.value.kind, 元数据.value.size, 列表.value, 是文件.value, 是目录.value, 重命名.ok, 读取.value, 追加.value, 片段.value, 遍历.value, 删除.ok, 删除目录.ok]*
|
||||
`return [创建.ok, 写入.value.count, 存在.value, 元数据.value.kind, 元数据.value.size, 列表.value, 是文件.value, 是目录.value, 重命名.ok, 读取.value, 追加.value, 片段.value, 遍历.value, 删除.ok, 删除目录.ok]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
http: "std:net/http"
|
||||
---
|
||||
|
||||
# @exec http
|
||||
|
||||
## 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"**
|
||||
@@ -19,4 +21,4 @@ import:
|
||||
|
||||
**请求文本 = http.字节文本(请求字节)**
|
||||
|
||||
*[响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求, 响应字节.value.status, 正文.value, 请求文本.value]*
|
||||
`return [响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求, 响应字节.value.status, 正文.value, 请求文本.value]`
|
||||
@@ -3,8 +3,10 @@ import:
|
||||
io: "std:io"
|
||||
---
|
||||
|
||||
# @exec io
|
||||
|
||||
## main
|
||||
|
||||
> io.写值("stdlib self-test")
|
||||
> **io.写值("stdlib self-test")**
|
||||
|
||||
*None*
|
||||
`return None`
|
||||
+11
-7
@@ -3,6 +3,8 @@ import:
|
||||
json: "std:data/json"
|
||||
---
|
||||
|
||||
# @exec json
|
||||
|
||||
## main
|
||||
|
||||
**`对象 = json.解析("{\"a\": 1, \"b\": [true, null, \"说\\\"好\"]}")`**
|
||||
@@ -17,22 +19,24 @@ import:
|
||||
|
||||
**深度下标 = 0**
|
||||
|
||||
- `深度下标 < 130`
|
||||
`while 深度下标 < 130`
|
||||
|
||||
**深度文本 = 深度文本 + "["**
|
||||
**深度文本 = 深度文本 + "["**
|
||||
|
||||
**深度下标 = 深度下标 + 1**
|
||||
**深度下标 = 深度下标 + 1**
|
||||
|
||||
`end`
|
||||
**深度下标 = 0**
|
||||
|
||||
- `深度下标 < 130`
|
||||
`while 深度下标 < 130`
|
||||
|
||||
**深度文本 = 深度文本 + "]"**
|
||||
**深度文本 = 深度文本 + "]"**
|
||||
|
||||
**深度下标 = 深度下标 + 1**
|
||||
**深度下标 = 深度下标 + 1**
|
||||
|
||||
`end`
|
||||
**深度 = json.解析(深度文本)**
|
||||
|
||||
**`多个 = json.解析多个("1 2 [3]")`**
|
||||
|
||||
*[对象.ok, 对象.value.a, 对象.value.b[2], json.格式化(浮点.value), 失败.ok, 失败.code, 尾随.code, 深度.ok, 深度.code, 多个.ok, 多个.value]*
|
||||
`return [对象.ok, 对象.value.a, 对象.value.b[2], json.格式化(浮点.value), 失败.ok, 失败.code, 尾随.code, 深度.ok, 深度.code, 多个.ok, 多个.value]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
path: "std:path"
|
||||
---
|
||||
|
||||
# @exec path
|
||||
|
||||
## main
|
||||
|
||||
*[path.连接(["a", "", "b", "c"]), path.文件名("a/b/c.txt"), path.目录名("a/b/c.txt"), path.扩展名("a/b/c.txt"), path.规范("./a//b/../c"), path.规范("/a/./b/../c"), path.规范("../a/b"), path.是否绝对("/a"), path.是否绝对("a")]*
|
||||
`return [path.连接(["a", "", "b", "c"]), path.文件名("a/b/c.txt"), path.目录名("a/b/c.txt"), path.扩展名("a/b/c.txt"), path.规范("./a//b/../c"), path.规范("/a/./b/../c"), path.规范("../a/b"), path.是否绝对("/a"), path.是否绝对("a")]`
|
||||
@@ -3,8 +3,10 @@ import:
|
||||
process: "std:process"
|
||||
---
|
||||
|
||||
# @exec process
|
||||
|
||||
## main
|
||||
|
||||
**结果 = process.运行("/bin/echo", ["hello"])**
|
||||
|
||||
*[结果.ok, 结果.value.status, 结果.value.stdout, 结果.value.stderr]*
|
||||
`return [结果.ok, 结果.value.status, 结果.value.stdout, 结果.value.stderr]`
|
||||
@@ -3,10 +3,12 @@ import:
|
||||
random: "std:random"
|
||||
---
|
||||
|
||||
# @exec random
|
||||
|
||||
## main
|
||||
|
||||
**字节结果 = random.字节(16)**
|
||||
|
||||
**整数结果 = random.整数(100)**
|
||||
|
||||
*[字节结果.ok, 长度(字节结果.value), 整数结果.ok, 整数结果.value >= 0, 整数结果.value < 100]*
|
||||
`return [字节结果.ok, 长度(字节结果.value), 整数结果.ok, 整数结果.value >= 0, 整数结果.value < 100]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
regex: "std:regex"
|
||||
---
|
||||
|
||||
# @exec regex
|
||||
|
||||
## main
|
||||
|
||||
**`星号 = regex.匹配("a*b", "aaab")`**
|
||||
@@ -31,4 +33,4 @@ import:
|
||||
|
||||
**捕获 = regex.捕获("^(a+)(b|c)$", "aab")**
|
||||
|
||||
*[星号.value, 加号.value, 锚点.value, 类.value, 否定类.value, 任意.value, 可选.value, 转义.value, 搜索.value, 无效.ok, 无效.code, 不支持.code, 扩展.value, 捕获.value.matched, 捕获.value.groups]*
|
||||
`return [星号.value, 加号.value, 锚点.value, 类.value, 否定类.value, 任意.value, 可选.value, 转义.value, 搜索.value, 无效.ok, 无效.code, 不支持.code, 扩展.value, 捕获.value.matched, 捕获.value.groups]`
|
||||
@@ -3,8 +3,10 @@ import:
|
||||
stream: "std:io/stream"
|
||||
---
|
||||
|
||||
# @exec stream
|
||||
|
||||
## main
|
||||
|
||||
**结果 = stream.写文本("stream\n")**
|
||||
|
||||
*[结果.ok, 结果.value.count]*
|
||||
`return [结果.ok, 结果.value.count]`
|
||||
@@ -3,6 +3,8 @@ import:
|
||||
table: "std:data/table"
|
||||
---
|
||||
|
||||
# @exec table
|
||||
|
||||
## main
|
||||
|
||||
**数据 = 表(["名称", "分数"], [["甲", 87], ["乙", 92], ["丙", 71]])**
|
||||
@@ -25,4 +27,4 @@ import:
|
||||
|
||||
**分组 = table.分组(表(["类别", "值"], [["a", 1], ["b", 2], ["a", 3]]), 0, 1)**
|
||||
|
||||
*[选择, 过滤, 排序, 追加, 设置, 连接, 分组, 数据, table.转GFM(数据)]*
|
||||
`return [选择, 过滤, 排序, 追加, 设置, 连接, 分组, 数据, table.转GFM(数据)]`
|
||||
@@ -3,10 +3,12 @@ import:
|
||||
text: "std:text"
|
||||
---
|
||||
|
||||
# @exec text
|
||||
|
||||
## main
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**长度 = text.utf8编码("é", 内存, 0)**
|
||||
|
||||
*[长度, 内存[0], 内存[1]]*
|
||||
`return [长度, 内存[0], 内存[1]]`
|
||||
@@ -3,10 +3,12 @@ import:
|
||||
utf8: "std:text/utf8"
|
||||
---
|
||||
|
||||
# @exec utf8
|
||||
|
||||
## main
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**长度 = utf8.编码("é", 内存, 0)**
|
||||
|
||||
*[长度, utf8.解码(内存, 0, 长度).value, utf8.解码(内存, 0, 1).ok]*
|
||||
`return [长度, utf8.解码(内存, 0, 长度).value, utf8.解码(内存, 0, 1).ok]`
|
||||
+34
-24
@@ -1,8 +1,10 @@
|
||||
|
||||
# @exec text
|
||||
## utf8编码
|
||||
|
||||
`(文本, 内存, 基址)`
|
||||
`params(文本, 内存, 基址)`
|
||||
|
||||
把 Unicode 标量序列按 UTF-8 写入 `Memory`,返回字节长度。
|
||||
把 Unicode 标量序列按 UTF-8 写入 Memory,返回字节长度。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -10,46 +12,54 @@
|
||||
|
||||
**偏移 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 < 128`
|
||||
`if 码点 < 128`
|
||||
|
||||
**`内存[基址 + 偏移] = 码点`**
|
||||
**`内存[基址 + 偏移] = 码点`**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
2. `码点 < 2048`
|
||||
`else`
|
||||
`if 码点 < 2048`
|
||||
|
||||
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
|
||||
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 2**
|
||||
**偏移 = 偏移 + 2**
|
||||
|
||||
3. `码点 < 65536`
|
||||
`else`
|
||||
`if 码点 < 65536`
|
||||
|
||||
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
|
||||
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 3**
|
||||
**偏移 = 偏移 + 3**
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
|
||||
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 4**
|
||||
**偏移 = 偏移 + 4**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*偏移*
|
||||
`end`
|
||||
`return 偏移`
|
||||
+122
-88
@@ -1,27 +1,29 @@
|
||||
|
||||
# @exec utf8
|
||||
## 成功
|
||||
|
||||
`(值)`
|
||||
`params(值)`
|
||||
|
||||
*{ok: true, value: 值}*
|
||||
`return {ok: true, value: 值}`
|
||||
|
||||
## 失败
|
||||
|
||||
`(代码, 消息)`
|
||||
`params(代码, 消息)`
|
||||
|
||||
*{ok: false, code: 代码, message: 消息}*
|
||||
`return {ok: false, code: 代码, message: 消息}`
|
||||
|
||||
## 续字节
|
||||
|
||||
`(字节)`
|
||||
`params(字节)`
|
||||
|
||||
*字节 >= 128 and 字节 < 192*
|
||||
`return 字节 >= 128 and 字节 < 192`
|
||||
|
||||
## 编码
|
||||
|
||||
`(文本, 内存, 基址)`
|
||||
`params(文本, 内存, 基址)`
|
||||
|
||||
把 Unicode 标量序列按 UTF-8 写入 `Memory`,返回字节长度。
|
||||
这是 `.mq.md` 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
|
||||
把 Unicode 标量序列按 UTF-8 写入 Memory,返回字节长度。
|
||||
这是 .mq.md 自己实现的编码逻辑,不是 Rust 内建 UTF-8 转换。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
@@ -29,125 +31,142 @@
|
||||
|
||||
**偏移 = 0**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `码点 < 128`
|
||||
`if 码点 < 128`
|
||||
|
||||
**`内存[基址 + 偏移] = 码点`**
|
||||
**`内存[基址 + 偏移] = 码点`**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
2. `码点 < 2048`
|
||||
`else`
|
||||
`if 码点 < 2048`
|
||||
|
||||
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
|
||||
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 2**
|
||||
**偏移 = 偏移 + 2**
|
||||
|
||||
3. `码点 < 65536`
|
||||
`else`
|
||||
`if 码点 < 65536`
|
||||
|
||||
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
|
||||
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 3**
|
||||
**偏移 = 偏移 + 3**
|
||||
|
||||
4. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
|
||||
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
|
||||
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
|
||||
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
|
||||
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
|
||||
|
||||
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
|
||||
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
|
||||
|
||||
**偏移 = 偏移 + 4**
|
||||
**偏移 = 偏移 + 4**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*偏移*
|
||||
`end`
|
||||
`return 偏移`
|
||||
|
||||
## 解码2
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
`params(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 1 >= 长度`
|
||||
`if 偏移 + 1 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
`return [文本, 偏移, "utf8.truncated"]`
|
||||
|
||||
`end`
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
1. `not 续字节(b1)`
|
||||
`if not 续字节(b1)`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
`return [文本, 偏移, "utf8.invalid_continuation"]`
|
||||
|
||||
`end`
|
||||
**码点 = (内存[基址 + 偏移] - 192) * 64 + (b1 - 128)**
|
||||
|
||||
1. `码点 < 128`
|
||||
`if 码点 < 128`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
`return [文本, 偏移, "utf8.invalid_scalar"]`
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 2, ""]*
|
||||
`end`
|
||||
`return [文本 + text.from_codepoint(码点), 偏移 + 2, ""]`
|
||||
|
||||
## 解码3
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
`params(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 2 >= 长度`
|
||||
`if 偏移 + 2 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
`return [文本, 偏移, "utf8.truncated"]`
|
||||
|
||||
`end`
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
**b2 = 内存[基址 + 偏移 + 2]**
|
||||
|
||||
1. `not (续字节(b1) and 续字节(b2))`
|
||||
`if not (续字节(b1) and 续字节(b2))`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
`return [文本, 偏移, "utf8.invalid_continuation"]`
|
||||
|
||||
`end`
|
||||
**码点 = (内存[基址 + 偏移] - 224) * 4096 + (b1 - 128) * 64 + (b2 - 128)**
|
||||
|
||||
1. `码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
|
||||
`if 码点 < 2048 or (码点 >= 55296 and 码点 <= 57343)`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
`return [文本, 偏移, "utf8.invalid_scalar"]`
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 3, ""]*
|
||||
`end`
|
||||
`return [文本 + text.from_codepoint(码点), 偏移 + 3, ""]`
|
||||
|
||||
## 解码4
|
||||
|
||||
`(内存, 基址, 长度, 偏移, 文本)`
|
||||
`params(内存, 基址, 长度, 偏移, 文本)`
|
||||
|
||||
1. `偏移 + 3 >= 长度`
|
||||
`if 偏移 + 3 >= 长度`
|
||||
|
||||
*[文本, 偏移, "utf8.truncated"]*
|
||||
`return [文本, 偏移, "utf8.truncated"]`
|
||||
|
||||
`end`
|
||||
**b1 = 内存[基址 + 偏移 + 1]**
|
||||
|
||||
**b2 = 内存[基址 + 偏移 + 2]**
|
||||
|
||||
**b3 = 内存[基址 + 偏移 + 3]**
|
||||
|
||||
1. `not (续字节(b1) and 续字节(b2) and 续字节(b3))`
|
||||
`if not (续字节(b1) and 续字节(b2) and 续字节(b3))`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_continuation"]*
|
||||
`return [文本, 偏移, "utf8.invalid_continuation"]`
|
||||
|
||||
`end`
|
||||
**码点 = (内存[基址 + 偏移] - 240) * 262144 + (b1 - 128) * 4096 + (b2 - 128) * 64 + (b3 - 128)**
|
||||
|
||||
1. `码点 < 65536 or 码点 > 1114111`
|
||||
`if 码点 < 65536 or 码点 > 1114111`
|
||||
|
||||
*[文本, 偏移, "utf8.invalid_scalar"]*
|
||||
`return [文本, 偏移, "utf8.invalid_scalar"]`
|
||||
|
||||
*[文本 + text.from_codepoint(码点), 偏移 + 4, ""]*
|
||||
`end`
|
||||
`return [文本 + text.from_codepoint(码点), 偏移 + 4, ""]`
|
||||
|
||||
## 解码
|
||||
|
||||
`(内存, 基址, 长度)`
|
||||
`params(内存, 基址, 长度)`
|
||||
|
||||
**偏移 = 0**
|
||||
|
||||
@@ -155,70 +174,85 @@
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
- `偏移 < 长度 and 错误 == ""`
|
||||
`while 偏移 < 长度 and 错误 == ""`
|
||||
|
||||
**首 = 内存[基址 + 偏移]**
|
||||
**首 = 内存[基址 + 偏移]**
|
||||
|
||||
1. `首 < 128`
|
||||
`if 首 < 128`
|
||||
|
||||
**文本 = 文本 + text.from_codepoint(首)**
|
||||
**文本 = 文本 + text.from_codepoint(首)**
|
||||
|
||||
**偏移 = 偏移 + 1**
|
||||
**偏移 = 偏移 + 1**
|
||||
|
||||
2. `首 < 194`
|
||||
`else`
|
||||
`if 首 < 194`
|
||||
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
|
||||
3. `首 < 224`
|
||||
`else`
|
||||
`if 首 < 224`
|
||||
|
||||
**结果 = 解码2(内存, 基址, 长度, 偏移, 文本)**
|
||||
**结果 = 解码2(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
**错误 = 结果[2]**
|
||||
|
||||
4. `首 < 240`
|
||||
`else`
|
||||
`if 首 < 240`
|
||||
|
||||
**结果 = 解码3(内存, 基址, 长度, 偏移, 文本)**
|
||||
**结果 = 解码3(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
**错误 = 结果[2]**
|
||||
|
||||
5. `首 < 245`
|
||||
`else`
|
||||
`if 首 < 245`
|
||||
|
||||
**结果 = 解码4(内存, 基址, 长度, 偏移, 文本)**
|
||||
**结果 = 解码4(内存, 基址, 长度, 偏移, 文本)**
|
||||
|
||||
**文本 = 结果[0]**
|
||||
**文本 = 结果[0]**
|
||||
|
||||
**偏移 = 结果[1]**
|
||||
**偏移 = 结果[1]**
|
||||
|
||||
**错误 = 结果[2]**
|
||||
**错误 = 结果[2]**
|
||||
|
||||
6. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
**错误 = "utf8.invalid_lead"**
|
||||
|
||||
1. `错误 == ""`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 错误 == ""`
|
||||
|
||||
*成功(文本)*
|
||||
`return 成功(文本)`
|
||||
|
||||
2. `true`
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
*失败(错误, "UTF-8 解码失败")*
|
||||
`return 失败(错误, "UTF-8 解码失败")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## utf8编码
|
||||
|
||||
`(文本, 内存, 基址)`
|
||||
`params(文本, 内存, 基址)`
|
||||
|
||||
*编码(文本, 内存, 基址)*
|
||||
`return 编码(文本, 内存, 基址)`
|
||||
|
||||
## utf8解码
|
||||
|
||||
`(内存, 基址, 长度)`
|
||||
`params(内存, 基址, 长度)`
|
||||
|
||||
*解码(内存, 基址, 长度)*
|
||||
`return 解码(内存, 基址, 长度)`
|
||||
Reference in new issue
Block a user