feat(lang): implement RFC-LANG-0002 unified semantics (breaking)

One Markdown-to-program mapping, no version selector or legacy mapper.

Frontend/runtime:
- resolve @exec/@knowledge heading declarations with nearest-ancestor
  inheritance; only an Exec H2 is callable; add UnitKind::Knowledge and
  sem.* diagnostics (mode marker, unit name, exec-without-function,
  payload nesting/empty, syntax, control stack, params position,
  version directive, inferred-signature lint)
- extract Strong/Emph/inline Code as one-statement carriers, refuse
  nested carriers, and honour heading segment boundaries so an if/while
  cannot be closed across a heading
- lower if/else/while/end to the existing Branch/Loop/Return IR with a
  compile-time control stack; keep payload-relative source spans through
  expression parsing and rewrite them onto the file
- accept params(...) only as the first direct Exec H2 payload; keep the
  existing free-variable inference and named-only rule for inferred
  signatures; fix duplicate-mode/else/end and Knowledge-body inertness
- reject marqdo_semantics/syntax front matter directives

Data and tests:
- migrate stdlib/**/*.mq.md, regenerate stdlib.json digest
- migrate examples and the full semantics/negative/runtime corpus to
  the only current syntax; expected values/runtime codes preserved
- migrate Rust-embedded .mq.md fixtures, init template, CLI help and
  security-lane fixtures

Verified: cargo fmt --check, cargo clippy -D warnings and
cargo test --workspace --locked (green).
This commit is contained in:
cflmy committed 2026-11-21 12:26:46 +08:00
1 parent 49835424ee
commit e029853e32
375 files changed
+6591 -4790

No files matched your search

+131 -90
View File
@@ -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: 毫秒}`