migrate: own format/io/console/stream sources and preserve the print alias target
Move the official stdlib IO modules to their unique source authority and keep the frozen public paths, including io.mq.md#写值 as the official print/打印 alias target. Carry the pre-migration product assertions into component cases with real resource-endpoint evidence for console. Refs cflmy/marqdo#9, cflmy/marqdo-std-io#1
This commit was merged in pull request #2.
This commit is contained in:
1 parent
945e76eed8
commit
e18b0b1b40
16 files changed
+1043
-6
No files matched your search
@@ -0,0 +1,334 @@
|
||||
|
||||
# @exec format
|
||||
## 字节长度
|
||||
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**总字节 = 0**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
|
||||
`if 码点 < 128`
|
||||
|
||||
**总字节 = 总字节 + 1**
|
||||
|
||||
`else`
|
||||
`if 码点 < 2048`
|
||||
|
||||
**总字节 = 总字节 + 2**
|
||||
|
||||
`else`
|
||||
`if 码点 < 65536`
|
||||
|
||||
**总字节 = 总字节 + 3**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**总字节 = 总字节 + 4**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 总字节`
|
||||
|
||||
## 显示
|
||||
|
||||
`params(值)`
|
||||
|
||||
按语言值语义格式化,用于与内置 打印 的输出对照。
|
||||
覆盖 Unit/None/Bool/Int/Float/Text/List/Map/Table/Formula/Code/
|
||||
FunctionRef/PluginHandle/Memory/Resource。
|
||||
|
||||
**类别 = 类型(值)**
|
||||
|
||||
`if 类别 == "Unit"`
|
||||
|
||||
`return "Unit"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "None"`
|
||||
|
||||
`return "None"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Text"`
|
||||
|
||||
`return 值`
|
||||
|
||||
`else`
|
||||
`if 类别 == "List"`
|
||||
|
||||
`return 列表显示(值)`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Map"`
|
||||
|
||||
`return 映射显示(值)`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Table"`
|
||||
|
||||
`return 表格显示(值)`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Formula"`
|
||||
|
||||
`return "Formula(" + 转义文本(值.latex) + ")"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Code"`
|
||||
|
||||
`return "Code(" + 转义文本(值.language) + ", " + 文本(字节长度(值.source)) + " bytes)"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "FunctionRef"`
|
||||
|
||||
`return "FunctionRef(" + 值.module + "::" + 值.name + ")"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "PluginHandle"`
|
||||
|
||||
`return "PluginHandle(" + 值.plugin + "::" + 值.export + ")"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Memory"`
|
||||
|
||||
`return "Memory"`
|
||||
|
||||
`else`
|
||||
`if 类别 == "Resource"`
|
||||
|
||||
`return "Resource"`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return 文本(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 列表显示
|
||||
|
||||
`params(列表)`
|
||||
|
||||
**结果 = "["**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + ", "**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 值显示(列表[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果 + "]"`
|
||||
|
||||
## 映射显示
|
||||
|
||||
`params(映射)`
|
||||
|
||||
**键 = map.keys(映射)**
|
||||
|
||||
**结果 = "{"**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(键)`
|
||||
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + ", "**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**当前键 = 键[下标]**
|
||||
|
||||
**结果 = 结果 + 当前键 + ": " + 值显示(map.get(映射, 当前键))**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果 + "}"`
|
||||
|
||||
## 表格显示
|
||||
|
||||
`params(表格)`
|
||||
|
||||
**列 = table.columns(表格)**
|
||||
|
||||
**结果 = "Table[" + 连接文本(列, " | ") + "]{"**
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
`while 行号 < 长度(表格)`
|
||||
|
||||
`if 行号 > 0`
|
||||
|
||||
**结果 = 结果 + "; "**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**行 = table.row(表格, 行号)**
|
||||
|
||||
**单元格 = []**
|
||||
|
||||
**列号 = 0**
|
||||
|
||||
`while 列号 < 长度(行)`
|
||||
|
||||
**单元格 = list.append(单元格, 显示(行[列号]))**
|
||||
|
||||
**列号 = 列号 + 1**
|
||||
|
||||
`end`
|
||||
**结果 = 结果 + 连接文本(单元格, " | ")**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果 + "}"`
|
||||
|
||||
## 连接文本
|
||||
|
||||
`params(列表, 分隔)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(列表)`
|
||||
|
||||
`if 下标 > 0`
|
||||
|
||||
**结果 = 结果 + 分隔**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**结果 = 结果 + 列表[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 值显示
|
||||
|
||||
`params(值)`
|
||||
|
||||
列表/映射/表格内部使用 repr 语义:Text 带引号并转义,其它值递归显示。
|
||||
|
||||
**类别 = 类型(值)**
|
||||
|
||||
`if 类别 == "Text"`
|
||||
|
||||
`return 转义文本(值)`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return 显示(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 转义文本
|
||||
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**`结果 = "\""`**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
|
||||
`if 码点 == 34`
|
||||
|
||||
**`结果 = 结果 + "\\\""`**
|
||||
|
||||
`else`
|
||||
`if 码点 == 92`
|
||||
|
||||
**`结果 = 结果 + "\\\\"`**
|
||||
|
||||
`else`
|
||||
`if 码点 == 10`
|
||||
|
||||
**`结果 = 结果 + "\\n"`**
|
||||
|
||||
`else`
|
||||
`if 码点 == 9`
|
||||
|
||||
**`结果 = 结果 + "\\t"`**
|
||||
|
||||
`else`
|
||||
`if 码点 == 13`
|
||||
|
||||
**`结果 = 结果 + "\\r"`**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(码点)**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
``return 结果 + "\""``
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
import:
|
||||
format: "./format.mq.md"
|
||||
stream: "./io/stream.mq.md"
|
||||
console: "./io/console.mq.md"
|
||||
---
|
||||
|
||||
# @exec io
|
||||
|
||||
## 写文本
|
||||
|
||||
`params(文本)`
|
||||
|
||||
`return stream.写文本(文本)`
|
||||
|
||||
## 写全部
|
||||
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||
|
||||
## 写值
|
||||
|
||||
`params(值)`
|
||||
|
||||
按语言值语义格式化后输出,并追加换行。
|
||||
|
||||
`return stream.写文本(format.显示(值) + "\n")`
|
||||
|
||||
## 读取行
|
||||
|
||||
`params()`
|
||||
|
||||
`return console.读取行()`
|
||||
|
||||
## 打印
|
||||
|
||||
`params(值)`
|
||||
|
||||
`return console.打印(值)`
|
||||
@@ -0,0 +1,146 @@
|
||||
---
|
||||
import:
|
||||
format: "../format.mq.md"
|
||||
stream: "./stream.mq.md"
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec console
|
||||
|
||||
## 读取行
|
||||
|
||||
`params()`
|
||||
|
||||
从已授权的 stdio/stdin 资源读取一行。按有界块读取并增量解码 UTF-8;
|
||||
EOF 返回 {ok:false, code:"io.eof"},无效 UTF-8 返回可恢复错误。
|
||||
|
||||
**流 = resource.open("stdio", "stdin", ["read"])**
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**字节 = []**
|
||||
|
||||
**结束 = false**
|
||||
|
||||
**错误 = ""**
|
||||
|
||||
`while not 结束`
|
||||
|
||||
**读取 = resource.read(流, 内存, 0, 1)**
|
||||
|
||||
`if not 读取.ok`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
**错误 = 读取.code**
|
||||
|
||||
`else`
|
||||
`if 读取.value.eof and 读取.value.count == 0`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
`else`
|
||||
`if 内存[0] == 10`
|
||||
|
||||
**结束 = true**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**字节 = list.append(字节, 内存[0])**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
`if 错误 != ""`
|
||||
|
||||
`return {ok: false, code: 错误, message: "stdin read failed"}`
|
||||
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
`return 关闭`
|
||||
|
||||
`else`
|
||||
`if 长度(字节) == 0`
|
||||
|
||||
`return {ok: false, code: "io.eof", message: "end of input"}`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**数据长度 = 长度(字节)**
|
||||
|
||||
`if 数据长度 > 0 and 字节[数据长度 - 1] == 13`
|
||||
|
||||
**数据长度 = 数据长度 - 1**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**页 = (数据长度 + 65535) // 65536**
|
||||
|
||||
**数据 = memory.new(页, 页)**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 数据长度`
|
||||
|
||||
**`数据[下标] = 字节[下标]`**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
**解码 = utf8.解码(数据, 0, 数据长度)**
|
||||
|
||||
`if not 解码.ok`
|
||||
|
||||
`return 解码`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: 解码.value}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 打印
|
||||
|
||||
`params(值)`
|
||||
|
||||
**结果 = stream.写文本(format.显示(值) + "\n")**
|
||||
|
||||
`if not 结果.ok`
|
||||
|
||||
`return 结果`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: None}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`params(文本)`
|
||||
|
||||
`return stream.写文本(文本)`
|
||||
|
||||
## 写全部
|
||||
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||
@@ -0,0 +1,122 @@
|
||||
---
|
||||
import:
|
||||
format: "../format.mq.md"
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
# @exec stream
|
||||
|
||||
## 页数
|
||||
|
||||
`params(字节)`
|
||||
|
||||
**页 = (字节 + 65535) // 65536**
|
||||
|
||||
`if 页 == 0`
|
||||
|
||||
`return 1`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return 页`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
## 写全部
|
||||
|
||||
`params(流, 内存, 起点, 长度)`
|
||||
|
||||
把 长度 字节从 内存 写到资源流。短写只返回实际数量,调用方继续;
|
||||
零进度返回 io.write_zero,不静默吞掉失败。
|
||||
|
||||
**已写 = 0**
|
||||
|
||||
**失败 = false**
|
||||
|
||||
**本次 = {ok: true, value: {count: 0}}**
|
||||
|
||||
**错误结果 = {ok: false, code: "io.write_zero", message: "resource.write made no progress"}**
|
||||
|
||||
`while 已写 < 长度`
|
||||
|
||||
**剩余 = 长度 - 已写**
|
||||
|
||||
**本次 = resource.write(流, 内存, 起点 + 已写, 剩余)**
|
||||
|
||||
`if not 本次.ok`
|
||||
|
||||
**失败 = true**
|
||||
|
||||
**错误结果 = 本次**
|
||||
|
||||
**已写 = 长度**
|
||||
|
||||
`else`
|
||||
`if 本次.value.count == 0`
|
||||
|
||||
**失败 = true**
|
||||
|
||||
**已写 = 长度**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**已写 = 已写 + 本次.value.count**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`if 失败`
|
||||
|
||||
`return 错误结果`
|
||||
|
||||
`else`
|
||||
`if 已写 < 长度`
|
||||
|
||||
`return 错误结果`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: {count: 长度}}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
## 写文本
|
||||
|
||||
`params(文本)`
|
||||
|
||||
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。内存按实际字节长度增长,
|
||||
写入循环检查 {ok,count};只有全部字节写完并关闭成功才返回成功。
|
||||
|
||||
**字节数 = format.字节长度(文本)**
|
||||
|
||||
**内存 = memory.new(页数(字节数), 页数(字节数))**
|
||||
|
||||
**长度 = utf8.编码(文本, 内存, 0)**
|
||||
|
||||
**流 = resource.open("stdio", "stdout", ["write"])**
|
||||
|
||||
**写结果 = 写全部(流, 内存, 0, 长度)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
`if not 写结果.ok`
|
||||
|
||||
`return 写结果`
|
||||
|
||||
`else`
|
||||
`if not 关闭.ok`
|
||||
|
||||
`return 关闭`
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
`return {ok: true, value: {count: 长度}}`
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
Reference in new issue
Block a user