From b00b018056e99b55c1bc7eec35945d3a8c0a4e8b Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 6 Aug 2026 11:53:14 +0800 Subject: [PATCH] Add object primitives, official ext/llm, and HTTPS/dotenv platform. Introduce # objects with ## methods (handles + ` x.method `), ship OpenAI-compatible ext/llm, and upgrade net/sys for HTTPS headers and dotenv loading. Co-authored-by: Cursor --- .cursor/skills/marqdo/SKILL.md | 7 +- .cursor/skills/marqdo/reference.md | 9 +- .gitignore | 1 + Cargo.lock | 361 +++++++++++++++++++++++++++- Cargo.toml | 4 + README.md | 4 +- doc/design/dependencies.md | 1 + doc/design/ext-llm.md | 59 +++++ doc/design/markdown-mapping.md | 13 +- doc/design/objects.md | 73 ++++++ doc/design/stdlib-modules.md | 6 +- ext/llm.mq.md | 130 ++++++++++ ext/大模型.mq.md | 130 ++++++++++ lib/foreign.mq.md | 8 +- lib/fs.mq.md | 14 +- lib/json.mq.md | 13 +- lib/math.mq.md | 72 +++--- lib/net.mq.md | 20 +- lib/sys.mq.md | 20 +- lib/table.mq.md | 4 +- lib/text.mq.md | 6 +- lib/time.mq.md | 10 +- lib/外联.mq.md | 8 +- lib/数学.mq.md | 72 +++--- lib/文件.mq.md | 14 +- lib/文本.mq.md | 6 +- lib/时间.mq.md | 10 +- lib/系统.mq.md | 20 +- lib/网络.mq.md | 20 +- lib/表.mq.md | 4 +- public/stdlib/net.mq.md | 6 +- public/structure/import/utils.mq.md | 4 +- public/structure/import/工具.mq.md | 4 +- skills/marqdo/SKILL.md | 7 +- skills/marqdo/reference.md | 9 +- src/ast.rs | 17 +- src/bytecode/compile.rs | 117 ++++++++- src/bytecode/mod.rs | 9 + src/bytecode/vm.rs | 93 +++++++ src/host/dispatch.rs | 39 ++- src/host/json.rs | 22 ++ src/host/net.rs | 214 ++++++++++------- src/host/sys.rs | 82 +++++++ src/interp.rs | 89 ++++++- src/load.rs | 40 ++- src/parse/expr.rs | 39 ++- src/parse/mod.rs | 2 +- src/view/render.rs | 9 +- tests/ext/llm-complete.mq.md | 15 ++ tests/ext/llm-import.mq.md | 10 + tests/gold.rs | 20 ++ tests/lib/json-quote.mq.md | 10 + tests/lib/sys-dotenv.mq.md | 15 ++ tests/structure/import/utils.mq.md | 4 +- tests/structure/object-handle.mq.md | 37 +++ 55 files changed, 1741 insertions(+), 291 deletions(-) create mode 100644 doc/design/ext-llm.md create mode 100644 doc/design/objects.md create mode 100644 ext/llm.mq.md create mode 100644 ext/大模型.mq.md create mode 100644 tests/ext/llm-complete.mq.md create mode 100644 tests/ext/llm-import.mq.md create mode 100644 tests/lib/json-quote.mq.md create mode 100644 tests/lib/sys-dotenv.mq.md create mode 100644 tests/structure/object-handle.mq.md diff --git a/.cursor/skills/marqdo/SKILL.md b/.cursor/skills/marqdo/SKILL.md index 4628549..6749974 100644 --- a/.cursor/skills/marqdo/SKILL.md +++ b/.cursor/skills/marqdo/SKILL.md @@ -28,12 +28,14 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords. | Marker | Meaning | |--------|---------| -| `#` … `######` | Function; heading depth = nesting/scope | -| `- name` under a function heading | Parameter | +| `#` | **Object / type** (constructor body). `# main` = entry object. | +| `##` … `######` | **Function / method** (nesting by heading depth). | +| `- name` under a heading | Parameter | | `- …` inside body | Loop | | `+ …` or `1.` | Branch arm (condition) | | Line that is only `*` | Else arm | | `> fn args` | Call (named `k=v` or positional) | +| `` > `obj`.method args `` | Method call (`obj` must be a map with `_type`) | | Frontmatter `> path.mq.md` | Import | | `*…*` | Statement (bind / expr) | | `**…**` | Return value | @@ -119,6 +121,7 @@ title: example - Import **English** or **Chinese** library file; call that file’s API names (do not mix). - `lib/…` resolves via `MARQDO_LIB`, cwd `lib/`, or `lib/` next to the `marqdo` binary. +- Official optional extensions: `ext/llm.mq.md` / `ext/大模型.mq.md` — `# llm` / `# 大模型` object handles + `## complete` / `## 运行`. Free `## load_env`. See `doc/design/ext-llm.md` and `doc/design/objects.md`. - Builtins (no import): `print`/`打印`, `input`/`输入`, `len`/`长度`, `str`/`文本`, `int`/`整数`; literals `True`/`真`, `False`/`假`, `None`/`空`; logic `and`/`且`, `or`/`或`, `not`/`非`. Stdlib map: [reference.md](reference.md). diff --git a/.cursor/skills/marqdo/reference.md b/.cursor/skills/marqdo/reference.md index 0b905ea..ccf2275 100644 --- a/.cursor/skills/marqdo/reference.md +++ b/.cursor/skills/marqdo/reference.md @@ -17,6 +17,8 @@ Diagnostics look like `path:line:col: message` (1-based line/col). Stdlib search order for `lib/…` imports: `MARQDO_LIB`, `./lib`, directory of the `marqdo` executable (and a few parents). Prefer the **bundle zip** (`marqdo.exe` + `lib/`) over a bare exe. +官方扩展(`ext/…`,非 stdlib):`MARQDO_EXT`、`./ext`、或可执行文件旁 `ext/`。对象原语见 `doc/design/objects.md`。LLM:`ext/llm.mq.md` / `ext/大模型.mq.md`(`# llm` 句柄 + `` `x`.complete `` / `` `x`.运行 ``;项目本地 `.env` 配密钥)。 + ## Builtins (no import) | EN | ZH | Role | @@ -38,12 +40,13 @@ Import one file; use **that** file’s function names. | `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… | | `lib/table.mq.md` | `lib/表.mq.md` | table/rows helpers | | `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem | -| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd | +| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv | | `lib/time.mq.md` | `lib/时间.mq.md` | time/format | -| `lib/net.mq.md` | `lib/网络.mq.md` | network helpers | -| `lib/json.mq.md` | (see design) | JSON | +| `lib/net.mq.md` | `lib/网络.mq.md` | HTTP(S) helpers (+ optional headers) | +| `lib/json.mq.md` | (see design) | JSON (+ `quote`) | | `lib/math.mq.md` | `lib/数学.mq.md` | num, trig, random, formula, plot | | `lib/foreign.mq.md` | `lib/外联.mq.md` | run foreign fenced blocks | +| `ext/llm.mq.md` | `ext/大模型.mq.md` | `# llm` / `# 大模型` object + chat methods | Open the imported `.mq.md` under `lib/` to see exact `#` function names and parameters. Gold tests: `tests/lib/`, `tests/structure/`. diff --git a/.gitignore b/.gitignore index ae8fa23..3e97ad8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.pdb .DS_Store .marqdo/ +.env # Generated HTML beside tracked public/*.mq.md (sources stay in git) /public/index.html diff --git a/Cargo.lock b/Cargo.lock index dc3ad20..e559070 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aho-corasick" version = "1.1.5" @@ -13,9 +19,9 @@ dependencies = [ [[package]] name = "android_system_properties" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" dependencies = [ "libc", ] @@ -56,7 +62,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -67,7 +73,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -103,6 +109,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bitflags" version = "2.13.1" @@ -211,6 +223,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + [[package]] name = "difflib" version = "0.4.0" @@ -229,6 +250,16 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "float-cmp" version = "0.9.0" @@ -238,6 +269,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures-core" version = "0.3.33" @@ -271,6 +311,17 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.4.1" @@ -307,6 +358,37 @@ dependencies = [ "cc", ] +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "279259b0ac81c89d11c290495fdcfa96ea3643b7df311c138b6fe8ca5237f0f8" +dependencies = [ + "idna_mapping", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "idna_mapping" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c13906586a4b339310541a274dd927aff6fcbb5b8e3af90634c4b31681c792" +dependencies = [ + "unicode-joining-type", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -344,17 +426,20 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" [[package]] name = "marqdo" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "assert_cmd", "chrono", "clap", + "idna_adapter", "predicates", "pulldown-cmark", "serde_json", "thiserror", "tiny_http", + "ureq", + "zeroize", ] [[package]] @@ -363,6 +448,16 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" @@ -390,6 +485,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -477,9 +578,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.16" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fcfdb36bda0c880c5931cdc7a2bcdc8ba4556847b9d912bca70bc94708711ad" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" dependencies = [ "aho-corasick", "memchr", @@ -492,6 +593,54 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustls" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" +dependencies = [ + "log", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.102.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -546,18 +695,36 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "slab" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.119" @@ -618,24 +785,101 @@ dependencies = [ "log", ] +[[package]] +name = "tinyvec" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb4ebadaa0af04fab11ae01eb5f9fdb5f9c5b875506e210e71c07873528baa7f" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "unicase" version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" +[[package]] +name = "unicode-bidi" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-joining-type" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8d00a78170970967fdb83f9d49b92f959ab2bb829186b113e4f4604ad98e180" + +[[package]] +name = "unicode-normalization" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-width" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "2.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d11a831e3c0b56e438a28308e7c810799e3c118417f342d30ecec080105395cd" +dependencies = [ + "base64", + "flate2", + "log", + "once_cell", + "rustls", + "rustls-pki-types", + "rustls-webpki", + "url", + "webpki-roots 0.26.11", +] + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" @@ -651,6 +895,12 @@ dependencies = [ "libc", ] +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "wasm-bindgen" version = "0.2.126" @@ -696,6 +946,24 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "webpki-roots" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +dependencies = [ + "webpki-roots 1.0.9", +] + +[[package]] +name = "webpki-roots" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "windows-core" version = "0.62.2" @@ -755,6 +1023,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.61.2" @@ -764,6 +1041,76 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + [[package]] name = "zmij" version = "1.0.23" diff --git a/Cargo.toml b/Cargo.toml index 7b6a49c..559872b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,10 @@ pulldown-cmark = "0.12" serde_json = "1" thiserror = "1" tiny_http = "0.12" +# Pin for Cargo/rustc 1.81 (newer rustls/idna/zeroize need edition2024). +ureq = { version = "=2.9.7", default-features = false, features = ["tls", "gzip"] } +idna_adapter = "=1.1.0" +zeroize = "=1.8.1" [dev-dependencies] assert_cmd = "=2.0.14" diff --git a/README.md b/README.md index 24842ca..1f4b7d6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件, 欢迎访问 [marqdo 官方网站](https://www.marqdo.com/) 阅读更多特性与可执行文档。 -语法宪法:[doc/design/markdown-mapping.md](doc/design/markdown-mapping.md) · 调用实参:[call-arguments.md](doc/design/call-arguments.md) · 用户站:[user-site.md](doc/design/user-site.md) · 浏览:[view.md](doc/design/view.md) · 调试:[view-debug.md](doc/design/view-debug.md) · OKF 清单:[catalog-cli.md](doc/design/catalog-cli.md) · VS Code 扩展:[vscode-extension.md](doc/design/vscode-extension.md)(分支 `vscode-extension`) · **AI Skill**:[skills/marqdo/](skills/marqdo/)(说明 [ai-skill.md](doc/design/ai-skill.md)) · 金样例:[tests/](tests/) · 用户文档:[public/](public/) +语法宪法:[doc/design/markdown-mapping.md](doc/design/markdown-mapping.md) · 对象:[objects.md](doc/design/objects.md) · 调用实参:[call-arguments.md](doc/design/call-arguments.md) · 用户站:[user-site.md](doc/design/user-site.md) · 浏览:[view.md](doc/design/view.md) · 调试:[view-debug.md](doc/design/view-debug.md) · OKF 清单:[catalog-cli.md](doc/design/catalog-cli.md) · VS Code 扩展:[vscode-extension.md](doc/design/vscode-extension.md)(分支 `vscode-extension`) · **AI Skill**:[skills/marqdo/](skills/marqdo/)(说明 [ai-skill.md](doc/design/ai-skill.md)) · 官方扩展:[ext-llm.md](doc/design/ext-llm.md)(`ext/llm`) · 金样例:[tests/](tests/) · 用户文档:[public/](public/) --- @@ -116,7 +116,7 @@ cargo run -- view output public -o public powershell -File ./scripts/build-public.ps1 ``` -**发布包(GitHub Releases)**:单独 `.exe` **不含** `lib/`;请下载带 `lib/` 的 zip,或另下 `*-stdlib.zip` 解压到可执行文件旁(亦可设 `MARQDO_LIB`)。 +**发布包(GitHub Releases)**:单独 `.exe` **不含** `lib/` / `ext/`;请下载带 `lib/`(及可选 `ext/`)的 zip,或另下 `*-stdlib.zip` 解压到可执行文件旁(亦可设 `MARQDO_LIB` / `MARQDO_EXT`)。OpenAI 兼容聊天见 [ext-llm.md](doc/design/ext-llm.md)(在项目目录自建 `.env`,勿提交密钥)。 文档:用户站 [public/](public/) · 设计 [doc/](doc/) · OKF / catalog [catalog-cli.md](doc/design/catalog-cli.md) · 调试 [view-debug.md](doc/design/view-debug.md) diff --git a/doc/design/dependencies.md b/doc/design/dependencies.md index 7e2cf61..dde6088 100644 --- a/doc/design/dependencies.md +++ b/doc/design/dependencies.md @@ -81,6 +81,7 @@ Marqdo 换 **Rust 或 C++** 都不改变「主前端不该是 Flex/Bison」这 | **thiserror** / **anyhow** | 错误类型 | 诊断与 CLI 报错 | | **clap** | CLI | `marqdo run [path]` | | **serde** + **serde_yaml** | 元信息 | 仅解析 frontmatter 中的 YAML **键值**;`> x.mq.md` 仍按**行**抽取(见 Spike) | +| **ureq** (=2.9.7) | HTTP(S) | `lib/net`;为 Cargo 1.81 另钉 `idna_adapter`/`zeroize`(见根 `Cargo.toml`) | #### 强烈建议 diff --git a/doc/design/ext-llm.md b/doc/design/ext-llm.md new file mode 100644 index 0000000..5149a3c --- /dev/null +++ b/doc/design/ext-llm.md @@ -0,0 +1,59 @@ +# Official extensions (`ext/`) + +| | | +|---|---| +| Status | Accepted (v0.1.x) | +| Date | 2026-08-06 | +| Related | [objects.md](objects.md) | + +## Scope + +`ext/` holds **official optional extensions** shipped with Marqdo. They are **not** stdlib (`lib/`). Users import only what they need: + +```markdown +--- +> ext/llm.mq.md +--- +``` + +Resolution: `MARQDO_EXT`, `./ext`, `ext/` next to the binary (same pattern as `MARQDO_LIB`). + +## Platform prerequisites + +| Capability | Surface | +|------------|---------| +| HTTPS + headers | `lib/net` → `http_post` / `headers=` map | +| Dotenv | `## load_env` in ext (or `load_dotenv` host) | +| JSON quote | `lib/json` → `quote` | + +## `ext/llm` / `ext/大模型` (object handles) + +| English | Chinese | Role | +|---------|---------|------| +| `## load_env` | `## 加载环境` | Free function: load `.env` | +| `# llm` | `# 大模型` | Object ctor → handle map | +| `## complete` / `## chat` | `## 运行` / `## 聊天` | Methods on the handle | + +```markdown +--- +> ext/llm.mq.md +--- + +# main + +> load_env + +*`model` = > llm * +*`reply` = > `model`.complete prompt=Say hi in one word * +> print text=`reply` +``` + +Environment (create a **project-local** `.env`; do not commit secrets): + +```env +OPENAI_API_KEY=sk-... +OPENAI_BASE_URL=https://api.openai.com/v1 +OPENAI_MODEL=gpt-4o-mini +``` + +Fallbacks: `MARQDO_LLM_API_KEY`, `MARQDO_LLM_BASE_URL`, `MARQDO_LLM_MODEL`. diff --git a/doc/design/markdown-mapping.md b/doc/design/markdown-mapping.md index fba05ab..1607193 100644 --- a/doc/design/markdown-mapping.md +++ b/doc/design/markdown-mapping.md @@ -39,12 +39,13 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置 | 标记 | 语义 | |------|------| -| `#` … `######` | 函数;层数 = 作用域 | -| 函数头下 `-` 纯名 | 形参 | +| `#` … `######` | **`#` = 对象/类型**;**`##`+ = 函数/方法**(层差=嵌套)。见 [objects.md](objects.md) | +| 函数头下 `-` 纯名 | 形参 / 构造参数 | | 体内 `-` | 循环 | | 体内 `+` 或 `1.` | 分支 | | 分支臂头整行恰为 `*` | else | | `> 函数 形参=实参` 或 `> 函数 实参` | 调用(具名 / 位置,见 [call-arguments.md](call-arguments.md)) | +| `` > `对象`.方法 … `` | 方法调用(实例 map 须含 `_type`) | | frontmatter `> path.mq.md` | 跨文件导入 | | `*…*` | **程序语句**(绑定、表达式句、可写的调用句等) | | `**…**` | **返回值**(架构级;函数结果) | @@ -111,9 +112,11 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置 --- -## 6. 函数、作用域、提升 +## 6. 对象、函数、作用域 -规则同 v0。推荐: +- `# Name`:对象(构造体);`# main` 为入口对象。 +- `##` / `###` …:函数或方法;挂在对象下则为方法。 +- 标准库顶层 API 使用自由函数 `## name`(见 [objects.md](objects.md))。 ```markdown # main @@ -128,7 +131,7 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置 (需要打印时对返回值再 `> print`,或直接 `> print`。) -形参 `- 名` → 体内 `` `名` ``。 +形参 `- 名` → 体内 `` `名` ``。方法体内自动绑定 `自` / `self`。 --- diff --git a/doc/design/objects.md b/doc/design/objects.md new file mode 100644 index 0000000..6e52b6d --- /dev/null +++ b/doc/design/objects.md @@ -0,0 +1,73 @@ +# Objects(`#`)与函数(`##`+) + +| | | +|---|---| +| 状态 | Accepted | +| 日期 | 2026-08-06 | +| 相关 | [markdown-mapping.md](markdown-mapping.md) · [ext-llm.md](ext-llm.md) | + +## 1. 约定 + +| 标题层 | 含义 | +|--------|------| +| `#`(恰好一层) | **对象 / 类型**。正文 = 构造体;调用类型名 = 构造实例。 | +| `##` 及更深 | **函数**(自由函数或方法)。层数差表示嵌套,与 v0.1 相同。 | + +`# main` 是**入口对象**:运行文件 = 执行其构造体。若无 `main`,且文件恰好只有一个无参顶层 `#` 对象(如 `# Hello World`),则把它当作入口。 + +## 2. 实例句柄 + +构造返回值若为 `map`,运行时写入 `_type`(文本,值为类型名),形成句柄。 +方法调用通过 `_type` 找到定义对象,再解析其下的 `##` 方法。 + +方法体内自动绑定: + +- `自`(中文) +- `self`(英文) + +二者同一接收者。 + +## 3. 调用外形 + +```markdown +# 大模型 + - 模型名 + +*`自` = …配置 map…* +**`自`** + +## 运行 + - 提示 + +…使用 `自` … +**`内容`** + +# main + +*`模型` = > 大模型 模型名=gpt-4o-mini * +*`回复` = > `模型`.运行 提示=你好 * +> print text=`回复` +``` + +- `> 大模型 …` — 构造 +- `` > `模型`.运行 … `` — 方法(接收者 + `.` + 方法名) +- `## 自由函数` 挂在模块顶层(非某对象子标题)— 与旧版顶层 `# http_get` 对等,现写为 `## http_get` + +## 4. 标准库 + +官方 `lib/*.mq.md` 以**顶层自由函数**为主:全部为 `##`(不再使用 `#` 冒充类型)。 +真正的类型化扩展(如 `ext/llm`)使用 `# 类型` + `##` 方法。 + +## 5. 非目标(本波) + +- 继承 / 接口 / 可见性 +- 无 `_type` 的「鸭子」方法分发 +- 用表格声明字段 schema(表格仍表示集合) + +## 6. 迁移摘要 + +| 旧 | 新 | +|----|----| +| 库顶层 `# http_get` | `## http_get` | +| `# main` + `## 嵌套` | 不变(main = 入口对象) | +| `ext` 顶层 `# complete` | `# llm` / `# 大模型` 构造 + `## complete` / `## 运行` | diff --git a/doc/design/stdlib-modules.md b/doc/design/stdlib-modules.md index 8c378cd..d24941e 100644 --- a/doc/design/stdlib-modules.md +++ b/doc/design/stdlib-modules.md @@ -82,6 +82,7 @@ OS / HTTP / 时钟 / … |------|------|------|------| | `env_get` | `取环境` | `name` / `名` | 文本或 `None` | | `env_set` | `设环境` | `name`, `value` / `名`, `值` | `None` | +| `load_dotenv` | `加载环境` | 可选 `path` | 新写入的键数量;缺文件 → `0`;不覆盖已有环境变量 | | `args` | `参数表` | (无) | 文本列表 | | `cwd` | `工作目录` | (无) | 文本 | | `exit` | `退出` | `code` / `码` | 不返回 | @@ -120,6 +121,7 @@ JSON 是专有缩写,**很难也不必要**做合规中文库名/API 翻译。 | `stringify` | `value`, 可选 `indent` | JSON 文本 | | `get` | `value`, `key` | 值或 `None` | | `keys` | `value` | 文本列表 | +| `quote` | `text` | JSON 字符串字面量(含引号,便于拼请求体) | **类型映射(草案)**: @@ -143,10 +145,12 @@ JSON 是专有缩写,**很难也不必要**做合规中文库名/API 翻译。 | `http_post` | `提交` | `url`, `body`, 可选 `content_type` | 同上 | | `url_encode` | `编码地址` | `text` / `内容` | 文本 | -仅 **HTTP 明文**(HTTPS 本波未做,会诊断提示);短超时、响应体上限;默认需 `--allow-net`;view / 静态站默认关。 +仅 **HTTP / HTTPS**(ureq);短超时、响应体上限;默认 CLI 开网络。可选具名实参 `headers=`(map)、`content_type=`;`http_post` 默认 JSON Content-Type。 **路径字面量:** 具名实参里 `path=a/b` 会被解析成除法。请用无斜杠文件名,或先赋给变量再传入。 +官方可选扩展(非 stdlib):[`ext-llm.md`](ext-llm.md)(`ext/llm` OpenAI 兼容聊天)。 + --- ## 4. 中英路径总表(本波) diff --git a/ext/llm.mq.md b/ext/llm.mq.md new file mode 100644 index 0000000..049f671 --- /dev/null +++ b/ext/llm.mq.md @@ -0,0 +1,130 @@ +--- +title: ext/llm +description: Official OpenAI-compatible LLM object (English). Not stdlib — import ext/llm.mq.md. +> lib/sys.mq.md +> lib/net.mq.md +> lib/json.mq.md +--- + +## load_env + +Load `.env` from cwd (optional named arg `path=`). Does not override existing variables. + +**> load_dotenv** + +# llm + +Construct an LLM handle from `OPENAI_*` / `MARQDO_LLM_*` (default base OpenAI v1, model `gpt-4o-mini`). + +*`d` = > parse text={"base":"https://api.openai.com/v1","model":"gpt-4o-mini","suffix":"/chat/completions","bearer":"Bearer ","p1":"{\"model\":","p2":",\"messages\":[{\"role\":\"user\",\"content\":","p3":"}]}","h1":"{\"Authorization\":","h2":"}","a1":"{\"api_key\":","a2":",\"base_url\":","a3":",\"model\":","a4":",\"suffix\":","a5":",\"bearer\":","a6":",\"p1\":","a7":",\"p2\":","a8":",\"p3\":","a9":",\"h1\":","a10":",\"h2\":","a11":"}"} * + +*`api_key` = > env_get name=OPENAI_API_KEY * ++ `api_key` + *`_k` = 1* ++ * + *`api_key` = > env_get name=MARQDO_LLM_API_KEY * ++ `api_key` + *`_k` = 1* ++ * + > print text=ext/llm: set OPENAI_API_KEY or MARQDO_LLM_API_KEY + > exit code=1 + +*`base_url` = > env_get name=OPENAI_BASE_URL * ++ `base_url` + *`_k` = 1* ++ * + *`base_url` = > env_get name=MARQDO_LLM_BASE_URL * ++ `base_url` + *`_k` = 1* ++ * + *`base_url` = > get value=`d` key=base * + +*`model` = > env_get name=OPENAI_MODEL * ++ `model` + *`_k` = 1* ++ * + *`model` = > env_get name=MARQDO_LLM_MODEL * ++ `model` + *`_k` = 1* ++ * + *`model` = > get value=`d` key=model * + +*`q_key` = > quote text=`api_key` * +*`q_base` = > quote text=`base_url` * +*`q_model` = > quote text=`model` * +*`suffix` = > get value=`d` key=suffix * +*`bearer` = > get value=`d` key=bearer * +*`p1` = > get value=`d` key=p1 * +*`p2` = > get value=`d` key=p2 * +*`p3` = > get value=`d` key=p3 * +*`h1` = > get value=`d` key=h1 * +*`h2` = > get value=`d` key=h2 * +*`q_suf` = > quote text=`suffix` * +*`q_br` = > quote text=`bearer` * +*`q_p1` = > quote text=`p1` * +*`q_p2` = > quote text=`p2` * +*`q_p3` = > quote text=`p3` * +*`q_h1` = > quote text=`h1` * +*`q_h2` = > quote text=`h2` * +*`a1` = > get value=`d` key=a1 * +*`a2` = > get value=`d` key=a2 * +*`a3` = > get value=`d` key=a3 * +*`a4` = > get value=`d` key=a4 * +*`a5` = > get value=`d` key=a5 * +*`a6` = > get value=`d` key=a6 * +*`a7` = > get value=`d` key=a7 * +*`a8` = > get value=`d` key=a8 * +*`a9` = > get value=`d` key=a9 * +*`a10` = > get value=`d` key=a10 * +*`a11` = > get value=`d` key=a11 * +*`raw` = `a1` + `q_key` + `a2` + `q_base` + `a3` + `q_model` + `a4` + `q_suf` + `a5` + `q_br` + `a6` + `q_p1` + `a7` + `q_p2` + `a8` + `q_p3` + `a9` + `q_h1` + `a10` + `q_h2` + `a11` * +**> parse text=`raw`** + +## complete + - prompt + +Chat completion using `self` / `自` handle fields. + +*`api_key` = > get value=`self` key=api_key * +*`base_url` = > get value=`self` key=base_url * +*`model` = > get value=`self` key=model * +*`suffix` = > get value=`self` key=suffix * +*`bearer` = > get value=`self` key=bearer * +*`p1` = > get value=`self` key=p1 * +*`p2` = > get value=`self` key=p2 * +*`p3` = > get value=`self` key=p3 * +*`h1` = > get value=`self` key=h1 * +*`h2` = > get value=`self` key=h2 * +*`url` = `base_url` + `suffix` * +*`auth` = `bearer` + `api_key` * +*`q_auth` = > quote text=`auth` * +*`hdr_raw` = `h1` + `q_auth` + `h2` * +*`headers` = > parse text=`hdr_raw` * +*`q_model` = > quote text=`model` * +*`q_prompt` = > quote text=`prompt` * +*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` * +*`resp` = > http_post url=`url` body=`body` headers=`headers` * +*`status` = > get value=`resp` key=status * +*`raw` = > get value=`resp` key=body * + ++ `status` == 200 + *`data` = > parse text=`raw` * + *`choices` = > get value=`data` key=choices * + *`first` = > at value=`choices` index=0 * + *`message` = > get value=`first` key=message * + *`content` = > get value=`message` key=content * + **`content`** ++ * + > print text=ext/llm: HTTP error + > print text=`status` + > print text=`raw` + > exit code=1 + +--- + +## chat + - prompt + +Alias for `complete`. + +**> `self`.complete prompt=`prompt`** diff --git a/ext/大模型.mq.md b/ext/大模型.mq.md new file mode 100644 index 0000000..a2b6b46 --- /dev/null +++ b/ext/大模型.mq.md @@ -0,0 +1,130 @@ +--- +title: ext/大模型 +description: 官方 OpenAI 兼容大模型对象(中文)。非标准库 — 导入 ext/大模型.mq.md。 +> lib/系统.mq.md +> lib/网络.mq.md +> lib/json.mq.md +--- + +## 加载环境 + +从工作目录加载 `.env`(可选具名实参 `path=`)。不覆盖已有变量。 + +**> load_dotenv** + +# 大模型 + +从 `OPENAI_*` / `MARQDO_LLM_*` 构造句柄(默认 OpenAI v1 + `gpt-4o-mini`)。 + +*`d` = > parse text={"base":"https://api.openai.com/v1","model":"gpt-4o-mini","suffix":"/chat/completions","bearer":"Bearer ","p1":"{\"model\":","p2":",\"messages\":[{\"role\":\"user\",\"content\":","p3":"}]}","h1":"{\"Authorization\":","h2":"}","a1":"{\"api_key\":","a2":",\"base_url\":","a3":",\"model\":","a4":",\"suffix\":","a5":",\"bearer\":","a6":",\"p1\":","a7":",\"p2\":","a8":",\"p3\":","a9":",\"h1\":","a10":",\"h2\":","a11":"}"} * + +*`api_key` = > 取环境 名=OPENAI_API_KEY * ++ `api_key` + *`_k` = 1* ++ * + *`api_key` = > 取环境 名=MARQDO_LLM_API_KEY * ++ `api_key` + *`_k` = 1* ++ * + > 打印 内容=ext/大模型: 请设置 OPENAI_API_KEY 或 MARQDO_LLM_API_KEY + > 退出 码=1 + +*`base_url` = > 取环境 名=OPENAI_BASE_URL * ++ `base_url` + *`_k` = 1* ++ * + *`base_url` = > 取环境 名=MARQDO_LLM_BASE_URL * ++ `base_url` + *`_k` = 1* ++ * + *`base_url` = > get value=`d` key=base * + +*`model` = > 取环境 名=OPENAI_MODEL * ++ `model` + *`_k` = 1* ++ * + *`model` = > 取环境 名=MARQDO_LLM_MODEL * ++ `model` + *`_k` = 1* ++ * + *`model` = > get value=`d` key=model * + +*`q_key` = > quote text=`api_key` * +*`q_base` = > quote text=`base_url` * +*`q_model` = > quote text=`model` * +*`suffix` = > get value=`d` key=suffix * +*`bearer` = > get value=`d` key=bearer * +*`p1` = > get value=`d` key=p1 * +*`p2` = > get value=`d` key=p2 * +*`p3` = > get value=`d` key=p3 * +*`h1` = > get value=`d` key=h1 * +*`h2` = > get value=`d` key=h2 * +*`q_suf` = > quote text=`suffix` * +*`q_br` = > quote text=`bearer` * +*`q_p1` = > quote text=`p1` * +*`q_p2` = > quote text=`p2` * +*`q_p3` = > quote text=`p3` * +*`q_h1` = > quote text=`h1` * +*`q_h2` = > quote text=`h2` * +*`a1` = > get value=`d` key=a1 * +*`a2` = > get value=`d` key=a2 * +*`a3` = > get value=`d` key=a3 * +*`a4` = > get value=`d` key=a4 * +*`a5` = > get value=`d` key=a5 * +*`a6` = > get value=`d` key=a6 * +*`a7` = > get value=`d` key=a7 * +*`a8` = > get value=`d` key=a8 * +*`a9` = > get value=`d` key=a9 * +*`a10` = > get value=`d` key=a10 * +*`a11` = > get value=`d` key=a11 * +*`raw` = `a1` + `q_key` + `a2` + `q_base` + `a3` + `q_model` + `a4` + `q_suf` + `a5` + `q_br` + `a6` + `q_p1` + `a7` + `q_p2` + `a8` + `q_p3` + `a9` + `q_h1` + `a10` + `q_h2` + `a11` * +**> parse text=`raw`** + +## 运行 + - 提示 + +使用 `自` / `self` 句柄字段做聊天补全。 + +*`api_key` = > get value=`自` key=api_key * +*`base_url` = > get value=`自` key=base_url * +*`model` = > get value=`自` key=model * +*`suffix` = > get value=`自` key=suffix * +*`bearer` = > get value=`自` key=bearer * +*`p1` = > get value=`自` key=p1 * +*`p2` = > get value=`自` key=p2 * +*`p3` = > get value=`自` key=p3 * +*`h1` = > get value=`自` key=h1 * +*`h2` = > get value=`自` key=h2 * +*`url` = `base_url` + `suffix` * +*`auth` = `bearer` + `api_key` * +*`q_auth` = > quote text=`auth` * +*`hdr_raw` = `h1` + `q_auth` + `h2` * +*`headers` = > parse text=`hdr_raw` * +*`q_model` = > quote text=`model` * +*`q_prompt` = > quote text=`提示` * +*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` * +*`resp` = > http_post url=`url` body=`body` headers=`headers` * +*`status` = > get value=`resp` key=status * +*`raw` = > get value=`resp` key=body * + ++ `status` == 200 + *`data` = > parse text=`raw` * + *`choices` = > get value=`data` key=choices * + *`first` = > at value=`choices` index=0 * + *`message` = > get value=`first` key=message * + *`content` = > get value=`message` key=content * + **`content`** ++ * + > 打印 内容=ext/大模型: HTTP 错误 + > 打印 内容=`status` + > 打印 内容=`raw` + > 退出 码=1 + +--- + +## 聊天 + - 提示 + +`运行` 的别名。 + +**> `自`.运行 提示=`提示`** diff --git a/lib/foreign.mq.md b/lib/foreign.mq.md index 5b85bd3..dc80693 100644 --- a/lib/foreign.mq.md +++ b/lib/foreign.mq.md @@ -3,23 +3,23 @@ title: lib/foreign description: Run bound code fences and inline sources via local interpreters --- -# set_cmd +## set_cmd - lang - cmd **> host_foreign_set_cmd lang=`lang` cmd=`cmd`** -# run +## run - code **> host_foreign_run code=`code`** -# run_lang +## run_lang - lang - source **> host_foreign_run_lang lang=`lang` source=`source`** -# langs +## langs **> host_foreign_langs** diff --git a/lib/fs.mq.md b/lib/fs.mq.md index c7baaa3..883e6c0 100644 --- a/lib/fs.mq.md +++ b/lib/fs.mq.md @@ -3,39 +3,39 @@ title: lib/fs description: English filesystem wrappers --- -# read_text +## read_text - path **> host_read_text path=`path`** -# write_text +## write_text - path - text **> host_write_text path=`path` text=`text`** -# append_text +## append_text - path - text **> host_append_text path=`path` text=`text`** -# exists +## exists - path **> host_exists path=`path`** -# list_dir +## list_dir - path **> host_list_dir path=`path`** -# make_dir +## make_dir - path **> host_make_dir path=`path`** -# remove +## remove - path **> host_remove path=`path`** diff --git a/lib/json.mq.md b/lib/json.mq.md index 427a485..6a5841c 100644 --- a/lib/json.mq.md +++ b/lib/json.mq.md @@ -3,23 +3,28 @@ title: lib/json description: JSON parse/stringify (shared EN/ZH import) --- -# parse +## parse - text **> host_json_parse text=`text`** -# stringify +## stringify - value **> host_json_stringify value=`value`** -# get +## get - value - key **> host_json_get value=`value` key=`key`** -# keys +## keys - value **> host_json_keys value=`value`** + +## quote + - text + +**> host_json_quote text=`text`** diff --git a/lib/math.mq.md b/lib/math.mq.md index 0d99068..977073c 100644 --- a/lib/math.mq.md +++ b/lib/math.mq.md @@ -3,183 +3,183 @@ title: lib/math description: High-school math — num, random, formula, plot --- -# pi +## pi **> host_pi** -# e +## e **> host_e** -# num +## num - value **> host_num value=`value`** -# add +## add - a - b **> host_math_add a=`a` b=`b`** -# sub +## sub - a - b **> host_math_sub a=`a` b=`b`** -# mul +## mul - a - b **> host_math_mul a=`a` b=`b`** -# div +## div - a - b **> host_math_div a=`a` b=`b`** -# pow +## pow - a - b **> host_math_pow a=`a` b=`b`** -# neg +## neg - value **> host_math_neg value=`value`** -# sin +## sin - value **> host_math_sin value=`value`** -# cos +## cos - value **> host_math_cos value=`value`** -# tan +## tan - value **> host_math_tan value=`value`** -# asin +## asin - value **> host_math_asin value=`value`** -# acos +## acos - value **> host_math_acos value=`value`** -# atan +## atan - value **> host_math_atan value=`value`** -# sqrt +## sqrt - value **> host_math_sqrt value=`value`** -# abs +## abs - value **> host_math_abs value=`value`** -# ln +## ln - value **> host_math_ln value=`value`** -# exp +## exp - value **> host_math_exp value=`value`** -# floor +## floor - value **> host_math_floor value=`value`** -# ceil +## ceil - value **> host_math_ceil value=`value`** -# min +## min - a - b **> host_math_min a=`a` b=`b`** -# max +## max - a - b **> host_math_max a=`a` b=`b`** -# random +## random **> host_random** -# random_int +## random_int - min - max **> host_random_int min=`min` max=`max`** -# seed +## seed - value **> host_seed value=`value`** -# formula +## formula - text **> host_formula value=`text`** -# simplify +## simplify - formula **> host_simplify value=`formula`** -# expand +## expand - formula **> host_expand value=`formula`** -# diff +## diff - formula - var **> host_diff formula=`formula` var=`var`** -# subs +## subs - formula - var - value **> host_subs formula=`formula` var=`var` value=`value`** -# eval +## eval - formula - var - value **> host_eval formula=`formula` var=`var` value=`value`** -# solve +## solve - formula - var **> host_solve formula=`formula` var=`var`** -# plot +## plot - formula - var - min @@ -187,13 +187,13 @@ description: High-school math — num, random, formula, plot **> host_plot formula=`formula` var=`var` min=`min` max=`max`** -# plot_points +## plot_points - xs - ys **> host_plot_points xs=`xs` ys=`ys`** -# plot_conic +## plot_conic - kind - a diff --git a/lib/net.mq.md b/lib/net.mq.md index c4e93e6..d022f8b 100644 --- a/lib/net.mq.md +++ b/lib/net.mq.md @@ -1,20 +1,32 @@ --- title: lib/net -description: English minimal HTTP wrappers +description: English HTTP(S) wrappers. Optional headers / content_type via host aliases. --- -# http_get +## http_get - url +HTTPS supported. Optional named arg `headers=` (map). + **> host_http_get url=`url`** -# http_post +## http_post - url - body +Default content type is JSON. Optional `content_type=` / `headers=` (map). + **> host_http_post url=`url` body=`body`** -# url_encode +## http_request + - method + - url + +Optional `body=` / `content_type=` / `headers=`. + +**> host_http_request method=`method` url=`url`** + +## url_encode - text **> host_url_encode text=`text`** diff --git a/lib/sys.mq.md b/lib/sys.mq.md index 213e132..f0c6df4 100644 --- a/lib/sys.mq.md +++ b/lib/sys.mq.md @@ -1,33 +1,39 @@ --- title: lib/sys -description: English system wrappers +description: English system wrappers (load_dotenv accepts optional path via host alias) --- -# env_get +## env_get - name **> host_env_get name=`name`** -# env_set +## env_set - name - value **> host_env_set name=`name` value=`value`** -# args +## load_dotenv + +Load `.env` (optional named arg `path=`). Existing process env is not overridden. + +**> host_dotenv_load** + +## args **> host_args** -# cwd +## cwd **> host_cwd** -# exit +## exit - code **> host_exit code=`code`** -# exec +## exec - cmd **> host_exec cmd=`cmd`** diff --git a/lib/table.mq.md b/lib/table.mq.md index 1b6a8f1..dddadb6 100644 --- a/lib/table.mq.md +++ b/lib/table.mq.md @@ -3,12 +3,12 @@ title: lib/table description: English table helpers over len / at --- -# rows +## rows - xs **> len `xs`** -# row_at +## row_at - xs - i diff --git a/lib/text.mq.md b/lib/text.mq.md index 07e66ac..8f4386e 100644 --- a/lib/text.mq.md +++ b/lib/text.mq.md @@ -3,18 +3,18 @@ title: lib/text description: English text helpers over trim / split / join --- -# str_trim +## str_trim - s **> trim `s`** -# str_split +## str_split - s - sep **> split value=`s` sep=`sep`** -# str_join +## str_join - xs - sep diff --git a/lib/time.mq.md b/lib/time.mq.md index 8c89b98..200e6c7 100644 --- a/lib/time.mq.md +++ b/lib/time.mq.md @@ -3,26 +3,26 @@ title: lib/time description: English time wrappers --- -# now_unix +## now_unix **> host_now_unix** -# now_ms +## now_ms **> host_now_ms** -# format +## format - unix - pattern **> host_format_time unix=`unix` pattern=`pattern`** -# sleep_ms +## sleep_ms - ms **> host_sleep_ms ms=`ms`** -# parse +## parse - text - pattern diff --git a/lib/外联.mq.md b/lib/外联.mq.md index dbb10b0..8aec62a 100644 --- a/lib/外联.mq.md +++ b/lib/外联.mq.md @@ -3,23 +3,23 @@ title: lib/外联 description: 通过本机解释器运行绑定的代码围栏与内联源码 --- -# 设置命令 +## 设置命令 - 语言 - 命令 **> host_foreign_set_cmd lang=`语言` cmd=`命令`** -# 运行 +## 运行 - 代码 **> host_foreign_run code=`代码`** -# 按语言运行 +## 按语言运行 - 语言 - 源码 **> host_foreign_run_lang lang=`语言` source=`源码`** -# 语言表 +## 语言表 **> host_foreign_langs** diff --git a/lib/数学.mq.md b/lib/数学.mq.md index 1ab4574..1ff4e8e 100644 --- a/lib/数学.mq.md +++ b/lib/数学.mq.md @@ -3,183 +3,183 @@ title: lib/数学 description: 高中数学 — 数值、随机、公式、作图 --- -# 圆周率 +## 圆周率 **> host_pi** -# 自然底数 +## 自然底数 **> host_e** -# 数 +## 数 - value **> host_num value=`value`** -# 加 +## 加 - a - b **> host_math_add a=`a` b=`b`** -# 减 +## 减 - a - b **> host_math_sub a=`a` b=`b`** -# 乘 +## 乘 - a - b **> host_math_mul a=`a` b=`b`** -# 除 +## 除 - a - b **> host_math_div a=`a` b=`b`** -# 幂 +## 幂 - a - b **> host_math_pow a=`a` b=`b`** -# 负 +## 负 - value **> host_math_neg value=`value`** -# 正弦 +## 正弦 - value **> host_math_sin value=`value`** -# 余弦 +## 余弦 - value **> host_math_cos value=`value`** -# 正切 +## 正切 - value **> host_math_tan value=`value`** -# 反正弦 +## 反正弦 - value **> host_math_asin value=`value`** -# 反余弦 +## 反余弦 - value **> host_math_acos value=`value`** -# 反正切 +## 反正切 - value **> host_math_atan value=`value`** -# 平方根 +## 平方根 - value **> host_math_sqrt value=`value`** -# 绝对值 +## 绝对值 - value **> host_math_abs value=`value`** -# 对数 +## 对数 - value **> host_math_ln value=`value`** -# 指数 +## 指数 - value **> host_math_exp value=`value`** -# 向下取整 +## 向下取整 - value **> host_math_floor value=`value`** -# 向上取整 +## 向上取整 - value **> host_math_ceil value=`value`** -# 最小 +## 最小 - a - b **> host_math_min a=`a` b=`b`** -# 最大 +## 最大 - a - b **> host_math_max a=`a` b=`b`** -# 随机 +## 随机 **> host_random** -# 随机整数 +## 随机整数 - min - max **> host_random_int min=`min` max=`max`** -# 设种子 +## 设种子 - value **> host_seed value=`value`** -# 公式 +## 公式 - text **> host_formula value=`text`** -# 化简 +## 化简 - formula **> host_simplify value=`formula`** -# 展开 +## 展开 - formula **> host_expand value=`formula`** -# 求导 +## 求导 - formula - var **> host_diff formula=`formula` var=`var`** -# 代入 +## 代入 - formula - var - value **> host_subs formula=`formula` var=`var` value=`value`** -# 求值 +## 求值 - formula - var - value **> host_eval formula=`formula` var=`var` value=`value`** -# 求解 +## 求解 - formula - var **> host_solve formula=`formula` var=`var`** -# 绘图 +## 绘图 - formula - var - min @@ -187,13 +187,13 @@ description: 高中数学 — 数值、随机、公式、作图 **> host_plot formula=`formula` var=`var` min=`min` max=`max`** -# 绘点列 +## 绘点列 - xs - ys **> host_plot_points xs=`xs` ys=`ys`** -# 绘圆锥 +## 绘圆锥 - kind - a diff --git a/lib/文件.mq.md b/lib/文件.mq.md index d779edb..109f2a5 100644 --- a/lib/文件.mq.md +++ b/lib/文件.mq.md @@ -3,39 +3,39 @@ title: lib/文件 description: 中文文件系统包装 --- -# 读文本 +## 读文本 - 路径 **> host_read_text path=`路径`** -# 写文本 +## 写文本 - 路径 - 内容 **> host_write_text path=`路径` text=`内容`** -# 追加文本 +## 追加文本 - 路径 - 内容 **> host_append_text path=`路径` text=`内容`** -# 存在 +## 存在 - 路径 **> host_exists path=`路径`** -# 列目录 +## 列目录 - 路径 **> host_list_dir path=`路径`** -# 建目录 +## 建目录 - 路径 **> host_make_dir path=`路径`** -# 删除 +## 删除 - 路径 **> host_remove path=`路径`** diff --git a/lib/文本.mq.md b/lib/文本.mq.md index cd3f7cf..3a4c9fd 100644 --- a/lib/文本.mq.md +++ b/lib/文本.mq.md @@ -3,18 +3,18 @@ title: lib/文本 description: 中文文本库:去空白 / 拆分 / 拼接 --- -# 去空白 +## 去空白 - s **> trim `s`** -# 拆分 +## 拆分 - s - sep **> split value=`s` sep=`sep`** -# 拼接 +## 拼接 - xs - sep diff --git a/lib/时间.mq.md b/lib/时间.mq.md index 93d3071..7047d19 100644 --- a/lib/时间.mq.md +++ b/lib/时间.mq.md @@ -3,26 +3,26 @@ title: lib/时间 description: 中文时间包装 --- -# 此刻秒 +## 此刻秒 **> host_now_unix** -# 此刻毫秒 +## 此刻毫秒 **> host_now_ms** -# 格式化 +## 格式化 - 秒 - 格式 **> host_format_time unix=`秒` pattern=`格式`** -# 睡眠毫秒 +## 睡眠毫秒 - 毫秒 **> host_sleep_ms ms=`毫秒`** -# 解析时间 +## 解析时间 - 内容 - 格式 diff --git a/lib/系统.mq.md b/lib/系统.mq.md index cf695dd..d75a562 100644 --- a/lib/系统.mq.md +++ b/lib/系统.mq.md @@ -1,33 +1,39 @@ --- title: lib/系统 -description: 中文系统包装 +description: 中文系统包装(加载环境 可选 路径=) --- -# 取环境 +## 取环境 - 名 **> host_env_get name=`名`** -# 设环境 +## 设环境 - 名 - 值 **> host_env_set name=`名` value=`值`** -# 参数表 +## 加载环境 + +加载 `.env`(可选具名实参 `路径=`)。不覆盖已有进程环境变量。 + +**> host_dotenv_load** + +## 参数表 **> host_args** -# 工作目录 +## 工作目录 **> host_cwd** -# 退出 +## 退出 - 码 **> host_exit code=`码`** -# 执行 +## 执行 - 命令 **> host_exec cmd=`命令`** diff --git a/lib/网络.mq.md b/lib/网络.mq.md index af79c2b..4cd9952 100644 --- a/lib/网络.mq.md +++ b/lib/网络.mq.md @@ -1,20 +1,32 @@ --- title: lib/网络 -description: 中文简易网络包装 +description: 中文 HTTP(S) 包装。可选 headers / content_type 经宿主别名绑定。 --- -# 获取 +## 获取 - 地址 +支持 HTTPS。可选具名实参 `headers=`(映射)。 + **> host_http_get url=`地址`** -# 提交 +## 提交 - 地址 - 内容 +默认 Content-Type 为 JSON。可选 `content_type=` / `headers=`。 + **> host_http_post url=`地址` body=`内容`** -# 编码地址 +## 请求 + - 方法 + - 地址 + +可选 `body=` / `content_type=` / `headers=`。 + +**> host_http_request method=`方法` url=`地址`** + +## 编码地址 - 内容 **> host_url_encode text=`内容`** diff --git a/lib/表.mq.md b/lib/表.mq.md index 47bdc19..fa743be 100644 --- a/lib/表.mq.md +++ b/lib/表.mq.md @@ -3,12 +3,12 @@ title: lib/表 description: 中文表库:行数 / 取行 --- -# 行数 +## 行数 - xs **> len `xs`** -# 取行 +## 取行 - xs - i diff --git a/public/stdlib/net.mq.md b/public/stdlib/net.mq.md index e1a6e8f..d8a934e 100644 --- a/public/stdlib/net.mq.md +++ b/public/stdlib/net.mq.md @@ -1,12 +1,12 @@ --- -title: lib/net — HTTP -description: Minimal HTTP client and URL encode +title: lib/net — HTTP(S) +description: Minimal HTTP(S) client and URL encode > lib/net.mq.md --- # main -Import lib/net.mq.md. Functions: http_get(url) returns map status/body, http_post(url, body), url_encode(text). v1 speaks cleartext http only. +Import lib/net.mq.md. Functions: http_get(url), http_post(url, body), http_request(method, url), url_encode(text). HTTPS and optional headers= map are supported. Default post content-type is JSON. *`e` = > url_encode text=hello world * diff --git a/public/structure/import/utils.mq.md b/public/structure/import/utils.mq.md index 91d02a9..f9e5226 100644 --- a/public/structure/import/utils.mq.md +++ b/public/structure/import/utils.mq.md @@ -3,12 +3,12 @@ title: Utility module description: Imported by main; bold is a return value --- -# add_one +## add_one - n **`n` + 1** -# greet +## greet - who > print text=Hello, `who`! diff --git a/public/structure/import/工具.mq.md b/public/structure/import/工具.mq.md index 45ff898..deb6091 100644 --- a/public/structure/import/工具.mq.md +++ b/public/structure/import/工具.mq.md @@ -3,12 +3,12 @@ title: 工具模块 description: 被 main 导入;粗体为返回值 --- -# 加一 +## 加一 - n **`n` + 1** -# 问候 +## 问候 - 谁 > 打印 内容=你好,`谁`! diff --git a/skills/marqdo/SKILL.md b/skills/marqdo/SKILL.md index 4628549..6749974 100644 --- a/skills/marqdo/SKILL.md +++ b/skills/marqdo/SKILL.md @@ -28,12 +28,14 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords. | Marker | Meaning | |--------|---------| -| `#` … `######` | Function; heading depth = nesting/scope | -| `- name` under a function heading | Parameter | +| `#` | **Object / type** (constructor body). `# main` = entry object. | +| `##` … `######` | **Function / method** (nesting by heading depth). | +| `- name` under a heading | Parameter | | `- …` inside body | Loop | | `+ …` or `1.` | Branch arm (condition) | | Line that is only `*` | Else arm | | `> fn args` | Call (named `k=v` or positional) | +| `` > `obj`.method args `` | Method call (`obj` must be a map with `_type`) | | Frontmatter `> path.mq.md` | Import | | `*…*` | Statement (bind / expr) | | `**…**` | Return value | @@ -119,6 +121,7 @@ title: example - Import **English** or **Chinese** library file; call that file’s API names (do not mix). - `lib/…` resolves via `MARQDO_LIB`, cwd `lib/`, or `lib/` next to the `marqdo` binary. +- Official optional extensions: `ext/llm.mq.md` / `ext/大模型.mq.md` — `# llm` / `# 大模型` object handles + `## complete` / `## 运行`. Free `## load_env`. See `doc/design/ext-llm.md` and `doc/design/objects.md`. - Builtins (no import): `print`/`打印`, `input`/`输入`, `len`/`长度`, `str`/`文本`, `int`/`整数`; literals `True`/`真`, `False`/`假`, `None`/`空`; logic `and`/`且`, `or`/`或`, `not`/`非`. Stdlib map: [reference.md](reference.md). diff --git a/skills/marqdo/reference.md b/skills/marqdo/reference.md index 0b905ea..ccf2275 100644 --- a/skills/marqdo/reference.md +++ b/skills/marqdo/reference.md @@ -17,6 +17,8 @@ Diagnostics look like `path:line:col: message` (1-based line/col). Stdlib search order for `lib/…` imports: `MARQDO_LIB`, `./lib`, directory of the `marqdo` executable (and a few parents). Prefer the **bundle zip** (`marqdo.exe` + `lib/`) over a bare exe. +官方扩展(`ext/…`,非 stdlib):`MARQDO_EXT`、`./ext`、或可执行文件旁 `ext/`。对象原语见 `doc/design/objects.md`。LLM:`ext/llm.mq.md` / `ext/大模型.mq.md`(`# llm` 句柄 + `` `x`.complete `` / `` `x`.运行 ``;项目本地 `.env` 配密钥)。 + ## Builtins (no import) | EN | ZH | Role | @@ -38,12 +40,13 @@ Import one file; use **that** file’s function names. | `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… | | `lib/table.mq.md` | `lib/表.mq.md` | table/rows helpers | | `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem | -| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd | +| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv | | `lib/time.mq.md` | `lib/时间.mq.md` | time/format | -| `lib/net.mq.md` | `lib/网络.mq.md` | network helpers | -| `lib/json.mq.md` | (see design) | JSON | +| `lib/net.mq.md` | `lib/网络.mq.md` | HTTP(S) helpers (+ optional headers) | +| `lib/json.mq.md` | (see design) | JSON (+ `quote`) | | `lib/math.mq.md` | `lib/数学.mq.md` | num, trig, random, formula, plot | | `lib/foreign.mq.md` | `lib/外联.mq.md` | run foreign fenced blocks | +| `ext/llm.mq.md` | `ext/大模型.mq.md` | `# llm` / `# 大模型` object + chat methods | Open the imported `.mq.md` under `lib/` to see exact `#` function names and parameters. Gold tests: `tests/lib/`, `tests/structure/`. diff --git a/src/ast.rs b/src/ast.rs index a4c95aa..fab421b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -13,6 +13,7 @@ pub struct Module { #[derive(Debug, Clone)] pub struct Function { pub name: String, + /// Heading depth 1–6. Level 1 = object/type; level ≥ 2 = function/method. pub level: u8, pub span: Span, pub params: Vec, @@ -20,6 +21,13 @@ pub struct Function { pub children: Vec, } +impl Function { + /// `# Name` — object / type (constructor body). + pub fn is_object(&self) -> bool { + self.level == 1 + } +} + #[derive(Debug, Clone)] pub enum Stmt { Assign { @@ -64,6 +72,8 @@ pub struct BranchArm { #[derive(Debug, Clone)] pub struct CallExpr { pub callee: String, + /// When set, `` `recv`.callee `` method call; receiver is a variable name. + pub receiver: Option, pub args: Vec, } @@ -148,8 +158,9 @@ pub fn format_ast_dump(path: &str, module: &Module) -> String { fn dump_fun(out: &mut String, fun: &Function, depth: usize) { let pad = " ".repeat(depth); + let kind = if fun.is_object() { "object" } else { "fun" }; out.push_str(&format!( - "{pad}(fun level={} {:?} params={:?} @{}\n", + "{pad}({kind} level={} {:?} params={:?} @{}\n", fun.level, fun.name, fun.params, fun.span )); for stmt in &fun.body { @@ -179,8 +190,8 @@ fn dump_stmt(out: &mut String, stmt: &Stmt, depth: usize) { } Stmt::Call { call, span } => { out.push_str(&format!( - "{pad}(call {:?} {:?} @{span})\n", - call.callee, call.args + "{pad}(call recv={:?} {:?} {:?} @{span}\n", + call.receiver, call.callee, call.args )); } Stmt::Branch { arms, span } => { diff --git a/src/bytecode/compile.rs b/src/bytecode/compile.rs index 047cf89..1d6cc28 100644 --- a/src/bytecode/compile.rs +++ b/src/bytecode/compile.rs @@ -13,6 +13,7 @@ use crate::value::Value; struct FlatFun { name: String, + level: u8, parent: Option, params: Vec, body: Vec, @@ -33,10 +34,30 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result { .iter() .position(|f| f.name == "main" && f.parent.is_none()) .or_else(|| { - flat.iter() - .position(|f| f.parent.is_none() && f.params.is_empty()) + let tops: Vec = flat + .iter() + .enumerate() + .filter(|(_, f)| f.parent.is_none() && f.level == 1 && f.params.is_empty()) + .map(|(i, _)| i) + .collect(); + if tops.len() == 1 { + Some(tops[0]) + } else { + None + } }) - .ok_or_else(|| anyhow::anyhow!("bytecode: no entry function"))?; + .ok_or_else(|| anyhow::anyhow!("bytecode: no `# main` object"))?; + + let mut objects = Vec::new(); + let mut methods = Vec::new(); + for (i, f) in flat.iter().enumerate() { + if f.parent.is_none() && f.level == 1 { + objects.push((f.name.clone(), i)); + for &cid in &f.children { + methods.push((i, flat[cid].name.clone(), cid)); + } + } + } let path_buf = path.map(|p| p.to_path_buf()); let mut functions = Vec::new(); @@ -44,13 +65,19 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result { functions.push(compile_function(i, &flat, path_buf.as_deref())?); } - Ok(Program { functions, entry }) + Ok(Program { + functions, + entry, + objects, + methods, + }) } fn collect_fun(fun: &Function, parent: Option, flat: &mut Vec) -> usize { let id = flat.len(); flat.push(FlatFun { name: fun.name.clone(), + level: fun.level, parent, params: fun.params.clone(), body: fun.body.clone(), @@ -80,6 +107,18 @@ fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> Resu for (i, p) in fun.params.iter().enumerate() { locals.insert(p.clone(), i as u8); } + let is_method = fun + .parent + .map(|pid| flat[pid].level == 1 && flat[pid].parent.is_none()) + .unwrap_or(false); + if is_method { + for name in ["自", "self"] { + if !locals.contains_key(name) { + let s = locals.len() as u8; + locals.insert(name.to_string(), s); + } + } + } let mut c = FnCompiler { path, flat, @@ -324,6 +363,10 @@ impl<'a> FnCompiler<'a> { call.callee = crate::aliases::normalize_call_callee_and_args(&call.callee, &mut call.args); + if let Some(recv) = &call.receiver { + return self.compile_method_call(recv, &call, as_stmt); + } + match call.callee.as_str() { "print" => { let text_expr = call @@ -499,6 +542,72 @@ impl<'a> FnCompiler<'a> { ))); } self.emit(Op::Call(fid as u16, params.len() as u8)); + if self.flat[fid].parent.is_none() && self.flat[fid].level == 1 { + let ti = self.add_const(Value::Text(self.flat[fid].name.clone())); + self.emit(Op::TagInstance(ti)); + } + if as_stmt { + self.emit(Op::Pop); + } + Ok(()) + } + + fn compile_method_call(&mut self, recv: &str, call: &CallExpr, as_stmt: bool) -> Result<()> { + // Resolve method among all object children by name (unique enough for compile); + // runtime still checks `_type`. + let mut method_fid = None; + for f in self.flat { + if f.parent.is_none() && f.level == 1 { + for &cid in &f.children { + if self.flat[cid].name == call.callee { + method_fid = Some(cid); + break; + } + } + } + if method_fid.is_some() { + break; + } + } + let fid = method_fid.ok_or_else(|| { + self.err(format!("unknown method `{}`", call.callee)) + })?; + let params = self.flat[fid].params.clone(); + let mut named: HashMap = HashMap::new(); + let mut positionals = Vec::new(); + for a in &call.args { + match a { + Arg::Positional(e) => positionals.push(e), + Arg::Named { name, value } => { + named.insert(name.clone(), value); + } + } + } + let mut pos_i = 0; + for p in ¶ms { + if let Some(e) = named.get(p) { + self.compile_expr(e)?; + } else if pos_i < positionals.len() { + self.compile_expr(positionals[pos_i])?; + pos_i += 1; + } else { + return Err(self.err(format!("missing argument for parameter `{p}`"))); + } + } + if pos_i < positionals.len() { + return Err(self.err(format!( + "too many positional arguments ({} extra)", + positionals.len() - pos_i + ))); + } + let slot = self + .locals + .get(recv) + .copied() + .ok_or_else(|| self.err(format!("undefined variable `{recv}`")))?; + self.emit(Op::GetLocal(slot)); + let name_i = self.add_const(Value::Text(call.callee.clone())); + self.emit(Op::MethodCall(name_i, params.len() as u8)); if as_stmt { self.emit(Op::Pop); } diff --git a/src/bytecode/mod.rs b/src/bytecode/mod.rs index 0cfaf59..0a1ec80 100644 --- a/src/bytecode/mod.rs +++ b/src/bytecode/mod.rs @@ -54,6 +54,11 @@ pub enum Op { GetIndex, /// Call function by index; argc values are on the stack (param order). Call(u16, u8), + /// After a constructor Call: tag top-of-stack map with `_type` = constants[idx]. + TagInstance(u16), + /// Method call: stack has argc args (param order) then receiver on top. + /// constants[name_idx] is method name text; resolve via receiver `_type`. + MethodCall(u16, u8), /// Host primitive: pop `argc` args (param order), push result. HostCall(u16, u8), Return, @@ -75,6 +80,10 @@ pub struct FnChunk { pub struct Program { pub functions: Vec, pub entry: usize, + /// Top-level object name → function index (level-1). + pub objects: Vec<(String, usize)>, + /// (object_fn_idx, method_name) → method fn idx + pub methods: Vec<(usize, String, usize)>, } impl Program { diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs index 36e1daa..1abdd4c 100644 --- a/src/bytecode/vm.rs +++ b/src/bytecode/vm.rs @@ -336,6 +336,69 @@ impl Vm { slots, }); } + Op::TagInstance(idx) => { + let type_name = match fun.constants.get(idx as usize) { + Some(Value::Text(s)) => s.clone(), + _ => { + return Err(self.err_at(span, "TagInstance needs type name constant")); + } + }; + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + stack.push(tag_instance_value(&type_name, v)); + } + Op::MethodCall(name_idx, argc) => { + let argc = argc as usize; + let method_name = match fun.constants.get(name_idx as usize) { + Some(Value::Text(s)) => s.clone(), + _ => { + return Err(self.err_at(span, "MethodCall needs method name constant")); + } + }; + let recv = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let mut vals = Vec::with_capacity(argc); + for _ in 0..argc { + vals.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); + } + vals.reverse(); + let type_name = instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?; + let obj_idx = program + .objects + .iter() + .find(|(n, _)| n == &type_name) + .map(|(_, i)| *i) + .ok_or_else(|| { + self.err_at(span, format!("unknown object type `{type_name}`")) + })?; + let method_fid = program + .methods + .iter() + .find(|(o, m, _)| *o == obj_idx && m == &method_name) + .map(|(_, _, i)| *i) + .ok_or_else(|| { + self.err_at( + span, + format!("unknown method `{type_name}.{method_name}`"), + ) + })?; + let callee = &program.functions[method_fid]; + if callee.params.len() != argc { + return Err(self.err_at(span, "method argument count mismatch")); + } + let mut slots = vec![Value::None; callee.locals.len().max(1)]; + for (i, v) in vals.into_iter().enumerate() { + slots[i] = v; + } + for (i, name) in callee.locals.iter().enumerate() { + if name == "自" || name == "self" { + slots[i] = recv.clone(); + } + } + frames.push(Frame { + fn_idx: method_fid, + ip: 0, + slots, + }); + } Op::Return => { let ret = pop(&mut stack).unwrap_or(Value::None); frames.pop(); @@ -397,3 +460,33 @@ fn cmp(stack: &mut Vec, pred: impl Fn(std::cmp::Ordering) -> bool) -> Res stack.push(Value::Bool(pred(ord))); Ok(()) } + +fn instance_type_name_bc(v: &Value) -> Result { + match v { + Value::Map(entries) => entries + .iter() + .find(|(k, _)| k == "_type") + .and_then(|(_, v)| match v { + Value::Text(s) => Some(s.clone()), + _ => None, + }) + .ok_or_else(|| "method receiver needs a map with `_type`".into()), + _ => Err("method receiver must be an object map".into()), + } +} + +fn tag_instance_value(type_name: &str, value: Value) -> Value { + match value { + Value::Map(mut entries) => { + if !entries.iter().any(|(k, _)| k == "_type") { + entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); + } + Value::Map(entries) + } + Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]), + other => Value::Map(vec![ + ("_type".into(), Value::Text(type_name.into())), + ("value".into(), other), + ]), + } +} diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index 9f84179..e29dd03 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -74,6 +74,9 @@ pub enum HostFn { ForeignRun = 63, ForeignRunLang = 64, ForeignLangs = 65, + DotenvLoad = 66, + HttpRequest = 67, + JsonQuote = 68, } impl HostFn { @@ -144,6 +147,9 @@ impl HostFn { 63 => Self::ForeignRun, 64 => Self::ForeignRunLang, 65 => Self::ForeignLangs, + 66 => Self::DotenvLoad, + 67 => Self::HttpRequest, + 68 => Self::JsonQuote, _ => return None, }) } @@ -172,8 +178,9 @@ impl HostFn { "host_json_stringify" => Self::JsonStringify, "host_json_get" => Self::JsonGet, "host_json_keys" => Self::JsonKeys, - "host_http_get" => Self::HttpGet, - "host_http_post" => Self::HttpPost, + // Aliases so optional `headers` / `content_type` bind like plot. + "host_http_get" | "http_get" => Self::HttpGet, + "host_http_post" | "http_post" => Self::HttpPost, "host_url_encode" => Self::UrlEncode, "host_pi" => Self::Pi, "host_e" => Self::EConst, @@ -217,6 +224,9 @@ impl HostFn { "host_foreign_run" => Self::ForeignRun, "host_foreign_run_lang" => Self::ForeignRunLang, "host_foreign_langs" => Self::ForeignLangs, + "host_dotenv_load" | "load_dotenv" => Self::DotenvLoad, + "host_http_request" | "http_request" => Self::HttpRequest, + "host_json_quote" | "quote" => Self::JsonQuote, _ => return None, }) } @@ -292,6 +302,9 @@ impl HostFn { Self::ForeignRun => "host_foreign_run", Self::ForeignRunLang => "host_foreign_run_lang", Self::ForeignLangs => "host_foreign_langs", + Self::DotenvLoad => "host_dotenv_load", + Self::HttpRequest => "host_http_request", + Self::JsonQuote => "host_json_quote", } } @@ -309,11 +322,13 @@ impl HostFn { Self::EnvSet => &["name", "value"], Self::Exit => &["code"], Self::Exec => &["cmd"], - Self::JsonParse | Self::UrlEncode => &["text"], + Self::JsonParse | Self::UrlEncode | Self::JsonQuote => &["text"], Self::JsonStringify | Self::JsonKeys => &["value"], Self::JsonGet => &["value", "key"], Self::HttpGet => &["url"], Self::HttpPost => &["url", "body"], + Self::HttpRequest => &["method", "url"], + Self::DotenvLoad => &[], Self::Pi | Self::EConst | Self::Random => &[], Self::Num | Self::MathNeg | Self::MathSin | Self::MathCos | Self::MathTan | Self::MathAsin | Self::MathAcos | Self::MathAtan | Self::MathSqrt @@ -338,7 +353,10 @@ impl HostFn { pub fn optional_params(self) -> &'static [&'static str] { match self { Self::JsonStringify => &["indent"], - Self::HttpPost => &["content_type"], + Self::HttpGet => &["headers"], + Self::HttpPost => &["content_type", "headers"], + Self::HttpRequest => &["body", "content_type", "headers"], + Self::DotenvLoad => &["path"], Self::Exec => &["args"], Self::Solve => &["min", "max"], Self::Plot => &["steps", "path", "derivative", "grid"], @@ -385,6 +403,7 @@ pub fn call_host( } HostFn::EnvGet => sys::env_get(require(bound, "name")?), HostFn::EnvSet => sys::env_set(require(bound, "name")?, require(bound, "value")?), + HostFn::DotenvLoad => sys::dotenv_load(ctx, bound.get("path")), HostFn::Args => sys::args(ctx), HostFn::Cwd => sys::cwd(ctx), HostFn::Exit => sys::exit(ctx, require(bound, "code")?), @@ -395,12 +414,22 @@ pub fn call_host( } HostFn::JsonGet => json::get(require(bound, "value")?, require(bound, "key")?), HostFn::JsonKeys => json::keys(require(bound, "value")?), - HostFn::HttpGet => net::http_get(ctx, require(bound, "url")?), + HostFn::JsonQuote => json::quote(require(bound, "text")?), + HostFn::HttpGet => net::http_get(ctx, require(bound, "url")?, bound.get("headers")), HostFn::HttpPost => net::http_post( ctx, require(bound, "url")?, require(bound, "body")?, bound.get("content_type"), + bound.get("headers"), + ), + HostFn::HttpRequest => net::http_request( + ctx, + require(bound, "method")?, + require(bound, "url")?, + bound.get("body"), + bound.get("content_type"), + bound.get("headers"), ), HostFn::UrlEncode => net::url_encode(require(bound, "text")?), HostFn::Pi => math::pi(), diff --git a/src/host/json.rs b/src/host/json.rs index 410a2d0..f5bb302 100644 --- a/src/host/json.rs +++ b/src/host/json.rs @@ -53,6 +53,17 @@ pub fn keys(value: &Value) -> Result { } } +/// JSON-encode a text value including surrounding quotes (for building request bodies). +pub fn quote(text: &Value) -> Result { + let s = match text { + Value::Text(s) => s.as_str(), + _ => return Err("json quote needs text".into()), + }; + serde_json::to_string(s) + .map(Value::Text) + .map_err(|e| format!("json quote: {e}")) +} + fn json_to_value(v: &serde_json::Value) -> Result { Ok(match v { serde_json::Value::Null => Value::None, @@ -111,3 +122,14 @@ fn value_to_json(v: &Value) -> Result { Value::Code(c) => serde_json::Value::String(c.source.clone()), }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn quote_escapes_text() { + let q = quote(&Value::Text("a\"b".into())).unwrap(); + assert_eq!(q, Value::Text("\"a\\\"b\"".into())); + } +} diff --git a/src/host/net.rs b/src/host/net.rs index a6852df..5a6a6df 100644 --- a/src/host/net.rs +++ b/src/host/net.rs @@ -1,7 +1,5 @@ -//! Minimal HTTP host primitives (HTTP cleartext in v1; HTTPS deferred). +//! HTTP host primitives (HTTP and HTTPS via ureq). -use std::io::{Read, Write}; -use std::net::TcpStream; use std::time::Duration; use crate::host::HostContext; @@ -14,104 +12,96 @@ fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> { } } -struct UrlParts { - host: String, - port: u16, - path: String, -} - -fn parse_http_url(url: &str) -> Result { - let rest = url - .strip_prefix("http://") - .ok_or_else(|| { - if url.starts_with("https://") { - "https is not supported yet (use http:// or wait for TLS)".to_string() - } else { - "url must start with http://".into() - } - })?; - let (hostport, path) = match rest.split_once('/') { - Some((h, p)) => (h, format!("/{p}")), - None => (rest, "/".into()), +fn headers_from_value(headers: Option<&Value>) -> Result, String> { + let Some(h) = headers else { + return Ok(Vec::new()); }; - let (host, port) = if let Some((h, p)) = hostport.split_once(':') { - let port: u16 = p - .parse() - .map_err(|_| format!("invalid port in url: {p}"))?; - (h.to_string(), port) - } else { - (hostport.to_string(), 80) - }; - if host.is_empty() { - return Err("url missing host".into()); + match h { + Value::None => Ok(Vec::new()), + Value::Map(entries) => Ok(entries + .iter() + .map(|(k, v)| (k.clone(), v.as_display())) + .collect()), + _ => Err("headers must be a map (e.g. from json parse)".into()), } - Ok(UrlParts { host, port, path }) } fn http_exchange( method: &str, url: &str, body: Option<&str>, - content_type: &str, + content_type: Option<&str>, + extra_headers: &[(String, String)], ) -> Result<(u16, String), String> { - let parts = parse_http_url(url)?; - let addr = format!("{}:{}", parts.host, parts.port); - let mut stream = TcpStream::connect(&addr).map_err(|e| format!("http connect {addr}: {e}"))?; - stream - .set_read_timeout(Some(Duration::from_secs(10))) - .ok(); - stream - .set_write_timeout(Some(Duration::from_secs(10))) - .ok(); + if !(url.starts_with("http://") || url.starts_with("https://")) { + return Err("url must start with http:// or https://".into()); + } + let agent = ureq::AgentBuilder::new() + .timeout_connect(Duration::from_secs(15)) + .timeout_read(Duration::from_secs(120)) + .build(); - let body = body.unwrap_or(""); - let mut req = format!( - "{method} {path} HTTP/1.0\r\nHost: {host}\r\nConnection: close\r\nUser-Agent: marqdo\r\n", - path = parts.path, - host = parts.host, - ); - if method == "POST" { - req.push_str(&format!( - "Content-Type: {content_type}\r\nContent-Length: {}\r\n", - body.len() - )); + let mut req = match method { + "GET" => agent.get(url), + "POST" => agent.post(url), + "PUT" => agent.put(url), + "DELETE" => agent.request("DELETE", url), + other => return Err(format!("unsupported http method: {other}")), + }; + req = req.set("User-Agent", "marqdo"); + if let Some(ct) = content_type { + if !ct.is_empty() { + req = req.set("Content-Type", ct); + } } - req.push_str("\r\n"); - if method == "POST" { - req.push_str(body); + for (k, v) in extra_headers { + req = req.set(k, v); } - stream - .write_all(req.as_bytes()) - .map_err(|e| format!("http write: {e}"))?; - let mut buf = Vec::new(); - stream - .read_to_end(&mut buf) - .map_err(|e| format!("http read: {e}"))?; - if buf.len() > 2 * 1024 * 1024 { - return Err("http response exceeds 2MiB limit".into()); + let resp = if let Some(b) = body { + req.send_string(b) + } else { + req.call() + }; + + match resp { + Ok(r) => { + let status = r.status(); + let text = r + .into_string() + .map_err(|e| format!("http read body: {e}"))?; + if text.len() > 8 * 1024 * 1024 { + return Err("http response exceeds 8MiB limit".into()); + } + Ok((status, text)) + } + Err(ureq::Error::Status(code, r)) => { + let text = r.into_string().unwrap_or_default(); + Ok((code, text)) + } + Err(e) => Err(format!("http {method} {url}: {e}")), } - let text = String::from_utf8_lossy(&buf); - let (header, body) = text.split_once("\r\n\r\n").unwrap_or((text.as_ref(), "")); - let status = header - .lines() - .next() - .and_then(|line| line.split_whitespace().nth(1)) - .and_then(|s| s.parse::().ok()) - .unwrap_or(0); - Ok((status, body.to_string())) } -pub fn http_get(ctx: &HostContext, url: &Value) -> Result { +fn response_map(status: u16, body: String) -> Value { + Value::Map(vec![ + ("status".into(), Value::Int(status as i64)), + ("body".into(), Value::Text(body)), + ]) +} + +pub fn http_get( + ctx: &HostContext, + url: &Value, + headers: Option<&Value>, +) -> Result { if !ctx.allow_net() { return Err("http_get denied by host policy".into()); } let url = as_text(url, "url")?; - let (status, body) = http_exchange("GET", url, None, "")?; - Ok(Value::Map(vec![ - ("status".into(), Value::Int(status as i64)), - ("body".into(), Value::Text(body)), - ])) + let hdrs = headers_from_value(headers)?; + let (status, body) = http_exchange("GET", url, None, None, &hdrs)?; + Ok(response_map(status, body)) } pub fn http_post( @@ -119,6 +109,7 @@ pub fn http_post( url: &Value, body: &Value, content_type: Option<&Value>, + headers: Option<&Value>, ) -> Result { if !ctx.allow_net() { return Err("http_post denied by host policy".into()); @@ -127,13 +118,44 @@ pub fn http_post( let body = as_text(body, "body")?; let ct = match content_type { Some(v) => as_text(v, "content_type")?, - None => "text/plain; charset=utf-8", + None => "application/json; charset=utf-8", }; - let (status, resp) = http_exchange("POST", url, Some(body), ct)?; - Ok(Value::Map(vec![ - ("status".into(), Value::Int(status as i64)), - ("body".into(), Value::Text(resp)), - ])) + let hdrs = headers_from_value(headers)?; + let (status, resp) = http_exchange("POST", url, Some(body), Some(ct), &hdrs)?; + Ok(response_map(status, resp)) +} + +/// Generic request: method + url + optional body / content_type / headers. +pub fn http_request( + ctx: &HostContext, + method: &Value, + url: &Value, + body: Option<&Value>, + content_type: Option<&Value>, + headers: Option<&Value>, +) -> Result { + if !ctx.allow_net() { + return Err("http_request denied by host policy".into()); + } + let method = as_text(method, "method")?.to_uppercase(); + let url = as_text(url, "url")?; + let body_s = match body { + None | Some(Value::None) => None, + Some(v) => Some(as_text(v, "body")?), + }; + let ct = match content_type { + None | Some(Value::None) => { + if body_s.is_some() && method == "POST" { + Some("application/json; charset=utf-8") + } else { + None + } + } + Some(v) => Some(as_text(v, "content_type")?), + }; + let hdrs = headers_from_value(headers)?; + let (status, resp) = http_exchange(&method, url, body_s, ct, &hdrs)?; + Ok(response_map(status, resp)) } pub fn url_encode(text: &Value) -> Result { @@ -150,3 +172,21 @@ pub fn url_encode(text: &Value) -> Result { .collect(), )) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn headers_from_map() { + let m = Value::Map(vec![ + ("Authorization".into(), Value::Text("Bearer x".into())), + ("X-Test".into(), Value::Int(1)), + ]); + let h = headers_from_value(Some(&m)).unwrap(); + assert_eq!(h.len(), 2); + assert!(h + .iter() + .any(|(k, v)| k == "Authorization" && v == "Bearer x")); + } +} diff --git a/src/host/sys.rs b/src/host/sys.rs index c4d9e0c..b9b26b6 100644 --- a/src/host/sys.rs +++ b/src/host/sys.rs @@ -34,6 +34,54 @@ pub fn env_set(name: &Value, value: &Value) -> Result { Ok(Value::None) } +/// Load KEY=VALUE pairs from a `.env` file into the process environment. +/// Existing variables are **not** overridden. Default path: `.env` under host cwd. +pub fn dotenv_load(ctx: &HostContext, path: Option<&Value>) -> Result { + let rel = match path { + None | Some(Value::None) => ".env".to_string(), + Some(v) => as_text(v, "path")?.to_string(), + }; + let file = if std::path::Path::new(&rel).is_absolute() { + std::path::PathBuf::from(&rel) + } else { + ctx.cwd.join(&rel) + }; + let text = match std::fs::read_to_string(&file) { + Ok(t) => t, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Value::Int(0)), + Err(e) => { + return Err(format!("dotenv_load {}: {e}", file.display())); + } + }; + let mut loaded = 0i64; + for raw in text.lines() { + let line = raw.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + let line = line.strip_prefix("export ").unwrap_or(line).trim(); + let Some((k, v)) = line.split_once('=') else { + continue; + }; + let key = k.trim(); + if key.is_empty() { + continue; + } + if std::env::var_os(key).is_some() { + continue; + } + let mut val = v.trim(); + if (val.starts_with('"') && val.ends_with('"')) + || (val.starts_with('\'') && val.ends_with('\'')) + { + val = &val[1..val.len() - 1]; + } + std::env::set_var(key, val); + loaded += 1; + } + Ok(Value::Int(loaded)) +} + pub fn args(ctx: &HostContext) -> Result { Ok(Value::List( ctx.argv.iter().map(|s| Value::Text(s.clone())).collect(), @@ -77,3 +125,37 @@ pub fn exec(ctx: &HostContext, cmd: &Value, args: Option<&Value>) -> Result = module.functions.iter().filter(|f| f.level == 1).collect(); - if tops.is_empty() { - bail!("no `# main` and no level-1 function to run"); + // Document-as-entry: sole top-level `#` object with no params (e.g. `# Hello World`). + let entries: Vec<&Function> = module + .functions + .iter() + .filter(|f| f.is_object() && f.params.is_empty()) + .collect(); + if entries.len() == 1 { + return self.run_function(module, entries[0], Env::new(), &[]); } - let mut last = Value::None; - for fun in tops { - if fun.params.is_empty() { - last = self.run_function(module, fun, Env::new(), &[])?; - } - } - Ok(last) + bail!("no `# main` object to run"); } fn run_function( @@ -443,6 +442,10 @@ impl Interpreter { } } + if let Some(recv_name) = &call.receiver { + return self.eval_method_call(module, env, recv_name, &call.callee, &ev_args); + } + match call.callee.as_str() { "print" => { let bound = bind_args(&["text".into()], &ev_args, false) @@ -574,10 +577,76 @@ impl Interpreter { call_env.set(k, v); } + let result = self.run_function(module, target, call_env, &[])?; + if target.is_object() { + Ok(tag_instance(&target.name, result)) + } else { + Ok(result) + } + } + + fn eval_method_call( + &mut self, + module: &Module, + env: &mut Env, + recv_name: &str, + method: &str, + ev_args: &[EvArg], + ) -> Result { + let recv = env + .get(recv_name) + .cloned() + .ok_or_else(|| self.err(format!("undefined variable `{recv_name}`")))?; + let type_name = instance_type_name(&recv).map_err(|m| self.err(m))?; + let obj = find_top(module, &type_name) + .filter(|f| f.is_object()) + .ok_or_else(|| self.err(format!("unknown object type `{type_name}`")))?; + let target = obj + .children + .iter() + .find(|c| c.name == method) + .ok_or_else(|| self.err(format!("unknown method `{type_name}.{method}`")))?; + let bound = bind_args(&target.params, ev_args, false).map_err(|m| self.err(m))?; + let mut call_env = Env::new(); + for (k, v) in bound { + call_env.set(k, v); + } + call_env.set("自".into(), recv.clone()); + call_env.set("self".into(), recv); self.run_function(module, target, call_env, &[]) } } +fn instance_type_name(v: &Value) -> std::result::Result { + match v { + Value::Map(entries) => entries + .iter() + .find(|(k, _)| k == "_type") + .and_then(|(_, v)| match v { + Value::Text(s) => Some(s.clone()), + _ => None, + }) + .ok_or_else(|| "method receiver needs a map with `_type`".into()), + _ => Err("method receiver must be an object map".into()), + } +} + +fn tag_instance(type_name: &str, value: Value) -> Value { + match value { + Value::Map(mut entries) => { + if !entries.iter().any(|(k, _)| k == "_type") { + entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); + } + Value::Map(entries) + } + Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]), + other => Value::Map(vec![ + ("_type".into(), Value::Text(type_name.into())), + ("value".into(), other), + ]), + } +} + /// Bind evaluated args onto parameter names. Err is a plain message (caller adds span/path). fn bind_args( params: &[String], diff --git a/src/load.rs b/src/load.rs index d8411f8..397d1da 100644 --- a/src/load.rs +++ b/src/load.rs @@ -41,8 +41,8 @@ fn load_module_inner(path: &Path, visited: &mut HashSet) -> Result Result { let rel = rel.replace('\\', "/"); let direct = from_dir.join(&rel); @@ -72,6 +72,22 @@ pub fn resolve_import(from_dir: &Path, rel: &str) -> Result { ); } + if let Some(remainder) = normalized.strip_prefix("ext/") { + for root in ext_search_roots() { + let as_ext_dir = root.join(remainder); + if as_ext_dir.is_file() { + return Ok(as_ext_dir); + } + let as_repo_root = root.join("ext").join(remainder); + if as_repo_root.is_file() { + return Ok(as_repo_root); + } + } + bail!( + "cannot resolve extension import `{rel}` (set MARQDO_EXT or keep an ext/ next to the project)" + ); + } + Ok(direct) } @@ -95,6 +111,26 @@ fn lib_search_roots() -> Vec { roots } +fn ext_search_roots() -> Vec { + let mut roots = Vec::new(); + if let Ok(h) = env::var("MARQDO_EXT") { + roots.push(PathBuf::from(h)); + } + if let Ok(cwd) = env::current_dir() { + roots.push(cwd.join("ext")); + roots.push(cwd); + } + if let Ok(exe) = env::current_exe() { + if let Some(dir) = exe.parent() { + roots.push(dir.join("ext")); + roots.push(dir.join("../ext")); + roots.push(dir.join("../../ext")); + roots.push(dir.join("../../../ext")); + } + } + roots +} + fn attach_path(path: &Path, err: anyhow::Error) -> anyhow::Error { if let Some(d) = err.downcast_ref::() { if d.path.is_none() { diff --git a/src/parse/expr.rs b/src/parse/expr.rs index a1744d2..83cbb27 100644 --- a/src/parse/expr.rs +++ b/src/parse/expr.rs @@ -366,13 +366,14 @@ fn is_word_end(s: &str, len: usize) -> bool { } /// Parse `name key=val …` / positional args; returns call + bytes consumed. +/// Callee may be a bare name or method form `` `recv`.method ``. pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> { use crate::ast::Arg; let s = s.trim_start(); let original_len = s.len(); - let callee = { + let callee_tok = { let mut parts = s.split_whitespace(); parts .next() @@ -380,9 +381,13 @@ pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> { .to_string() }; + let (receiver, callee) = split_method_callee(&callee_tok) + .map(|(r, m)| (Some(r), m)) + .unwrap_or((None, callee_tok.clone())); + let after_callee_offset = { - let idx = s.find(&callee).unwrap(); - idx + callee.len() + let idx = s.find(&callee_tok).unwrap(); + idx + callee_tok.len() }; let mut args_str = s[after_callee_offset..].trim_start(); let mut args = Vec::new(); @@ -411,7 +416,33 @@ pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> { } } - Ok((CallExpr { callee, args }, original_len)) + Ok(( + CallExpr { + callee, + receiver, + args, + }, + original_len, + )) +} + +/// `` `recv`.method `` → (recv, method). +fn split_method_callee(tok: &str) -> Option<(String, String)> { + if !tok.starts_with('`') { + return None; + } + let rest = &tok[1..]; + let end = rest.find('`')?; + let recv = rest[..end].to_string(); + if recv.is_empty() { + return None; + } + let after = &rest[end + 1..]; + let method = after.strip_prefix('.')?; + if method.is_empty() || method.contains('`') || method.contains('.') { + return None; + } + Some((recv, method.to_string())) } /// If `s` begins with `ident=`, return (ident, rest_after_eq). diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 292125c..0dc7e67 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -31,7 +31,7 @@ pub fn parse_classified(lines: &[ClassifiedLine]) -> Result { functions.push(cur.parse_function(1)?); } else { bail!( - "{}:1: expected function heading `# …`, got: {trimmed}", + "{}:1: expected heading (`#` object or `##` function), got: {trimmed}", line.line_no ); } diff --git a/src/view/render.rs b/src/view/render.rs index d514afd..4ab9aca 100644 --- a/src/view/render.rs +++ b/src/view/render.rs @@ -573,7 +573,14 @@ fn stmt_shell(line: u32, inner: &str, mode: StructureMode) -> String { } fn call_display(call: &CallExpr) -> String { - let mut s = format!("> {}", call.callee); + let mut s = String::from("> "); + if let Some(recv) = &call.receiver { + s.push('`'); + s.push_str(recv); + s.push('`'); + s.push('.'); + } + s.push_str(&call.callee); for a in &call.args { match a { Arg::Positional(e) => { diff --git a/tests/ext/llm-complete.mq.md b/tests/ext/llm-complete.mq.md new file mode 100644 index 0000000..83a8a87 --- /dev/null +++ b/tests/ext/llm-complete.mq.md @@ -0,0 +1,15 @@ +--- +title: ext llm DeepSeek complete (live) +description: Live OpenAI-compatible chat via DeepSeek. Needs tests/ext/.env (gitignored). Not in gold suite. +> ext/llm.mq.md +--- + +# main + +> load_env + +*`model` = > llm * + +*`reply` = > `model`.complete prompt=Reply with exactly one English word: pong * + +> print text=`reply` diff --git a/tests/ext/llm-import.mq.md b/tests/ext/llm-import.mq.md new file mode 100644 index 0000000..5223e24 --- /dev/null +++ b/tests/ext/llm-import.mq.md @@ -0,0 +1,10 @@ +--- +title: ext llm import +> ext/llm.mq.md +--- + +# main + +> load_env + +> print text=ext-ok diff --git a/tests/gold.rs b/tests/gold.rs index e9a77de..e2c33ea 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -62,6 +62,11 @@ fn structure_hello() { assert_out("tests/structure/hello.mq.md", "Hello World!"); } +#[test] +fn structure_object_handle() { + assert_out("tests/structure/object-handle.mq.md", "counter\n3\n4"); +} + #[test] fn structure_nested_call() { assert_out("tests/structure/nested-call.mq.md", "Hello World!"); @@ -580,6 +585,21 @@ fn lib_sys_cwd() { assert_out("tests/lib/sys-cwd.mq.md", "cwd-ok"); } +#[test] +fn lib_sys_dotenv() { + assert_out("tests/lib/sys-dotenv.mq.md", "loaded"); +} + +#[test] +fn lib_json_quote() { + assert_out("tests/lib/json-quote.mq.md", "\"hi\""); +} + +#[test] +fn ext_llm_import() { + assert_out("tests/ext/llm-import.mq.md", "ext-ok"); +} + #[test] fn lib_net_encode() { assert_out("tests/lib/net-encode.mq.md", "a+b"); diff --git a/tests/lib/json-quote.mq.md b/tests/lib/json-quote.mq.md new file mode 100644 index 0000000..5e04afa --- /dev/null +++ b/tests/lib/json-quote.mq.md @@ -0,0 +1,10 @@ +--- +title: json quote +> lib/json.mq.md +--- + +# main + +*`q` = > quote text=hi * + +> print text=`q` diff --git a/tests/lib/sys-dotenv.mq.md b/tests/lib/sys-dotenv.mq.md new file mode 100644 index 0000000..c3bf72a --- /dev/null +++ b/tests/lib/sys-dotenv.mq.md @@ -0,0 +1,15 @@ +--- +title: sys dotenv +> lib/sys.mq.md +> lib/fs.mq.md +--- + +# main + +> write_text path=dotenv-fixture.env text=MARQDO_DOTENV_FIXTURE=loaded + +> load_dotenv path=dotenv-fixture.env + +*`v` = > env_get name=MARQDO_DOTENV_FIXTURE * + +> print text=`v` diff --git a/tests/structure/import/utils.mq.md b/tests/structure/import/utils.mq.md index 8d7a2fc..b2e4eed 100644 --- a/tests/structure/import/utils.mq.md +++ b/tests/structure/import/utils.mq.md @@ -3,12 +3,12 @@ title: 工具函数 description: 供 frontmatter > 导入;返回用粗体;副作用用输出函数 --- -# 加一 +## 加一 - n **`n` + 1** -# 问候 +## 问候 - 谁 > print text=你好,`谁`! diff --git a/tests/structure/object-handle.mq.md b/tests/structure/object-handle.mq.md new file mode 100644 index 0000000..e4980f0 --- /dev/null +++ b/tests/structure/object-handle.mq.md @@ -0,0 +1,37 @@ +--- +title: object handle smoke +> lib/json.mq.md +--- + +# counter + - n + +*`t` = > parse text={"p":"{\"n\":","s":"}"} * +*`qn` = > str `n` * +*`p` = > get value=`t` key=p * +*`s` = > get value=`t` key=s * +*`raw` = `p` + `qn` + `s` * +**> parse text=`raw`** + +## bump + +*`n` = > get value=`self` key=n * +*`n2` = `n` + 1* +*`t` = > parse text={"p":"{\"n\":","s":"}"} * +*`qn` = > str `n2` * +*`p` = > get value=`t` key=p * +*`s` = > get value=`t` key=s * +*`raw` = `p` + `qn` + `s` * +*`m` = > parse text=`raw` * +**`m`** + +# main + +*`c` = > counter n=3 * +*`ty` = > get value=`c` key=_type * +> print text=`ty` +*`n` = > get value=`c` key=n * +> print text=`n` +*`c2` = > `c`.bump * +*`n2` = > get value=`c2` key=n * +> print text=`n2`