diff --git a/Cargo.lock b/Cargo.lock index 668cafe..75fb0b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,15 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.18" @@ -77,12 +86,28 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + [[package]] name = "arrayvec" version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "async-event" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1222afd3d2bce3995035054046a279ae7aa154d70d0766cea050073f3fd7ddf" +dependencies = [ + "loom", + "pin-project-lite", +] + [[package]] name = "async-fn-stream" version = "0.1.0" @@ -259,6 +284,30 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chacha20" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "rand_core 0.10.0", +] + +[[package]] +name = "chrono" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "clap" version = "4.5.54" @@ -326,6 +375,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.16" @@ -335,6 +390,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -413,6 +477,12 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "645cbb3a84e60b7531617d5ae4e57f7e27308f6445f5abf653209ea76dec8dff" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foldhash" version = "0.2.0" @@ -525,6 +595,19 @@ dependencies = [ "slab", ] +[[package]] +name = "generator" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e" +dependencies = [ + "cc", + "libc", + "log", + "rustversion", + "windows", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -554,10 +637,24 @@ checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "r-efi", + "r-efi 5.3.0", "wasip2", ] +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "rand_core 0.10.0", + "wasip2", + "wasip3", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -567,6 +664,15 @@ dependencies = [ "ahash", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + [[package]] name = "hashbrown" version = "0.16.1" @@ -575,7 +681,7 @@ checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" dependencies = [ "allocator-api2", "equivalent", - "foldhash", + "foldhash 0.2.0", ] [[package]] @@ -584,6 +690,36 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + [[package]] name = "include_dir" version = "0.7.4" @@ -611,6 +747,8 @@ checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" dependencies = [ "equivalent", "hashbrown 0.16.1", + "serde", + "serde_core", ] [[package]] @@ -667,6 +805,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" version = "0.2.175" @@ -699,6 +843,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + [[package]] name = "logwise" version = "0.2.3" @@ -737,6 +887,28 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c980f57d231d31d90700b8a9440f42f237f09d8ad02e82636140e3d760b2768" +[[package]] +name = "loom" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + [[package]] name = "memchr" version = "2.7.4" @@ -862,12 +1034,22 @@ dependencies = [ name = "orchid-api-traits" version = "0.1.0" dependencies = [ + "chrono", "futures", "itertools", "never", "ordered-float", ] +[[package]] +name = "orchid-async-utils" +version = "0.1.0" +dependencies = [ + "futures", + "itertools", + "task-local", +] + [[package]] name = "orchid-base" version = "0.1.0" @@ -886,7 +1068,10 @@ dependencies = [ "orchid-api", "orchid-api-derive", "orchid-api-traits", + "orchid-async-utils", "ordered-float", + "rand 0.10.0", + "rand_chacha 0.10.0", "regex", "rust-embed", "substack", @@ -899,9 +1084,9 @@ dependencies = [ name = "orchid-extension" version = "0.1.0" dependencies = [ + "async-event", "async-fn-stream", "async-once-cell", - "bound", "derive_destructure", "dyn-clone", "futures", @@ -917,6 +1102,7 @@ dependencies = [ "orchid-api", "orchid-api-derive", "orchid-api-traits", + "orchid-async-utils", "orchid-base", "ordered-float", "pastey", @@ -932,6 +1118,7 @@ dependencies = [ name = "orchid-host" version = "0.1.0" dependencies = [ + "async-event", "async-fn-stream", "async-once-cell", "bound", @@ -962,8 +1149,10 @@ dependencies = [ name = "orchid-std" version = "0.1.0" dependencies = [ + "async-event", "async-fn-stream", "async-once-cell", + "chrono", "futures", "hashbrown 0.16.1", "itertools", @@ -1030,7 +1219,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1060,6 +1249,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2 1.0.104", + "syn 2.0.112", +] + [[package]] name = "priority" version = "0.1.1" @@ -1081,7 +1280,7 @@ version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" dependencies = [ - "unicode-xid", + "unicode-xid 0.1.0", ] [[package]] @@ -1137,6 +1336,12 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + [[package]] name = "radium" version = "0.7.0" @@ -1164,6 +1369,17 @@ dependencies = [ "rand_core 0.9.3", ] +[[package]] +name = "rand" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc266eb313df6c5c09c1c7b1fbe2510961e5bcd3add930c1e31f7ed9da0feff8" +dependencies = [ + "chacha20", + "getrandom 0.4.2", + "rand_core 0.10.0", +] + [[package]] name = "rand_chacha" version = "0.3.1" @@ -1184,6 +1400,16 @@ dependencies = [ "rand_core 0.9.3", ] +[[package]] +name = "rand_chacha" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e6af7f3e25ded52c41df4e0b1af2d047e45896c2f3281792ed68a1c243daedb" +dependencies = [ + "ppv-lite86", + "rand_core 0.10.0", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -1202,6 +1428,12 @@ dependencies = [ "getrandom 0.3.4", ] +[[package]] +name = "rand_core" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" + [[package]] name = "redox_syscall" version = "0.5.8" @@ -1349,6 +1581,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scopeguard" version = "1.2.0" @@ -1361,6 +1599,12 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + [[package]] name = "serde" version = "1.0.228" @@ -1410,10 +1654,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.2.16", "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1507,7 +1760,7 @@ checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.13", - "unicode-xid", + "unicode-xid 0.1.0", ] [[package]] @@ -1593,6 +1846,15 @@ dependencies = [ "syn 2.0.112", ] +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + [[package]] name = "tinyvec" version = "1.8.1" @@ -1680,6 +1942,67 @@ dependencies = [ "winnow", ] +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2 1.0.104", + "quote 1.0.42", + "syn 2.0.112", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "trait-set" version = "0.3.0" @@ -1721,6 +2044,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "unsync-pipe" version = "0.2.0" @@ -1745,6 +2074,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "version_check" version = "0.9.5" @@ -1773,7 +2108,16 @@ version = "1.0.1+wasi-0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.46.0", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", ] [[package]] @@ -1866,6 +2210,28 @@ dependencies = [ "syn 2.0.112", ] +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + [[package]] name = "wasm_safe_mutex" version = "0.1.2" @@ -1890,6 +2256,18 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + [[package]] name = "web-sys" version = "0.3.83" @@ -1919,19 +2297,81 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2 1.0.104", + "quote 1.0.42", + "syn 2.0.112", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2 1.0.104", + "quote 1.0.42", + "syn 2.0.112", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +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", + "windows-targets 0.52.6", ] [[package]] @@ -1940,7 +2380,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1952,34 +2392,67 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + [[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_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[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.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[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.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1992,24 +2465,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[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.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[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.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[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.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" @@ -2031,6 +2528,94 @@ version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn 2.0.112", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2 1.0.104", + "quote 1.0.42", + "syn 2.0.112", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid 0.2.6", + "wasmparser", +] + [[package]] name = "wyz" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 0982d91..c54fcf3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ members = [ "xtask", "async-fn-stream", "unsync-pipe", + "orchid-async-utils", ] diff --git a/notes/commands.md b/notes/commands.md new file mode 100644 index 0000000..404da3f --- /dev/null +++ b/notes/commands.md @@ -0,0 +1,21 @@ +# Foreword + +Commands exist to allow programs to choose a course of action rather than merely respond to external queries with data. In a sense, commands are data that describes the totality of what a program will do. + +Commands are not equivalent to the instructions of an imperative programming language. If that were the case, they would describe a transition from a previous state to a new state in which additional instructions may be attempted, but commands do not have an "outcome". To the extent that they allow the continuation to be selected, they must encode that within themselves. + +## Are commands unique + +Yes. Since they own the entire program, and since all expressions are younger than all their subexpressions, they cannot be exactly identical to any other subexpression. + +## Are commands exclusive + +Not really. What control flow primitives exist between commands is up to the environment / interpreter. It may make sense to introduce a "parallel" primitive depending on the nature of the commands. In the abstract, we cannot talk about "the current command". + +# Extensions + +The orchid embedder and extension API mean something different by command than any particular programmer or embedder does, and something different still from what Orcx and systems programmers do. The Orchid extension API should not assume any capability that may make an embedder's job unduly difficult. + +## Continuation + +Since commands are expected to be composed into arbitrarily deep TC structures,to avoid a memory leak, commands should not remain passively present in the system; they must be able to express certain outcomes as plain data and return. The most obvious of such outcomes is the single continuation wherein a subexpression evaluating to another command from the same set will eventually run, but other ones may potentially exist. Are there any that cannot be emulated by continuing with a call to an environment constant which expresses the outcome in terms of its parameters? diff --git a/orchid-api-traits/Cargo.toml b/orchid-api-traits/Cargo.toml index 6f7bdd5..ac4050f 100644 --- a/orchid-api-traits/Cargo.toml +++ b/orchid-api-traits/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.43" futures = { version = "0.3.31", features = ["std"], default-features = false } itertools = "0.14.0" never = "0.1.0" diff --git a/orchid-api-traits/src/coding.rs b/orchid-api-traits/src/coding.rs index 0496f78..b1599d1 100644 --- a/orchid-api-traits/src/coding.rs +++ b/orchid-api-traits/src/coding.rs @@ -7,6 +7,7 @@ use std::ops::{Range, RangeInclusive}; use std::pin::Pin; use std::rc::Rc; use std::sync::Arc; +use std::time::Duration; use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use never::Never; @@ -352,3 +353,26 @@ impl Encode for char { (*self as u32).encode(write).await } } + +impl Decode for Duration { + async fn decode(mut read: Pin<&mut R>) -> io::Result { + Ok(Self::new(u64::decode(read.as_mut()).await?, u32::decode(read).await?)) + } +} +impl Encode for Duration { + async fn encode(&self, mut write: Pin<&mut W>) -> io::Result<()> { + self.as_secs().encode(write.as_mut()).await?; + self.subsec_nanos().encode(write).await + } +} +impl Decode for chrono::TimeDelta { + async fn decode(mut read: Pin<&mut R>) -> io::Result { + Ok(Self::new(i64::decode(read.as_mut()).await?, u32::decode(read).await?).unwrap()) + } +} +impl Encode for chrono::TimeDelta { + async fn encode(&self, mut write: Pin<&mut W>) -> io::Result<()> { + self.num_seconds().encode(write.as_mut()).await?; + self.subsec_nanos().encode(write).await + } +} diff --git a/orchid-api-traits/src/hierarchy.rs b/orchid-api-traits/src/hierarchy.rs index fa738db..1c060bd 100644 --- a/orchid-api-traits/src/hierarchy.rs +++ b/orchid-api-traits/src/hierarchy.rs @@ -8,8 +8,8 @@ impl TLBool for TLTrue {} pub struct TLFalse; impl TLBool for TLFalse {} -/// A type that implements [Hierarchy]. Used to select implementations of traits -/// on the hierarchy +/// A type that implements [Extends] or a root. Used to select implementations +/// of traits on the hierarchy pub trait InHierarchy: Clone { /// Indicates that this hierarchy element is a leaf. Leaves can never have /// children diff --git a/orchid-api/src/atom.rs b/orchid-api/src/atom.rs index 78d6bd3..5c33c1b 100644 --- a/orchid-api/src/atom.rs +++ b/orchid-api/src/atom.rs @@ -49,16 +49,16 @@ pub struct Atom { /// Construction is always explicit and atoms are never cloned. /// /// Atoms with `drop == None` are also known as trivial, they can be - /// duplicated and stored with no regard to expression lifetimes. NOTICE + /// duplicated and stored with no regard to expression lifetimes. Note /// that this only applies to the atom. If it's referenced with an /// [ExprTicket], the ticket itself can still expire. /// - /// Notice also that the atoms still expire when the system is dropped, and + /// Note also that the atoms still expire when the system is dropped, and /// are not portable across instances of the same system, so this doesn't /// imply that the atom is serializable. pub drop: Option, - /// Data stored in the atom. This could be a key into a map, or the raw data - /// of the atom if it isn't too big. + /// Data stored in the atom. This could be a key into a map, the raw data + /// of the atom if it isn't too big, or even a pointer. pub data: AtomData, } @@ -91,7 +91,7 @@ impl Request for SerializeAtom { #[extends(HostExtReq)] pub struct DeserAtom(pub SysId, pub Vec, pub Vec); impl Request for DeserAtom { - type Response = Atom; + type Response = LocalAtom; } /// A request blindly routed to the system that provides an atom. @@ -109,10 +109,24 @@ impl Request for Fwd { type Response = Option>; } +/// What to do after a command has finished executing #[derive(Clone, Debug, Coding)] pub enum NextStep { - Continue(Expression), - Halt, + /// Add more work. Parallel work is fairly executed in parallel, so different + /// command chains can block on each other. When the command queue is empty, + /// the interpreter may exit without waiting for timers. + Continue { + /// Run these commands immediately. Since timers don't keep the interpreter + /// alive, this should usually be non-empty, but this is not required. + immediate: Vec, + /// Schedule these commands after the specified number of milliseconds, if + /// the interpreter had not exited by then. + delayed: Vec<(NonZeroU64, Expression)>, + }, + /// Discard the rest of the queue and exit. It is possible to fail the program + /// without raising an error because the convention on most OSes is to + /// separate error reporting from a failure exit. + Exit { success: bool }, } #[derive(Clone, Debug, Coding, Hierarchy)] #[extends(AtomReq, HostExtReq)] diff --git a/orchid-api/src/binary.rs b/orchid-api/src/binary.rs index 9820818..75c0292 100644 --- a/orchid-api/src/binary.rs +++ b/orchid-api/src/binary.rs @@ -69,8 +69,10 @@ pub struct SpawnerBin { pub data: *const (), /// `self` pub drop: extern "C" fn(*const ()), - /// `&self` Add a future to this extension's task - pub spawn: extern "C" fn(*const (), FutureBin), + /// `&self` Add a future to this extension's task after a configurable delay + /// measured in milliseconds. By itself, a pending timer never prevents + /// extension shutdown. + pub spawn: extern "C" fn(*const (), u64, FutureBin), } /// Extension context. diff --git a/orchid-api/src/error.rs b/orchid-api/src/error.rs index a4d69b2..971c945 100644 --- a/orchid-api/src/error.rs +++ b/orchid-api/src/error.rs @@ -1,4 +1,5 @@ use std::num::NonZeroU16; +use std::rc::Rc; use std::sync::Arc; use orchid_api_derive::Coding; @@ -28,7 +29,7 @@ pub struct OrcError { pub description: TStr, /// Specific information about the exact error, preferably containing concrete /// values. - pub message: Arc, + pub message: Rc, /// Specific code fragments that have contributed to the emergence of the /// error. pub locations: Vec, diff --git a/orchid-api/src/expr.rs b/orchid-api/src/expr.rs index e61d430..4fd0c85 100644 --- a/orchid-api/src/expr.rs +++ b/orchid-api/src/expr.rs @@ -4,7 +4,7 @@ use std::num::NonZeroU64; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use crate::{Atom, ExtHostNotif, ExtHostReq, Location, OrcError, SysId, TStrv}; +use crate::{Atom, ExtHostNotif, ExtHostReq, LocalAtom, Location, OrcError, SysId, TStrv}; /// An arbitrary ID associated with an expression on the host side. Incoming /// tickets always come with some lifetime guarantee, which can be extended with @@ -65,8 +65,7 @@ pub enum ExpressionKind { /// Insert a new atom in the tree. When the clause is used in the const tree, /// the atom must be trivial. This is always a newly constructed atom, if you /// want to reference an existing atom, use the corresponding [ExprTicket]. - /// Because the atom is newly constructed, it also must belong to this system. - NewAtom(Atom), + NewAtom(LocalAtom), /// A reference to a constant Const(TStrv), /// A static runtime error. @@ -123,7 +122,7 @@ pub enum ExprNotif { #[derive(Clone, Debug, Coding, Hierarchy)] #[extends(ExprReq, ExtHostReq)] -pub struct Create(pub Expression); +pub struct Create(pub SysId, pub Expression); impl Request for Create { type Response = ExprTicket; } diff --git a/orchid-api/src/proto.rs b/orchid-api/src/proto.rs index 19c35f4..b7d186d 100644 --- a/orchid-api/src/proto.rs +++ b/orchid-api/src/proto.rs @@ -29,7 +29,7 @@ use futures::{AsyncRead, AsyncWrite, AsyncWriteExt}; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::{Channel, Decode, Encode, MsgSet, Request, read_exact}; -use crate::{Sweeped, atom, expr, interner, lexer, logging, parser, system, tree}; +use crate::{atom, expr, interner, lexer, logging, parser, system, tree}; static HOST_INTRO: &[u8] = b"Orchid host, binary API v0\n"; #[derive(Clone, Debug)] @@ -97,7 +97,7 @@ pub enum ExtHostReq { pub enum ExtHostNotif { ExprNotif(expr::ExprNotif), Log(logging::Log), - Sweeped(Sweeped), + Sweeped(interner::Sweeped), } pub struct ExtHostChannel; diff --git a/orchid-async-utils/Cargo.toml b/orchid-async-utils/Cargo.toml new file mode 100644 index 0000000..60851df --- /dev/null +++ b/orchid-async-utils/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "orchid-async-utils" +version = "0.1.0" +edition = "2024" + +[dependencies] +futures = { version = "0.3.31", default-features = false, features = [ + "std", + "async-await", +] } +itertools = "0.14.0" +task-local = "0.1.0" diff --git a/orchid-base/src/future_debug.rs b/orchid-async-utils/src/debug.rs similarity index 79% rename from orchid-base/src/future_debug.rs rename to orchid-async-utils/src/debug.rs index a4d9d08..a0ed65d 100644 --- a/orchid-base/src/future_debug.rs +++ b/orchid-async-utils/src/debug.rs @@ -1,3 +1,9 @@ +//! Note that these utilities are safe and simple in order to facilitate +//! debugging without adding more points of failure, but they're not efficient; +//! they may perform heap allocations, I/O and other expensive operations, or +//! even block the thread altogether waiting for input whenever they receive +//! control + use std::cell::RefCell; use std::fmt::Display; use std::pin::pin; @@ -19,10 +25,7 @@ impl Wake for OnPollWaker { } } -/// Attach a callback to the [Future] protocol for testing and debugging. Note -/// that this function is safe and simple in order to facilitate debugging -/// without adding more points of failure, but it's not fast; it performs a heap -/// allocation on each poll of the returned future. +/// Attach a callback to the [Future] protocol for testing and debugging. pub async fn on_wake( f: F, wake: impl Fn() + Clone + Send + Sync + 'static, @@ -56,6 +59,8 @@ pub async fn wrap_poll( .await } +/// Respond to [Stream::poll_next] with a callback. The semantics of the +/// callback are identical to that in [wrap_poll] pub fn wrap_poll_next<'a, S: Stream + 'a>( s: S, mut cb: impl FnMut(Box bool + '_>) + 'a, @@ -73,6 +78,7 @@ pub fn wrap_poll_next<'a, S: Stream + 'a>( }) } +/// Attach a callback to the [Stream] protocol for testing and debugging. pub fn on_stream_wake<'a, S: Stream + 'a>( s: S, wake: impl Fn() + Clone + Send + Sync + 'static, @@ -88,21 +94,26 @@ task_local! { static LABEL_STATE: Vec> } +/// Add a label to the "label stack" for the duration of a future that helps you +/// efficiently visualize important aspects of the call stack during logging pub async fn with_label(label: &str, f: Fut) -> Fut::Output { let mut new_lbl = LABEL_STATE.try_with(|lbl| lbl.clone()).unwrap_or_default(); new_lbl.push(Rc::new(label.to_string())); LABEL_STATE.scope(new_lbl, f).await } +/// Allows to print the label stack pub fn label() -> impl Display + Clone + Send + Sync + 'static { LABEL_STATE.try_with(|lbl| lbl.iter().join("/")).unwrap_or("".to_string()) } +/// Displays the label stack when printed pub struct Label; impl Display for Label { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", label()) } } +/// Attaches generic eprintln handlers to a future pub async fn eprint_events(note: &str, f: Fut) -> Fut::Output { let label = label(); let note1 = note.to_string(); @@ -116,6 +127,7 @@ pub async fn eprint_events(note: &str, f: Fut) -> Fut::Output { .await } +/// Attaches generic eprintln handlers to a stream pub fn eprint_stream_events<'a, S: Stream + 'a>( note: &'a str, s: S, @@ -137,12 +149,12 @@ impl Wake for SpinWaker { } /// A dumb executor that keeps synchronously re-running the future as long as it -/// keeps synchronously waking itself. +/// keeps synchronously waking itself. This is useful for deterministic tests +/// that don't contain side effects or threading. /// /// # Panics /// -/// If the future doesn't wake itself and doesn't settle. This is useful for -/// deterministic tests that don't contain side effects or threading. +/// If the future doesn't wake itself and doesn't settle. pub fn spin_on(f: Fut) -> Fut::Output { let repeat = Arc::new(SpinWaker(AtomicBool::new(false))); let mut f = pin!(f); @@ -161,8 +173,11 @@ pub fn spin_on(f: Fut) -> Fut::Output { /// called once the particular constraint preventing a drop has passed pub fn assert_no_drop(msg: &'static str) -> PanicOnDrop { PanicOnDrop(true, msg) } +/// This object will panic if dropped. Call [Self::defuse] when dropping is safe +/// again pub struct PanicOnDrop(bool, &'static str); impl PanicOnDrop { + /// Allow dropping the object without causing a panic pub fn defuse(mut self) { self.0 = false; } } impl Drop for PanicOnDrop { diff --git a/orchid-async-utils/src/lib.rs b/orchid-async-utils/src/lib.rs new file mode 100644 index 0000000..3147a6b --- /dev/null +++ b/orchid-async-utils/src/lib.rs @@ -0,0 +1,5 @@ +pub mod debug; +mod localset; +pub use localset::*; +mod task_future; +pub use task_future::*; diff --git a/orchid-base/src/localset.rs b/orchid-async-utils/src/localset.rs similarity index 100% rename from orchid-base/src/localset.rs rename to orchid-async-utils/src/localset.rs diff --git a/orchid-async-utils/src/task_future.rs b/orchid-async-utils/src/task_future.rs new file mode 100644 index 0000000..2332b0e --- /dev/null +++ b/orchid-async-utils/src/task_future.rs @@ -0,0 +1,92 @@ +use std::any::Any; +use std::cell::RefCell; +use std::marker::PhantomData; +use std::pin::Pin; +use std::rc::Rc; +use std::task::{Context, Poll, Waker}; + +use futures::future::{FusedFuture, LocalBoxFuture}; + +struct State { + work: Option>>, + result: Option>, + waker: Waker, +} + +/// A fused future that can be passed to a non-polymorphic executor that doesn't +/// process results and doesn't return handles +pub struct Pollable(Rc>); +impl FusedFuture for Pollable { + fn is_terminated(&self) -> bool { + let g = self.0.borrow(); + g.work.is_none() || g.result.is_some() + } +} +impl Future for Pollable { + type Output = (); + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let mut g = self.0.borrow_mut(); + match &mut *g { + State { result: Some(_), .. } | State { work: None, .. } => Poll::Ready(()), + State { work: Some(work), waker, result } => match work.as_mut().poll(cx) { + Poll::Pending => { + waker.clone_from(cx.waker()); + Poll::Pending + }, + Poll::Ready(val) => { + *result = Some(val); + g.work = None; + Poll::Ready(()) + }, + }, + } + } +} + +/// An object that can be used to inspect the state of the task +pub struct Handle(Rc>, PhantomData); +impl Handle { + /// Immediately stop working on this task, and return the result if it has + /// already finished + pub fn abort(&self) -> Option { + let mut g = self.0.borrow_mut(); + g.work.take(); + match g.result.take() { + Some(val) => Some(*val.downcast().expect("Mismatch between type of future and handle")), + None => { + g.waker.wake_by_ref(); + None + }, + } + } + /// Determine if there's any more work to do on this task + pub fn is_finished(&self) -> bool { + let g = self.0.borrow(); + g.result.is_some() || g.work.is_none() + } + /// "finish" the freestanding task, and return the future instead + pub async fn join(self) -> T { + let work = { + let mut g = self.0.borrow_mut(); + if let Some(val) = g.result.take() { + return *val.downcast().expect("Mistmatch between type of future and handle"); + } + g.waker.wake_by_ref(); + g.work.take().expect("Attempted to join task that was already aborted") + }; + *work.await.downcast().expect("Mismatch between type of future and handle") + } +} + +/// Split a future into an object that can be polled and one that returns +/// information on its progress and its result. The first one can be passed to +/// an executor or localset, the second can be used to manage it +pub fn to_task + 'static>(f: F) -> (Pollable, Handle) { + let dyn_future = Box::pin(async { Box::new(f.await) as Box }); + let state = Rc::new(RefCell::new(State { + result: None, + work: Some(dyn_future), + waker: Waker::noop().clone(), + })); + (Pollable(state.clone()), Handle(state, PhantomData)) +} diff --git a/orchid-base/Cargo.toml b/orchid-base/Cargo.toml index fdef100..99b8b48 100644 --- a/orchid-base/Cargo.toml +++ b/orchid-base/Cargo.toml @@ -8,6 +8,7 @@ edition = "2024" [dependencies] unsync-pipe = { version = "0.2.0", path = "../unsync-pipe" } async-fn-stream = { version = "0.1.0", path = "../async-fn-stream" } +orchid-async-utils = { version = "0.1.0", path = "../orchid-async-utils" } async-once-cell = "0.5.4" bound = "0.6.0" derive_destructure = "1.0.0" @@ -33,3 +34,5 @@ task-local = "0.1.0" [dev-dependencies] futures = "0.3.31" +rand = "0.10.0" +rand_chacha = "0.10.0" diff --git a/orchid-base/src/binary.rs b/orchid-base/src/binary.rs index 3474201..bfdab22 100644 --- a/orchid-base/src/binary.rs +++ b/orchid-base/src/binary.rs @@ -54,6 +54,7 @@ static BORROWED_VTABLE: RawWakerVTable = RawWakerVTable::new( /// Convert a future to a binary-compatible format that can be sent across /// dynamic library boundaries +#[must_use] pub fn future_to_vt + 'static>(fut: Fut) -> api::binary::FutureBin { let wide_box = Box::new(fut) as WideBox; let data = Box::into_raw(Box::new(wide_box)); diff --git a/orchid-base/src/box_cow.rs b/orchid-base/src/box_cow.rs deleted file mode 100644 index de844c3..0000000 --- a/orchid-base/src/box_cow.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::borrow::Borrow; -use std::ops::Deref; -use std::sync::Arc; - -pub enum ArcCow<'a, T: ?Sized + ToOwned> { - Borrowed(&'a T), - Owned(Arc), -} -impl ArcCow<'_, T> { - pub fn owned(value: T::Owned) -> Self { Self::Owned(Arc::new(value)) } -} -impl Clone for ArcCow<'_, T> { - fn clone(&self) -> Self { - match self { - Self::Borrowed(r) => Self::Borrowed(r), - Self::Owned(b) => Self::Owned(b.clone()), - } - } -} - -impl Deref for ArcCow<'_, T> { - type Target = T; - fn deref(&self) -> &Self::Target { - match self { - Self::Borrowed(t) => t, - Self::Owned(b) => b.as_ref().borrow(), - } - } -} diff --git a/orchid-base/src/boxed_iter.rs b/orchid-base/src/boxed_iter.rs index 37975c0..b465b77 100644 --- a/orchid-base/src/boxed_iter.rs +++ b/orchid-base/src/boxed_iter.rs @@ -3,20 +3,22 @@ use std::iter; /// A trait object of [Iterator] to be assigned to variables that may be -/// initialized form multiple iterators of incompatible types +/// initialized form iterators of multiple or unknown types pub type BoxedIter<'a, T> = Box + 'a>; /// creates a [BoxedIter] of a single element +#[must_use] pub fn box_once<'a, T: 'a>(t: T) -> BoxedIter<'a, T> { Box::new(iter::once(t)) } /// creates an empty [BoxedIter] +#[must_use] pub fn box_empty<'a, T: 'a>() -> BoxedIter<'a, T> { Box::new(iter::empty()) } /// Chain various iterators into a [BoxedIter] #[macro_export] macro_rules! box_chain { ($curr:expr) => { - Box::new($curr) as $crate::boxed_iter::BoxedIter<_> + Box::new($curr) as $crate::BoxedIter<_> }; ($curr:expr, $($rest:expr),*) => { - Box::new($curr$(.chain($rest))*) as $crate::boxed_iter::BoxedIter<_> + Box::new($curr$(.chain($rest))*) as $crate::BoxedIter<_> }; } diff --git a/orchid-base/src/char_filter.rs b/orchid-base/src/char_filter.rs index 17274f1..3b72305 100644 --- a/orchid-base/src/char_filter.rs +++ b/orchid-base/src/char_filter.rs @@ -5,9 +5,9 @@ use itertools::Itertools; use crate::api; -pub type CRange = RangeInclusive; - +/// A fast character filter to avoid superfluous extension calls in the lexer. pub trait ICFilter: fmt::Debug { + /// Returns an ordered set of character ranges fn ranges(&self) -> &[RangeInclusive]; } impl ICFilter for [RangeInclusive] { @@ -17,7 +17,10 @@ impl ICFilter for api::CharFilter { fn ranges(&self) -> &[RangeInclusive] { &self.0 } } -fn try_merge_char_ranges(left: CRange, right: CRange) -> Result { +fn try_merge_char_ranges( + left: RangeInclusive, + right: RangeInclusive, +) -> Result, (RangeInclusive, RangeInclusive)> { match *left.end() as u32 + 1 < *right.start() as u32 { true => Err((left, right)), false => Ok(*left.start()..=*right.end()), @@ -25,8 +28,9 @@ fn try_merge_char_ranges(left: CRange, right: CRange) -> Result) -> api::CharFilter { +/// requirements of [api::CharFilter] +#[must_use] +pub fn mk_char_filter(items: impl IntoIterator>) -> api::CharFilter { api::CharFilter( (items.into_iter()) .filter(|r| *r.start() as u32 <= *r.end() as u32) @@ -37,6 +41,7 @@ pub fn mk_char_filter(items: impl IntoIterator) -> api::CharFilte } /// Decide whether a char filter matches a character via binary search +#[must_use] pub fn char_filter_match(cf: &(impl ICFilter + ?Sized), c: char) -> bool { match cf.ranges().binary_search_by_key(&c, |l| *l.end()) { Ok(_) => true, // c is the end of a range @@ -48,6 +53,7 @@ pub fn char_filter_match(cf: &(impl ICFilter + ?Sized), c: char) -> bool { /// Merge two char filters into a filter that matches if either of the /// constituents would match. +#[must_use] pub fn char_filter_union( l: &(impl ICFilter + ?Sized), r: &(impl ICFilter + ?Sized), diff --git a/orchid-base/src/combine.rs b/orchid-base/src/combine.rs deleted file mode 100644 index 281cfeb..0000000 --- a/orchid-base/src/combine.rs +++ /dev/null @@ -1,24 +0,0 @@ -//! The concept of a fallible merger - -use never::Never; - -/// Fallible, type-preserving variant of [std::ops::Add] implemented by a -/// variety of types for different purposes. Very broadly, if the operation -/// succeeds, the result should represent _both_ inputs. -pub trait Combine: Sized { - /// Information about the failure - type Error; - - /// Merge two values into a value that represents both, if this is possible. - fn combine(self, other: Self) -> Result; -} - -impl Combine for Never { - type Error = Never; - fn combine(self, _: Self) -> Result { match self {} } -} - -impl Combine for () { - type Error = Never; - fn combine(self, (): Self) -> Result { Ok(()) } -} diff --git a/orchid-base/src/reqnot.rs b/orchid-base/src/comm.rs similarity index 98% rename from orchid-base/src/reqnot.rs rename to orchid-base/src/comm.rs index 4286abb..138a3a1 100644 --- a/orchid-base/src/reqnot.rs +++ b/orchid-base/src/comm.rs @@ -16,9 +16,8 @@ use futures::{ }; use hashbrown::HashMap; use orchid_api_traits::{Decode, Encode, Request, UnderRoot}; - -use crate::future_debug::{PanicOnDrop, assert_no_drop}; -use crate::localset::LocalSet; +use orchid_async_utils::LocalSet; +use orchid_async_utils::debug::{PanicOnDrop, assert_no_drop}; #[must_use = "Receipts indicate that a required action has been performed within a function. \ Most likely this should be returned somewhere."] @@ -445,10 +444,10 @@ mod test { use futures::{SinkExt, StreamExt, join}; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; + use orchid_async_utils::debug::spin_on; use unsync_pipe::pipe; - use crate::future_debug::spin_on; - use crate::reqnot::{ClientExt, MsgReaderExt, ReqReaderExt, io_comm}; + use crate::comm::{ClientExt, MsgReaderExt, ReqReaderExt, io_comm}; #[derive(Clone, Debug, PartialEq, Coding, Hierarchy)] #[extendable] diff --git a/orchid-base/src/error.rs b/orchid-base/src/error.rs index fd7f577..e06555f 100644 --- a/orchid-base/src/error.rs +++ b/orchid-base/src/error.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::ffi::OsStr; use std::fmt; +use std::num::{NonZero, NonZeroUsize}; use std::ops::Add; use std::rc::Rc; use std::sync::Arc; @@ -10,9 +11,7 @@ use futures::future::join_all; use itertools::Itertools; use task_local::task_local; -use crate::api; -use crate::interner::{IStr, es, is}; -use crate::location::Pos; +use crate::{IStr, Pos, api, es, is}; /// A point of interest in resolving the error, such as the point where /// processing got stuck, a command that is likely to be incorrect @@ -24,12 +23,15 @@ pub struct ErrPos { pub message: Option>, } impl ErrPos { + /// Create from a position with a position-specific message. If there's no + /// message, use `Pos::into` + #[must_use] pub fn new(msg: &str, position: Pos) -> Self { Self { message: Some(Arc::new(msg.to_string())), position } } - async fn from_api(api: &api::ErrLocation) -> Self { + async fn from_api(api: api::ErrLocation) -> Self { Self { - message: Some(api.message.clone()).filter(|s| !s.is_empty()), + message: Some(api.message).filter(|s| !s.is_empty()), position: Pos::from_api(&api.location).await, } } @@ -52,10 +54,16 @@ impl fmt::Display for ErrPos { } } +/// An error that occurred in Orchid code, whether at startup or runtime #[derive(Clone, Debug)] pub struct OrcErr { + /// A generic error message used in categorizing errors + /// You can also equality-compare atoms with these message tokens pub description: IStr, - pub message: Arc, + /// A specific error message that may include values relevant in resolving the + /// error + pub message: Rc, + /// Various locations in code that may be useful in resolving the error pub positions: Vec, } impl OrcErr { @@ -66,11 +74,11 @@ impl OrcErr { locations: self.positions.iter().map(ErrPos::to_api).collect(), } } - async fn from_api(api: &api::OrcError) -> Self { + async fn from_api(api: api::OrcError) -> Self { Self { description: es(api.description).await, - message: api.message.clone(), - positions: join_all(api.locations.iter().map(ErrPos::from_api)).await, + message: api.message, + positions: join_all(api.locations.into_iter().map(ErrPos::from_api)).await, } } } @@ -87,6 +95,8 @@ impl fmt::Display for OrcErr { } } +/// Rust error produced when an Orchid error condition arises but no +/// specific errors are listed. This is always a Rust programmer error. #[derive(Clone, Debug)] pub struct EmptyErrv; impl fmt::Display for EmptyErrv { @@ -95,39 +105,57 @@ impl fmt::Display for EmptyErrv { } } +/// A container for one or more errors. Code that supports error recovery should +/// use these instead of plain [OrcErr] objects. #[derive(Clone, Debug)] pub struct OrcErrv(Vec); impl OrcErrv { + /// Create from individual errors. If you have exactly one initial error, see + /// [mk_errv] pub fn new(errors: impl IntoIterator) -> Result { let v = errors.into_iter().collect_vec(); if v.is_empty() { Err(EmptyErrv) } else { Ok(Self(v)) } } + /// Add additional errors to this container. Since `OrcErrv` also implements + /// [IntoIterator], this can take `(Self, Self)` #[must_use] pub fn extended(mut self, errors: impl IntoIterator) -> Self where Self: Extend { self.extend(errors); self } + /// Determine how many distinct errors there are in the container #[must_use] - pub fn len(&self) -> usize { self.0.len() } - #[must_use] - pub fn is_empty(&self) -> bool { self.len() == 0 } + pub fn len(&self) -> NonZeroUsize { NonZero::new(self.0.len()).expect("OrcErrv cannot be empty") } + /// See if any errors match a particular filter criteria. This is useful for + /// sentinel errors whch are produced by user code to trigger unique + /// behaviours such as a lexer mismatch #[must_use] pub fn any(&self, f: impl FnMut(&OrcErr) -> bool) -> bool { self.0.iter().any(f) } + /// Remove all errors that don't match a filter criterion. If no errors match, + /// nothing is returned #[must_use] pub fn keep_only(self, f: impl FnMut(&OrcErr) -> bool) -> Option { let v = self.0.into_iter().filter(f).collect_vec(); if v.is_empty() { None } else { Some(Self(v)) } } + /// If there is exactly one error, return it. Mostly used for simplified + /// printing #[must_use] pub fn one(&self) -> Option<&OrcErr> { (self.0.len() == 1).then(|| &self.0[9]) } + /// Iterate over all positions of all errors pub fn pos_iter(&self) -> impl Iterator + '_ { self.0.iter().flat_map(|e| e.positions.iter().cloned()) } + /// Serialize for transmission + #[must_use] pub fn to_api(&self) -> Vec { self.0.iter().map(OrcErr::to_api).collect() } - pub async fn from_api<'a>(api: impl IntoIterator) -> Self { + /// Deserialize from transmission + #[must_use] + pub async fn from_api(api: impl IntoIterator) -> Self { Self(join_all(api.into_iter().map(OrcErr::from_api)).await) } + /// Iterate over the errors without consuming the collection pub fn iter(&self) -> impl Iterator + '_ { self.0.iter().cloned() } } impl From for OrcErrv { @@ -156,8 +184,11 @@ impl fmt::Display for OrcErrv { } } +/// A result from a function that may return multiple errors. pub type OrcRes = Result; +/// If two fallible values both succeed return both values, otherwise return +/// all errors. pub fn join_ok(left: OrcRes, right: OrcRes) -> OrcRes<(T, U)> { match (left, right) { (Ok(t), Ok(u)) => Ok((t, u)), @@ -187,15 +218,22 @@ macro_rules! join_ok { }; (@TYPES) => { () }; (@VALUES $name:ident $(: $ty:ty)? = $val:expr ; $($names:ident $(: $tys:ty)? = $vals:expr;)*) => { - $crate::error::join_ok($val, $crate::join_ok!(@VALUES $($names $(: $tys)? = $vals;)*)) + $crate::join_ok($val, $crate::join_ok!(@VALUES $($names $(: $tys)? = $vals;)*)) }; (@VALUES) => { Ok(()) }; } +/// Create an errv without an associated position, as opposed to [mk_errv]. +/// While this is technically legal and sometimes needed in library code, all +/// errors that are technically possible to associate with at least one position +/// should be. +#[must_use] pub fn mk_errv_floating(description: IStr, message: impl AsRef) -> OrcErrv { mk_errv::(description, message, []) } +/// Create an errv. The third argument can be an iterable of [ErrPos] or [Pos]. +#[must_use] pub fn mk_errv>( description: IStr, message: impl AsRef, @@ -203,12 +241,14 @@ pub fn mk_errv>( ) -> OrcErrv { OrcErr { description, - message: Arc::new(message.as_ref().to_string()), + message: Rc::new(message.as_ref().to_string()), positions: posv.into_iter().map_into().collect(), } .into() } +/// Convert a standard IO error into an Orchid error +#[must_use] pub async fn async_io_err>( err: std::io::Error, posv: impl IntoIterator, @@ -216,6 +256,8 @@ pub async fn async_io_err>( mk_errv(is(&err.kind().to_string()).await, err.to_string(), posv) } +/// Decode an Unicode string, or produce a common error related to Unicode +/// decoding pub async fn os_str_to_string>( str: &OsStr, posv: impl IntoIterator, @@ -262,6 +304,9 @@ pub async fn try_with_reporter(fut: impl Future>) -> OrcRe } } +/// Determine if there are pending errors or if this overarching procedure has a +/// chance to succeed +#[must_use] pub async fn is_erroring() -> bool { (REPORTER.try_with(|r| !r.errors.borrow().is_empty())) .expect("Sidechannel errors must be caught by a reporter") diff --git a/orchid-base/src/event.rs b/orchid-base/src/event.rs deleted file mode 100644 index 0f982db..0000000 --- a/orchid-base/src/event.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! Multiple-listener-single-delivery event system. - -use std::mem; -use std::sync::Mutex; -use std::sync::mpsc::{self, sync_channel}; - -struct Reply { - resub: bool, - outcome: Result, -} - -struct Listener { - sink: mpsc::SyncSender, - source: mpsc::Receiver>, -} - -pub struct Event { - listeners: Mutex>>, -} -impl Event { - pub const fn new() -> Self { Self { listeners: Mutex::new(Vec::new()) } } - - pub fn dispatch(&self, mut ev: T) -> Option { - let mut listeners = self.listeners.lock().unwrap(); - let mut alt_list = Vec::with_capacity(listeners.len()); - mem::swap(&mut *listeners, &mut alt_list); - let mut items = alt_list.into_iter(); - while let Some(l) = items.next() { - l.sink.send(ev).unwrap(); - let Reply { resub, outcome } = l.source.recv().unwrap(); - if resub { - listeners.push(l); - } - match outcome { - Ok(res) => { - listeners.extend(items); - return Some(res); - }, - Err(next) => { - ev = next; - }, - } - } - None - } - - pub fn get_one(&self, mut filter: impl FnMut(&T) -> bool, f: impl FnOnce(T) -> (U, V)) -> V { - let mut listeners = self.listeners.lock().unwrap(); - let (sink, request) = sync_channel(0); - let (response, source) = sync_channel(0); - listeners.push(Listener { sink, source }); - mem::drop(listeners); - loop { - let t = request.recv().unwrap(); - if filter(&t) { - let (u, v) = f(t); - response.send(Reply { resub: false, outcome: Ok(u) }).unwrap(); - return v; - } - response.send(Reply { resub: true, outcome: Err(t) }).unwrap(); - } - } -} - -impl Default for Event { - fn default() -> Self { Self::new() } -} diff --git a/orchid-base/src/format.rs b/orchid-base/src/format.rs index 8c9bc8c..d66fe0b 100644 --- a/orchid-base/src/format.rs +++ b/orchid-base/src/format.rs @@ -14,21 +14,27 @@ use regex::Regex; use crate::{api, match_mapping}; +/// A unit of formattable text where the formatter must make a single choice +/// Converting from various types via [Into::into] keeps strings intact, but +/// [str::parse] resolves escape sequences #[derive(Clone, Debug, Hash, PartialEq, Eq)] #[must_use] pub struct FmtUnit { + /// Sub-units pub subs: Vec, + /// Parsed text templates for how to render this text pub variants: Rc, } impl FmtUnit { pub fn new(variants: Rc, subs: impl IntoIterator) -> Self { Self { subs: subs.into_iter().collect(), variants } } + /// Deserialize from message pub fn from_api(api: &api::FormattingUnit) -> Self { Self { subs: api.subs.iter().map(Self::from_api).collect(), variants: Rc::new(Variants( - (api.variants.iter().map(|var| Variant { + (api.variants.iter().map(|var| FmtVariant { bounded: var.bounded, elements: var.elements.iter().map(FmtElement::from_api).collect(), })) @@ -36,6 +42,8 @@ impl FmtUnit { )), } } + /// Serialize into message. String interner IDs used in the structure must + /// remain valid. pub fn to_api(&self) -> api::FormattingUnit { api::FormattingUnit { subs: self.subs.iter().map(Self::to_api).collect(), @@ -46,11 +54,13 @@ impl FmtUnit { .collect(), } } + /// Shorthand for a variable-length list that can be formatted in exactly one + /// way pub fn sequence( head: &str, delim: &str, tail: &str, - seq_bnd: Option, + seq_bnd: bool, seq: impl IntoIterator, ) -> Self { let items = seq.into_iter().collect_vec(); @@ -69,18 +79,37 @@ impl FromStr for FmtUnit { } } +/// A single element of a format string. Composes into [FmtVariant] #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub enum FmtElement { - Sub { slot: u32, bounded: Option }, + /// a reference to an interpolable subunit in the enclosing [FmtUnit] + Sub { + /// Index into [FmtUnit::subs] + slot: u32, + /// Whether the subunit can use an unbounded (`Some(false)`) [FmtVariant], + /// it is restricted to bounded (`Some(true)`) [FmtVariant], or it should + /// inherit this information from the enclosing unit, meaning that the slot + /// is at the very end of the format string + bounded: Option, + }, + /// a string snippet String(Rc), + /// an indented block Indent(Vec), } impl FmtElement { + /// Create a plain string snippet pub fn str(s: &'_ str) -> Self { Self::String(Rc::new(s.to_string())) } + /// Create a slot for a subunit pub fn sub(slot: u32, bounded: Option) -> Self { Self::Sub { slot, bounded } } + /// Create a slot for a subunit's bounded representation pub fn bounded(i: u32) -> Self { Self::sub(i, Some(true)) } + /// Create a slot for any representation of a subunit pub fn unbounded(i: u32) -> Self { Self::sub(i, Some(false)) } + /// Create an end slot bounded by the enclosing unit if that is bounded pub fn last(i: u32) -> Self { Self::sub(i, None) } + /// Create a sequence of `len` unbounded slots capped by a slot of the + /// specified boundedness pub fn sequence(len: usize, bounded: Option) -> Vec { match len.try_into().unwrap() { 0u32 => vec![], @@ -88,6 +117,7 @@ impl FmtElement { n => (0..n - 1).map(FmtElement::unbounded).chain([FmtElement::sub(n - 1, bounded)]).collect(), } } + /// Decode from a message pub fn from_api(api: &api::FormattingElement) -> Self { match_mapping!(api, api::FormattingElement => FmtElement { Indent(v => v.iter().map(FmtElement::from_api).collect()), @@ -95,6 +125,7 @@ impl FmtElement { Sub{ *slot, *bounded }, }) } + /// Encode to message pub fn to_api(&self) -> api::FormattingElement { match_mapping!(self, FmtElement => api::FormattingElement { Indent(v => v.iter().map(FmtElement::to_api).collect()), @@ -104,39 +135,16 @@ impl FmtElement { } } +/// A particular way in which a value may be formatted in text. #[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub struct Variant { +pub struct FmtVariant { + /// Whether this representation has an intrinsic end marker or it needs the + /// parent to provide one pub bounded: bool, + /// Template string syntax elements pub elements: Vec, } -#[test] -fn variants_parse_test() { - let vars = Rc::new(Variants::default().bounded("({{{0}}})")); - let expected_vars = Rc::new(Variants(vec![Variant { - bounded: true, - elements: vec![ - FmtElement::String(Rc::new("({".to_string())), - FmtElement::Sub { bounded: Some(false), slot: 0 }, - FmtElement::String(Rc::new("})".to_string())), - ], - }])); - assert_eq!(vars.as_ref(), expected_vars.as_ref()); - let unit = vars.units(["1".into()]); - assert_eq!(unit, FmtUnit { - subs: vec![FmtUnit { - subs: vec![], - variants: Rc::new(Variants(vec![Variant { - bounded: true, - elements: vec![FmtElement::String(Rc::new("1".to_string()))] - }])) - }], - variants: expected_vars - }); - let str = take_first(&unit, true); - assert_eq!(str, "({1})"); -} - /// Represents a collection of formatting strings for the same set of parameters /// from which the formatter can choose within their associated constraints. /// @@ -145,7 +153,7 @@ fn variants_parse_test() { /// - {0l} causes the current end restriction to be applied to the parameter. /// This is to be used if the parameter is at the very end of the variant. #[derive(Clone, Debug, Hash, PartialEq, Eq, Default)] -pub struct Variants(pub Vec); +pub struct Variants(pub Vec); impl Variants { fn parse_phs(s: &'_ str) -> Vec { let re = Regex::new(r"(?\{\d+?[bl]?\})|(\{\{)|(\}\})").unwrap(); @@ -216,7 +224,7 @@ impl Variants { } } fn add(&mut self, bounded: bool, s: &'_ str) { - self.0.push(Variant { bounded, elements: Self::parse(s) }) + self.0.push(FmtVariant { bounded, elements: Self::parse(s) }) } /// This option is available in all positions. /// See [Variants] for a description of the format strings @@ -231,35 +239,42 @@ impl Variants { self.add(false, s); self } + /// Produces formatting options for `len` parameters separated by `delim`. + /// `seq_bnd` indicates whether `delim` and `tail` can unambiguously indicate + /// the end of a subsequence. For consistency, the stricter of the two is + /// expected to be used pub fn sequence( mut self, len: usize, head: &str, delim: &str, tail: &str, - seq_bnd: Option, + seq_bnd: bool, ) -> Self { let seq = chain!( [FmtElement::str(head)], Itertools::intersperse( - FmtElement::sequence(len, seq_bnd).into_iter(), + FmtElement::sequence(len, Some(seq_bnd)).into_iter(), FmtElement::str(delim), ), [FmtElement::str(tail)], ); - self.0.push(Variant { bounded: true, elements: seq.collect_vec() }); + self.0.push(FmtVariant { bounded: true, elements: seq.collect_vec() }); self } + /// Pair the slots with subunits to produce a [FmtUnit] pub fn units_own(self, subs: impl IntoIterator) -> FmtUnit { FmtUnit::new(Rc::new(self), subs) } + /// Pair the slots with subunits to produce a [FmtUnit] by reference. These + /// objects should preferably be thread-locally cached whenever possible. pub fn units(self: &Rc, subs: impl IntoIterator) -> FmtUnit { FmtUnit::new(self.clone(), subs) } } impl From> for Variants { fn from(value: Rc) -> Self { - Self(vec![Variant { elements: vec![FmtElement::String(value)], bounded: true }]) + Self(vec![FmtVariant { elements: vec![FmtElement::String(value)], bounded: true }]) } } impl From for Variants { @@ -304,23 +319,18 @@ pub async fn take_first_fmt(v: &(impl Format + ?Sized)) -> String { take_first(&v.print(&FmtCtxImpl { _foo: PhantomData }).await, false) } +/// [Default] this if you need one #[derive(Default)] pub struct FmtCtxImpl<'a> { _foo: PhantomData<&'a ()>, } -pub trait FmtCtx { - // fn print_as(&self, p: &(impl Format + ?Sized)) -> impl Future where Self: Sized { - // async { - // // for now, always take the first option which is probably the one-line - // form let variants = p.print(self).await; - // take_first(&variants, true) - // } - // } -} +/// Additional settings to the formatter. Implemented by [FmtCtxImpl]. Currently +/// not in use +pub trait FmtCtx {} impl FmtCtx for FmtCtxImpl<'_> {} +/// A value that can be formatted into a string with multiple possible forms pub trait Format { #[must_use] fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> impl Future + 'a; @@ -337,3 +347,37 @@ pub async fn fmt_v( ) -> impl Iterator { join_all(v.into_iter().map(|f| async move { take_first_fmt(f.borrow()).await })).await.into_iter() } + +#[cfg(test)] +mod test { + use std::rc::Rc; + + use crate::format::{FmtElement, FmtUnit, FmtVariant, Variants, take_first}; + + #[test] + fn variants_parse_test() { + let vars = Rc::new(Variants::default().bounded("({{{0}}})")); + let expected_vars = Rc::new(Variants(vec![FmtVariant { + bounded: true, + elements: vec![ + FmtElement::String(Rc::new("({".to_string())), + FmtElement::Sub { bounded: Some(false), slot: 0 }, + FmtElement::String(Rc::new("})".to_string())), + ], + }])); + assert_eq!(vars.as_ref(), expected_vars.as_ref()); + let unit = vars.units(["1".into()]); + assert_eq!(unit, FmtUnit { + subs: vec![FmtUnit { + subs: vec![], + variants: Rc::new(Variants(vec![FmtVariant { + bounded: true, + elements: vec![FmtElement::String(Rc::new("1".to_string()))] + }])) + }], + variants: expected_vars + }); + let str = take_first(&unit, true); + assert_eq!(str, "({1})"); + } +} diff --git a/orchid-base/src/handler.rs b/orchid-base/src/handler.rs deleted file mode 100644 index 077685e..0000000 --- a/orchid-base/src/handler.rs +++ /dev/null @@ -1,116 +0,0 @@ -//! Impure functions that can be triggered by Orchid code when a command -//! evaluates to an atom representing a command - -use std::any::{Any, TypeId}; -use std::cell::RefCell; - -use hashbrown::HashMap; -use trait_set::trait_set; - -use super::nort::Expr; -use crate::foreign::atom::Atomic; -use crate::foreign::error::RTResult; -use crate::foreign::to_clause::ToClause; -use crate::location::CodeLocation; - -trait_set! { - trait Handler = for<'a> Fn(&'a dyn Any, CodeLocation) -> Expr; -} - -enum HTEntry<'a> { - Handler(Box), - Forward(&'a (dyn Handler + 'a)), -} -impl<'a> AsRef for HTEntry<'a> { - fn as_ref(&self) -> &(dyn Handler + 'a) { - match self { - HTEntry::Handler(h) => &**h, - HTEntry::Forward(h) => *h, - } - } -} - -/// A table of impure command handlers exposed to Orchid -#[derive(Default)] -pub struct HandlerTable<'a> { - handlers: HashMap>, -} -impl<'a> HandlerTable<'a> { - /// Create a new [HandlerTable] - #[must_use] - pub fn new() -> Self { Self { handlers: HashMap::new() } } - - /// Add a handler function to interpret a command and select the continuation. - /// See [HandlerTable#with] for a declarative option. - pub fn register(&mut self, f: impl for<'b> FnMut(&'b T) -> R + 'a) { - let cell = RefCell::new(f); - let cb = move |a: &dyn Any, loc: CodeLocation| { - cell.borrow_mut()(a.downcast_ref().expect("found by TypeId")).to_expr(loc) - }; - let prev = self.handlers.insert(TypeId::of::(), HTEntry::Handler(Box::new(cb))); - assert!(prev.is_none(), "A handler for this type is already registered"); - } - - /// Add a handler function to interpret a command and select the continuation. - /// See [HandlerTable#register] for a procedural option. - pub fn with(mut self, f: impl FnMut(&T) -> RTResult + 'a) -> Self { - self.register(f); - self - } - - /// Find and execute the corresponding handler for this type - pub fn dispatch(&self, arg: &dyn Atomic, loc: CodeLocation) -> Option { - (self.handlers.get(&arg.as_any_ref().type_id())).map(|ent| ent.as_ref()(arg.as_any_ref(), loc)) - } - - /// Combine two non-overlapping handler sets - #[must_use] - pub fn combine(mut self, other: Self) -> Self { - for (key, value) in other.handlers { - let prev = self.handlers.insert(key, value); - assert!(prev.is_none(), "Duplicate handlers") - } - self - } - - /// Add entries that forward requests to a borrowed non-overlapping handler - /// set - pub fn link<'b: 'a>(mut self, other: &'b HandlerTable<'b>) -> Self { - for (key, value) in other.handlers.iter() { - let prev = self.handlers.insert(*key, HTEntry::Forward(value.as_ref())); - assert!(prev.is_none(), "Duplicate handlers") - } - self - } -} - -#[cfg(test)] -#[allow(unconditional_recursion)] -#[allow(clippy::ptr_arg)] -mod test { - use std::marker::PhantomData; - - use super::HandlerTable; - - /// Ensure that the method I use to verify covariance actually passes with - /// covariant and fails with invariant - /// - /// The failing case: - /// ``` - /// struct Cov2<'a>(PhantomData<&'a mut &'a ()>); - /// fn fail<'a>(_c: &Cov2<'a>, _s: &'a String) { fail(_c, &String::new()) } - /// ``` - #[allow(unused)] - fn covariant_control() { - struct Cov<'a>(PhantomData<&'a ()>); - fn pass<'a>(_c: &Cov<'a>, _s: &'a String) { pass(_c, &String::new()) } - } - - /// The &mut ensures that 'a in the two functions must be disjoint, and that - /// ht must outlive both. For this to compile, Rust has to cast ht to the - /// shorter lifetimes, ensuring covariance - #[allow(unused)] - fn assert_covariant() { - fn pass<'a>(_ht: HandlerTable<'a>, _s: &'a String) { pass(_ht, &String::new()) } - } -} diff --git a/orchid-base/src/id_store.rs b/orchid-base/src/id_store.rs index 0b31e8b..c399fe6 100644 --- a/orchid-base/src/id_store.rs +++ b/orchid-base/src/id_store.rs @@ -1,50 +1,120 @@ -use std::num::NonZeroU64; -use std::ops::{Deref, DerefMut}; -use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Mutex, MutexGuard, OnceLock}; +use std::ops::{Index, IndexMut}; -use hashbrown::HashMap; +use itertools::Itertools; +enum Rec { + Val(T), + Next(usize), +} + +/// A simple and very fast store that assigns small stable integer IDs to +/// objects. It uses a free-list for O(1) insertion, deletion and retrieval. pub struct IdStore { - table: OnceLock>>, - id: AtomicU64, + first: usize, + values: Vec>, } impl IdStore { - pub const fn new() -> Self { Self { table: OnceLock::new(), id: AtomicU64::new(1) } } - pub fn add(&self, t: T) -> IdRecord<'_, T> { - let tbl = self.table.get_or_init(Mutex::default); - let mut tbl_g = tbl.lock().unwrap(); - let id: NonZeroU64 = self.id.fetch_add(1, Ordering::Relaxed).try_into().unwrap(); - assert!(tbl_g.insert(id, t).is_none(), "atom ID wraparound"); - IdRecord(id, tbl_g) + pub fn new() -> Self { IdStore { first: 0, values: Vec::new() } } + pub fn add(&mut self, value: T) -> usize { + if self.first == 0 && self.values.is_empty() { + self.first = 1; + self.values.push(Rec::Val(value)); + return 0; + } + if self.first == self.values.len() { + let len = self.values.len(); + self.values.extend((len..len * 2).map(|i| Rec::Next(i + 1))); + } + let Some(rec) = self.values.get_mut(self.first) else { + panic!("Bounds check and growth above") + }; + let Rec::Next(next) = rec else { + panic!("first should always point to an empty space or one past the length") + }; + let id = std::mem::replace(&mut self.first, *next); + *rec = Rec::Val(value); + id } - pub fn get(&self, id: impl Into) -> Option> { - let tbl = self.table.get_or_init(Mutex::default); - let tbl_g = tbl.lock().unwrap(); - let id64 = id.into(); - if tbl_g.contains_key(&id64) { Some(IdRecord(id64, tbl_g)) } else { None } + pub fn add_with(&mut self, cb: impl FnOnce(usize) -> T) -> usize { self.add(cb(self.first)) } + pub fn remove(&mut self, id: usize) -> T { + let Some(rec) = self.values.get_mut(id) else { panic!("Index out of bounds") }; + let Rec::Val(val) = std::mem::replace(rec, Rec::Next(self.first)) else { + panic!("Index vacated") + }; + self.first = id; + val + } + pub fn iter(&self) -> impl Iterator { + (self.values.iter().enumerate()) + .filter_map(|(i, rec)| if let Rec::Val(val) = rec { Some((i, val)) } else { None }) + } + pub fn iter_mut(&mut self) -> impl Iterator { + (self.values.iter_mut().enumerate()) + .filter_map(|(i, rec)| if let Rec::Val(val) = rec { Some((i, val)) } else { None }) + } +} +#[allow(clippy::type_complexity, reason = "This is verbose enough as it is")] +pub struct IntoIter( + std::iter::FilterMap< + std::iter::Enumerate>>, + fn((usize, Rec)) -> Option<(usize, T)>, + >, +); +impl Iterator for IntoIter { + type Item = (usize, T); + fn next(&mut self) -> Option { self.0.next() } + fn size_hint(&self) -> (usize, Option) { self.0.size_hint() } +} +impl IntoIterator for IdStore { + type Item = (usize, T); + type IntoIter = IntoIter; + fn into_iter(self) -> Self::IntoIter { + IntoIter( + (self.values.into_iter().enumerate()) + .filter_map(|(i, rec)| if let Rec::Val(val) = rec { Some((i, val)) } else { None }), + ) + } +} +impl Index for IdStore { + type Output = T; + fn index(&self, index: usize) -> &Self::Output { + match self.values.get(index) { + Some(Rec::Val(val)) => val, + _ => panic!("Invalid or stale index"), + } + } +} +impl IndexMut for IdStore { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + match self.values.get_mut(index) { + Some(Rec::Val(val)) => val, + _ => panic!("Invalid or stale index"), + } } - pub fn is_empty(&self) -> bool { self.len() == 0 } - pub fn len(&self) -> usize { self.table.get().map(|t| t.lock().unwrap().len()).unwrap_or(0) } } impl Default for IdStore { fn default() -> Self { Self::new() } } +impl FromIterator for IdStore { + fn from_iter>(iter: T) -> Self { + let values = iter.into_iter().map(|a| Rec::Val(a)).collect_vec(); + Self { first: values.len(), values } + } +} -pub struct IdRecord<'a, T>(NonZeroU64, MutexGuard<'a, HashMap>); -impl IdRecord<'_, T> { - pub fn id(&self) -> NonZeroU64 { self.0 } - pub fn remove(mut self) -> T { self.1.remove(&self.0).unwrap() } -} -impl Deref for IdRecord<'_, T> { - type Target = T; - fn deref(&self) -> &Self::Target { - self.1.get(&self.0).expect("Existence checked on construction") - } -} -impl DerefMut for IdRecord<'_, T> { - fn deref_mut(&mut self) -> &mut Self::Target { - self.1.get_mut(&self.0).expect("Existence checked on construction") +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn add_and_retrieve() { + let mut store = IdStore::new(); + let key1 = store.add(14); + let key2 = store.add(34); + assert_eq!(store[key1], 14); + assert_eq!(store[key2], 34); + assert_eq!(store.remove(key1), 14); + assert_eq!(store.iter().collect_vec(), vec![(key2, &34)]); } } diff --git a/orchid-base/src/interner.rs b/orchid-base/src/interner.rs index da44ac0..d6b5f51 100644 --- a/orchid-base/src/interner.rs +++ b/orchid-base/src/interner.rs @@ -10,22 +10,27 @@ use task_local::task_local; use crate::api; +/// Implementation-specific backing object for an interned string. pub trait IStrHandle: AsRef { fn rc(&self) -> Rc; } +/// Implementation-specific backing object for an interned sequence of interned +/// strings. pub trait IStrvHandle: AsRef<[IStr]> { fn rc(&self) -> Rc>; } +/// Interned string created with [is] or [es] #[derive(Clone)] pub struct IStr(pub api::TStr, pub Rc); impl IStr { - /// Obtain a unique ID for this interned data. + /// Obtain a unique ID for this interned data /// /// NOTICE: the ID is guaranteed to be the same for any interned instance of /// the same value only as long as at least one instance exists. If a value is - /// no longer interned, the interner is free to forget about it. + /// no longer interned, the interner is free to forget about it pub fn to_api(&self) -> api::TStr { self.0 } + /// Owned reference to a shared instance of the interned string pub fn rc(&self) -> Rc { self.1.rc() } } impl Deref for IStr { @@ -45,15 +50,18 @@ impl Display for IStr { impl Debug for IStr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "IStr({self}") } } + +/// Interned string sequence #[derive(Clone)] pub struct IStrv(pub api::TStrv, pub Rc); impl IStrv { - /// Obtain a unique ID for this interned data. + /// Obtain a unique ID for this interned data /// /// NOTICE: the ID is guaranteed to be the same for any interned instance of /// the same value only as long as at least one instance exists. If a value is - /// no longer interned, the interner is free to forget about it. + /// no longer interned, the interner is free to forget about it pub fn to_api(&self) -> api::TStrv { self.0 } + /// Owned reference to a shared instance of the interned sequence pub fn rc(&self) -> Rc> { self.1.rc() } } impl Deref for IStrv { @@ -84,10 +92,23 @@ impl Debug for IStrv { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "IStrv({self})") } } +/// Injectable interner interface +/// +/// [Self::is] and [Self::iv] return an existing ID if any [IStrHandle] or +/// [IStrvHandle] for the same value is still live, and any ID currently not +/// used with the same type otherwise +/// +/// [Self::es] and [Self::ev] find an existing value by its key if any +/// [IStrHandle] or [IStrvHandle] for the same ID is still live. If all objects +/// are gone the functions may work or panic. pub trait InternerSrv { + /// Intern a string fn is<'a>(&'a self, v: &'a str) -> LocalBoxFuture<'a, IStr>; + /// Find an existing string by its key fn es(&self, t: api::TStr) -> LocalBoxFuture<'_, IStr>; + /// Intern a str vector fn iv<'a>(&'a self, v: &'a [IStr]) -> LocalBoxFuture<'a, IStrv>; + /// Find an existing str vector by its key fn ev(&self, t: api::TStrv) -> LocalBoxFuture<'_, IStrv>; } @@ -95,6 +116,8 @@ task_local! { static INTERNER: Rc; } +/// Install a global interner. Within this future, the global [is], [iv], [es] +/// and [ev] functions call the provided [InternerSrv] pub async fn with_interner(val: Rc, fut: F) -> F::Output { INTERNER.scope(val, fut).await } @@ -103,11 +126,28 @@ fn get_interner() -> Rc { INTERNER.try_with(|i| i.clone()).expect("Interner not initialized") } +/// Intern a `String` (find its ID or assign it a new one) pub async fn is(v: &str) -> IStr { get_interner().is(v).await } +/// Intern a `Vec` (find its ID or assign it a new one) pub async fn iv(v: &[IStr]) -> IStrv { get_interner().iv(v).await } +/// Find a live [IStr] by its ID +/// +/// # Panics +/// +/// This function may panic if there are no other references to the [IStr] we're +/// searching for, as the interner is free to forget about unreferenced values pub async fn es(v: api::TStr) -> IStr { get_interner().es(v).await } +/// Find a live [IStrv] by its ID +/// +/// # Panics +/// +/// This function may panic if there are no other references to the [IStrv] +/// we're searching for, as the interner is free to forget about unreferenced +/// values pub async fn ev(v: api::TStrv) -> IStrv { get_interner().ev(v).await } +/// Basic engine for an interner that supports recovering if a token is not +/// found locally. pub mod local_interner { use std::borrow::Borrow; use std::cell::RefCell; @@ -144,6 +184,7 @@ pub mod local_interner { fn new_interned(token: Self::Token, handle: Rc>) -> Self::Interned; } + /// String-specific values for [InternableCard] #[derive(Default, Debug)] pub struct StrBranch; impl InternableCard for StrBranch { @@ -154,6 +195,7 @@ pub mod local_interner { fn new_interned(t: Self::Token, h: Rc>) -> Self::Interned { IStr(t, h) } } + /// Vector-specific values for [InternableCard] #[derive(Default, Debug)] pub struct StrvBranch; impl InternableCard for StrvBranch { @@ -208,8 +250,8 @@ pub mod local_interner { /// Information retained about an interned token indexed both by key and /// value. struct Rec { - /// This reference is weak, but the [Drop] handler of [Handle] removes all - /// [Rec]s from the interner so it is guaranteed to be live. + /// This reference is weak, but the [Drop] handler of [Handle] removes the + /// [Rec] from the interner so it is guaranteed to be live. handle: Weak>, /// Keys for indexing from either table data: Data, diff --git a/orchid-base/src/iter_utils.rs b/orchid-base/src/iter_print.rs similarity index 85% rename from orchid-base/src/iter_utils.rs rename to orchid-base/src/iter_print.rs index aadf8ba..45a953c 100644 --- a/orchid-base/src/iter_utils.rs +++ b/orchid-base/src/iter_print.rs @@ -17,6 +17,8 @@ impl<'a, I: Iterator + Clone, E: fmt::Display> fmt::Display for PrintL } pub trait IteratorPrint: Iterator + Clone { + /// Pretty-print a list with a comma-separated enumeration and an operator + /// word (such as "and" or "or") inserted before the last fn display<'a>(self, operator: &'a str) -> PrintList<'a, Self, Self::Item> { PrintList(self, operator) } diff --git a/orchid-base/src/lib.rs b/orchid-base/src/lib.rs index 9ed8ff6..e7f4011 100644 --- a/orchid-base/src/lib.rs +++ b/orchid-base/src/lib.rs @@ -1,33 +1,44 @@ pub use async_once_cell; use orchid_api as api; -pub mod binary; -pub mod box_cow; -pub mod boxed_iter; -pub mod char_filter; +mod on_drop; +pub use on_drop::*; +mod binary; +pub use binary::*; +mod id_store; +pub use id_store::*; +mod boxed_iter; +pub use boxed_iter::*; +mod char_filter; +pub use char_filter::*; pub mod clone; -pub mod combine; -pub mod error; -pub mod event; -pub mod format; -pub mod future_debug; -pub mod id_store; -pub mod interner; -pub mod iter_utils; -pub mod join; -mod localset; -pub mod location; -pub mod logging; +mod error; +pub use error::*; +mod format; +pub use format::*; +mod interner; +pub use interner::*; +mod iter_print; +pub use iter_print::*; +mod join; +pub use join::*; +mod location; +pub use location::*; +mod logging; +pub use logging::*; mod match_mapping; -pub mod msg; -pub mod name; -pub mod number; -pub mod parse; -pub mod pure_seq; -pub mod reqnot; -pub mod sequence; -pub mod side; -pub mod stash; -pub mod tl_cache; -pub mod tokens; -pub mod tree; +mod name; +pub use name::*; +mod number; +pub use number::*; +mod parse; +pub use parse::*; +mod comm; +pub use comm::*; +mod side; +pub use side::*; +mod stash; +pub use stash::*; +mod tl_cache; +mod tree; +pub use tree::*; diff --git a/orchid-base/src/location.rs b/orchid-base/src/location.rs index ef0b5bb..7c23ff4 100644 --- a/orchid-base/src/location.rs +++ b/orchid-base/src/location.rs @@ -7,25 +7,34 @@ use std::ops::{Add, AddAssign, Range}; use futures::future::join_all; use trait_set::trait_set; -use crate::error::ErrPos; -use crate::interner::{IStr, es, is}; -use crate::name::Sym; -use crate::{api, match_mapping, sym}; +use crate::{ErrPos, IStr, IteratorPrint, Sym, api, es, is, match_mapping, sym}; trait_set! { pub trait GetSrc = FnMut(&Sym) -> IStr; } +/// One or more positions in code that are associated with an event, value, or +/// other consequence of that code #[derive(Debug, Clone, PartialEq, Eq)] pub enum Pos { + /// Location not known, for example because the expression was decoded from a + /// source that doesn't have a meaningful location attached, from a format + /// that does not encode location data None, + /// If the expression in question is a slot, it receives the substituted + /// expression's position. If the expression is being placed into a slot, this + /// is discarded. In all other cases, it is a conflict and an error SlotTarget, /// Used in functions to denote the generated code that carries on the - /// location of the call. Not allowed in the const tree. + /// location of the call. Not allowed in the const tree Inherit, + /// ID and parameters of a generator (such as an extension) Gen(CodeGenInfo), /// Range and file SrcRange(SrcRange), + /// More than one positions. This vec should not contain another [Pos::Multi] + /// and should be `>=2` long. To ensure this, use `+` and `+=` to combine + /// positions and do not construct this directly. Multi(Vec), } impl Pos { @@ -33,6 +42,7 @@ impl Pos { match self { Self::Gen(g) => g.to_string(), Self::SrcRange(sr) => sr.pretty_print(&get_src(&sr.path)), + Self::Multi(posv) => posv.iter().display("and").to_string(), // Can't pretty print partial and meta-location other => format!("{other:?}"), } @@ -106,7 +116,7 @@ impl SrcRange { pub fn new(range: Range, path: &Sym) -> Self { Self { range: range.clone(), path: path.clone() } } - /// Create a dud [SourceRange] for testing. Its value is unspecified and + /// Create a dud [SrcRange] for testing. Its value is unspecified and /// volatile. pub async fn mock() -> Self { Self { range: 0..1, path: sym!(test) } } /// Path the source text was loaded from @@ -123,6 +133,10 @@ impl SrcRange { pub fn map_range(&self, map: impl FnOnce(Range) -> Range) -> Self { Self { range: map(self.range()), path: self.path() } } + /// Format the range in a way that VSCode can convert to a link and is + /// human-readable. For this operation we need the source code text, but + /// holding it in the position object is a bit heavy so instead we take it as + /// an argument pub fn pretty_print(&self, src: &str) -> String { let (sl, sc) = pos2lc(src, self.range.start); let (el, ec) = pos2lc(src, self.range.end); @@ -132,13 +146,21 @@ impl SrcRange { (false, _) => format!("{sl}:{sc}..{el}:{ec}"), } } + /// Zero-width position at a certain offset pub fn zw(path: Sym, pos: u32) -> Self { Self { path, range: pos..pos } } + /// Deserialize from a message pub async fn from_api(api: &api::SourceRange) -> Self { Self { path: Sym::from_api(api.path).await, range: api.range.clone() } } + /// Serialize to a message pub fn to_api(&self) -> api::SourceRange { api::SourceRange { path: self.path.to_api(), range: self.range.clone() } } + /// Connect two ranges into one + /// + /// # Panics + /// + /// if the ranges are not from the same file pub fn to(&self, rhs: &Self) -> Self { assert_eq!(self.path, rhs.path, "Range continues across files"); Self { path: self.path(), range: self.start().min(rhs.start())..self.end().max(rhs.end()) } @@ -173,9 +195,11 @@ impl CodeGenInfo { } /// Syntactic location pub fn pos(&self) -> Pos { Pos::Gen(self.clone()) } + /// Deserialize from a message pub async fn from_api(api: &api::CodeGenInfo) -> Self { Self { generator: Sym::from_api(api.generator).await, details: es(api.details).await } } + /// Serialize to a message pub fn to_api(&self) -> api::CodeGenInfo { api::CodeGenInfo { generator: self.generator.to_api(), details: self.details.to_api() } } diff --git a/orchid-base/src/logging.rs b/orchid-base/src/logging.rs index 334234f..f1a59aa 100644 --- a/orchid-base/src/logging.rs +++ b/orchid-base/src/logging.rs @@ -1,31 +1,27 @@ use std::any::Any; -use std::cell::RefCell; use std::fmt::Arguments; -use std::io::Write; use std::rc::Rc; use futures::future::LocalBoxFuture; use task_local::task_local; -use crate::api; - -task_local! { - static DEFAULT_WRITER: RefCell> -} - -/// Set the stream used for [api::LogStrategy::Default]. If not set, -/// [std::io::stderr] will be used. -pub async fn with_default_stream(stderr: impl Write + 'static, fut: F) -> F::Output { - DEFAULT_WRITER.scope(RefCell::new(Box::new(stderr)), fut).await -} +use crate::{api, clone}; +/// A first argument to [write!] and [writeln!] that causes them to return a +/// future. pub trait LogWriter { fn write_fmt<'a>(&'a self, fmt: Arguments<'a>) -> LocalBoxFuture<'a, ()>; } +/// Injectable logging service passed to [with_logger] pub trait Logger: Any { + /// Obtain a writer that processes the message according to the given category fn writer(&self, category: &str) -> Rc; + /// Obtain a serializable limited description of what will eventually happen + /// to the message fn strat(&self, category: &str) -> api::LogStrategy; + /// Determine whether a certain category would get processed at all. This is a + /// shortcut fn is_active(&self, category: &str) -> bool { !matches!(self.strat(category), api::LogStrategy::Discard) } @@ -35,43 +31,46 @@ task_local! { static LOGGER: Rc; } +/// Within the future passed to this function the freestanding [log] and +/// [get_logger] functions can be used pub async fn with_logger(logger: impl Logger + 'static, fut: F) -> F::Output { LOGGER.scope(Rc::new(logger), fut).await } +/// Obtain an async log writer +/// +/// ``` +/// use orchid_base::log; +/// async { +/// let user = "lorentz"; +/// writeln!(log("info"), "Hello from {user}").await +/// }; +/// ``` pub fn log(category: &str) -> Rc { LOGGER.try_with(|l| l.writer(category)).expect("Logger not set!") } +/// Obtain a reference to the current [Logger]. This is mostly useful for +/// [Logger::is_active]-based optimizations pub fn get_logger() -> Rc { LOGGER.try_with(|l| l.clone()).expect("Logger not set!") } -pub mod test { - use std::fmt::Arguments; - use std::rc::Rc; - - use futures::future::LocalBoxFuture; - - use crate::clone; - use crate::logging::{LogWriter, Logger}; - - #[derive(Clone)] - pub struct TestLogger(Rc LocalBoxFuture<'static, ()>>); - impl LogWriter for TestLogger { - fn write_fmt<'a>(&'a self, fmt: Arguments<'a>) -> LocalBoxFuture<'a, ()> { - (self.0)(fmt.to_string()) - } - } - impl Logger for TestLogger { - fn strat(&self, _category: &str) -> orchid_api::LogStrategy { orchid_api::LogStrategy::Default } - fn writer(&self, _category: &str) -> std::rc::Rc { Rc::new(self.clone()) } - } - impl TestLogger { - pub fn new(f: impl AsyncFn(String) + 'static) -> Self { - let f = Rc::new(f); - Self(Rc::new(move |s| clone!(f; Box::pin(async move { f(s).await })))) - } - } - impl Default for TestLogger { - fn default() -> Self { TestLogger::new(async |s| eprint!("{s}")) } +#[derive(Clone)] +pub struct TestLogger(Rc LocalBoxFuture<'static, ()>>); +impl LogWriter for TestLogger { + fn write_fmt<'a>(&'a self, fmt: Arguments<'a>) -> LocalBoxFuture<'a, ()> { + (self.0)(fmt.to_string()) } } +impl Logger for TestLogger { + fn strat(&self, _category: &str) -> orchid_api::LogStrategy { orchid_api::LogStrategy::Default } + fn writer(&self, _category: &str) -> std::rc::Rc { Rc::new(self.clone()) } +} +impl TestLogger { + pub fn new(f: impl AsyncFn(String) + 'static) -> Self { + let f = Rc::new(f); + Self(Rc::new(move |s| clone!(f; Box::pin(async move { f(s).await })))) + } +} +impl Default for TestLogger { + fn default() -> Self { TestLogger::new(async |s| eprint!("{s}")) } +} diff --git a/orchid-base/src/msg.rs b/orchid-base/src/msg.rs deleted file mode 100644 index 1ed0c09..0000000 --- a/orchid-base/src/msg.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::io; -use std::pin::Pin; - -use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; -use orchid_api_traits::{Decode, Encode}; - -pub async fn send_msg(mut write: Pin<&mut impl AsyncWrite>, msg: &[u8]) -> io::Result<()> { - let mut len_buf = vec![]; - let len_prefix = u32::try_from(msg.len()).expect("Message over 4GB not permitted on channel"); - len_prefix.encode_vec(&mut len_buf); - write.write_all(&len_buf).await?; - write.write_all(msg).await?; - write.flush().await -} - -pub async fn recv_msg(mut read: Pin<&mut impl AsyncRead>) -> io::Result> { - let mut len_buf = [0u8; (u32::BITS / 8) as usize]; - read.read_exact(&mut len_buf).await?; - let len = u32::decode(Pin::new(&mut &len_buf[..])).await?; - let mut msg = vec![0u8; len as usize]; - read.read_exact(&mut msg).await?; - Ok(msg) -} diff --git a/orchid-base/src/name.rs b/orchid-base/src/name.rs index 18c0fbc..3191597 100644 --- a/orchid-base/src/name.rs +++ b/orchid-base/src/name.rs @@ -11,8 +11,7 @@ use futures::future::{OptionFuture, join_all}; use itertools::Itertools; use trait_set::trait_set; -use crate::api; -use crate::interner::{IStr, IStrv, es, ev, is, iv}; +use crate::{IStr, IStrv, api, es, ev, is, iv}; trait_set! { /// Traits that all name iterators should implement @@ -143,7 +142,6 @@ impl VName { } /// Read a `::` separated namespaced name pub async fn parse(s: &str) -> Result { Self::new(VPath::parse(s).await) } - pub async fn literal(s: &'static str) -> Self { Self::parse(s).await.expect("empty literal !?") } /// Obtain an iterator over the segments of the name pub fn iter(&self) -> impl Iterator + '_ { self.0.iter().cloned() } } @@ -213,10 +211,13 @@ impl Sym { pub fn id(&self) -> NonZeroU64 { self.0.to_api().0 } /// Extern the sym for editing pub fn to_vname(&self) -> VName { VName(self[..].to_vec()) } + /// Decode from a message pub async fn from_api(marker: api::TStrv) -> Sym { Self::from_tok(ev(marker).await).expect("Empty sequence found for serialized Sym") } + /// Encode into a message pub fn to_api(&self) -> api::TStrv { self.tok().to_api() } + /// Copy the symbol and extend it with a suffix pub async fn suffix(&self, tokv: impl IntoIterator) -> Sym { Self::new(self.0.iter().cloned().chain(tokv)).await.unwrap() } @@ -246,7 +247,7 @@ impl Deref for Sym { /// An abstraction over tokenized vs non-tokenized names so that they can be /// handled together in datastructures. The names can never be empty -#[allow(clippy::len_without_is_empty)] // never empty +#[allow(clippy::len_without_is_empty, reason = "never empty")] pub trait NameLike: 'static + Clone + Eq + Hash + fmt::Debug + fmt::Display + Borrow<[IStr]> { @@ -292,11 +293,11 @@ impl NameLike for VName {} #[macro_export] macro_rules! sym { ($seg1:tt $( :: $seg:tt)*) => { - $crate::tl_cache!(async $crate::name::Sym : { - $crate::name::Sym::from_tok( - $crate::interner::iv(&[ - $crate::interner::is($crate::sym!(@SEG $seg1)).await - $( , $crate::interner::is($crate::sym!(@SEG $seg)).await )* + $crate::tl_cache!(async $crate::Sym : { + $crate::Sym::from_tok( + $crate::iv(&[ + $crate::is($crate::sym!(@SEG $seg1)).await + $( , $crate::is($crate::sym!(@SEG $seg)).await )* ]) .await ).unwrap() @@ -316,10 +317,10 @@ macro_rules! sym { #[macro_export] macro_rules! vname { ($seg1:tt $( :: $seg:tt)*) => { - $crate::tl_cache!(async $crate::name::VName : { - $crate::name::VName::new([ - $crate::interner::is(stringify!($seg1)).await - $( , $crate::interner::is(stringify!($seg)).await )* + $crate::tl_cache!(async $crate::VName : { + $crate::VName::new([ + $crate::is(stringify!($seg1)).await + $( , $crate::is(stringify!($seg)).await )* ]).unwrap() }) }; @@ -331,28 +332,26 @@ macro_rules! vname { #[macro_export] macro_rules! vpath { ($seg1:tt $( :: $seg:tt)*) => { - $crate::tl_cache!(async $crate::name::VPath : { - $crate::name::VPath(vec![ - $crate::interner::is(stringify!($seg1)).await - $( , $crate::interner::is(stringify!($seg)).await )* + $crate::tl_cache!(async $crate::VPath : { + $crate::VPath::new(vec![ + $crate::is(stringify!($seg1)).await + $( , $crate::is(stringify!($seg)).await )* ]) }) }; () => { - $crate::name::VPath(vec![]) + $crate::VPath::new(vec![]) } } #[cfg(test)] -pub mod test { +mod test { use std::borrow::Borrow; use orchid_api_traits::spin_on; - use super::{NameLike, Sym, VName}; - use crate::interner::local_interner::local_interner; - use crate::interner::{IStr, is, with_interner}; - use crate::name::VPath; + use crate::local_interner::local_interner; + use crate::{IStr, NameLike, Sym, VName, VPath, is, with_interner}; #[test] pub fn recur() { diff --git a/orchid-base/src/nort.rs b/orchid-base/src/nort.rs deleted file mode 100644 index 77e0409..0000000 --- a/orchid-base/src/nort.rs +++ /dev/null @@ -1,283 +0,0 @@ -//! The NORT (Normal Order Referencing Tree) is the interpreter's runtime -//! representation of Orchid programs. -//! -//! It uses a locator tree to find bound variables in lambda functions, which -//! necessitates a normal reduction order because modifying the body by reducing -//! expressions would invalidate any locators in enclosing lambdas. -//! -//! Clauses are held in a mutable `Arc>`, so that after substitution -//! the instances of the argument remain linked and a reduction step applied to -//! any instance transforms all of them. -//! -//! To improve locality and make the tree less deep and locators shorter, -//! function calls store multiple arguments in a deque. - -use std::collections::VecDeque; -use std::fmt; -use std::ops::DerefMut; -use std::sync::{Arc, Mutex, MutexGuard, TryLockError}; - -use itertools::Itertools; - -use super::path_set::PathSet; -use crate::foreign::atom::Atom; -#[allow(unused)] // for doc -use crate::foreign::atom::Atomic; -use crate::foreign::error::{RTErrorObj, RTResult}; -use crate::foreign::try_from_expr::TryFromExpr; -use crate::location::CodeLocation; -use crate::name::Sym; -#[allow(unused)] // for doc -use crate::parse::parsed; -use crate::utils::ddispatch::request; - -/// Kinda like [AsMut] except it supports a guard -pub(crate) trait AsDerefMut { - fn as_deref_mut(&mut self) -> impl DerefMut + '_; -} - -/// An expression with metadata -#[derive(Clone)] -pub struct Expr { - /// The actual value - pub clause: ClauseInst, - /// Information about the code that produced this value - pub location: CodeLocation, -} -impl Expr { - /// Constructor - pub fn new(clause: ClauseInst, location: CodeLocation) -> Self { Self { clause, location } } - /// Obtain the location of the expression - pub fn location(&self) -> CodeLocation { self.location.clone() } - - /// Convert into any type that implements [TryFromExpr]. Calls to this - /// function are generated wherever a conversion is elided in an extern - /// function. - pub fn downcast(self) -> RTResult { - let Expr { mut clause, location } = self; - loop { - let cls_deref = clause.cls_mut(); - match &*cls_deref { - Clause::Identity(alt) => { - let temp = alt.clone(); - drop(cls_deref); - clause = temp; - }, - _ => { - drop(cls_deref); - return T::from_expr(Expr { clause, location }); - }, - }; - } - } - - /// Visit all expressions in the tree. The search can be exited early by - /// returning [Some] - /// - /// See also [parsed::Expr::search_all] - pub fn search_all(&self, predicate: &mut impl FnMut(&Self) -> Option) -> Option { - if let Some(t) = predicate(self) { - return Some(t); - } - self.clause.inspect(|c| match c { - Clause::Identity(_alt) => unreachable!("Handled by inspect"), - Clause::Apply { f, x } => - (f.search_all(predicate)).or_else(|| x.iter().find_map(|x| x.search_all(predicate))), - Clause::Lambda { body, .. } => body.search_all(predicate), - Clause::Constant(_) | Clause::LambdaArg | Clause::Atom(_) | Clause::Bottom(_) => None, - }) - } - - /// Clone the refcounted [ClauseInst] out of the expression - #[must_use] - pub fn clsi(&self) -> ClauseInst { self.clause.clone() } - - /// Read-Write access to the [Clause] - /// - /// # Panics - /// - /// if the clause is already borrowed - pub fn cls_mut(&self) -> MutexGuard<'_, Clause> { self.clause.cls_mut() } -} - -impl fmt::Debug for Expr { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}@{}", self.clause, self.location) - } -} - -impl fmt::Display for Expr { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.clause) } -} - -impl AsDerefMut for Expr { - fn as_deref_mut(&mut self) -> impl DerefMut + '_ { self.clause.cls_mut() } -} - -/// A wrapper around expressions to handle their multiple occurences in -/// the tree together -#[derive(Clone)] -pub struct ClauseInst(pub Arc>); -impl ClauseInst { - /// Wrap a [Clause] in a shared container so that normalization steps are - /// applied to all references - #[must_use] - pub fn new(cls: Clause) -> Self { Self(Arc::new(Mutex::new(cls))) } - - /// Take the [Clause] out of this container if it's the last reference to it, - /// or return self. - pub fn try_unwrap(self) -> Result { - Arc::try_unwrap(self.0).map(|c| c.into_inner().unwrap()).map_err(Self) - } - - /// Read-Write access to the shared clause instance - /// - /// if the clause is already borrowed, this will block until it is released. - pub fn cls_mut(&self) -> MutexGuard<'_, Clause> { self.0.lock().unwrap() } - - /// Call a predicate on the clause, returning whatever the - /// predicate returns. This is a convenience function for reaching - /// through the [Mutex]. The clause will never be [Clause::Identity]. - #[must_use] - pub fn inspect(&self, predicate: impl FnOnce(&Clause) -> T) -> T { - match &*self.cls_mut() { - Clause::Identity(sub) => sub.inspect(predicate), - x => predicate(x), - } - } - - /// If this expression is an [Atomic], request an object of the given type. - /// If it's not an atomic, fail the request automatically. - #[must_use = "your request might not have succeeded"] - pub fn request(&self) -> Option { - match &*self.cls_mut() { - Clause::Atom(a) => request(&*a.0), - Clause::Identity(alt) => alt.request(), - _ => None, - } - } - - /// Associate a location with this clause - pub fn into_expr(self, location: CodeLocation) -> Expr { - Expr { clause: self.clone(), location: location.clone() } - } - /// Check ahead-of-time if this clause contains an atom. Calls - /// [ClauseInst#cls] for read access. - /// - /// Since atoms cannot become normalizable, if this is true and previous - /// normalization failed, the atom is known to be in normal form. - pub fn is_atom(&self) -> bool { matches!(&*self.cls_mut(), Clause::Atom(_)) } - - /// Tries to unwrap the [Arc]. If that fails, clones it field by field. - /// If it's a [Clause::Atom] which cannot be cloned, wraps it in a - /// [Clause::Identity]. - /// - /// Implementation of [crate::foreign::to_clause::ToClause::to_clause]. The - /// trait is more general so it requires a location which this one doesn't. - pub fn into_cls(self) -> Clause { - self.try_unwrap().unwrap_or_else(|clsi| match &*clsi.cls_mut() { - Clause::Apply { f, x } => Clause::Apply { f: f.clone(), x: x.clone() }, - Clause::Atom(_) => Clause::Identity(clsi.clone()), - Clause::Bottom(e) => Clause::Bottom(e.clone()), - Clause::Constant(c) => Clause::Constant(c.clone()), - Clause::Identity(sub) => Clause::Identity(sub.clone()), - Clause::Lambda { args, body } => Clause::Lambda { args: args.clone(), body: body.clone() }, - Clause::LambdaArg => Clause::LambdaArg, - }) - } - - /// Decides if this clause is the exact same instance as another. Most useful - /// to detect potential deadlocks. - pub fn is_same(&self, other: &Self) -> bool { Arc::ptr_eq(&self.0, &other.0) } -} - -impl fmt::Debug for ClauseInst { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0.try_lock() { - Ok(expr) => write!(f, "{expr:?}"), - Err(TryLockError::Poisoned(_)) => write!(f, ""), - Err(TryLockError::WouldBlock) => write!(f, ""), - } - } -} - -impl fmt::Display for ClauseInst { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.0.try_lock() { - Ok(expr) => write!(f, "{expr}"), - Err(TryLockError::Poisoned(_)) => write!(f, ""), - Err(TryLockError::WouldBlock) => write!(f, ""), - } - } -} - -impl AsDerefMut for ClauseInst { - fn as_deref_mut(&mut self) -> impl DerefMut + '_ { self.cls_mut() } -} - -/// Distinct types of expressions recognized by the interpreter -#[derive(Debug)] -pub enum Clause { - /// An expression that causes an error - Bottom(RTErrorObj), - /// Indicates that this [ClauseInst] has the same value as the other - /// [ClauseInst]. This has two benefits; - /// - /// - [Clause] and therefore [Atomic] doesn't have to be [Clone] which saves - /// many synchronization primitives and reference counters in usercode - /// - it enforces on the type level that all copies are normalized together, - /// so accidental inefficiency in the interpreter is rarer. - /// - /// That being said, it's still arbitrary many indirections, so when possible - /// APIs should be usable with a [ClauseInst] directly. - Identity(ClauseInst), - /// An opaque non-callable value, eg. a file handle - Atom(Atom), - /// A function application - Apply { - /// Function to be applied - f: Expr, - /// Argument to be substituted in the function - x: VecDeque, - }, - /// A name to be looked up in the interpreter's symbol table - Constant(Sym), - /// A function - Lambda { - /// A collection of (zero or more) paths to placeholders belonging to this - /// function - args: Option, - /// The tree produced by this function, with placeholders where the - /// argument will go - body: Expr, - }, - /// A placeholder within a function that will be replaced upon application - LambdaArg, -} -impl Clause { - /// Wrap a clause in a refcounted lock - pub fn into_inst(self) -> ClauseInst { ClauseInst::new(self) } - /// Wrap a clause in an expression. - pub fn into_expr(self, location: CodeLocation) -> Expr { self.into_inst().into_expr(location) } -} - -impl fmt::Display for Clause { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Clause::Atom(a) => write!(f, "{a:?}"), - Clause::Bottom(err) => write!(f, "bottom({err})"), - Clause::LambdaArg => write!(f, "arg"), - Clause::Apply { f: fun, x } => write!(f, "({fun} {})", x.iter().join(" ")), - Clause::Lambda { args, body } => match args { - Some(path) => write!(f, "[\\{path}.{body}]"), - None => write!(f, "[\\_.{body}]"), - }, - Clause::Constant(t) => write!(f, "{t}"), - Clause::Identity(other) => write!(f, "{{{other}}}"), - } - } -} - -impl AsDerefMut for Clause { - fn as_deref_mut(&mut self) -> impl DerefMut + '_ { self } -} diff --git a/orchid-base/src/number.rs b/orchid-base/src/number.rs index 88a18ce..ee4d0f1 100644 --- a/orchid-base/src/number.rs +++ b/orchid-base/src/number.rs @@ -3,10 +3,7 @@ use std::ops::Range; use ordered_float::NotNan; -use crate::error::{OrcErrv, mk_errv}; -use crate::interner::is; -use crate::location::SrcRange; -use crate::name::Sym; +use crate::{OrcErrv, SrcRange, Sym, is, mk_errv}; /// A number, either floating point or unsigned int, parsed by Orchid. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/orchid-base/src/old-atom.rs b/orchid-base/src/old-atom.rs deleted file mode 100644 index f3e8ba1..0000000 --- a/orchid-base/src/old-atom.rs +++ /dev/null @@ -1,261 +0,0 @@ -//! Adaptor trait to embed Rust values in Orchid expressions - -use std::any::Any; -use std::fmt; -use std::sync::{Arc, Mutex}; - -use never::Never; - -use super::error::{RTError, RTResult}; -use crate::interpreter::context::{RunEnv, RunParams}; -use crate::interpreter::nort; -use crate::location::{CodeLocation, SourceRange}; -use crate::name::NameLike; -use crate::parse::lexer::Lexeme; -use crate::parse::parsed; -use crate::utils::ddispatch::{request, Request, Responder}; - -/// Information returned by [Atomic::run]. -pub enum AtomicReturn { - /// No work was done. If the atom takes an argument, it can be provided now - Inert(Atom), - /// Work was done, returns new clause and consumed gas. 1 gas is already - /// consumed by the virtual call, so nonzero values indicate expensive - /// operations. - Change(usize, nort::Clause), -} -impl AtomicReturn { - /// Report indicating that the value is inert. The result here is always [Ok], - /// it's meant to match the return type of [Atomic::run] - #[allow(clippy::unnecessary_wraps)] - pub fn inert(this: T) -> Result { Ok(Self::Inert(Atom::new(this))) } -} - -/// Returned by [Atomic::run] -pub type AtomicResult = RTResult; - -/// General error produced when a non-function [Atom] is applied to something as -/// a function. -#[derive(Clone)] -pub struct NotAFunction(pub nort::Expr); -impl RTError for NotAFunction {} -impl fmt::Display for NotAFunction { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?} is not a function", self.0) - } -} - -/// Information about a function call presented to an external function -pub struct CallData<'a, 'b> { - /// Location of the function expression - pub location: CodeLocation, - /// The argument the function was called on. Functions are curried - pub arg: nort::Expr, - /// Globally available information such as the list of all constants - pub env: &'a RunEnv<'b>, - /// Resource limits and other details specific to this interpreter run - pub params: &'a mut RunParams, -} - -/// Information about a normalization run presented to an atom -pub struct RunData<'a, 'b> { - /// Location of the atom - pub location: CodeLocation, - /// Globally available information such as the list of all constants - pub env: &'a RunEnv<'b>, - /// Resource limits and other details specific to this interpreter run - pub params: &'a mut RunParams, -} - -/// Functionality the interpreter needs to handle a value -/// -/// # Lifecycle methods -/// -/// Atomics expose the methods [Atomic::redirect], [Atomic::run], -/// [Atomic::apply] and [Atomic::apply_mut] to interact with the interpreter. -/// The interpreter first tries to call `redirect` to find a subexpression to -/// normalize. If it returns `None` or the subexpression is inert, `run` is -/// called. `run` takes ownership of the value and returns a new one. -/// -/// If `run` indicated in its return value that the result is inert and the atom -/// is in the position of a function, `apply` or `apply_mut` is called depending -/// upon whether the atom is referenced elsewhere. `apply` falls back to -/// `apply_mut` so implementing it is considered an optimization to avoid -/// excessive copying. -/// -/// Atoms don't generally have to be copyable because clauses are refcounted in -/// the interpreter, but Orchid code is always free to duplicate the references -/// and apply them as functions to multiple different arguments so atoms that -/// represent functions have to support application by-ref without consuming the -/// function itself. -pub trait Atomic: Any + fmt::Debug + Responder + Send -where Self: 'static -{ - /// Casts this value to [Any] so that its original value can be salvaged - /// during introspection by other external code. - /// - /// This function should be implemented in exactly one way: - /// - /// ```ignore - /// fn as_any(self: Box) -> Box { self } - /// ``` - #[must_use] - fn as_any(self: Box) -> Box; - /// See [Atomic::as_any], exactly the same but for references - #[must_use] - fn as_any_ref(&self) -> &dyn Any; - /// Print the atom's type name. Should only ever be implemented as - /// - /// ```ignore - /// fn type_name(&self) -> &'static str { std::any::type_name::() } - /// ``` - fn type_name(&self) -> &'static str; - - /// Returns a reference to a possible expression held inside the atom which - /// can be reduced. For an overview of the lifecycle see [Atomic] - fn redirect(&mut self) -> Option<&mut nort::Expr>; - - /// Attempt to normalize this value. If it wraps a value, this should report - /// inert. If it wraps a computation, it should execute one logical step of - /// the computation and return a structure representing the next. - /// - /// For an overview of the lifecycle see [Atomic] - fn run(self: Box, run: RunData) -> AtomicResult; - - /// Combine the function with an argument to produce a new clause. Falls back - /// to [Atomic::apply_mut] by default. - /// - /// For an overview of the lifecycle see [Atomic] - fn apply(mut self: Box, call: CallData) -> RTResult { self.apply_mut(call) } - - /// Combine the function with an argument to produce a new clause - /// - /// For an overview of the lifecycle see [Atomic] - fn apply_mut(&mut self, call: CallData) -> RTResult; - - /// Must return true for atoms parsed from identical source. - /// If the atom cannot be parsed from source, it can safely be ignored - #[allow(unused_variables)] - fn parser_eq(&self, other: &dyn Atomic) -> bool { false } - - /// Wrap the atom in a clause to be placed in an [AtomicResult]. - #[must_use] - fn atom_cls(self) -> nort::Clause - where Self: Sized { - nort::Clause::Atom(Atom(Box::new(self))) - } - - /// Shorthand for `self.atom_cls().to_inst()` - fn atom_clsi(self) -> nort::ClauseInst - where Self: Sized { - self.atom_cls().into_inst() - } - - /// Wrap the atom in a new expression instance to be placed in a tree - #[must_use] - fn atom_expr(self, location: CodeLocation) -> nort::Expr - where Self: Sized { - self.atom_clsi().into_expr(location) - } - - /// Wrap the atom in a clause to be placed in a - /// [crate::parse::parsed::SourceLine]. - #[must_use] - fn ast_cls(self) -> parsed::Clause - where Self: Sized + Clone { - parsed::Clause::Atom(AtomGenerator::cloner(self)) - } - - /// Wrap the atom in an expression to be placed in a - /// [crate::parse::parsed::SourceLine]. - #[must_use] - fn ast_exp(self, range: SourceRange) -> parsed::Expr - where Self: Sized + Clone { - self.ast_cls().into_expr(range) - } - - /// Wrap this atomic value in a lexeme. This means that the atom will - /// participate in macro reproject, so it must implement [Atomic::parser_eq]. - fn lexeme(self) -> Lexeme - where Self: Sized + Clone { - Lexeme::Atom(AtomGenerator::cloner(self)) - } -} - -/// A struct for generating any number of [Atom]s. Since atoms aren't Clone, -/// this represents the ability to create any number of instances of an atom -#[derive(Clone)] -pub struct AtomGenerator(Arc Atom + Send + Sync>); -impl AtomGenerator { - /// Use a factory function to create any number of atoms - pub fn new(f: impl Fn() -> Atom + Send + Sync + 'static) -> Self { Self(Arc::new(f)) } - /// Clone a representative atom when called - pub fn cloner(atom: impl Atomic + Clone) -> Self { - let lock = Mutex::new(atom); - Self::new(move || Atom::new(lock.lock().unwrap().clone())) - } - /// Generate an atom - pub fn run(&self) -> Atom { self.0() } -} -impl fmt::Debug for AtomGenerator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.run()) } -} -impl PartialEq for AtomGenerator { - fn eq(&self, other: &Self) -> bool { self.run().0.parser_eq(&*other.run().0) } -} - -/// Represents a black box unit of data with its own normalization steps. -/// Typically Rust functions integrated with [super::fn_bridge::xfn] will -/// produce and consume [Atom]s to represent both raw data, pending -/// computational tasks, and curried partial calls awaiting their next argument. -pub struct Atom(pub Box); -impl Atom { - /// Wrap an [Atomic] in a type-erased box - #[must_use] - pub fn new(data: T) -> Self { Self(Box::new(data) as Box) } - /// Get the contained data - #[must_use] - pub fn data(&self) -> &dyn Atomic { self.0.as_ref() as &dyn Atomic } - /// Test the type of the contained data without downcasting - #[must_use] - pub fn is(&self) -> bool { self.data().as_any_ref().is::() } - /// Downcast contained data, panic if it isn't the specified type - #[must_use] - pub fn downcast(self) -> T { - *self.0.as_any().downcast().expect("Type mismatch on Atom::cast") - } - /// Normalize the contained data - pub fn run(self, run: RunData<'_, '_>) -> AtomicResult { self.0.run(run) } - /// Request a delegate from the encapsulated data - pub fn request(&self) -> Option { request(self.0.as_ref()) } - /// Downcast the atom to a concrete atomic type, or return the original atom - /// if it is not the specified type - pub fn try_downcast(self) -> Result { - match self.0.as_any_ref().is::() { - true => Ok(*self.0.as_any().downcast().expect("checked just above")), - false => Err(self), - } - } - /// Downcast an atom by reference - pub fn downcast_ref(&self) -> Option<&T> { self.0.as_any_ref().downcast_ref() } - /// Combine the function with an argument to produce a new clause - pub fn apply(self, call: CallData) -> RTResult { self.0.apply(call) } - /// Combine the function with an argument to produce a new clause - pub fn apply_mut(&mut self, call: CallData) -> RTResult { self.0.apply_mut(call) } -} - -impl fmt::Debug for Atom { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.data()) } -} - -impl Responder for Never { - fn respond(&self, _request: Request) { match *self {} } -} -impl Atomic for Never { - fn as_any(self: Box) -> Box { match *self {} } - fn as_any_ref(&self) -> &dyn Any { match *self {} } - fn type_name(&self) -> &'static str { match *self {} } - fn redirect(&mut self) -> Option<&mut nort::Expr> { match *self {} } - fn run(self: Box, _: RunData) -> AtomicResult { match *self {} } - fn apply_mut(&mut self, _: CallData) -> RTResult { match *self {} } -} diff --git a/orchid-base/src/on_drop.rs b/orchid-base/src/on_drop.rs new file mode 100644 index 0000000..846b1fd --- /dev/null +++ b/orchid-base/src/on_drop.rs @@ -0,0 +1,5 @@ +pub struct OnDrop(Option); +impl Drop for OnDrop { + fn drop(&mut self) { (self.0.take().unwrap())() } +} +pub fn on_drop(f: F) -> OnDrop { OnDrop(Some(f)) } diff --git a/orchid-base/src/parse.rs b/orchid-base/src/parse.rs index dac198e..6d8b099 100644 --- a/orchid-base/src/parse.rs +++ b/orchid-base/src/parse.rs @@ -6,17 +6,19 @@ use futures::FutureExt; use futures::future::join_all; use itertools::Itertools; -use crate::api; -use crate::error::{OrcErrv, OrcRes, mk_errv, report}; -use crate::format::{FmtCtx, FmtUnit, Format, fmt}; -use crate::interner::{IStr, es, is}; -use crate::location::SrcRange; -use crate::name::{Sym, VName, VPath}; -use crate::tree::{ExprRepr, ExtraTok, Paren, TokTree, Token, ttv_fmt, ttv_range}; +use crate::{ + ExprRepr, ExtraTok, FmtCtx, FmtUnit, Format, IStr, OrcErrv, OrcRes, Paren, SrcRange, Sym, + TokTree, Token, VName, VPath, api, es, fmt, is, mk_errv, report, ttv_fmt, ttv_range, +}; +/// A character that can appear at the start of a name; `[a-zA-Z_]` pub fn name_start(c: char) -> bool { c.is_alphabetic() || c == '_' } +/// A character that can appear inside a name after the start `[a-zA-Z0-9_]` pub fn name_char(c: char) -> bool { name_start(c) || c.is_numeric() } -pub fn op_char(c: char) -> bool { !name_char(c) && !unrep_space(c) && !"()[]{}\\".contains(c) } +/// A character that can appear in an operator. Anything except +/// `a-zA-Z0-9_()[]{}\` or whitespace +pub fn op_char(c: char) -> bool { !name_char(c) && !c.is_whitespace() && !"()[]{}\\".contains(c) } +/// Any whitespace except a line break pub fn unrep_space(c: char) -> bool { c.is_whitespace() && !"\r\n".contains(c) } /// A cheaply copiable subsection of a document that holds onto context data and @@ -31,7 +33,10 @@ where A: ExprRepr, X: ExtraTok, { + /// Create a snippet from a fallback token for position tracking and a range + /// of tokens pub fn new(prev: &'a TokTree, cur: &'a [TokTree]) -> Self { Self { prev, cur } } + /// Split snippet at index pub fn split_at(self, pos: u32) -> (Self, Self) { let Self { prev, cur } = self; let fst = Self { prev, cur: &cur[..pos as usize] }; @@ -39,23 +44,35 @@ where let snd = Self { prev: new_prev, cur: &self.cur[pos as usize..] }; (fst, snd) } + /// Find the first index that matches a condition pub fn find_idx(self, mut f: impl FnMut(&Token) -> bool) -> Option { self.cur.iter().position(|t| f(&t.tok)).map(|t| t as u32) } + /// Get the n-th token pub fn get(self, idx: u32) -> Option<&'a TokTree> { self.cur.get(idx as usize) } + /// Count how many tokens long the current sequence is. Parenthesized + /// subsequences count as 1 pub fn len(self) -> u32 { self.cur.len() as u32 } + /// The fallback token that can be used for error reporting if this snippet is + /// unexpectedly empty pub fn prev(self) -> &'a TokTree { self.prev } + /// Create a position that describes all tokens in this snippet pub fn sr(self) -> SrcRange { ttv_range(self.cur).unwrap_or_else(|| self.prev.sr.clone()) } - pub fn pop_front(self) -> Option<(&'a TokTree, Self)> { + /// Split the first token + pub fn split_first(self) -> Option<(&'a TokTree, Self)> { self.cur.first().map(|r| (r, self.split_at(1).1)) } - pub fn pop_back(self) -> Option<(Self, &'a TokTree)> { + /// Split the last token + pub fn split_last(self) -> Option<(Self, &'a TokTree)> { self.cur.last().map(|r| (self.split_at(self.len() - 1).0, r)) } + /// Split the snippet at the first token that matches the predicate pub fn split_once(self, f: impl FnMut(&Token) -> bool) -> Option<(Self, Self)> { let idx = self.find_idx(f)?; Some((self.split_at(idx).0, self.split_at(idx + 1).1)) } + /// Split the snippet at each occurrence of a delimiter matched by the + /// predicate pub fn split(mut self, mut f: impl FnMut(&Token) -> bool) -> impl Iterator { iter::from_fn(move || { if self.is_empty() { @@ -66,7 +83,10 @@ where Some(ret) }) } - pub fn is_empty(self) -> bool { self.len() == 0 } + /// Returns true if the snippet contains no tokens. Note that thanks to the + /// fallback, an empty snippet still has a queriable position + pub fn is_empty(self) -> bool { self.cur.is_empty() } + /// Skip tokens that are not meant to be significant inside expressions pub fn skip_fluff(self) -> Self { let non_fluff_start = self.find_idx(|t| !matches!(t, Token::BR | Token::Comment(_))); self.split_at(non_fluff_start.unwrap_or(self.len())).1 @@ -86,6 +106,7 @@ impl Format for Snippet<'_, A, X> { } } +/// A comment as parsed from code #[derive(Clone, Debug)] pub struct Comment { pub text: IStr, @@ -114,6 +135,15 @@ impl fmt::Display for Comment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "--[{}]--", self.text) } } +/// Split a snippet into items by line breaks outside parentheses, unwrap lines +/// that are entirely wrapped in a single set of round parens for multiline +/// items, and associate all comments with the next non-comment line +/// +/// The parse result's [Parsed::output] is the comments, the line's contents are +/// held in [Parsed::tail] so semantically your line parser "continues" for each +/// line separately +/// +/// This is the procedure for module parsing pub async fn line_items<'a, A: ExprRepr, X: ExtraTok>( snip: Snippet<'a, A, X>, ) -> Vec, A, X>> { @@ -141,10 +171,11 @@ pub async fn line_items<'a, A: ExprRepr, X: ExtraTok>( items } +/// Pop the next token that isn't a comment or line break pub async fn try_pop_no_fluff<'a, A: ExprRepr, X: ExtraTok>( snip: Snippet<'a, A, X>, ) -> ParseRes<'a, &'a TokTree, A, X> { - match snip.skip_fluff().pop_front() { + match snip.skip_fluff().split_first() { Some((output, tail)) => Ok(Parsed { output, tail }), None => Err(mk_errv(is("Unexpected end").await, "Line ends abruptly; more tokens were expected", [ @@ -153,6 +184,7 @@ pub async fn try_pop_no_fluff<'a, A: ExprRepr, X: ExtraTok>( } } +/// Fail if the snippet isn't empty pub async fn expect_end(snip: Snippet<'_, impl ExprRepr, impl ExtraTok>) -> OrcRes<()> { match snip.skip_fluff().get(0) { Some(surplus) => Err(mk_errv( @@ -164,6 +196,7 @@ pub async fn expect_end(snip: Snippet<'_, impl ExprRepr, impl ExtraTok>) -> OrcR } } +/// Read a token and ensure that it matches the specified keyword pub async fn expect_tok<'a, A: ExprRepr, X: ExtraTok>( snip: Snippet<'a, A, X>, tok: IStr, @@ -179,6 +212,8 @@ pub async fn expect_tok<'a, A: ExprRepr, X: ExtraTok>( } } +/// Report an error related to a token that can conveniently use the token's +/// text representation in the long message pub async fn token_errv( tok: &TokTree, description: &'static str, @@ -187,17 +222,21 @@ pub async fn token_errv( mk_errv(is(description).await, message(&fmt(tok).await), [tok.sr.pos()]) } +/// Success output of parsers pub struct Parsed<'a, T, H: ExprRepr, X: ExtraTok> { + /// Information obtained from consumed tokens pub output: T, + /// Input to next parser pub tail: Snippet<'a, H, X>, } pub type ParseRes<'a, T, H, X> = OrcRes>; +/// Parse a `namespaced::name` or a `namespaced::(multi name)` pub async fn parse_multiname<'a, A: ExprRepr, X: ExtraTok>( tail: Snippet<'a, A, X>, ) -> ParseRes<'a, Vec, A, X> { - let Some((tt, tail)) = tail.skip_fluff().pop_front() else { + let Some((tt, tail)) = tail.skip_fluff().split_first() else { return Err(mk_errv( is("Expected token").await, "Expected a name, a parenthesized list of names, or a globstar.", @@ -226,7 +265,7 @@ pub async fn parse_multiname<'a, A: ExprRepr, X: ExtraTok>( Token::S(Paren::Round, b) => { let mut o = Vec::new(); let mut body = Snippet::new(tt, b); - while let Some((output, tail)) = body.pop_front() { + while let Some((output, tail)) = body.split_first() { match rec(output).boxed_local().await { Ok(names) => o.extend(names), Err(e) => report(e), diff --git a/orchid-base/src/pure_seq.rs b/orchid-base/src/pure_seq.rs deleted file mode 100644 index 76cb025..0000000 --- a/orchid-base/src/pure_seq.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! Methods to operate on Rust vectors in a declarative manner - -use std::iter; - -/// Pure version of [Vec::push] -/// -/// Create a new vector consisting of the provided vector with the -/// element appended. See [pushed_ref] to use it with a slice -pub fn pushed>(vec: I, t: I::Item) -> C { - vec.into_iter().chain(iter::once(t)).collect() -} - -/// Pure version of [Vec::push] -/// -/// Create a new vector consisting of the provided slice with the -/// element appended. See [pushed] for the owned version -pub fn pushed_ref<'a, T: Clone + 'a, C: FromIterator>( - vec: impl IntoIterator, - t: T, -) -> C { - vec.into_iter().cloned().chain(iter::once(t)).collect() -} - -/// Push an element on the adhoc stack, pass it to the callback, then pop the -/// element out again. -pub fn with_pushed( - vec: &mut Vec, - item: T, - cb: impl for<'a> FnOnce(&'a mut Vec) -> U, -) -> (T, U) { - vec.push(item); - let out = cb(vec); - let item = vec.pop().expect("top element stolen by callback"); - (item, out) -} diff --git a/orchid-base/src/sequence.rs b/orchid-base/src/sequence.rs deleted file mode 100644 index 082316d..0000000 --- a/orchid-base/src/sequence.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! An alternative to `Iterable` in many languages, a [Fn] that returns an -//! iterator. - -use std::rc::Rc; - -use trait_set::trait_set; - -use super::boxed_iter::BoxedIter; - -trait_set! { - trait Payload<'a, T> = Fn() -> BoxedIter<'a, T> + 'a; -} - -/// Dynamic iterator building callback. Given how many trait objects this -/// involves, it may actually be slower than C#. -pub struct Sequence<'a, T: 'a>(Rc>); -impl<'a, T: 'a> Sequence<'a, T> { - /// Construct from a concrete function returning a concrete iterator - pub fn new + 'a>(f: impl Fn() -> I + 'a) -> Self { - Self(Rc::new(move || Box::new(f().into_iter()))) - } - /// Get an iterator from the function - pub fn iter(&self) -> BoxedIter<'_, T> { (self.0)() } -} -impl<'a, T: 'a> Clone for Sequence<'a, T> { - fn clone(&self) -> Self { Self(self.0.clone()) } -} diff --git a/orchid-base/src/side.rs b/orchid-base/src/side.rs index 249f0e5..d238314 100644 --- a/orchid-base/src/side.rs +++ b/orchid-base/src/side.rs @@ -4,7 +4,7 @@ use std::fmt; use std::ops::Not; -use crate::boxed_iter::BoxedIter; +use itertools::Either; /// A primitive for encoding the two sides Left and Right. While booleans /// are technically usable for this purpose, they're very easy to confuse @@ -67,10 +67,13 @@ impl Side { } /// Walk a double ended iterator (assumed to be left-to-right) in this /// direction - pub fn walk<'a, I: DoubleEndedIterator + 'a>(&self, iter: I) -> BoxedIter<'a, I::Item> { + pub fn walk<'a, I: DoubleEndedIterator + 'a>( + &self, + iter: I, + ) -> impl Iterator + 'a { match self { - Side::Right => Box::new(iter) as BoxedIter, - Side::Left => Box::new(iter.rev()), + Side::Right => Either::Right(iter), + Side::Left => Either::Left(iter.rev()), } } } diff --git a/orchid-base/src/tl_cache.rs b/orchid-base/src/tl_cache.rs index 9af0cc1..1bb4556 100644 --- a/orchid-base/src/tl_cache.rs +++ b/orchid-base/src/tl_cache.rs @@ -1,3 +1,18 @@ +/// Cache a value in a [thread_local!]. Supports synchronous and asynchronous +/// initializers +/// +/// ``` +/// #[macro_use] +/// use orchid_base::tl_cache; +/// +/// // simple synchronous case +/// let foo = tl_cache!(Rc>: vec![0; 1024]); +/// async { +/// async fn complex_operation(x: usize) -> usize { x + 1 } +/// // async case +/// let bar = tl_cache!(async usize: complex_operation(0).await) +/// } +/// ``` #[macro_export] macro_rules! tl_cache { ($ty:ty : $expr:expr) => {{ diff --git a/orchid-base/src/tokens.rs b/orchid-base/src/tokens.rs deleted file mode 100644 index 55ce142..0000000 --- a/orchid-base/src/tokens.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub use api::Paren; - -use crate::api; - -pub const PARENS: &[(char, char, Paren)] = - &[('(', ')', Paren::Round), ('[', ']', Paren::Square), ('{', '}', Paren::Curly)]; diff --git a/orchid-base/src/tree.rs b/orchid-base/src/tree.rs index 6056f52..bd1951e 100644 --- a/orchid-base/src/tree.rs +++ b/orchid-base/src/tree.rs @@ -1,4 +1,3 @@ -use std::borrow::Borrow; use std::fmt::{self, Debug, Display}; use std::future::Future; use std::marker::PhantomData; @@ -12,62 +11,67 @@ use never::Never; use orchid_api_traits::Coding; use trait_set::trait_set; -use crate::error::OrcErrv; -use crate::format::{FmtCtx, FmtUnit, Format, Variants}; -use crate::interner::{IStr, es}; -use crate::location::{Pos, SrcRange}; -use crate::name::{Sym, VName, VPath}; -use crate::parse::Snippet; -use crate::{api, match_mapping, tl_cache}; +use crate::{ + FmtCtx, FmtUnit, Format, IStr, OrcErrv, Pos, Snippet, SrcRange, Sym, VName, VPath, Variants, api, + es, match_mapping, tl_cache, +}; +/// The 3 types of parentheses Orchid's lexer recognizes as intrinsic groups in +/// the S-tree +pub type Paren = api::Paren; + +/// Helper table with different kinds of parentheses recognized by the language. +/// opening, closing, variant name +pub const PARENS: &[(char, char, Paren)] = + &[('(', ')', Paren::Round), ('[', ']', Paren::Square), ('{', '}', Paren::Curly)]; + +/// Extension interface for embedded expressions and expression construction +/// commands inside token trees pub trait TokenVariant: Format + Clone + fmt::Debug { + /// Additional arguments to the deserializer. If deserialization of a token + /// type is impossible, set this to a sentinel unit type that describes why. + /// If you set this to [Never], your token tree type can never be + /// deserialized. type FromApiCtx<'a>; + /// Additional arguments to the serializer. If serialization of a token type + /// is forbidden, set this to a sentinel unit type that describes how to avoid + /// it. + /// If you set this to [Never], your token tree type can never be serialized. type ToApiCtx<'a>; + /// Deserializer #[must_use] fn from_api( - api: &ApiEquiv, + api: ApiEquiv, ctx: &mut Self::FromApiCtx<'_>, pos: SrcRange, ) -> impl Future; + /// Serializer #[must_use] fn into_api(self, ctx: &mut Self::ToApiCtx<'_>) -> impl Future; } impl TokenVariant for Never { type FromApiCtx<'a> = (); type ToApiCtx<'a> = (); - async fn from_api(_: &T, _: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { + async fn from_api(_: T, _: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { panic!("Cannot deserialize Never") } async fn into_api(self, _: &mut Self::ToApiCtx<'_>) -> T { match self {} } } trait_set! { - // TokenHandle + /// [api::Token::Handle] variant pub trait ExprRepr = TokenVariant; - // TokenExpr + /// [api::Token::NewExpr] variant pub trait ExtraTok = TokenVariant; } trait_set! { + /// Callback to callback to [recur]. pub trait RecurCB = Fn(TokTree) -> TokTree; } -pub fn recur( - tt: TokTree, - f: &impl Fn(TokTree, &dyn RecurCB) -> TokTree, -) -> TokTree { - f(tt, &|TokTree { sr: range, tok }| { - let tok = match tok { - tok @ (Token::BR | Token::Bottom(_) | Token::Comment(_) | Token::Name(_)) => tok, - tok @ (Token::Handle(_) | Token::NewExpr(_)) => tok, - Token::NS(n, b) => Token::NS(n, Box::new(recur(*b, f))), - Token::LambdaHead(arg) => Token::LambdaHead(Box::new(recur(*arg, f))), - Token::S(p, b) => Token::S(p, b.into_iter().map(|tt| recur(tt, f)).collect_vec()), - }; - TokTree { sr: range, tok } - }) -} - +/// An atom that can be passed through the API boundary as part of an +/// expression. In particular, atoms created by extensions use this form. pub trait AtomRepr: Clone + Format { type Ctx: ?Sized; #[must_use] @@ -93,6 +97,7 @@ impl Display for TokHandle<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Handle({})", self.0.0) } } +/// Lexer output #[derive(Clone, Debug)] pub struct TokTree { pub tok: Token, @@ -102,22 +107,37 @@ pub struct TokTree { pub sr: SrcRange, } impl TokTree { + /// Visit all tokens, modify them at will, and optionally recurse into them by + /// calling the callback passed to your callback + pub fn recur(self, f: &impl Fn(Self, &dyn RecurCB) -> Self) -> Self { + f(self, &|TokTree { sr: range, tok }| { + let tok = match tok { + tok @ (Token::BR | Token::Bottom(_) | Token::Comment(_) | Token::Name(_)) => tok, + tok @ (Token::Handle(_) | Token::NewExpr(_)) => tok, + Token::NS(n, b) => Token::NS(n, Box::new(b.recur(f))), + Token::LambdaHead(arg) => Token::LambdaHead(Box::new(arg.recur(f))), + Token::S(p, b) => Token::S(p, b.into_iter().map(|tt| tt.recur(f)).collect_vec()), + }; + TokTree { sr: range, tok } + }) + } + pub async fn from_api( - tt: &api::TokenTree, + tt: api::TokenTree, hctx: &mut H::FromApiCtx<'_>, xctx: &mut X::FromApiCtx<'_>, src: &Sym, ) -> Self { - let pos = SrcRange::new(tt.range.clone(), src); - let tok = match_mapping!(&tt.token, api::Token => Token:: { + let pos = SrcRange::new(tt.range, src); + let tok = match_mapping!(tt.token, api::Token => Token:: { BR, - NS(n => es(*n).await, - b => Box::new(Self::from_api(b, hctx, xctx, src).boxed_local().await)), + NS(n => es(n).await, + b => Box::new(Self::from_api(*b, hctx, xctx, src).boxed_local().await)), Bottom(e => OrcErrv::from_api(e).await), - LambdaHead(arg => Box::new(Self::from_api(arg, hctx, xctx, src).boxed_local().await)), - Name(n => es(*n).await), - S(*par, b => ttv_from_api(b, hctx, xctx, src).await), - Comment(c => es(*c).await), + LambdaHead(arg => Box::new(Self::from_api(*arg, hctx, xctx, src).boxed_local().await)), + Name(n => es(n).await), + S(par, b => ttv_from_api(b, hctx, xctx, src).await), + Comment(c => es(c).await), NewExpr(expr => X::from_api(expr, xctx, pos.clone()).await), Handle(tk => H::from_api(tk, hctx, pos.clone()).await) }); @@ -186,21 +206,22 @@ impl Format for TokTree { } } +/// Receive a token sequence from API pub async fn ttv_from_api( - tokv: impl IntoIterator>, + tokv: impl IntoIterator, hctx: &mut H::FromApiCtx<'_>, xctx: &mut X::FromApiCtx<'_>, src: &Sym, ) -> Vec> { stream(async |mut cx| { for tok in tokv { - cx.emit(TokTree::::from_api(tok.borrow(), hctx, xctx, src).boxed_local().await).await + cx.emit(TokTree::::from_api(tok, hctx, xctx, src).boxed_local().await).await } }) .collect() .await } - +/// Encode a token sequence for sending pub async fn ttv_into_api( tokv: impl IntoIterator>, hctx: &mut H::ToApiCtx<'_>, @@ -215,6 +236,7 @@ pub async fn ttv_into_api( .await } +/// Enclose the tokens in `()` if there is more than one pub fn wrap_tokv( items: impl IntoIterator>, ) -> TokTree { @@ -229,8 +251,6 @@ pub fn wrap_tokv( } } -pub use api::Paren; - /// Lexer output variant #[derive(Clone, Debug)] pub enum Token { @@ -272,8 +292,10 @@ impl Format for Token { async fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit { match self { Self::BR => "\n".to_string().into(), - Self::Bottom(err) if err.len() == 1 => format!("Bottom({}) ", err.one().unwrap()).into(), - Self::Bottom(err) => format!("Botttom(\n{}) ", indent(&err.to_string())).into(), + Self::Bottom(err) => match err.one() { + Some(err) => format!("Bottom({err}) ").into(), + None => format!("Botttom(\n{}) ", indent(&err.to_string())).into(), + }, Self::Comment(c) => format!("--[{c}]--").into(), Self::LambdaHead(arg) => tl_cache!(Rc: Rc::new(Variants::default().bounded("\\{0b}."))) @@ -295,16 +317,20 @@ impl Format for Token { } } +/// Find the location that best describes a sequence of tokens if the sequence +/// isn't empty pub fn ttv_range<'a>(ttv: &[TokTree]) -> Option { let range = ttv.first()?.sr.range.start..ttv.last().unwrap().sr.range.end; Some(SrcRange { path: ttv.first().unwrap().sr.path(), range }) } +/// Pretty-print a token sequence pub async fn ttv_fmt<'a: 'b, 'b>( ttv: impl IntoIterator>, c: &(impl FmtCtx + ?Sized), ) -> FmtUnit { - FmtUnit::sequence("", " ", "", None, join_all(ttv.into_iter().map(|t| t.print(c))).await) + FmtUnit::sequence("", " ", "", true, join_all(ttv.into_iter().map(|t| t.print(c))).await) } +/// Indent a string by two spaces pub fn indent(s: &str) -> String { s.replace("\n", "\n ") } diff --git a/orchid-extension/Cargo.toml b/orchid-extension/Cargo.toml index af47d84..d239e81 100644 --- a/orchid-extension/Cargo.toml +++ b/orchid-extension/Cargo.toml @@ -6,9 +6,9 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-event = "0.2.1" async-fn-stream = { version = "0.1.0", path = "../async-fn-stream" } async-once-cell = "0.5.4" -bound = "0.6.0" derive_destructure = "1.0.0" dyn-clone = "1.0.20" futures = { version = "0.3.31", default-features = false, features = [ @@ -27,6 +27,7 @@ once_cell = "1.21.3" orchid-api = { version = "0.1.0", path = "../orchid-api" } orchid-api-derive = { version = "0.1.0", path = "../orchid-api-derive" } orchid-api-traits = { version = "0.1.0", path = "../orchid-api-traits" } +orchid-async-utils = { version = "0.1.0", path = "../orchid-async-utils" } orchid-base = { version = "0.1.0", path = "../orchid-base" } ordered-float = "5.1.0" pastey = "0.2.1" diff --git a/orchid-extension/src/atom.rs b/orchid-extension/src/atom.rs index f102bac..2fb5100 100644 --- a/orchid-extension/src/atom.rs +++ b/orchid-extension/src/atom.rs @@ -4,87 +4,82 @@ use std::fmt::{self, Debug}; use std::future::Future; use std::io; use std::marker::PhantomData; -use std::num::NonZeroU32; +use std::num::{NonZero, NonZeroU32, NonZeroU64}; use std::ops::Deref; use std::pin::Pin; use std::rc::Rc; +use std::time::Duration; use dyn_clone::{DynClone, clone_box}; use futures::future::LocalBoxFuture; +use futures::stream::LocalBoxStream; use futures::{AsyncWrite, FutureExt, StreamExt, stream}; use orchid_api_derive::Coding; use orchid_api_traits::{Coding, Decode, InHierarchy, Request, UnderRoot, enc_vec}; -use orchid_base::error::{OrcErrv, OrcRes, mk_errv, mk_errv_floating}; -use orchid_base::format::{FmtCtx, FmtUnit, Format, fmt, take_first}; -use orchid_base::interner::is; -use orchid_base::location::Pos; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqReader, ReqReaderExt}; +use orchid_base::{ + FmtCtx, FmtUnit, Format, IStr, OrcErrv, Pos, Receipt, ReqHandle, ReqReader, ReqReaderExt, Sym, + fmt, is, mk_errv, mk_errv_floating, take_first, +}; use trait_set::trait_set; use crate::api; +use crate::atom_owned::{OwnedAtom, get_obj_store}; use crate::conv::ToExpr; use crate::entrypoint::request; -// use crate::error::{ProjectError, ProjectResult}; use crate::expr::{Expr, ExprData, ExprHandle, ExprKind}; -use crate::gen_expr::GExpr; -use crate::system::{DynSystemCard, atom_by_idx, atom_info_for, cted, downcast_atom}; +use crate::gen_expr::{GExpr, IntoGExprStream}; +use crate::system::{DynSystemCardExt, cted, sys_id}; +/// Every atom managed via this system starts with an ID into the type table #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Coding)] pub struct AtomTypeId(pub NonZeroU32); -pub trait AtomCard: 'static + Sized { - type Data: Clone + Coding + Sized; -} - pub trait AtomicVariant {} + +/// A value managed by Orchid. The type should also be registered in the +/// [crate::SystemCard] through [AtomicFeatures::ops] which is provided +/// indirectly by either [crate::OwnedAtom] or [crate::ThinAtom] pub trait Atomic: 'static + Sized { + /// Either [crate::OwnedVariant] or [crate::ThinVariant] depending on whether + /// the value implements [crate::OwnedAtom] or [crate::ThinAtom] type Variant: AtomicVariant; + /// Serializable data that gets sent inside the atom to other systems that + /// depend on this system. Methods on this value are directly accessible + /// through [TAtom], and this data can also be used for optimized public + /// functions. The serialized form should have a reasonable length to avoid + /// overburdening the protocol. type Data: Clone + Coding + Sized + 'static; /// Register handlers for IPC calls. If this atom implements [Supports], you /// should register your implementations here. If this atom doesn't /// participate in IPC at all, the default implementation is fine - fn reg_reqs() -> MethodSetBuilder { MethodSetBuilder::new() } -} -impl AtomCard for A { - type Data = ::Data; + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new() } } +/// Shared interface of all atom types created in this library for use by the +/// library that defines them. This is provided by [Atomic] and either +/// [crate::OwnedAtom] or [crate::ThinAtom] pub trait AtomicFeatures: Atomic { + /// Convert a value of this atom inside the defining system into a function + /// that will perform registrations and serialization + #[allow(private_interfaces)] fn factory(self) -> AtomFactory; - type Info: AtomDynfo; - fn info() -> Self::Info; - fn dynfo() -> Box; + /// Expose all operations that can be performed on an instance of this type in + /// an instanceless vtable. This vtable must be registered by the + /// [crate::System]. + fn ops() -> Box; } -pub trait ToAtom { - fn to_atom_factory(self) -> AtomFactory; -} -impl ToAtom for A { - fn to_atom_factory(self) -> AtomFactory { self.factory() } -} -impl ToAtom for AtomFactory { - fn to_atom_factory(self) -> AtomFactory { self } -} -pub trait AtomicFeaturesImpl { +pub(crate) trait AtomicFeaturesImpl { fn _factory(self) -> AtomFactory; - type _Info: AtomDynfo; + type _Info: AtomOps; fn _info() -> Self::_Info; } impl> AtomicFeatures for A { + #[allow(private_interfaces)] fn factory(self) -> AtomFactory { self._factory() } - type Info = >::_Info; - fn info() -> Self::Info { Self::_info() } - fn dynfo() -> Box { Box::new(Self::info()) } -} - -pub fn get_info( - sys: &(impl DynSystemCard + ?Sized), -) -> (AtomTypeId, Box) { - atom_info_for(sys, TypeId::of::()).unwrap_or_else(|| { - panic!("Atom {} not associated with system {}", type_name::(), sys.name()) - }) + fn ops() -> Box { Box::new(Self::_info()) } } +/// A reference to a value of some [Atomic] type. This owns an [Expr] #[derive(Clone)] pub struct ForeignAtom { pub(crate) expr: Rc, @@ -92,7 +87,9 @@ pub struct ForeignAtom { pub(crate) pos: Pos, } impl ForeignAtom { + /// Obtain the position in code of the expression pub fn pos(&self) -> Pos { self.pos.clone() } + /// Obtain the [Expr] pub fn ex(self) -> Expr { let (handle, pos) = (self.expr.clone(), self.pos.clone()); let data = ExprData { pos, kind: ExprKind::Atom(ForeignAtom { ..self }) }; @@ -101,10 +98,9 @@ impl ForeignAtom { pub(crate) fn new(handle: Rc, atom: api::Atom, pos: Pos) -> Self { ForeignAtom { atom, expr: handle, pos } } - pub async fn request>( - &self, - r: R, - ) -> Option { + /// Call an IPC method. If the type does not support the given method type, + /// this function returns [None] + pub async fn call>(&self, r: R) -> Option { let rep = (request(api::Fwd( self.atom.clone(), Sym::parse(::Root::NAME).await.unwrap().tok().to_api(), @@ -113,8 +109,33 @@ impl ForeignAtom { .await?; Some(R::Response::decode_slice(&mut &rep[..])) } - pub async fn downcast(self) -> Result, NotTypAtom> { - TAtom::downcast(self.ex().handle()).await + /// Attempt to downcast this value to a concrete atom type + pub fn downcast(self) -> Result, NotTypAtom> { + let mut data = &self.atom.data.0[..]; + let value = AtomTypeId::decode_slice(&mut data); + if cfg!(debug_assertions) { + let cted = cted(); + let own_inst = cted.inst(); + let owner_id = self.atom.owner; + let typ = type_name::(); + let owner = if sys_id() == owner_id { + own_inst.card() + } else { + (cted.deps().find(|s| s.id() == self.atom.owner)) + .ok_or_else(|| NotTypAtom { expr: self.clone().ex(), pos: self.pos(), typ })? + .get_card() + }; + let Some(ops) = owner.ops_by_atid(value) else { + panic!("{value:?} does not refer to an atom in {owner_id:?} when downcasting {typ}"); + }; + if ops.tid() != TypeId::of::() { + panic!( + "{value:?} of {owner_id:?} refers to a type other than {typ}. System version mismatch?" + ) + } + } + let value = A::Data::decode_slice(&mut data); + Ok(TAtom { value, untyped: self }) } } impl fmt::Display for ForeignAtom { @@ -139,13 +160,14 @@ impl ToExpr for ForeignAtom { pub struct NotTypAtom { pub pos: Pos, pub expr: Expr, - pub typ: Box, + pub typ: &'static str, } impl NotTypAtom { + /// Convert to a generic Orchid error pub async fn mk_err(&self) -> OrcErrv { mk_errv( is("Not the expected type").await, - format!("The expression {} is not a {}", fmt(&self.expr).await, self.typ.name()), + format!("The expression {} is not a {}", fmt(&self.expr).await, self.typ), [self.pos.clone()], ) } @@ -155,15 +177,21 @@ impl Debug for NotTypAtom { f.debug_struct("NotTypAtom") .field("pos", &self.pos) .field("expr", &self.expr) - .field("typ.name", &self.typ.name()) + .field("typ", &self.typ) .finish_non_exhaustive() } } +/// An IPC request associated with an atom. This type should either implement +/// [Request] or be the root of a [orchid_api_derive::Hierarchy] the leaves of +/// which implement [Request]. pub trait AtomMethod: Coding + InHierarchy { const NAME: &str; } -pub trait Supports: AtomCard { + +/// A handler for an [AtomMethod] on an [Atomic]. The [AtomMethod] must also be +/// registered in [Atomic::reg_methods] +pub trait Supports: Atomic { fn handle<'a>( &self, hand: Box + '_>, @@ -192,12 +220,17 @@ impl> HandleAtomMethod for AtomMethodHandler { +/// A collection of [Supports] impls for an [Atomic]. If a [Supports] +/// impl is not added to the method set, it will not be recognized. Note that +/// the [Supports] implementors must be registered, which are not necessarily +/// the same as the [Request] implementors +pub struct MethodSetBuilder { handlers: Vec<(&'static str, Rc>)>, } -impl MethodSetBuilder { +impl MethodSetBuilder { pub fn new() -> Self { Self { handlers: vec![] } } + /// Add an [AtomMethod] pub fn handle(mut self) -> Self where A: Supports { assert!(!M::NAME.is_empty(), "AtomMethod::NAME cannoot be empty"); @@ -205,7 +238,7 @@ impl MethodSetBuilder { self } - pub async fn pack(&self) -> MethodSet { + pub(crate) async fn pack(&self) -> MethodSet { MethodSet { handlers: stream::iter(self.handlers.iter()) .then(async |(k, v)| (Sym::parse(k).await.unwrap(), v.clone())) @@ -215,10 +248,10 @@ impl MethodSetBuilder { } } -pub struct MethodSet { +pub(crate) struct MethodSet { handlers: HashMap>>, } -impl MethodSet { +impl MethodSet { pub(crate) async fn dispatch<'a>( &self, atom: &'_ A, @@ -235,29 +268,45 @@ impl MethodSet { } } -impl Default for MethodSetBuilder { +impl Default for MethodSetBuilder { fn default() -> Self { Self::new() } } +/// A handle to a value defined by this or another system. This owns an [Expr] #[derive(Clone)] -pub struct TAtom { +pub struct TAtom { pub untyped: ForeignAtom, pub value: A::Data, } -impl TAtom { +impl TAtom { + /// Obtain the underlying [Expr] pub fn ex(&self) -> Expr { self.untyped.clone().ex() } + /// Obtain the position in code associated with the atom pub fn pos(&self) -> Pos { self.untyped.pos() } + /// Produce from an [ExprHandle] directly pub async fn downcast(expr: Rc) -> Result { match Expr::from_handle(expr).atom().await { Err(expr) => - Err(NotTypAtom { pos: expr.data().await.pos.clone(), expr, typ: Box::new(A::info()) }), - Ok(atm) => match downcast_atom::(atm).await { - Ok(tatom) => Ok(tatom), - Err(fa) => Err(NotTypAtom { pos: fa.pos.clone(), expr: fa.ex(), typ: Box::new(A::info()) }), - }, + Err(NotTypAtom { pos: expr.data().await.pos.clone(), expr, typ: type_name::() }), + Ok(atm) => atm.downcast(), } } - pub async fn request>(&self, req: R) -> R::Response + /// Find the instance associated with a [TAtom] that we own + /// + /// # Panics + /// + /// if we don't actually own this atom + pub async fn own(&self) -> A + where A: OwnedAtom { + let g = get_obj_store().objects.read().await; + let atom_id = self.untyped.atom.drop.expect("Owned atoms always have a drop ID"); + let dyn_atom = + g.get(&atom_id).expect("Atom ID invalid; atom type probably not owned by this crate"); + dyn_atom.as_any_ref().downcast_ref().cloned().expect("The ID should imply a type as well") + } + /// Call an IPC method on the value. Since we know the type, unlike + /// [ForeignAtom::call], we can ensure that the callee recognizes this method + pub async fn call>(&self, req: R) -> R::Response where A: Supports<::Root> { R::Response::decode_slice( &mut &(request(api::Fwd( @@ -283,9 +332,82 @@ impl Format for TAtom { } } -pub struct AtomCtx<'a>(pub &'a [u8], pub Option); +pub(crate) struct AtomCtx<'a>(pub &'a [u8], pub Option); -pub trait AtomDynfo: 'static { +pub enum Next { + ExitSuccess, + Continue(Continuation), +} + +#[derive(Default)] +pub struct Continuation { + immediate: Vec>, + delayed: Vec<(NonZeroU64, LocalBoxStream<'static, GExpr>)>, +} +impl Continuation { + pub fn immediate(mut self, expr: T) -> Self { + self.immediate.push(Box::pin(expr.into_gexpr_stream())); + self + } + pub fn schedule(mut self, delay: Duration, expr: T) -> Self { + let delay = delay.as_millis().try_into().unwrap(); + let Some(nzdelay) = NonZero::new(delay) else { return self.immediate(expr) }; + self.delayed.push((nzdelay, Box::pin(expr.into_gexpr_stream()))); + self + } +} + +impl From for Next { + fn from(value: Continuation) -> Self { Self::Continue(value) } +} + +impl From for CmdResult { + fn from(value: Continuation) -> Self { Ok(Next::Continue(value)) } +} + +pub enum CmdError { + Orc(OrcErrv), + FatalError, +} + +#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct FatalError; +impl From for CmdError { + fn from(FatalError: FatalError) -> Self { Self::FatalError } +} +impl From for CmdError { + fn from(value: OrcErrv) -> Self { Self::Orc(value) } +} + +pub(crate) async fn encode_command_result( + result: Result, +) -> api::OrcResult { + match result { + Ok(Next::ExitSuccess) => Ok(api::NextStep::Exit { success: true }), + Err(CmdError::FatalError) => Ok(api::NextStep::Exit { success: false }), + Err(CmdError::Orc(errv)) => Err(errv.to_api()), + Ok(Next::Continue(Continuation { immediate, delayed })) => Ok(api::NextStep::Continue { + immediate: (stream::iter(immediate).flatten()) + .then(async |x| x.serialize().await) + .collect() + .await, + delayed: stream::iter(delayed.into_iter().map(|(delay, expr)| { + expr.then(move |expr| async move { (delay, expr.serialize().await) }) + })) + .flatten() + .collect() + .await, + }), + } +} + +pub type CmdResult = Result; + +/// A vtable-like type that collects operations defined by an [Atomic] without +/// associating with an instance of that type. This must be registered in +/// [crate::SystemCard] +#[allow(private_interfaces)] +pub trait AtomOps: 'static { fn tid(&self) -> TypeId; fn name(&self) -> &'static str; fn decode<'a>(&'a self, ctx: AtomCtx<'a>) -> LocalBoxFuture<'a, Box>; @@ -298,25 +420,29 @@ pub trait AtomDynfo: 'static { key: Sym, req: Box + 'a>, ) -> LocalBoxFuture<'a, bool>; - fn command<'a>(&'a self, ctx: AtomCtx<'a>) -> LocalBoxFuture<'a, OrcRes>>; + fn command<'a>(&'a self, ctx: AtomCtx<'a>) -> LocalBoxFuture<'a, CmdResult>; fn serialize<'a, 'b: 'a>( &'a self, ctx: AtomCtx<'a>, write: Pin<&'b mut dyn AsyncWrite>, ) -> LocalBoxFuture<'a, Option>>; - fn deserialize<'a>(&'a self, data: &'a [u8], refs: &'a [Expr]) -> LocalBoxFuture<'a, api::Atom>; + fn deserialize<'a>( + &'a self, + data: &'a [u8], + refs: &'a [Expr], + ) -> LocalBoxFuture<'a, api::LocalAtom>; fn drop<'a>(&'a self, ctx: AtomCtx<'a>) -> LocalBoxFuture<'a, ()>; } trait_set! { - pub trait AtomFactoryFn = FnOnce() -> LocalBoxFuture<'static, api::Atom> + DynClone; + pub trait AtomFactoryFn = FnOnce() -> LocalBoxFuture<'static, api::LocalAtom> + DynClone; } -pub struct AtomFactory(Box); +pub(crate) struct AtomFactory(Box); impl AtomFactory { - pub fn new(f: impl AsyncFnOnce() -> api::Atom + Clone + 'static) -> Self { + pub fn new(f: impl AsyncFnOnce() -> api::LocalAtom + Clone + 'static) -> Self { Self(Box::new(|| f().boxed_local())) } - pub async fn build(self) -> api::Atom { (self.0)().await } + pub async fn build(self) -> api::LocalAtom { (self.0)().await } } impl Clone for AtomFactory { fn clone(&self) -> Self { AtomFactory(clone_box(&*self.0)) } @@ -333,6 +459,7 @@ impl Format for AtomFactory { } } +/// Error produced when an atom can not be applied to a value as a function pub async fn err_not_callable(unit: &FmtUnit) -> OrcErrv { mk_errv_floating( is("This atom is not callable").await, @@ -340,6 +467,7 @@ pub async fn err_not_callable(unit: &FmtUnit) -> OrcErrv { ) } +/// Error produced when an atom can not be the final value of the program pub async fn err_not_command(unit: &FmtUnit) -> OrcErrv { mk_errv_floating( is("This atom is not a command").await, @@ -347,11 +475,35 @@ pub async fn err_not_command(unit: &FmtUnit) -> OrcErrv { ) } +pub(crate) async fn err_exit_success_msg() -> IStr { is("Early successful exit").await } +pub(crate) async fn err_exit_failure_msg() -> IStr { is("Early failure exit").await } + +/// Sentinel error returnable from [crate::OwnedAtom::command] or +/// [crate::ThinAtom::command] to indicate that the program should exit with a +/// success +pub async fn err_exit_success() -> OrcErrv { + mk_errv_floating( + err_exit_success_msg().await, + "Sentinel error indicating that the program should exit with a success.", + ) +} + +/// Sentinel error returnable from [crate::OwnedAtom::command] or +/// [crate::ThinAtom::command] to indicate that the program should exit with a +/// failure +pub async fn err_exit_failure() -> OrcErrv { + mk_errv_floating( + err_exit_failure_msg().await, + "Sentinel error indicating that the program should exit with a failure \ + but without raising an error.", + ) +} + /// Read the type ID prefix from an atom, return type information and the rest /// of the data -pub(crate) fn resolve_atom_type(atom: &api::Atom) -> (Box, AtomTypeId, &[u8]) { +pub(crate) fn resolve_atom_type(atom: &api::Atom) -> (Box, AtomTypeId, &[u8]) { let mut data = &atom.data.0[..]; - let tid = AtomTypeId::decode_slice(&mut data); - let atom_record = atom_by_idx(cted().inst().card(), tid).expect("Unrecognized atom type ID"); - (atom_record, tid, data) + let atid = AtomTypeId::decode_slice(&mut data); + let atom_record = cted().inst().card().ops_by_atid(atid).expect("Unrecognized atom type ID"); + (atom_record, atid, data) } diff --git a/orchid-extension/src/atom_owned.rs b/orchid-extension/src/atom_owned.rs index 55555f3..a23b39e 100644 --- a/orchid-extension/src/atom_owned.rs +++ b/orchid-extension/src/atom_owned.rs @@ -17,21 +17,20 @@ use itertools::Itertools; use memo_map::MemoMap; use never::Never; use orchid_api_traits::{Decode, Encode, enc_vec}; -use orchid_base::error::OrcRes; -use orchid_base::format::{FmtCtx, FmtCtxImpl, FmtUnit, Format, take_first}; -use orchid_base::logging::log; -use orchid_base::name::Sym; +use orchid_base::{FmtCtx, FmtCtxImpl, FmtUnit, Format, Sym, log, take_first}; use task_local::task_local; -use crate::api; use crate::atom::{ - AtomCard, AtomCtx, AtomDynfo, AtomFactory, Atomic, AtomicFeaturesImpl, AtomicVariant, MethodSet, - MethodSetBuilder, TAtom, err_not_callable, err_not_command, get_info, + AtomCtx, AtomFactory, AtomOps, Atomic, AtomicFeaturesImpl, AtomicVariant, MethodSet, + MethodSetBuilder, err_not_callable, err_not_command, }; +use crate::conv::ToExpr; use crate::expr::Expr; use crate::gen_expr::{GExpr, bot}; -use crate::system::{cted, sys_id}; +use crate::system::{DynSystemCardExt, cted}; +use crate::{CmdError, CmdResult, api}; +/// Value of [Atomic::Variant] for a type that implements [OwnedAtom] pub struct OwnedVariant; impl AtomicVariant for OwnedVariant {} impl> AtomicFeaturesImpl for A { @@ -43,15 +42,15 @@ impl> AtomicFeaturesImpl(cted().inst().card()); + let (typ_id, _) = cted().inst().card().ops::(); let mut data = enc_vec(&typ_id); self.encode(Pin::<&mut Vec>::new(&mut data)).await; obj_store.objects.read().await.insert(atom_id, Box::new(self)); - api::Atom { drop: Some(atom_id), data: api::AtomData(data), owner: sys_id() } + api::LocalAtom { drop: Some(atom_id), data: api::AtomData(data) } }) } - fn _info() -> Self::_Info { OwnedAtomDynfo { msbuild: A::reg_reqs(), ms: OnceCell::new() } } - type _Info = OwnedAtomDynfo; + fn _info() -> Self::_Info { OwnedAtomOps { msbuild: A::reg_methods(), ms: OnceCell::new() } } + type _Info = OwnedAtomOps; } /// While an atom read guard is held, no atom can be removed. @@ -80,17 +79,15 @@ pub(crate) async fn take_atom(id: api::AtomId) -> Box { g.remove(&id).unwrap_or_else(|| panic!("Received invalid atom ID: {}", id.0)) } -pub struct OwnedAtomDynfo { +pub(crate) struct OwnedAtomOps { msbuild: MethodSetBuilder, ms: OnceCell>, } -impl AtomDynfo for OwnedAtomDynfo { - fn tid(&self) -> TypeId { TypeId::of::() } - fn name(&self) -> &'static str { type_name::() } +impl AtomOps for OwnedAtomOps { + fn tid(&self) -> TypeId { TypeId::of::() } + fn name(&self) -> &'static str { type_name::() } fn decode<'a>(&'a self, AtomCtx(data, ..): AtomCtx<'a>) -> LocalBoxFuture<'a, Box> { - Box::pin(async { - Box::new(::Data::decode_slice(&mut &data[..])) as Box - }) + Box::pin(async { Box::new(::Data::decode_slice(&mut &data[..])) as Box }) } fn call(&self, AtomCtx(_, id): AtomCtx, arg: Expr) -> LocalBoxFuture<'_, GExpr> { Box::pin(async move { @@ -123,7 +120,7 @@ impl AtomDynfo for OwnedAtomDynfo { &'a self, AtomCtx(_, id): AtomCtx<'a>, key: Sym, - req: Box + 'a>, + req: Box + 'a>, ) -> LocalBoxFuture<'a, bool> { Box::pin(async move { let a = AtomReadGuard::new(id.unwrap()).await; @@ -131,10 +128,7 @@ impl AtomDynfo for OwnedAtomDynfo { ms.dispatch(a.as_any_ref().downcast_ref().unwrap(), key, req).await }) } - fn command<'a>( - &'a self, - AtomCtx(_, id): AtomCtx<'a>, - ) -> LocalBoxFuture<'a, OrcRes>> { + fn command<'a>(&'a self, AtomCtx(_, id): AtomCtx<'a>) -> LocalBoxFuture<'a, CmdResult> { Box::pin(async move { take_atom(id.unwrap()).await.dyn_command().await }) } fn drop(&self, AtomCtx(_, id): AtomCtx) -> LocalBoxFuture<'_, ()> { @@ -151,19 +145,34 @@ impl AtomDynfo for OwnedAtomDynfo { AtomReadGuard::new(id).await.dyn_serialize(write).await }) } - fn deserialize<'a>(&'a self, data: &'a [u8], refs: &'a [Expr]) -> LocalBoxFuture<'a, api::Atom> { + fn deserialize<'a>( + &'a self, + data: &'a [u8], + refs: &'a [Expr], + ) -> LocalBoxFuture<'a, api::LocalAtom> { Box::pin(async move { - let refs = T::Refs::from_iter(refs.iter().cloned()); - let obj = T::deserialize(DeserCtxImpl(data), refs).await; + let refs = A::Refs::from_iter(refs.iter().cloned()); + let obj = A::deserialize(DeserCtxImpl(data), refs).await; obj._factory().build().await }) } } +/// Read from the buffer populated by a previous call to [OwnedAtom::serialize] pub trait DeserializeCtx: Sized { + /// Read a value from the head of the buffer fn read(&mut self) -> impl Future; + /// Check if the buffer is empty fn is_empty(&self) -> bool; + /// # Panics + /// + /// if the buffer isn't empty fn assert_empty(&self) { assert!(self.is_empty(), "Bytes found after decoding") } + /// Decode the only value in the buffer + /// + /// # Panics + /// + /// if the buffer has more data after the value was read fn decode(&mut self) -> impl Future { async { let t = self.read().await; @@ -179,6 +188,8 @@ impl DeserializeCtx for DeserCtxImpl<'_> { fn is_empty(&self) -> bool { self.0.is_empty() } } +/// Various collections of expr's that distinguish how many references the type +/// holds. See [OwnedAtom::Refs] for the list of permitted values pub trait RefSet { fn from_iter + ExactSizeIterator>(refs: I) -> Self; fn to_vec(self) -> Vec; @@ -211,10 +222,12 @@ impl RefSet for [Expr; N] { } } -/// Atoms that have a [Drop] +/// Atoms that have a [Drop]. Internal mutability is allowed for optimization +/// purposes, but new references to [Expr] should not be added to avoid +/// reference loops pub trait OwnedAtom: Atomic + Any + Clone + 'static { /// If serializable, the collection that best stores subexpression references - /// for this atom. + /// for this type. /// /// - `()` for no subexppressions, /// - `[Expr; N]` for a static number of subexpressions @@ -222,30 +235,38 @@ pub trait OwnedAtom: Atomic + Any + Clone + 'static { /// - `Never` if not serializable /// /// If this isn't `Never`, you must override the default, panicking - /// `serialize` and `deserialize` implementation + /// [Self::serialize] and [Self::deserialize] implementation type Refs: RefSet; + /// Obtain serializable representation fn val(&self) -> impl Future>; + /// Apply as a function while a different reference to the value exists. #[allow(unused_variables)] - fn call_ref(&self, arg: Expr) -> impl Future { + fn call_ref(&self, arg: Expr) -> impl Future { async move { bot(err_not_callable(&self.dyn_print().await).await) } } - fn call(self, arg: Expr) -> impl Future { + /// Apply as a function and consume + fn call(self, arg: Expr) -> impl Future { async { - let gcl = self.call_ref(arg).await; + let gcl = self.call_ref(arg).await.to_gen().await; self.free().await; gcl } } - #[allow(unused_variables)] - fn command(self) -> impl Future>> { - async move { Err(err_not_command(&self.dyn_print().await).await) } + /// Called when this is the final value of the program + fn command(self) -> impl Future { + async move { Err(CmdError::Orc(err_not_command(&self.dyn_print().await).await)) } } - #[allow(unused_variables)] + /// Drop and perform any cleanup. Unlike Rust's [Drop::drop], this is + /// guaranteed to be called fn free(self) -> impl Future { async {} } + /// Debug-print. This is the final fallback for Orchid's + /// `std::string::to_str`. #[allow(unused_variables)] fn print_atom<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> impl Future { async { format!("OwnedAtom({})", type_name::()).into() } } + /// Serialize this object. If the object is serializable you must override + /// this function, otherwise set [Self::Refs] to [Never]. #[allow(unused_variables)] fn serialize( &self, @@ -254,6 +275,8 @@ pub trait OwnedAtom: Atomic + Any + Clone + 'static { assert_serializable::(); async { panic!("Either implement serialize or set Refs to Never for {}", type_name::()) } } + /// Deserialize this object. If the object is serializable you must override + /// this function, otherwise set [Self::Refs] to [Never] #[allow(unused_variables)] fn deserialize(dctx: impl DeserializeCtx, refs: Self::Refs) -> impl Future { assert_serializable::(); @@ -263,18 +286,18 @@ pub trait OwnedAtom: Atomic + Any + Clone + 'static { } } +/// Debug-assert that the object opted in to serialization fn assert_serializable() { static MSG: &str = "The extension scaffold is broken, Never Refs should prevent serialization"; - assert_ne!(TypeId::of::(), TypeId::of::(), "{MSG}"); + debug_assert_ne!(TypeId::of::(), TypeId::of::(), "{MSG}"); } -pub trait DynOwnedAtom: DynClone + 'static { - fn atom_tid(&self) -> TypeId; +pub(crate) trait DynOwnedAtom: DynClone + 'static { fn as_any_ref(&self) -> &dyn Any; fn encode<'a>(&'a self, buffer: Pin<&'a mut dyn AsyncWrite>) -> LocalBoxFuture<'a, ()>; fn dyn_call_ref(&self, arg: Expr) -> LocalBoxFuture<'_, GExpr>; fn dyn_call(self: Box, arg: Expr) -> LocalBoxFuture<'static, GExpr>; - fn dyn_command(self: Box) -> LocalBoxFuture<'static, OrcRes>>; + fn dyn_command(self: Box) -> LocalBoxFuture<'static, CmdResult>; fn dyn_free(self: Box) -> LocalBoxFuture<'static, ()>; fn dyn_print(&self) -> LocalBoxFuture<'_, FmtUnit>; fn dyn_serialize<'a>( @@ -283,20 +306,17 @@ pub trait DynOwnedAtom: DynClone + 'static { ) -> LocalBoxFuture<'a, Option>>; } impl DynOwnedAtom for T { - fn atom_tid(&self) -> TypeId { TypeId::of::() } fn as_any_ref(&self) -> &dyn Any { self } fn encode<'a>(&'a self, buffer: Pin<&'a mut dyn AsyncWrite>) -> LocalBoxFuture<'a, ()> { async { self.val().await.as_ref().encode(buffer).await.unwrap() }.boxed_local() } fn dyn_call_ref(&self, arg: Expr) -> LocalBoxFuture<'_, GExpr> { - self.call_ref(arg).boxed_local() + async { self.call_ref(arg).await.to_gen().await }.boxed_local() } fn dyn_call(self: Box, arg: Expr) -> LocalBoxFuture<'static, GExpr> { - self.call(arg).boxed_local() - } - fn dyn_command(self: Box) -> LocalBoxFuture<'static, OrcRes>> { - self.command().boxed_local() + async { self.call(arg).await.to_gen().await }.boxed_local() } + fn dyn_command(self: Box) -> LocalBoxFuture<'static, CmdResult> { Box::pin(self.command()) } fn dyn_free(self: Box) -> LocalBoxFuture<'static, ()> { self.free().boxed_local() } fn dyn_print(&self) -> LocalBoxFuture<'_, FmtUnit> { async move { self.print_atom(&FmtCtxImpl::default()).await }.boxed_local() @@ -330,14 +350,8 @@ pub(crate) fn get_obj_store() -> Rc { OBJ_STORE.try_with(|store| store.clone()).expect("Owned atom store not initialized") } -pub async fn own(typ: &TAtom) -> A { - let g = get_obj_store().objects.read().await; - let atom_id = typ.untyped.atom.drop.expect("Owned atoms always have a drop ID"); - let dyn_atom = - g.get(&atom_id).expect("Atom ID invalid; atom type probably not owned by this crate"); - dyn_atom.as_any_ref().downcast_ref().cloned().expect("The ID should imply a type as well") -} - +/// Debug-print the entire object store. Most useful if the interpreter refuses +/// to shut down due to apparent refloops pub async fn debug_print_obj_store(show_atoms: bool) { let store = get_obj_store(); let keys = store.objects.read().await.keys().cloned().collect_vec(); diff --git a/orchid-extension/src/atom_thin.rs b/orchid-extension/src/atom_thin.rs index 921c5e0..a48c69d 100644 --- a/orchid-extension/src/atom_thin.rs +++ b/orchid-extension/src/atom_thin.rs @@ -6,40 +6,38 @@ use async_once_cell::OnceCell; use futures::future::LocalBoxFuture; use futures::{AsyncWrite, FutureExt}; use orchid_api_traits::{Coding, enc_vec}; -use orchid_base::error::OrcRes; -use orchid_base::format::FmtUnit; -use orchid_base::logging::log; -use orchid_base::name::Sym; +use orchid_base::{FmtUnit, Sym, log}; -use crate::api; use crate::atom::{ - AtomCard, AtomCtx, AtomDynfo, AtomFactory, Atomic, AtomicFeaturesImpl, AtomicVariant, MethodSet, - MethodSetBuilder, err_not_callable, err_not_command, get_info, + AtomCtx, AtomFactory, AtomOps, Atomic, AtomicFeaturesImpl, AtomicVariant, MethodSet, + MethodSetBuilder, err_not_callable, err_not_command, }; use crate::expr::Expr; use crate::gen_expr::{GExpr, bot}; -use crate::system::{cted, sys_id}; +use crate::system::{DynSystemCardExt, cted}; +use crate::{CmdResult, api}; +/// Value of [Atomic::Variant] for a type that implements [ThinAtom] pub struct ThinVariant; impl AtomicVariant for ThinVariant {} impl> AtomicFeaturesImpl for A { fn _factory(self) -> AtomFactory { AtomFactory::new(async move || { - let (id, _) = get_info::(cted().inst().card()); + let (id, _) = cted().inst().card().ops::(); let mut buf = enc_vec(&id); self.encode_vec(&mut buf); - api::Atom { drop: None, data: api::AtomData(buf), owner: sys_id() } + api::LocalAtom { drop: None, data: api::AtomData(buf) } }) } - fn _info() -> Self::_Info { ThinAtomDynfo { msbuild: Self::reg_reqs(), ms: OnceCell::new() } } - type _Info = ThinAtomDynfo; + fn _info() -> Self::_Info { ThinAtomOps { msbuild: Self::reg_methods(), ms: OnceCell::new() } } + type _Info = ThinAtomOps; } -pub struct ThinAtomDynfo { +pub(crate) struct ThinAtomOps { msbuild: MethodSetBuilder, ms: OnceCell>, } -impl AtomDynfo for ThinAtomDynfo { +impl AtomOps for ThinAtomOps { fn print<'a>(&self, AtomCtx(buf, _): AtomCtx<'a>) -> LocalBoxFuture<'a, FmtUnit> { Box::pin(async move { T::decode_slice(&mut &buf[..]).print().await }) } @@ -58,17 +56,14 @@ impl AtomDynfo for ThinAtomDynfo { &'a self, AtomCtx(buf, ..): AtomCtx<'a>, key: Sym, - req: Box + 'a>, + req: Box + 'a>, ) -> LocalBoxFuture<'a, bool> { Box::pin(async move { let ms = self.ms.get_or_init(self.msbuild.pack()).await; ms.dispatch(&T::decode_slice(&mut &buf[..]), key, req).await }) } - fn command<'a>( - &'a self, - AtomCtx(buf, _): AtomCtx<'a>, - ) -> LocalBoxFuture<'a, OrcRes>> { + fn command<'a>(&'a self, AtomCtx(buf, _): AtomCtx<'a>) -> LocalBoxFuture<'a, CmdResult> { async move { T::decode_slice(&mut &buf[..]).command().await }.boxed_local() } fn serialize<'a, 'b: 'a>( @@ -81,7 +76,11 @@ impl AtomDynfo for ThinAtomDynfo { Some(Vec::new()) }) } - fn deserialize<'a>(&'a self, data: &'a [u8], refs: &'a [Expr]) -> LocalBoxFuture<'a, api::Atom> { + fn deserialize<'a>( + &'a self, + data: &'a [u8], + refs: &'a [Expr], + ) -> LocalBoxFuture<'a, api::LocalAtom> { assert!(refs.is_empty(), "Refs found when deserializing thin atom"); Box::pin(async { T::decode_slice(&mut &data[..])._factory().build().await }) } @@ -93,16 +92,15 @@ impl AtomDynfo for ThinAtomDynfo { } } -pub trait ThinAtom: - AtomCard + Atomic + Coding + Send + Sync + 'static -{ +/// A simple value that is serializable and does not reference any other values +pub trait ThinAtom: Atomic + Atomic + Coding + 'static { #[allow(unused_variables)] fn call(&self, arg: Expr) -> impl Future { async move { bot(err_not_callable(&self.print().await).await) } } #[allow(unused_variables)] - fn command(&self) -> impl Future>> { - async move { Err(err_not_command(&self.print().await).await) } + fn command(&self) -> impl Future { + async move { Err(err_not_command(&self.print().await).await.into()) } } #[allow(unused_variables)] fn print(&self) -> impl Future { diff --git a/orchid-extension/src/binary.rs b/orchid-extension/src/binary.rs index 786cbf4..ac70346 100644 --- a/orchid-extension/src/binary.rs +++ b/orchid-extension/src/binary.rs @@ -1,7 +1,8 @@ use std::rc::Rc; +use std::time::Duration; use futures::future::LocalBoxFuture; -use orchid_base::binary::future_to_vt; +use orchid_base::future_to_vt; use crate::api; use crate::entrypoint::ExtensionBuilder; @@ -14,19 +15,23 @@ impl Drop for Spawner { fn drop(&mut self) { (self.0.drop)(self.0.data) } } impl Spawner { - pub fn spawn(&self, fut: LocalBoxFuture<'static, ()>) { - (self.0.spawn)(self.0.data, future_to_vt(fut)) + pub fn spawn(&self, delay: Duration, fut: LocalBoxFuture<'static, ()>) { + (self.0.spawn)(self.0.data, delay.as_millis().try_into().unwrap(), future_to_vt(fut)) } } pub fn orchid_extension_main_body(cx: ExtCx, builder: ExtensionBuilder) { - let spawner = Spawner(cx.spawner); - builder.build(ExtPort { - input: Box::pin(cx.input), - output: Box::pin(cx.output), - log: Box::pin(cx.log), - spawn: Rc::new(move |fut| spawner.spawn(fut)), - }); + let spawner = Rc::new(Spawner(cx.spawner)); + let spawner2 = spawner.clone(); + spawner2.spawn( + Duration::ZERO, + Box::pin(builder.run(ExtPort { + input: Box::pin(cx.input), + output: Box::pin(cx.output), + log: Box::pin(cx.log), + spawn: Rc::new(move |delay, fut| spawner.spawn(delay, fut)), + })), + ); } /// Generate entrypoint for the dylib extension loader diff --git a/orchid-extension/src/conv.rs b/orchid-extension/src/conv.rs index 32879d9..f687bef 100644 --- a/orchid-extension/src/conv.rs +++ b/orchid-extension/src/conv.rs @@ -3,16 +3,17 @@ use std::pin::Pin; use dyn_clone::DynClone; use never::Never; -use orchid_base::error::{OrcErrv, OrcRes, mk_errv}; -use orchid_base::format::{Format, fmt}; -use orchid_base::interner::is; -use orchid_base::location::Pos; +use orchid_base::{Format, OrcErrv, OrcRes, Pos, fmt, is, mk_errv}; use trait_set::trait_set; use crate::atom::{AtomicFeatures, ForeignAtom, TAtom}; use crate::expr::{Expr, ExprKind}; use crate::gen_expr::{GExpr, bot}; +/// Attempt to cast a generic Orchid expression reference to a concrete value. +/// Note that this cannot evaluate the expression, and if it is not already +/// evaluated, it will simply fail. Use [crate::ExecHandle::exec] inside +/// [crate::exec] to wait for an expression to be evaluated pub trait TryFromExpr: Sized { fn try_from_expr(expr: Expr) -> impl Future>; } @@ -27,6 +28,8 @@ impl TryFromExpr for (T, U) { } } +/// Error raised when a composite expression was assumed to be an +/// [crate::Atomic], or if the expression was not evaluated yet async fn err_not_atom(pos: Pos, value: &impl Format) -> OrcErrv { mk_errv(is("Expected an atom").await, format!("{} is not an atom", fmt(value).await), [pos]) } @@ -46,21 +49,43 @@ impl TryFromExpr for ForeignAtom { impl TryFromExpr for TAtom { async fn try_from_expr(expr: Expr) -> OrcRes { let f = ForeignAtom::try_from_expr(expr).await?; - match f.clone().downcast::().await { + match f.clone().downcast::() { Ok(a) => Ok(a), Err(e) => Err(e.mk_err().await), } } } +/// Values that are convertible to an Orchid expression. This could mean that +/// the value owns an [Expr] or it may involve more complex operations pub trait ToExpr { + /// Inline the value in an expression returned from a function or included in + /// the const tree returned by [crate::System::env] fn to_gen(self) -> impl Future; + /// Convert the value into a freestanding expression fn to_expr(self) -> impl Future where Self: Sized { async { self.to_gen().await.create().await } } } +pub struct ToExprFuture(pub F); + +impl> ToExpr for ToExprFuture { + async fn to_gen(self) -> GExpr { self.0.await.to_gen().await } + async fn to_expr(self) -> Expr + where Self: Sized { + self.0.await.to_expr().await + } +} +impl Future for ToExprFuture { + type Output = F::Output; + fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll { + unsafe { self.map_unchecked_mut(|this| &mut this.0) }.poll(cx) + } +} + +/// Type-erased [ToExpr] pub trait ToExprDyn { fn to_gen_dyn<'a>(self: Box) -> Pin + 'a>> where Self: 'a; @@ -79,6 +104,8 @@ impl ToExprDyn for T { } } trait_set! { + /// type-erased [ToExpr] and [Clone]. Needed for a value to be + /// included in [crate::System::env] pub trait ClonableToExprDyn = ToExprDyn + DynClone; } impl ToExpr for Box { diff --git a/orchid-extension/src/coroutine_exec.rs b/orchid-extension/src/coroutine_exec.rs index d85cd0b..f0deb5c 100644 --- a/orchid-extension/src/coroutine_exec.rs +++ b/orchid-extension/src/coroutine_exec.rs @@ -7,17 +7,16 @@ use futures::lock::Mutex; use futures::stream::{self, LocalBoxStream}; use futures::{FutureExt, SinkExt, StreamExt}; use never::Never; -use orchid_base::error::OrcRes; +use orchid_base::OrcRes; use crate::atom::Atomic; use crate::atom_owned::{OwnedAtom, OwnedVariant}; use crate::conv::{ToExpr, TryFromExpr}; use crate::expr::Expr; -use crate::gen_expr::{GExpr, arg, call, lambda, new_atom, seq}; +use crate::gen_expr::{GExpr, arg, call, lam, new_atom, seq}; enum Command { Execute(GExpr, Sender), - Register(GExpr, Sender), Halt(GExpr), } @@ -33,12 +32,9 @@ impl BuilderCoroutine { match cmd { None => panic!("Before the stream ends, we should have gotten a Halt"), Some(Command::Halt(expr)) => expr, - Some(Command::Execute(expr, reply)) => call( - lambda(0, [seq([arg(0)], call(new_atom(Replier { reply, builder: self }), [arg(0)]))]), - [expr], - ), - Some(Command::Register(expr, reply)) => - call(new_atom(Replier { reply, builder: self }), [expr]), + Some(Command::Execute(expr, reply)) => + call(lam::<0>(seq(arg(0), call(new_atom(Replier { reply, builder: self }), arg(0)))), expr) + .await, } } } @@ -55,7 +51,7 @@ impl Atomic for Replier { impl OwnedAtom for Replier { type Refs = Never; async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } - async fn call(mut self, arg: Expr) -> GExpr { + async fn call(mut self, arg: Expr) -> impl ToExpr { self.reply.send(arg).await.expect("What the heck"); std::mem::drop(self.reply); self.builder.run().await @@ -81,9 +77,4 @@ impl ExecHandle<'_> { self.0.send(Command::Execute(val.to_gen().await, reply_snd)).await.expect(WEIRD_DROP_ERR); T::try_from_expr(reply_recv.next().await.expect(WEIRD_DROP_ERR)).await } - pub async fn register(&mut self, val: impl ToExpr) -> Expr { - let (reply_snd, mut reply_recv) = channel(1); - self.0.send(Command::Register(val.to_gen().await, reply_snd)).await.expect(WEIRD_DROP_ERR); - reply_recv.next().await.expect(WEIRD_DROP_ERR) - } } diff --git a/orchid-extension/src/entrypoint.rs b/orchid-extension/src/entrypoint.rs index ba5c223..9847abb 100644 --- a/orchid-extension/src/entrypoint.rs +++ b/orchid-extension/src/entrypoint.rs @@ -1,30 +1,27 @@ +use std::any::Any; use std::cell::RefCell; use std::future::Future; +use std::marker::PhantomData; use std::mem; use std::num::NonZero; use std::pin::Pin; use std::rc::Rc; +use std::time::Duration; use futures::future::{LocalBoxFuture, join_all}; use futures::{AsyncWriteExt, StreamExt, stream}; use hashbrown::HashMap; use itertools::Itertools; use orchid_api_traits::{Decode, Encode, Request, UnderRoot, enc_vec}; -use orchid_base::char_filter::{char_filter_match, char_filter_union, mk_char_filter}; -use orchid_base::error::try_with_reporter; -use orchid_base::interner::{es, is, with_interner}; -use orchid_base::logging::{log, with_logger}; -use orchid_base::name::Sym; -use orchid_base::parse::{Comment, Snippet}; -use orchid_base::reqnot::{ - Client, ClientExt, CommCtx, MsgReader, MsgReaderExt, ReqHandleExt, ReqReaderExt, Witness, io_comm, +use orchid_async_utils::{Handle, to_task}; +use orchid_base::{ + Client, ClientExt, CommCtx, Comment, MsgReader, MsgReaderExt, ReqHandleExt, ReqReaderExt, + Snippet, Sym, TokenVariant, Witness, char_filter_match, char_filter_union, es, io_comm, is, log, + mk_char_filter, try_with_reporter, ttv_from_api, with_interner, with_logger, with_stash, }; -use orchid_base::stash::with_stash; -use orchid_base::tree::{TokenVariant, ttv_from_api}; use substack::Substack; use task_local::task_local; -use crate::api; use crate::atom::{AtomCtx, AtomTypeId, resolve_atom_type}; use crate::atom_owned::{take_atom, with_obj_store}; use crate::expr::{BorrowedExprStore, Expr, ExprHandle}; @@ -35,10 +32,11 @@ use crate::lexer::{LexContext, ekey_cascade, ekey_not_applicable}; use crate::logger::LoggerImpl; use crate::parser::{PTokTree, ParsCtx, get_const, linev_into_api, with_parsed_const_ctx}; use crate::reflection::with_refl_roots; -use crate::system::{SysCtx, atom_by_idx, cted, with_sys}; +use crate::system::{DynSystemCardExt, SysCtx, cted, with_sys}; use crate::system_ctor::{CtedObj, DynSystemCtor, SystemCtor}; use crate::tree::{TreeIntoApiCtxImpl, get_lazy, with_lazy_member_store}; use crate::trivial_req::TrivialReqCycle; +use crate::{api, encode_command_result}; task_local::task_local! { static CLIENT: Rc; @@ -100,6 +98,33 @@ impl) + 'static> ContextModifier for F { } } +pub(crate) trait DynTaskHandle: 'static { + fn abort(self: Box); + fn join(self: Box) -> LocalBoxFuture<'static, Box>; +} + +task_local! { + pub(crate) static SPAWN: + Rc>) -> Box + 'static> +} + +pub struct TaskHandle(Box, PhantomData); +impl TaskHandle { + pub fn abort(self) { self.0.abort(); } + pub async fn join(self) -> T { *self.0.join().await.downcast().unwrap() } +} + +pub fn spawn + 'static>(delay: Duration, f: F) -> TaskHandle { + SPAWN.with(|spawn| { + TaskHandle(spawn(delay, Box::pin(async { Box::new(f.await) as Box })), PhantomData) + }) +} + +impl DynTaskHandle for Handle> { + fn abort(self: Box) { Self::abort(&self); } + fn join(self: Box) -> LocalBoxFuture<'static, Box> { Box::pin(Self::join(*self)) } +} + pub struct ExtensionBuilder { pub name: &'static str, pub systems: Vec>, @@ -118,275 +143,272 @@ impl ExtensionBuilder { self.add_context(fun); self } - pub fn build(mut self, mut ctx: ExtPort) { + pub async fn run(mut self, mut ctx: ExtPort) { self.add_context(with_funs_ctx); self.add_context(with_parsed_const_ctx); self.add_context(with_obj_store); self.add_context(with_lazy_member_store); self.add_context(with_refl_roots); - (ctx.spawn)(Box::pin(async move { - let host_header = api::HostHeader::decode(ctx.input.as_mut()).await.unwrap(); - let decls = (self.systems.iter().enumerate()) - .map(|(id, sys)| (u16::try_from(id).expect("more than u16max system ctors"), sys)) - .map(|(id, sys)| sys.decl(api::SysDeclId(NonZero::new(id + 1).unwrap()))) - .collect_vec(); - api::ExtensionHeader { name: self.name.to_string(), systems: decls.clone() } - .encode(ctx.output.as_mut()) - .await - .unwrap(); - ctx.output.as_mut().flush().await.unwrap(); - let logger1 = LoggerImpl::from_api(&host_header.logger); - let logger2 = logger1.clone(); - let (client, comm_ctx, extension_srv) = io_comm(ctx.output, ctx.input); - let extension_fut = extension_srv.listen( - async |n: Box>| { - let notif = n.read().await.unwrap(); - match notif { - api::HostExtNotif::Exit => exit().await, + let spawn = ctx.spawn.clone(); + let host_header = api::HostHeader::decode(ctx.input.as_mut()).await.unwrap(); + let decls = (self.systems.iter().enumerate()) + .map(|(id, sys)| (u16::try_from(id).expect("more than u16max system ctors"), sys)) + .map(|(id, sys)| sys.decl(api::SysDeclId(NonZero::new(id + 1).unwrap()))) + .collect_vec(); + api::ExtensionHeader { name: self.name.to_string(), systems: decls.clone() } + .encode(ctx.output.as_mut()) + .await + .unwrap(); + ctx.output.as_mut().flush().await.unwrap(); + let logger1 = LoggerImpl::from_api(&host_header.logger); + let logger2 = logger1.clone(); + let (client, comm_ctx, extension_srv) = io_comm(ctx.output, ctx.input); + // this future will be ready once the extension cleanly exits + let extension_fut = extension_srv.listen( + async |n: Box>| { + let notif = n.read().await.unwrap(); + match notif { + api::HostExtNotif::Exit => exit().await, + } + Ok(()) + }, + async |mut reader| { + with_stash(async { + let req = reader.read_req().await.unwrap(); + let handle = reader.finish().await; + // Atom printing is never reported because it generates too much + // noise + if !matches!(req, api::HostExtReq::AtomReq(api::AtomReq::AtomPrint(_))) { + writeln!(log("msg"), "{} extension received request {req:?}", self.name).await; } - Ok(()) - }, - async |mut reader| { - with_stash(async { - let req = reader.read_req().await.unwrap(); - let handle = reader.finish().await; - // Atom printing is never reported because it generates too much - // noise - if !matches!(req, api::HostExtReq::AtomReq(api::AtomReq::AtomPrint(_))) { - writeln!(log("msg"), "{} extension received request {req:?}", self.name).await; - } - match req { - api::HostExtReq::SystemDrop(sys_drop) => { - SYSTEM_TABLE.with(|l| l.borrow_mut().remove(&sys_drop.0)); - handle.reply(&sys_drop, &()).await - }, - api::HostExtReq::AtomDrop(atom_drop @ api::AtomDrop(sys_id, atom)) => - with_sys_record(sys_id, async { - take_atom(atom).await.dyn_free().await; - handle.reply(&atom_drop, &()).await - }) - .await, - api::HostExtReq::Ping(ping @ api::Ping) => handle.reply(&ping, &()).await, - api::HostExtReq::Sweep(api::Sweep) => todo!(), - api::HostExtReq::SysReq(api::SysReq::NewSystem(new_sys)) => { - let (ctor_idx, _) = - (decls.iter().enumerate().find(|(_, s)| s.id == new_sys.system)) - .expect("NewSystem call received for invalid system"); - let cted = self.systems[ctor_idx].new_system(&new_sys); - let record = Rc::new(SystemRecord { cted: cted.clone() }); - SYSTEM_TABLE.with(|tbl| { - let mut g = tbl.borrow_mut(); - g.insert(new_sys.id, record); - }); - with_sys_record(new_sys.id, async { - let lex_filter = - cted.inst().dyn_lexers().iter().fold(api::CharFilter(vec![]), |cf, lx| { - char_filter_union(&cf, &mk_char_filter(lx.char_filter().iter().cloned())) - }); - let const_root = stream::iter(cted.inst().dyn_env().await) - .then(async |mem| { - let name = is(&mem.name).await; - let mut tia_ctx = TreeIntoApiCtxImpl { - basepath: &[], - path: Substack::Bottom.push(name.clone()), - }; - (name.to_api(), mem.kind.into_api(&mut tia_ctx).await) - }) - .collect() - .await; - let prelude = - cted.inst().dyn_prelude().await.iter().map(|sym| sym.to_api()).collect(); - let line_types = join_all( - (cted.inst().dyn_parsers().iter()) - .map(async |p| is(p.line_head()).await.to_api()), - ) + match req { + api::HostExtReq::SystemDrop(sys_drop) => { + SYSTEM_TABLE.with(|l| l.borrow_mut().remove(&sys_drop.0)); + handle.reply(&sys_drop, &()).await + }, + api::HostExtReq::AtomDrop(atom_drop @ api::AtomDrop(sys_id, atom)) => + with_sys_record(sys_id, async { + take_atom(atom).await.dyn_free().await; + handle.reply(&atom_drop, &()).await + }) + .await, + api::HostExtReq::Ping(ping @ api::Ping) => handle.reply(&ping, &()).await, + api::HostExtReq::Sweep(api::Sweep) => todo!(), + api::HostExtReq::SysReq(api::SysReq::NewSystem(new_sys)) => { + let (ctor_idx, _) = (decls.iter().enumerate().find(|(_, s)| s.id == new_sys.system)) + .expect("NewSystem call received for invalid system"); + let cted = self.systems[ctor_idx].new_system(&new_sys); + let record = Rc::new(SystemRecord { cted: cted.clone() }); + SYSTEM_TABLE.with(|tbl| { + let mut g = tbl.borrow_mut(); + g.insert(new_sys.id, record); + }); + with_sys_record(new_sys.id, async { + let lex_filter = + cted.inst().dyn_lexers().iter().fold(api::CharFilter(vec![]), |cf, lx| { + char_filter_union(&cf, &mk_char_filter(lx.char_filter().iter().cloned())) + }); + let const_root = stream::iter(cted.inst().dyn_env().await) + .then(async |mem| { + let name = is(&mem.name).await; + let mut tia_ctx = TreeIntoApiCtxImpl { + basepath: &[], + path: Substack::Bottom.push(name.clone()), + }; + (name.to_api(), mem.kind.into_api(&mut tia_ctx).await) + }) + .collect() .await; - let response = - api::NewSystemResponse { lex_filter, const_root, line_types, prelude }; - handle.reply(&new_sys, &response).await - }) - .await - }, - api::HostExtReq::GetMember(get_tree @ api::GetMember(sys_id, tree_id)) => - with_sys_record(sys_id, async { - let (path, tree) = get_lazy(tree_id).await; - let mut tia_ctx = - TreeIntoApiCtxImpl { path: Substack::Bottom, basepath: &path[..] }; - handle.reply(&get_tree, &tree.into_api(&mut tia_ctx).await).await - }) - .await, - api::HostExtReq::SysReq(api::SysReq::SysFwded(fwd)) => { - let fwd_tok = Witness::of(&fwd); - let api::SysFwded(sys_id, payload) = fwd; - with_sys_record(sys_id, async { - let mut reply = Vec::new(); - let req = TrivialReqCycle { req: &payload, rep: &mut reply }; - let _ = cted().inst().dyn_request(Box::new(req)).await; - handle.reply(fwd_tok, &reply).await - }) - .await - }, - api::HostExtReq::LexExpr(lex @ api::LexExpr { sys, src, text, pos, id }) => - with_sys_record(sys, async { - let text = es(text).await; - let src = Sym::from_api(src).await; - let expr_store = BorrowedExprStore::new(); - let tail = &text[pos as usize..]; - let trigger_char = tail.chars().next().unwrap(); - let ekey_na = ekey_not_applicable().await; - let ekey_cascade = ekey_cascade().await; - let lexers = cted().inst().dyn_lexers(); - for lx in - lexers.iter().filter(|l| char_filter_match(l.char_filter(), trigger_char)) - { - let ctx = LexContext::new(&expr_store, &text, id, pos, src.clone()); - match try_with_reporter(lx.lex(tail, &ctx)).await { - Err(e) if e.any(|e| *e == ekey_na) => continue, - Err(e) => { - let eopt = e.keep_only(|e| *e != ekey_cascade).map(|e| Err(e.to_api())); - expr_store.dispose().await; - return handle.reply(&lex, &eopt).await; - }, - Ok((s, expr)) => { - let expr = join_all( - (expr.into_iter()) - .map(|tok| async { tok.into_api(&mut (), &mut ()).await }), - ) - .await; - let pos = (text.len() - s.len()) as u32; - expr_store.dispose().await; - return handle.reply(&lex, &Some(Ok(api::LexedExpr { pos, expr }))).await; + let prelude = + cted.inst().dyn_prelude().await.iter().map(|sym| sym.to_api()).collect(); + let line_types = join_all( + (cted.inst().dyn_parsers().iter()) + .map(async |p| is(p.line_head()).await.to_api()), + ) + .await; + let response = + api::NewSystemResponse { lex_filter, const_root, line_types, prelude }; + handle.reply(&new_sys, &response).await + }) + .await + }, + api::HostExtReq::GetMember(get_tree @ api::GetMember(sys_id, tree_id)) => + with_sys_record(sys_id, async { + let (path, tree) = get_lazy(tree_id).await; + let mut tia_ctx = + TreeIntoApiCtxImpl { path: Substack::Bottom, basepath: &path[..] }; + handle.reply(&get_tree, &tree.into_api(&mut tia_ctx).await).await + }) + .await, + api::HostExtReq::SysReq(api::SysReq::SysFwded(fwd)) => { + let fwd_tok = Witness::of(&fwd); + let api::SysFwded(sys_id, payload) = fwd; + with_sys_record(sys_id, async { + let mut reply = Vec::new(); + let req = TrivialReqCycle { req: &payload, rep: &mut reply }; + let _ = cted().inst().dyn_request(Box::new(req)).await; + handle.reply(fwd_tok, &reply).await + }) + .await + }, + api::HostExtReq::LexExpr(lex @ api::LexExpr { sys, src, text, pos, id }) => + with_sys_record(sys, async { + let text = es(text).await; + let src = Sym::from_api(src).await; + let expr_store = BorrowedExprStore::new(); + let tail = &text[pos as usize..]; + let trigger_char = tail.chars().next().unwrap(); + let ekey_na = ekey_not_applicable().await; + let ekey_cascade = ekey_cascade().await; + let lexers = cted().inst().dyn_lexers(); + for lx in lexers.iter().filter(|l| char_filter_match(l.char_filter(), trigger_char)) + { + let ctx = LexContext::new(&expr_store, &text, id, pos, src.clone()); + match try_with_reporter(lx.lex(tail, &ctx)).await { + Err(e) if e.any(|e| *e == ekey_na) => continue, + Err(e) => { + let eopt = e.keep_only(|e| *e != ekey_cascade).map(|e| Err(e.to_api())); + expr_store.dispose().await; + return handle.reply(&lex, &eopt).await; + }, + Ok((s, expr)) => { + let expr = join_all( + (expr.into_iter()) + .map(|tok| async { tok.into_api(&mut (), &mut ()).await }), + ) + .await; + let pos = (text.len() - s.len()) as u32; + expr_store.dispose().await; + return handle.reply(&lex, &Some(Ok(api::LexedExpr { pos, expr }))).await; + }, + } + } + writeln!(log("warn"), "Got notified about n/a character '{trigger_char}'").await; + expr_store.dispose().await; + handle.reply(&lex, &None).await + }) + .await, + api::HostExtReq::ParseLine(pline) => { + let req = Witness::of(&pline); + let api::ParseLine { module, src, exported, comments, sys, line, idx } = pline; + with_sys_record(sys, async { + let parsers = cted().inst().dyn_parsers(); + let src = Sym::from_api(src).await; + let comments = + join_all(comments.iter().map(|c| Comment::from_api(c, src.clone()))).await; + let expr_store = BorrowedExprStore::new(); + let line: Vec = ttv_from_api(line, &mut &expr_store, &mut (), &src).await; + let snip = Snippet::new(line.first().expect("Empty line"), &line); + let parser = parsers[idx as usize]; + let module = Sym::from_api(module).await; + let pctx = ParsCtx::new(module); + let o_line = + match try_with_reporter(parser.parse(pctx, exported, comments, snip)).await { + Err(e) => Err(e.to_api()), + Ok(t) => Ok(linev_into_api(t).await), + }; + mem::drop(line); + expr_store.dispose().await; + handle.reply(req, &o_line).await + }) + .await + }, + api::HostExtReq::FetchParsedConst(ref fpc @ api::FetchParsedConst(sys, id)) => + with_sys_record(sys, async { + let cnst = get_const(id).await; + handle.reply(fpc, &cnst.serialize().await).await + }) + .await, + api::HostExtReq::AtomReq(atom_req) => { + let atom = atom_req.get_atom(); + with_sys_record(atom.owner, async { + let (nfo, id, buf) = resolve_atom_type(atom); + let actx = AtomCtx(buf, atom.drop); + match &atom_req { + api::AtomReq::SerializeAtom(ser) => { + let mut buf = enc_vec(&id); + match nfo.serialize(actx, Pin::<&mut Vec<_>>::new(&mut buf)).await { + None => handle.reply(ser, &None).await, + Some(refs) => { + let refs = + join_all(refs.into_iter().map(async |ex| ex.into_api(&mut ()).await)) + .await; + handle.reply(ser, &Some((buf, refs))).await }, } - } - writeln!(log("warn"), "Got notified about n/a character '{trigger_char}'").await; - expr_store.dispose().await; - handle.reply(&lex, &None).await - }) - .await, - api::HostExtReq::ParseLine(pline) => { - let api::ParseLine { module, src, exported, comments, sys, line, idx } = &pline; - with_sys_record(*sys, async { - let parsers = cted().inst().dyn_parsers(); - let src = Sym::from_api(*src).await; - let comments = - join_all(comments.iter().map(|c| Comment::from_api(c, src.clone()))).await; - let expr_store = BorrowedExprStore::new(); - let line: Vec = - ttv_from_api(line, &mut &expr_store, &mut (), &src).await; - let snip = Snippet::new(line.first().expect("Empty line"), &line); - let parser = parsers[*idx as usize]; - let module = Sym::from_api(*module).await; - let pctx = ParsCtx::new(module); - let o_line = - match try_with_reporter(parser.parse(pctx, *exported, comments, snip)).await { - Err(e) => Err(e.to_api()), - Ok(t) => Ok(linev_into_api(t).await), - }; - mem::drop(line); - expr_store.dispose().await; - handle.reply(&pline, &o_line).await - }) - .await - }, - api::HostExtReq::FetchParsedConst(ref fpc @ api::FetchParsedConst(sys, id)) => - with_sys_record(sys, async { - let cnst = get_const(id).await; - handle.reply(fpc, &cnst.serialize().await).await - }) - .await, - api::HostExtReq::AtomReq(atom_req) => { - let atom = atom_req.get_atom(); - with_sys_record(atom.owner, async { - let (nfo, id, buf) = resolve_atom_type(atom); - let actx = AtomCtx(buf, atom.drop); - match &atom_req { - api::AtomReq::SerializeAtom(ser) => { - let mut buf = enc_vec(&id); - match nfo.serialize(actx, Pin::<&mut Vec<_>>::new(&mut buf)).await { - None => handle.reply(ser, &None).await, - Some(refs) => { - let refs = - join_all(refs.into_iter().map(async |ex| ex.into_api(&mut ()).await)) - .await; - handle.reply(ser, &Some((buf, refs))).await - }, - } - }, - api::AtomReq::AtomPrint(print @ api::AtomPrint(_)) => - handle.reply(print, &nfo.print(actx).await.to_api()).await, - api::AtomReq::Fwded(fwded) => { - let api::Fwded(_, key, payload) = &fwded; - let mut reply = Vec::new(); - let key = Sym::from_api(*key).await; - let req = TrivialReqCycle { req: payload, rep: &mut reply }; - let some = nfo.handle_req(actx, key, Box::new(req)).await; - handle.reply(fwded, &some.then_some(reply)).await - }, - api::AtomReq::CallRef(call @ api::CallRef(_, arg)) => { - let expr_store = BorrowedExprStore::new(); - let expr_handle = ExprHandle::borrowed(*arg, &expr_store); - let ret = nfo.call_ref(actx, Expr::from_handle(expr_handle.clone())).await; - let api_expr = ret.serialize().await; - mem::drop(expr_handle); - expr_store.dispose().await; - handle.reply(call, &api_expr).await - }, - api::AtomReq::FinalCall(call @ api::FinalCall(_, arg)) => { - let expr_store = BorrowedExprStore::new(); - let expr_handle = ExprHandle::borrowed(*arg, &expr_store); - let ret = nfo.call(actx, Expr::from_handle(expr_handle.clone())).await; - let api_expr = ret.serialize().await; - mem::drop(expr_handle); - expr_store.dispose().await; - handle.reply(call, &api_expr).await - }, - api::AtomReq::Command(cmd @ api::Command(_)) => match nfo.command(actx).await { - Err(e) => handle.reply(cmd, &Err(e.to_api())).await, - Ok(opt) => match opt { - None => handle.reply(cmd, &Ok(api::NextStep::Halt)).await, - Some(cont) => { - let cont = cont.serialize().await; - handle.reply(cmd, &Ok(api::NextStep::Continue(cont))).await - }, - }, - }, - } - }) - .await - }, - api::HostExtReq::DeserAtom(deser) => { - let api::DeserAtom(sys, buf, refs) = &deser; - let read = &mut &buf[..]; - with_sys_record(*sys, async { - // SAFETY: deserialization implicitly grants ownership to previously owned exprs - let refs = (refs.iter()) - .map(|tk| Expr::from_handle(ExprHandle::deserialize(*tk))) - .collect_vec(); - let id = AtomTypeId::decode_slice(read); - let nfo = atom_by_idx(cted().inst().card(), id) - .expect("Deserializing atom with invalid ID"); - handle.reply(&deser, &nfo.deserialize(read, &refs).await).await - }) - .await - }, - } - }) - .await - }, - ); - // add essential services to the very tail, then fold all context into the run - // future - SYSTEM_TABLE - .scope( - RefCell::default(), - with_interner( - new_interner(), - with_logger( - logger2, - with_comm( - Rc::new(client), - comm_ctx, + }, + api::AtomReq::AtomPrint(print @ api::AtomPrint(_)) => + handle.reply(print, &nfo.print(actx).await.to_api()).await, + api::AtomReq::Fwded(fwded) => { + let api::Fwded(_, key, payload) = &fwded; + let mut reply = Vec::new(); + let key = Sym::from_api(*key).await; + let req = TrivialReqCycle { req: payload, rep: &mut reply }; + let some = nfo.handle_req(actx, key, Box::new(req)).await; + handle.reply(fwded, &some.then_some(reply)).await + }, + api::AtomReq::CallRef(call @ api::CallRef(_, arg)) => { + let expr_store = BorrowedExprStore::new(); + let expr_handle = ExprHandle::borrowed(*arg, &expr_store); + let ret = nfo.call_ref(actx, Expr::from_handle(expr_handle.clone())).await; + let api_expr = ret.serialize().await; + mem::drop(expr_handle); + expr_store.dispose().await; + handle.reply(call, &api_expr).await + }, + api::AtomReq::FinalCall(call @ api::FinalCall(_, arg)) => { + let expr_store = BorrowedExprStore::new(); + let expr_handle = ExprHandle::borrowed(*arg, &expr_store); + let ret = nfo.call(actx, Expr::from_handle(expr_handle.clone())).await; + let api_expr = ret.serialize().await; + mem::drop(expr_handle); + expr_store.dispose().await; + handle.reply(call, &api_expr).await + }, + api::AtomReq::Command(cmd @ api::Command(_)) => + handle.reply(cmd, &encode_command_result(nfo.command(actx).await).await).await, + } + }) + .await + }, + api::HostExtReq::DeserAtom(deser) => { + let api::DeserAtom(sys, buf, refs) = &deser; + let read = &mut &buf[..]; + with_sys_record(*sys, async { + // SAFETY: deserialization implicitly grants ownership to previously owned exprs + let refs = (refs.iter()) + .map(|tk| Expr::from_handle(ExprHandle::deserialize(*tk))) + .collect_vec(); + let id = AtomTypeId::decode_slice(read); + let nfo = (cted().inst().card().ops_by_atid(id)) + .expect("Deserializing atom with invalid ID"); + handle.reply(&deser, &nfo.deserialize(read, &refs).await).await + }) + .await + }, + } + }) + .await + }, + ); + // add essential services to the very tail, then fold all context into the run + // future + SYSTEM_TABLE + .scope( + RefCell::default(), + with_interner( + new_interner(), + with_logger( + logger2, + with_comm( + Rc::new(client), + comm_ctx, + SPAWN.scope( + Rc::new(move |delay, fut| { + let (poll, handle) = to_task(fut); + spawn(delay, Box::pin(poll)); + Box::new(handle) + }), (self.context.into_iter()).fold( Box::pin(async { extension_fut.await.unwrap() }) as LocalBoxFuture<()>, |fut, cx| cx.apply(fut), @@ -394,8 +416,8 @@ impl ExtensionBuilder { ), ), ), - ) - .await; - }) as Pin>); + ), + ) + .await; } } diff --git a/orchid-extension/src/error.rs b/orchid-extension/src/error.rs deleted file mode 100644 index afec34a..0000000 --- a/orchid-extension/src/error.rs +++ /dev/null @@ -1,310 +0,0 @@ -use std::any::Any; -use std::borrow::Cow; -use std::cell::RefCell; -use std::sync::{Arc, OnceLock}; -use std::{fmt, iter}; - -use dyn_clone::{clone_box, DynClone}; -use itertools::Itertools; -use orchid_base::boxed_iter::{box_once, BoxedIter}; -use orchid_base::clone; -use orchid_base::error::{ErrPos, OrcError}; -use orchid_base::interner::{deintern, intern}; -use orchid_base::location::{GetSrc, Pos}; -use orchid_base::reqnot::{ReqNot, Requester}; - -use crate::api; - -/// Errors addressed to the developer which are to be resolved with -/// code changes -pub trait ProjectError: Sized + Send + Sync + 'static { - /// A general description of this type of error - const DESCRIPTION: &'static str; - /// A formatted message that includes specific parameters - #[must_use] - fn message(&self) -> String { self.description().to_string() } - /// Code positions relevant to this error. If you don't implement this, you - /// must implement [ProjectError::one_position] - #[must_use] - fn positions(&self) -> impl IntoIterator + '_ { - box_once(ErrPos { position: self.one_position(), message: None }) - } - /// Short way to provide a single origin. If you don't implement this, you - /// must implement [ProjectError::positions] - #[must_use] - fn one_position(&self) -> Pos { - unimplemented!("Error type did not implement either positions or one_position") - } - /// Convert the error into an `Arc` to be able to - /// handle various errors together - #[must_use] - fn pack(self) -> ProjectErrorObj { Arc::new(self) } -} - -/// Object-safe version of [ProjectError]. Implement that instead of this. -pub trait DynProjectError: Send + Sync + 'static { - /// Access type information about this error - #[must_use] - fn as_any_ref(&self) -> &dyn Any; - /// Pack the error into a trait object, or leave it as-is if it's already a - /// trait object - #[must_use] - fn into_packed(self: Arc) -> ProjectErrorObj; - /// A general description of this type of error - #[must_use] - fn description(&self) -> Cow<'_, str>; - /// A formatted message that includes specific parameters - #[must_use] - fn message(&self) -> String { self.description().to_string() } - /// Code positions relevant to this error. - #[must_use] - fn positions(&self) -> BoxedIter<'_, ErrPos>; -} - -impl DynProjectError for T -where T: ProjectError -{ - fn as_any_ref(&self) -> &dyn Any { self } - fn into_packed(self: Arc) -> ProjectErrorObj { self } - fn description(&self) -> Cow<'_, str> { Cow::Borrowed(T::DESCRIPTION) } - fn message(&self) -> String { ProjectError::message(self) } - fn positions(&self) -> BoxedIter { Box::new(ProjectError::positions(self).into_iter()) } -} - -pub fn pretty_print(err: &dyn DynProjectError, get_src: &mut impl GetSrc) -> String { - let description = err.description(); - let message = err.message(); - let positions = err.positions().collect::>(); - let head = format!("Project error: {description}\n{message}"); - if positions.is_empty() { - head + "No origins specified" - } else { - iter::once(head) - .chain(positions.iter().map(|ErrPos { position: origin, message }| match message { - None => format!("@{}", origin.pretty_print(get_src)), - Some(msg) => format!("@{}: {msg}", origin.pretty_print(get_src)), - })) - .join("\n") - } -} - -impl DynProjectError for ProjectErrorObj { - fn as_any_ref(&self) -> &dyn Any { (**self).as_any_ref() } - fn description(&self) -> Cow<'_, str> { (**self).description() } - fn into_packed(self: Arc) -> ProjectErrorObj { (*self).clone() } - fn message(&self) -> String { (**self).message() } - fn positions(&self) -> BoxedIter<'_, ErrPos> { (**self).positions() } -} - -/// Type-erased [ProjectError] implementor through the [DynProjectError] -/// object-trait -pub type ProjectErrorObj = Arc; -/// Alias for a result with an error of [ProjectErrorObj]. This is the type of -/// result most commonly returned by pre-runtime operations. -pub type ProjectResult = Result; - -/// A trait for error types that are only missing an origin. Do not depend on -/// this trait, refer to [DynErrorSansOrigin] instead. -pub trait ErrorSansOrigin: Clone + Sized + Send + Sync + 'static { - /// General description of the error condition - const DESCRIPTION: &'static str; - /// Specific description of the error including code fragments or concrete - /// data if possible - fn message(&self) -> String { Self::DESCRIPTION.to_string() } - /// Convert the error to a type-erased structure for handling on shared - /// channels - fn pack(self) -> ErrorSansOriginObj { Box::new(self) } - /// A shortcut to streamline switching code between [ErrorSansOriginObj] and - /// concrete types - fn bundle(self, origin: &Pos) -> ProjectErrorObj { self.pack().bundle(origin) } -} - -/// Object-safe equivalent to [ErrorSansOrigin]. Implement that one instead of -/// this. Typically found as [ErrorSansOriginObj] -pub trait DynErrorSansOrigin: Any + Send + Sync + DynClone { - /// Allow to downcast the base object to distinguish between various errors. - /// The main intended purpose is to trigger a fallback when [CodeNotFound] is - /// encountered, but the possibilities are not limited to that. - fn as_any_ref(&self) -> &dyn Any; - /// Regularize the type - fn into_packed(self: Box) -> ErrorSansOriginObj; - /// Generic description of the error condition - fn description(&self) -> Cow<'_, str>; - /// Specific description of this particular error - fn message(&self) -> String; - /// Add an origin - fn bundle(self: Box, origin: &Pos) -> ProjectErrorObj; -} - -/// Type-erased [ErrorSansOrigin] implementor through the object-trait -/// [DynErrorSansOrigin]. This can be turned into a [ProjectErrorObj] with -/// [ErrorSansOriginObj::bundle]. -pub type ErrorSansOriginObj = Box; -/// A generic project result without origin -pub type ResultSansOrigin = Result; - -impl DynErrorSansOrigin for T { - fn description(&self) -> Cow<'_, str> { Cow::Borrowed(Self::DESCRIPTION) } - fn message(&self) -> String { (*self).message() } - fn as_any_ref(&self) -> &dyn Any { self } - fn into_packed(self: Box) -> ErrorSansOriginObj { (*self).pack() } - fn bundle(self: Box, origin: &Pos) -> ProjectErrorObj { - Arc::new(OriginBundle(origin.clone(), *self)) - } -} -impl Clone for ErrorSansOriginObj { - fn clone(&self) -> Self { clone_box(&**self) } -} -impl DynErrorSansOrigin for ErrorSansOriginObj { - fn description(&self) -> Cow<'_, str> { (**self).description() } - fn message(&self) -> String { (**self).message() } - fn as_any_ref(&self) -> &dyn Any { (**self).as_any_ref() } - fn into_packed(self: Box) -> ErrorSansOriginObj { *self } - fn bundle(self: Box, origin: &Pos) -> ProjectErrorObj { (*self).bundle(origin) } -} -impl fmt::Display for ErrorSansOriginObj { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - writeln!(f, "{}\nOrigin missing from error", self.message()) - } -} -impl fmt::Debug for ErrorSansOriginObj { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self}") } -} - -struct OriginBundle(Pos, T); -impl DynProjectError for OriginBundle { - fn as_any_ref(&self) -> &dyn Any { self.1.as_any_ref() } - fn into_packed(self: Arc) -> ProjectErrorObj { self } - fn description(&self) -> Cow<'_, str> { self.1.description() } - fn message(&self) -> String { self.1.message() } - fn positions(&self) -> BoxedIter { - box_once(ErrPos { position: self.0.clone(), message: None }) - } -} - -/// A collection for tracking fatal errors without halting. Participating -/// functions return [ProjectResult] even if they only ever construct [Ok]. When -/// they call other participating functions, instead of directly forwarding -/// errors with `?` they should prefer constructing a fallback value with -/// [Reporter::fallback]. If any error is added to a [Reporter] in a function, -/// the return value is valid but its meaning need not be related in any way to -/// the inputs. -/// -/// Returning [Err] from a function that accepts `&mut Reporter` indicates not -/// that there was a fatal error but that it wasn't possible to construct a -/// fallback, so if it can, the caller should construct one. -pub struct Reporter(RefCell>); -impl Reporter { - /// Create a new error reporter - pub fn new() -> Self { Self(RefCell::new(Vec::new())) } - /// Returns true if any errors were regorded. If this ever returns true, it - /// will always return true in the future. - pub fn failing(&self) -> bool { !self.0.borrow().is_empty() } - /// Report a fatal error - pub fn report(&self, error: ProjectErrorObj) { - match error.as_any_ref().downcast_ref::() { - None => self.0.borrow_mut().push(error), - Some(me) => - for err in me.0.iter() { - self.report(err.clone()) - }, - } - } - /// Catch a fatal error, report it, and substitute the value - pub fn fallback(&self, res: ProjectResult, cb: impl FnOnce(ProjectErrorObj) -> T) -> T { - res.inspect_err(|e| self.report(e.clone())).unwrap_or_else(cb) - } - /// Take the errors out of the reporter - #[must_use] - pub fn into_errors(self) -> Option> { - let v = self.0.into_inner(); - if v.is_empty() { None } else { Some(v) } - } - /// Raise an error if the reporter contains any errors - pub fn bind(self) -> ProjectResult<()> { - match self.into_errors() { - None => Ok(()), - Some(v) if v.len() == 1 => Err(v.into_iter().next().unwrap()), - Some(v) => Err(MultiError(v).pack()), - } - } -} - -impl Default for Reporter { - fn default() -> Self { Self::new() } -} - -fn unpack_into(err: impl DynProjectError, res: &mut Vec) { - match err.as_any_ref().downcast_ref::() { - Some(multi) => multi.0.iter().for_each(|e| unpack_into(e.clone(), res)), - None => res.push(Arc::new(err).into_packed()), - } -} - -pub fn unpack_err(err: ProjectErrorObj) -> Vec { - let mut out = Vec::new(); - unpack_into(err, &mut out); - out -} - -pub fn pack_err(iter: impl IntoIterator) -> ProjectErrorObj { - let mut errors = Vec::new(); - iter.into_iter().for_each(|e| unpack_into(e, &mut errors)); - if errors.len() == 1 { errors.into_iter().next().unwrap() } else { MultiError(errors).pack() } -} - -struct MultiError(Vec); -impl ProjectError for MultiError { - const DESCRIPTION: &'static str = "Multiple errors occurred"; - fn message(&self) -> String { format!("{} errors occurred", self.0.len()) } - fn positions(&self) -> impl IntoIterator + '_ { - self.0.iter().flat_map(|e| { - e.positions().map(|pos| { - let emsg = e.message(); - let msg = match pos.message { - None => emsg, - Some(s) if s.is_empty() => emsg, - Some(pmsg) => format!("{emsg}: {pmsg}"), - }; - ErrPos { position: pos.position, message: Some(Arc::new(msg)) } - }) - }) - } -} - -fn err_to_api(err: ProjectErrorObj) -> api::OrcErr { - api::OrcErr { - description: intern(&*err.description()).marker(), - message: Arc::new(err.message()), - locations: err.positions().map(|e| e.to_api()).collect_vec(), - } -} - -struct RelayedError { - pub id: Option, - pub reqnot: ReqNot, - pub details: OnceLock, -} -impl RelayedError { - fn details(&self) -> &OrcError { - let Self { id, reqnot, details: data } = self; - data.get_or_init(clone!(reqnot; move || { - let id = id.expect("Either data or ID must be initialized"); - let projerr = reqnot.request(api::GetErrorDetails(id)); - OrcError { - description: deintern(projerr.description), - message: projerr.message, - positions: projerr.locations.iter().map(ErrPos::from_api).collect_vec(), - } - })) - } -} -impl DynProjectError for RelayedError { - fn description(&self) -> Cow<'_, str> { Cow::Borrowed(self.details().description.as_str()) } - fn message(&self) -> String { self.details().message.to_string() } - fn as_any_ref(&self) -> &dyn std::any::Any { self } - fn into_packed(self: Arc) -> ProjectErrorObj { self } - fn positions(&self) -> BoxedIter<'_, ErrPos> { - Box::new(self.details().positions.iter().cloned()) - } -} diff --git a/orchid-extension/src/expr.rs b/orchid-extension/src/expr.rs index 472da2a..7507a24 100644 --- a/orchid-extension/src/expr.rs +++ b/orchid-extension/src/expr.rs @@ -1,16 +1,13 @@ use std::cell::RefCell; use std::fmt; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; use std::rc::Rc; use std::thread::panicking; use async_once_cell::OnceCell; use derive_destructure::destructure; use hashbrown::HashSet; -use orchid_base::error::OrcErrv; -use orchid_base::format::{FmtCtx, FmtUnit, Format}; -use orchid_base::location::Pos; -use orchid_base::stash::stash; +use orchid_base::{FmtCtx, FmtUnit, Format, OrcErrv, Pos, stash}; use crate::api; use crate::atom::ForeignAtom; @@ -123,7 +120,7 @@ impl Expr { let kind = match details.kind { api::InspectedKind::Atom(a) => ExprKind::Atom(ForeignAtom::new(self.handle.clone(), a, pos.clone())), - api::InspectedKind::Bottom(b) => ExprKind::Bottom(OrcErrv::from_api(&b).await), + api::InspectedKind::Bottom(b) => ExprKind::Bottom(OrcErrv::from_api(b).await), api::InspectedKind::Opaque => ExprKind::Opaque, }; ExprData { pos, kind } @@ -156,6 +153,13 @@ impl Format for Expr { } } } +impl Eq for Expr {} +impl PartialEq for Expr { + fn eq(&self, other: &Self) -> bool { self.handle == other.handle } +} +impl Hash for Expr { + fn hash(&self, state: &mut H) { self.handle.hash(state); } +} #[derive(Clone, Debug)] pub struct ExprData { diff --git a/orchid-extension/src/ext_port.rs b/orchid-extension/src/ext_port.rs index e83fbb4..f079945 100644 --- a/orchid-extension/src/ext_port.rs +++ b/orchid-extension/src/ext_port.rs @@ -1,5 +1,6 @@ use std::pin::Pin; use std::rc::Rc; +use std::time::Duration; use futures::future::LocalBoxFuture; use futures::{AsyncRead, AsyncWrite}; @@ -8,5 +9,5 @@ pub struct ExtPort { pub input: Pin>, pub output: Pin>, pub log: Pin>, - pub spawn: Rc)>, + pub spawn: Rc)>, } diff --git a/orchid-extension/src/func_atom.rs b/orchid-extension/src/func_atom.rs index 9484382..38c2872 100644 --- a/orchid-extension/src/func_atom.rs +++ b/orchid-extension/src/func_atom.rs @@ -11,11 +11,7 @@ use futures::{AsyncWrite, FutureExt}; use itertools::Itertools; use never::Never; use orchid_api_traits::Encode; -use orchid_base::clone; -use orchid_base::error::OrcRes; -use orchid_base::format::{FmtCtx, FmtUnit}; -use orchid_base::location::Pos; -use orchid_base::name::Sym; +use orchid_base::{FmtCtx, FmtUnit, OrcRes, Pos, Sym, clone}; use task_local::task_local; use trait_set::trait_set; @@ -129,7 +125,7 @@ impl Atomic for Fun { impl OwnedAtom for Fun { type Refs = Vec; async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } - async fn call_ref(&self, arg: Expr) -> GExpr { + async fn call_ref(&self, arg: Expr) -> impl ToExpr { let new_args = self.args.iter().cloned().chain([arg]).collect_vec(); if new_args.len() == self.record.argtyps.len() { (self.record.fun)(new_args).await.to_gen().await @@ -137,7 +133,6 @@ impl OwnedAtom for Fun { new_atom(Self { args: new_args, record: self.record.clone(), path: self.path.clone() }) } } - async fn call(self, arg: Expr) -> GExpr { self.call_ref(arg).await } async fn serialize(&self, write: Pin<&mut (impl AsyncWrite + ?Sized)>) -> Self::Refs { self.path.to_api().encode(write).await.unwrap(); self.args.clone() @@ -175,7 +170,7 @@ impl Atomic for Lambda { impl OwnedAtom for Lambda { type Refs = Never; async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } - async fn call_ref(&self, arg: Expr) -> GExpr { + async fn call_ref(&self, arg: Expr) -> impl ToExpr { let new_args = self.args.iter().cloned().chain([arg]).collect_vec(); if new_args.len() == self.record.argtyps.len() { (self.record.fun)(new_args).await.to_gen().await @@ -183,14 +178,13 @@ impl OwnedAtom for Lambda { new_atom(Self { args: new_args, record: self.record.clone() }) } } - async fn call(self, arg: Expr) -> GExpr { self.call_ref(arg).await } } mod expr_func_derives { use std::any::TypeId; use std::sync::OnceLock; - use orchid_base::error::OrcRes; + use orchid_base::OrcRes; use super::{ARGV, ExprFunc}; use crate::conv::{ToExpr, TryFromExpr}; @@ -224,7 +218,7 @@ mod expr_func_derives { expr_func_derive!(A, B, C); expr_func_derive!(A, B, C, D); expr_func_derive!(A, B, C, D, E); - // expr_func_derive!(A, B, C, D, E, F); + expr_func_derive!(A, B, C, D, E, F); // expr_func_derive!(A, B, C, D, E, F, G); // expr_func_derive!(A, B, C, D, E, F, G, H); // expr_func_derive!(A, B, C, D, E, F, G, H, I); diff --git a/orchid-extension/src/gen_expr.rs b/orchid-extension/src/gen_expr.rs index 345a682..3a6c595 100644 --- a/orchid-extension/src/gen_expr.rs +++ b/orchid-extension/src/gen_expr.rs @@ -1,17 +1,18 @@ use std::mem; +use std::pin::{Pin, pin}; use std::rc::Rc; -use futures::FutureExt; -use orchid_base::error::{OrcErr, OrcErrv}; -use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants}; -use orchid_base::location::Pos; -use orchid_base::name::Sym; -use orchid_base::{match_mapping, tl_cache}; +use futures::{FutureExt, Stream, StreamExt, stream}; +use orchid_base::{ + FmtCtx, FmtUnit, Format, OrcErr, OrcErrv, Pos, Sym, Variants, match_mapping, tl_cache, +}; use crate::api; -use crate::atom::{AtomFactory, ToAtom}; +use crate::atom::{AtomFactory, AtomicFeatures}; +use crate::conv::{ToExpr, ToExprFuture}; use crate::entrypoint::request; use crate::expr::Expr; +use crate::system::sys_id; #[derive(Clone, Debug)] pub struct GExpr { @@ -39,7 +40,7 @@ impl GExpr { } pub fn at(self, pos: Pos) -> Self { GExpr { pos, kind: self.kind } } pub async fn create(self) -> Expr { - Expr::deserialize(request(api::Create(self.serialize().await)).await).await + Expr::deserialize(request(api::Create(sys_id(), self.serialize().await)).await).await } } impl Format for GExpr { @@ -55,6 +56,7 @@ pub enum GExprKind { Arg(u64), Seq(Box, Box), Const(Sym), + #[allow(private_interfaces)] NewAtom(AtomFactory), Slot(Expr), Bottom(OrcErrv), @@ -105,27 +107,105 @@ impl Format for GExprKind { fn inherit(kind: GExprKind) -> GExpr { GExpr { pos: Pos::Inherit, kind } } -pub fn sym_ref(path: Sym) -> GExpr { inherit(GExprKind::Const(path)) } -/// Creates an expression from a new atom that we own. -pub fn new_atom(atom: A) -> GExpr { inherit(GExprKind::NewAtom(atom.to_atom_factory())) } - -pub fn seq(deps: impl IntoIterator, val: GExpr) -> GExpr { - fn recur(mut ops: impl Iterator) -> Option { - let op = ops.next()?; - Some(match recur(ops) { - None => op, - Some(rec) => inherit(GExprKind::Seq(Box::new(op), Box::new(rec))), - }) +impl ToExpr for Sym { + async fn to_expr(self) -> Expr + where Self: Sized { + self.to_gen().await.create().await } - recur(deps.into_iter().chain([val])).expect("Empty list provided to seq!") + async fn to_gen(self) -> GExpr { inherit(GExprKind::Const(self)) } +} +/// Creates an expression from a new atom that we own. +pub fn new_atom(atom: A) -> GExpr { inherit(GExprKind::NewAtom(atom.factory())) } + +pub fn seq( + deps: impl IntoGExprStream, + val: impl ToExpr, +) -> ToExprFuture> { + ToExprFuture(async { + async fn recur(mut ops: Pin<&mut impl Stream>) -> Option { + let op = ops.next().await?.to_gen().await; + Some(match recur(ops).boxed_local().await { + None => op, + Some(rec) => inherit(GExprKind::Seq(Box::new(op), Box::new(rec))), + }) + } + recur(pin!(deps.into_gexpr_stream().chain(stream::iter([val.to_gen().await])))) + .await + .expect("Empty list provided to seq!") + }) } pub fn arg(n: u64) -> GExpr { inherit(GExprKind::Arg(n)) } -pub fn lambda(n: u64, [b]: [GExpr; 1]) -> GExpr { inherit(GExprKind::Lambda(n, Box::new(b))) } +pub fn lam(b: impl ToExpr) -> ToExprFuture> { + dyn_lambda(N, b) +} -pub fn call(f: GExpr, argv: impl IntoIterator) -> GExpr { - (argv.into_iter()).fold(f, |f, x| inherit(GExprKind::Call(Box::new(f), Box::new(x)))) +pub fn dyn_lambda(n: u64, b: impl ToExpr) -> ToExprFuture> { + ToExprFuture(async move { inherit(GExprKind::Lambda(n, Box::new(b.to_gen().await))) }) +} + +pub trait IntoGExprStream { + fn into_gexpr_stream(self) -> impl Stream; +} +impl IntoGExprStream for T { + fn into_gexpr_stream(self) -> impl Stream { (self,).into_gexpr_stream() } +} +impl IntoGExprStream for Vec { + fn into_gexpr_stream(self) -> impl Stream { stream::iter(self) } +} + +mod tuple_impls { + use futures::{Stream, StreamExt, stream}; + + use super::IntoGExprStream; + use crate::conv::ToExpr; + use crate::gen_expr::GExpr; + + macro_rules! tuple_impl { + ($($T:ident)*) => { + pastey::paste!{ + impl<$($T: ToExpr),*> IntoGExprStream for ($($T,)*) { + fn into_gexpr_stream(self) -> impl Stream { + let ($([< $T:snake >],)*) = self; + stream::once(async { stream::iter([$([< $T:snake >].to_gen().await,)*]) }).flatten() + } + } + } + }; + } + + tuple_impl!(); + tuple_impl!(A); + tuple_impl!(A B); + tuple_impl!(A B C); + tuple_impl!(A B C D); + tuple_impl!(A B C D E); + tuple_impl!(A B C D E F); +} + +pub fn call( + f: impl ToExpr, + argv: impl IntoGExprStream, +) -> ToExprFuture> { + ToExprFuture(async { + (argv.into_gexpr_stream()) + .fold(f.to_gen().await, async |f, x| inherit(GExprKind::Call(Box::new(f), Box::new(x)))) + .await + }) +} + +pub fn call_v( + f: impl ToExpr, + argv: impl IntoIterator, +) -> ToExprFuture> { + ToExprFuture(async { + stream::iter(argv) + .fold(f.to_gen().await, async |f, x| { + inherit(GExprKind::Call(Box::new(f), Box::new(x.to_gen().await))) + }) + .await + }) } pub fn bot(ev: impl IntoIterator) -> GExpr { diff --git a/orchid-extension/src/interner.rs b/orchid-extension/src/interner.rs index 9e76ca1..22934a6 100644 --- a/orchid-extension/src/interner.rs +++ b/orchid-extension/src/interner.rs @@ -2,8 +2,8 @@ use std::rc::Rc; use futures::future::{LocalBoxFuture, join_all, ready}; use itertools::Itertools; -use orchid_base::interner::local_interner::{Int, StrBranch, StrvBranch}; -use orchid_base::interner::{IStr, IStrv, InternerSrv}; +use orchid_base::local_interner::{Int, StrBranch, StrvBranch}; +use orchid_base::{IStr, IStrv, InternerSrv}; use crate::api; use crate::entrypoint::{MUTE_REPLY, request}; diff --git a/orchid-extension/src/lexer.rs b/orchid-extension/src/lexer.rs index 7633a40..2bbd3c0 100644 --- a/orchid-extension/src/lexer.rs +++ b/orchid-extension/src/lexer.rs @@ -5,10 +5,7 @@ use std::ops::RangeInclusive; use futures::FutureExt; use futures::future::LocalBoxFuture; -use orchid_base::error::{OrcErrv, OrcRes, mk_errv}; -use orchid_base::interner::{IStr, is}; -use orchid_base::location::{Pos, SrcRange}; -use orchid_base::name::Sym; +use orchid_base::{IStr, OrcErrv, OrcRes, Pos, SrcRange, Sym, is, mk_errv}; use crate::api; use crate::entrypoint::request; @@ -50,7 +47,7 @@ impl<'a> LexContext<'a> { } pub fn src(&self) -> &Sym { &self.src } /// This function returns [PTokTree] because it can never return - /// [orchid_base::tree::Token::NewExpr]. You can use + /// [orchid_base::Token::NewExpr]. You can use /// [crate::parser::p_tree2gen] to convert this to [crate::tree::GenTokTree] /// for embedding in the return value. pub async fn recurse(&self, tail: &'a str) -> OrcRes<(&'a str, PTokTree)> { @@ -58,7 +55,7 @@ impl<'a> LexContext<'a> { let Some(lx) = request(api::SubLex { pos: start, id: self.id }).await else { return Err(err_cascade().await); }; - let tree = PTokTree::from_api(&lx.tree, &mut { self.exprs }, &mut (), &self.src).await; + let tree = PTokTree::from_api(lx.tree, &mut { self.exprs }, &mut (), &self.src).await; Ok((&self.text[lx.pos as usize..], tree)) } diff --git a/orchid-extension/src/lib.rs b/orchid-extension/src/lib.rs index f9586eb..f5f4694 100644 --- a/orchid-extension/src/lib.rs +++ b/orchid-extension/src/lib.rs @@ -1,8 +1,12 @@ +#![allow(refining_impl_trait, reason = "Has various false-positives around lints")] use orchid_api as api; -pub mod atom; -pub mod atom_owned; -pub mod atom_thin; +mod atom; +pub use atom::*; +mod atom_owned; +pub use atom_owned::*; +mod atom_thin; +pub use atom_thin::*; pub mod binary; pub mod conv; pub mod coroutine_exec; diff --git a/orchid-extension/src/logger.rs b/orchid-extension/src/logger.rs index ee8cc5b..ed0ab9a 100644 --- a/orchid-extension/src/logger.rs +++ b/orchid-extension/src/logger.rs @@ -5,8 +5,7 @@ use std::rc::Rc; use futures::future::LocalBoxFuture; use hashbrown::HashMap; -use orchid_base::interner::is; -use orchid_base::logging::{LogWriter, Logger}; +use orchid_base::{LogWriter, Logger, is}; use crate::api; use crate::entrypoint::notify; diff --git a/orchid-extension/src/msg.rs b/orchid-extension/src/msg.rs deleted file mode 100644 index 4c01e1b..0000000 --- a/orchid-extension/src/msg.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::pin::pin; - -use async_once_cell::OnceCell; -use futures::lock::Mutex; -use orchid_base::msg::{recv_msg, send_msg}; - -pub async fn send_parent_msg(msg: &[u8]) -> io::Result<()> { - let stdout_lk = STDOUT.get_or_init(async { Mutex::new(io::stdout()) }).await; - let mut stdout_g = stdout_lk.lock().await; - send_msg(pin!(&mut *stdout_g), msg).await -} -pub async fn recv_parent_msg() -> io::Result> { recv_msg(pin!(io::stdin())).await } diff --git a/orchid-extension/src/parser.rs b/orchid-extension/src/parser.rs index fbefe52..d92f1a2 100644 --- a/orchid-extension/src/parser.rs +++ b/orchid-extension/src/parser.rs @@ -1,18 +1,17 @@ +use std::cell::RefCell; use std::marker::PhantomData; +use std::num::NonZero; use async_fn_stream::stream; use futures::future::{LocalBoxFuture, join_all}; use futures::{FutureExt, Stream, StreamExt}; +use hashbrown::HashMap; use itertools::Itertools; use never::Never; -use orchid_base::error::{OrcErrv, OrcRes}; -use orchid_base::id_store::IdStore; -use orchid_base::interner::IStr; -use orchid_base::location::SrcRange; -use orchid_base::match_mapping; -use orchid_base::name::Sym; -use orchid_base::parse::{Comment, Snippet}; -use orchid_base::tree::{TokTree, Token, ttv_into_api}; +use orchid_base::{ + Comment, IStr, OrcErrv, OrcRes, Snippet, SrcRange, Sym, TokTree, Token, match_mapping, + ttv_into_api, +}; use task_local::task_local; use crate::api; @@ -92,11 +91,13 @@ impl<'a> ParsCtx<'a> { type BoxConstCallback = Box LocalBoxFuture<'static, GExpr>>; task_local! { - static PARSED_CONST_CTX: IdStore + static CONST_TBL: RefCell, BoxConstCallback>>; + static NEXT_CONST_ID: RefCell>; } pub(crate) fn with_parsed_const_ctx<'a>(fut: LocalBoxFuture<'a, ()>) -> LocalBoxFuture<'a, ()> { - Box::pin(PARSED_CONST_CTX.scope(IdStore::default(), fut)) + let id_cell = RefCell::new(NonZero::new(1).unwrap()); + Box::pin(CONST_TBL.scope(RefCell::default(), NEXT_CONST_ID.scope(id_cell, fut))) } pub struct ParsedLine { @@ -139,9 +140,11 @@ impl ParsedLine { name: mem.name.to_api(), exported: mem.exported, kind: match mem.kind { - ParsedMemKind::Const(cb) => api::ParsedMemberKind::Constant(api::ParsedConstId( - PARSED_CONST_CTX.with(|consts| consts.add(cb).id()), - )), + ParsedMemKind::Const(cb) => { + let id = NEXT_CONST_ID.with(|c| c.replace_with(|id| id.checked_add(1).unwrap())); + CONST_TBL.with(|consts| consts.borrow_mut().insert(id, cb)); + api::ParsedMemberKind::Constant(api::ParsedConstId(id)) + }, ParsedMemKind::Mod { lines, use_prelude } => api::ParsedMemberKind::Module { lines: linev_into_api(lines).boxed_local().await, use_prelude, @@ -192,7 +195,7 @@ impl ConstCtx { let resolve_names = api::ResolveNames { constid: self.constid, sys: sys_id(), names }; for name_opt in request(resolve_names).await { cx.emit(match name_opt { - Err(e) => Err(OrcErrv::from_api(&e).await), + Err(e) => Err(OrcErrv::from_api(e).await), Ok(name) => Ok(Sym::from_api(name).await), }) .await @@ -205,7 +208,7 @@ impl ConstCtx { } pub(crate) async fn get_const(id: api::ParsedConstId) -> GExpr { - let cb = PARSED_CONST_CTX - .with(|ent| ent.get(id.0).expect("Bad ID or double read of parsed const").remove()); + let cb = CONST_TBL + .with(|ent| ent.borrow_mut().remove(&id.0).expect("Bad ID or double read of parsed const")); cb(ConstCtx { constid: id }).await } diff --git a/orchid-extension/src/reflection.rs b/orchid-extension/src/reflection.rs index 27dbdab..d069989 100644 --- a/orchid-extension/src/reflection.rs +++ b/orchid-extension/src/reflection.rs @@ -6,8 +6,7 @@ use futures::future::LocalBoxFuture; use futures::lock::Mutex; use hashbrown::HashMap; use memo_map::MemoMap; -use orchid_base::interner::{IStr, es, iv}; -use orchid_base::name::{NameLike, VPath}; +use orchid_base::{IStr, NameLike, VPath, es, iv}; use task_local::task_local; use crate::api; diff --git a/orchid-extension/src/stream_reqs.rs b/orchid-extension/src/stream_reqs.rs index 172e0ff..fffba78 100644 --- a/orchid-extension/src/stream_reqs.rs +++ b/orchid-extension/src/stream_reqs.rs @@ -1,3 +1,5 @@ +use std::num::NonZero; + use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; @@ -5,13 +7,23 @@ use crate::atom::AtomMethod; /// Represents [std::io::ErrorKind] values that are produced while operating on /// already-opened files -#[derive(Clone, Debug, Hash, PartialEq, Eq, Coding)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Coding)] pub enum IoErrorKind { BrokenPipe, UnexpectedEof, ConnectionAborted, Other, } +impl IoErrorKind { + pub fn message(self) -> &'static str { + match self { + IoErrorKind::Other => "Failed to read from stream", + IoErrorKind::BrokenPipe => "Broken pipe", + IoErrorKind::UnexpectedEof => "Unexpected EOF", + IoErrorKind::ConnectionAborted => "Connection aborted", + } + } +} /// Represents [std::io::Error] values that are produced while operating on /// already-opened files @@ -21,10 +33,17 @@ pub struct IoError { pub kind: IoErrorKind, } -/// Read at most the specified number of bytes, but at least one byte, from a -/// stream. If the returned vector is empty, the stream has reached its end. +#[derive(Clone, Debug, Coding)] +pub enum ReadLimit { + End, + Delimiter(u8), + Length(NonZero), +} + +/// Read all available data from a stream. If the returned vector is empty, the +/// stream has reached its end. #[derive(Clone, Debug, Coding, Hierarchy)] -pub struct ReadReq(pub u64); +pub struct ReadReq(pub ReadLimit); impl Request for ReadReq { type Response = Result, IoError>; } diff --git a/orchid-extension/src/system.rs b/orchid-extension/src/system.rs index 94f05a6..b1272c8 100644 --- a/orchid-extension/src/system.rs +++ b/orchid-extension/src/system.rs @@ -1,19 +1,16 @@ -use std::any::{Any, TypeId}; +use std::any::{Any, TypeId, type_name}; use std::fmt::Debug; use std::future::Future; use std::num::NonZero; -use std::pin::Pin; use futures::FutureExt; use futures::future::LocalBoxFuture; use orchid_api_traits::{Coding, Decode, Encode, Request}; -use orchid_base::boxed_iter::BoxedIter; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqReader, ReqReaderExt}; +use orchid_base::{BoxedIter, Receipt, ReqHandle, ReqReader, ReqReaderExt, Sym}; use task_local::task_local; use crate::api; -use crate::atom::{AtomCtx, AtomDynfo, AtomTypeId, AtomicFeatures, ForeignAtom, TAtom, get_info}; +use crate::atom::{AtomOps, AtomTypeId, Atomic, AtomicFeatures}; use crate::coroutine_exec::Replier; use crate::entrypoint::request; use crate::func_atom::{Fun, Lambda}; @@ -22,96 +19,95 @@ use crate::parser::ParserObj; use crate::system_ctor::{CtedObj, SystemCtor}; use crate::tree::GenMember; -/// System as consumed by foreign code -pub trait SystemCard: Debug + Default + Send + Sync + 'static { +/// Description of a system. This is a distinct object because [SystemCtor] +/// isn't [Default] +pub trait SystemCard: Debug + Default + 'static { type Ctor: SystemCtor; type Req: Coding; - fn atoms() -> impl IntoIterator>>; + fn atoms() -> impl IntoIterator>>; } -pub trait DynSystemCard: Send + Sync + Any + 'static { +/// Type-erased [SystemCard] +pub trait DynSystemCard: Any + 'static { fn name(&self) -> &'static str; /// Atoms explicitly defined by the system card. Do not rely on this for /// querying atoms as it doesn't include the general atom types - fn atoms(&'_ self) -> BoxedIter<'_, Option>>; + fn atoms(&'_ self) -> BoxedIter<'_, Option>>; +} + +impl DynSystemCardExt for T {} +pub(crate) trait DynSystemCardExt: DynSystemCard { + fn ops_by_tid(&self, tid: TypeId) -> Option<(AtomTypeId, Box)> { + (self.atoms().enumerate().map(|(i, o)| (NonZero::new(i as u32 + 1).unwrap(), o))) + .chain(general_atoms().enumerate().map(|(i, o)| (NonZero::new(!(i as u32)).unwrap(), o))) + .filter_map(|(i, o)| o.map(|a| (AtomTypeId(i), a))) + .find(|ent| ent.1.tid() == tid) + } + fn ops_by_atid(&self, tid: AtomTypeId) -> Option> { + if (u32::from(tid.0) >> (u32::BITS - 1)) & 1 == 1 { + general_atoms().nth(!u32::from(tid.0) as usize).unwrap() + } else { + self.atoms().nth(u32::from(tid.0) as usize - 1).unwrap() + } + } + fn ops(&self) -> (AtomTypeId, Box) { + self + .ops_by_tid(TypeId::of::()) + .unwrap_or_else(|| panic!("{} is not an atom in {}", type_name::(), self.name())) + } } /// Atoms supported by this package which may appear in all extensions. /// The indices of these are bitwise negated, such that the MSB of an atom index /// marks whether it belongs to this package (0) or the importer (1) -fn general_atoms() -> impl Iterator>> { - [Some(Fun::dynfo()), Some(Lambda::dynfo()), Some(Replier::dynfo())].into_iter() -} - -pub fn atom_info_for( - sys: &(impl DynSystemCard + ?Sized), - tid: TypeId, -) -> Option<(AtomTypeId, Box)> { - (sys.atoms().enumerate().map(|(i, o)| (NonZero::new(i as u32 + 1).unwrap(), o))) - .chain(general_atoms().enumerate().map(|(i, o)| (NonZero::new(!(i as u32)).unwrap(), o))) - .filter_map(|(i, o)| o.map(|a| (AtomTypeId(i), a))) - .find(|ent| ent.1.tid() == tid) -} - -pub fn atom_by_idx( - sys: &(impl DynSystemCard + ?Sized), - tid: AtomTypeId, -) -> Option> { - if (u32::from(tid.0) >> (u32::BITS - 1)) & 1 == 1 { - general_atoms().nth(!u32::from(tid.0) as usize).unwrap() - } else { - sys.atoms().nth(u32::from(tid.0) as usize - 1).unwrap() - } -} - -pub async fn resolv_atom( - sys: &(impl DynSystemCard + ?Sized), - atom: &api::Atom, -) -> Box { - let tid = AtomTypeId::decode(Pin::new(&mut &atom.data.0[..])).await.unwrap(); - atom_by_idx(sys, tid).expect("Value of nonexistent type found") +fn general_atoms() -> impl Iterator>> { + [Some(Fun::ops()), Some(Lambda::ops()), Some(Replier::ops())].into_iter() } impl DynSystemCard for T { fn name(&self) -> &'static str { T::Ctor::NAME } - fn atoms(&'_ self) -> BoxedIter<'_, Option>> { + fn atoms(&'_ self) -> BoxedIter<'_, Option>> { Box::new(Self::atoms().into_iter()) } } /// System as defined by author -pub trait System: Send + Sync + SystemCard + 'static { - fn prelude() -> impl Future>; - fn env() -> impl Future>; - fn lexers() -> Vec; - fn parsers() -> Vec; +pub trait System: SystemCard + 'static { + fn prelude(&self) -> impl Future>; + fn env(&self) -> impl Future>; + fn lexers(&self) -> Vec; + fn parsers(&self) -> Vec; fn request<'a>( + &self, hand: Box + 'a>, req: Self::Req, ) -> impl Future>; } -pub trait DynSystem: Send + Sync + DynSystemCard + 'static { +pub trait DynSystem: DynSystemCard + 'static { fn dyn_prelude(&self) -> LocalBoxFuture<'_, Vec>; fn dyn_env(&self) -> LocalBoxFuture<'_, Vec>; fn dyn_lexers(&self) -> Vec; fn dyn_parsers(&self) -> Vec; - fn dyn_request<'a>(&self, hand: Box + 'a>) -> LocalBoxFuture<'a, Receipt<'a>>; + fn dyn_request<'a, 'b: 'a>( + &'a self, + hand: Box + 'b>, + ) -> LocalBoxFuture<'a, Receipt<'b>>; fn card(&self) -> &dyn DynSystemCard; } impl DynSystem for T { - fn dyn_prelude(&self) -> LocalBoxFuture<'_, Vec> { Box::pin(Self::prelude()) } - fn dyn_env(&self) -> LocalBoxFuture<'_, Vec> { Self::env().boxed_local() } - fn dyn_lexers(&self) -> Vec { Self::lexers() } - fn dyn_parsers(&self) -> Vec { Self::parsers() } - fn dyn_request<'a>( - &self, - mut hand: Box + 'a>, - ) -> LocalBoxFuture<'a, Receipt<'a>> { + fn dyn_prelude(&self) -> LocalBoxFuture<'_, Vec> { Box::pin(self.prelude()) } + fn dyn_env(&self) -> LocalBoxFuture<'_, Vec> { self.env().boxed_local() } + fn dyn_lexers(&self) -> Vec { self.lexers() } + fn dyn_parsers(&self) -> Vec { self.parsers() } + fn dyn_request<'a, 'b: 'a>( + &'a self, + mut hand: Box + 'b>, + ) -> LocalBoxFuture<'a, Receipt<'b>> { Box::pin(async move { let value = hand.read_req::<::Req>().await.unwrap(); - Self::request(hand.finish().await, value).await + self.request(hand.finish().await, value).await }) } fn card(&self) -> &dyn DynSystemCard { self } @@ -130,30 +126,6 @@ pub(crate) async fn with_sys(sys: SysCtx, fut: F) -> F::Output { pub fn sys_id() -> api::SysId { SYS_CTX.with(|cx| cx.0) } pub fn cted() -> CtedObj { SYS_CTX.with(|cx| cx.1.clone()) } -pub async fn downcast_atom(foreign: ForeignAtom) -> Result, ForeignAtom> -where A: AtomicFeatures { - let mut data = &foreign.atom.data.0[..]; - let value = AtomTypeId::decode_slice(&mut data); - let cted = cted(); - let own_inst = cted.inst(); - let owner = if sys_id() == foreign.atom.owner { - own_inst.card() - } else { - cted.deps().find(|s| s.id() == foreign.atom.owner).ok_or_else(|| foreign.clone())?.get_card() - }; - if owner.atoms().flatten().all(|dynfo| dynfo.tid() != TypeId::of::()) { - return Err(foreign); - } - let (typ_id, dynfo) = get_info::(owner); - if value != typ_id { - return Err(foreign); - } - let val = dynfo.decode(AtomCtx(data, foreign.atom.drop)).await; - let Ok(value) = val.downcast::() else { - panic!("decode of {} returned wrong type.", dynfo.name()); - }; - Ok(TAtom { value: *value, untyped: foreign }) -} /// Make a global request to a system that supports this request type. The /// target system must either be the system in which this function is called, or diff --git a/orchid-extension/src/system_ctor.rs b/orchid-extension/src/system_ctor.rs index d2008e8..f5c5b66 100644 --- a/orchid-extension/src/system_ctor.rs +++ b/orchid-extension/src/system_ctor.rs @@ -2,7 +2,7 @@ use std::any::Any; use std::fmt::Debug; use std::sync::Arc; -use orchid_base::boxed_iter::{BoxedIter, box_empty, box_once}; +use orchid_base::{BoxedIter, box_empty, box_once}; use ordered_float::NotNan; use crate::api; @@ -17,7 +17,7 @@ pub struct Cted { impl Clone for Cted { fn clone(&self) -> Self { Self { deps: self.deps.clone(), inst: self.inst.clone() } } } -pub trait DynCted: Debug + Send + Sync + 'static { +pub trait DynCted: Debug + 'static { fn as_any(&self) -> &dyn Any; fn deps<'a>(&'a self) -> BoxedIter<'a, &'a (dyn DynSystemHandle + 'a)>; fn inst(&self) -> Arc; @@ -29,7 +29,7 @@ impl DynCted for Cted { } pub type CtedObj = Arc; -pub trait DepSat: Debug + Clone + Send + Sync + 'static { +pub trait DepSat: Debug + Clone + 'static { fn iter<'a>(&'a self) -> BoxedIter<'a, &'a (dyn DynSystemHandle + 'a)>; } @@ -59,7 +59,7 @@ impl DepDef for () { fn report(_: &mut impl FnMut(&'static str)) {} } -pub trait SystemCtor: Debug + Send + Sync + 'static { +pub trait SystemCtor: Debug + 'static { type Deps: DepDef; type Instance: System; const NAME: &'static str; @@ -68,7 +68,7 @@ pub trait SystemCtor: Debug + Send + Sync + 'static { fn inst(&self, deps: ::Sat) -> Self::Instance; } -pub trait DynSystemCtor: Debug + Send + Sync + 'static { +pub trait DynSystemCtor: Debug + 'static { fn decl(&self, id: api::SysDeclId) -> api::SystemDecl; fn new_system(&self, new: &api::NewSystem) -> CtedObj; } @@ -91,8 +91,7 @@ impl DynSystemCtor for T { } mod dep_set_tuple_impls { - use orchid_base::box_chain; - use orchid_base::boxed_iter::BoxedIter; + use orchid_base::{BoxedIter, box_chain}; use pastey::paste; use super::{DepDef, DepSat}; diff --git a/orchid-extension/src/tokio.rs b/orchid-extension/src/tokio.rs index 7e61135..e06466d 100644 --- a/orchid-extension/src/tokio.rs +++ b/orchid-extension/src/tokio.rs @@ -11,22 +11,50 @@ use crate::ext_port::ExtPort; /// shutdown. #[cfg(feature = "tokio")] pub async fn tokio_entrypoint(builder: ExtensionBuilder) { + use std::cell::RefCell; + + use async_event::Event; use tokio::io::{stderr, stdin, stdout}; use tokio::task::{LocalSet, spawn_local}; use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt}; - let local_set = LocalSet::new(); - local_set.spawn_local(async { - builder.build(ExtPort { - input: Box::pin(stdin().compat()), - output: Box::pin(stdout().compat_write()), - log: Box::pin(stderr().compat_write()), - spawn: Rc::new(|fut| { - spawn_local(fut); - }), - }); - }); - local_set.await; + let cc = Rc::new(Event::new()); + let c = Rc::new(RefCell::new(1)); + LocalSet::new() + .run_until(async { + let counter = (c.clone(), cc.clone()); + builder + .run(ExtPort { + input: Box::pin(stdin().compat()), + output: Box::pin(stdout().compat_write()), + log: Box::pin(stderr().compat_write()), + spawn: Rc::new(move |delay, fut| { + let (c, cc) = counter.clone(); + if delay.is_zero() { + *c.borrow_mut() += 1; + cc.notify_all(); + spawn_local(async move { + fut.await; + *c.borrow_mut() -= 1; + cc.notify_all(); + }); + } else { + let at = tokio::time::Instant::now() + delay; + spawn_local(async move { + tokio::time::sleep_until(at).await; + *c.borrow_mut() += 1; + cc.notify_all(); + fut.await; + *c.borrow_mut() -= 1; + cc.notify_all(); + }); + } + }), + }) + .await; + cc.wait_until(|| (*c.borrow() == 0).then_some(())).await; + }) + .await; } #[macro_export] diff --git a/orchid-extension/src/tree.rs b/orchid-extension/src/tree.rs index 48eeafe..8e7717e 100644 --- a/orchid-extension/src/tree.rs +++ b/orchid-extension/src/tree.rs @@ -8,10 +8,7 @@ use futures::future::{LocalBoxFuture, join_all}; use futures::{FutureExt, StreamExt}; use hashbrown::HashMap; use itertools::Itertools; -use orchid_base::interner::{IStr, is}; -use orchid_base::location::SrcRange; -use orchid_base::name::Sym; -use orchid_base::tree::{TokTree, Token, TokenVariant}; +use orchid_base::{IStr, SrcRange, Sym, TokTree, Token, TokenVariant, is}; use substack::Substack; use task_local::task_local; use trait_set::trait_set; @@ -20,7 +17,7 @@ use crate::api; use crate::conv::ToExpr; use crate::expr::{BorrowedExprStore, Expr, ExprHandle}; use crate::func_atom::{ExprFunc, Fun}; -use crate::gen_expr::{GExpr, new_atom, sym_ref}; +use crate::gen_expr::{GExpr, new_atom}; pub type GenTokTree = TokTree; pub type GenTok = Token; @@ -28,7 +25,7 @@ pub type GenTok = Token; impl TokenVariant for GExpr { type FromApiCtx<'a> = (); type ToApiCtx<'a> = (); - async fn from_api(_: &api::Expression, _: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { + async fn from_api(_: api::Expression, _: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { panic!("Received new expression from host") } async fn into_api(self, _: &mut Self::ToApiCtx<'_>) -> api::Expression { self.serialize().await } @@ -36,16 +33,16 @@ impl TokenVariant for GExpr { impl TokenVariant for Expr { type FromApiCtx<'a> = &'a BorrowedExprStore; - async fn from_api(api: &api::ExprTicket, exprs: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { + async fn from_api(api: api::ExprTicket, exprs: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { // SAFETY: receiving trees from sublexers implies borrowing - Expr::from_handle(ExprHandle::borrowed(*api, exprs)) + Expr::from_handle(ExprHandle::borrowed(api, exprs)) } type ToApiCtx<'a> = (); async fn into_api(self, (): &mut Self::ToApiCtx<'_>) -> api::ExprTicket { self.handle().ticket() } } pub async fn x_tok(x: impl ToExpr) -> GenTok { GenTok::NewExpr(x.to_gen().await) } -pub async fn ref_tok(path: Sym) -> GenTok { GenTok::NewExpr(sym_ref(path)) } +pub async fn ref_tok(path: Sym) -> GenTok { GenTok::NewExpr(path.to_gen().await) } pub fn lazy( public: bool, diff --git a/orchid-extension/src/trivial_req.rs b/orchid-extension/src/trivial_req.rs index f3db144..5d30eb1 100644 --- a/orchid-extension/src/trivial_req.rs +++ b/orchid-extension/src/trivial_req.rs @@ -3,7 +3,7 @@ use std::pin::Pin; use futures::future::LocalBoxFuture; use futures::{AsyncRead, AsyncWrite}; -use orchid_base::reqnot::{Receipt, RepWriter, ReqHandle, ReqReader}; +use orchid_base::{Receipt, RepWriter, ReqHandle, ReqReader}; pub struct TrivialReqCycle<'a> { pub req: &'a [u8], diff --git a/orchid-host/Cargo.toml b/orchid-host/Cargo.toml index 2616eba..6208cac 100644 --- a/orchid-host/Cargo.toml +++ b/orchid-host/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-event = "0.2.1" async-fn-stream = { version = "0.1.0", path = "../async-fn-stream" } async-once-cell = "0.5.4" bound = "0.6.0" diff --git a/orchid-host/src/atom.rs b/orchid-host/src/atom.rs index 2a232c3..cbf65ac 100644 --- a/orchid-host/src/atom.rs +++ b/orchid-host/src/atom.rs @@ -5,16 +5,13 @@ use async_once_cell::OnceCell; use derive_destructure::destructure; #[cfg(feature = "orchid-extension")] use orchid_api_traits::{Request, UnderRoot}; -use orchid_base::format::{FmtCtx, FmtUnit, Format, take_first_fmt}; -use orchid_base::location::Pos; -use orchid_base::reqnot::ClientExt; -use orchid_base::tree::AtomRepr; +use orchid_base::{AtomRepr, ClientExt, FmtCtx, FmtUnit, Format, Pos, take_first_fmt}; #[cfg(feature = "orchid-extension")] -use orchid_extension::atom::AtomMethod; +use orchid_extension::AtomMethod; use crate::api; use crate::ctx::Ctx; -use crate::expr::{Expr, PathSetBuilder}; +use crate::expr::{Expr, ExprFromApiCtx, PathSetBuilder}; use crate::extension::Extension; use crate::system::System; @@ -66,7 +63,7 @@ impl AtomHand { method: M, ) -> Option { use orchid_api_traits::{Decode, Encode}; - use orchid_base::name::Sym; + use orchid_base::Sym; let name = Sym::parse(::Root::NAME).await.unwrap(); let mut buf = Vec::new(); @@ -76,15 +73,19 @@ impl AtomHand { } #[must_use] pub async fn call(self, arg: Expr) -> Expr { - let owner_sys = self.0.owner.clone(); - let ctx = owner_sys.ctx(); - let client = owner_sys.client(); + let owner = self.0.owner.clone(); + let ctx = owner.ctx(); + let client = owner.client(); ctx.exprs.give_expr(arg.clone()); let ret = match Rc::try_unwrap(self.0) { Ok(data) => client.request(api::FinalCall(data.api(), arg.id())).await.unwrap(), Err(hand) => client.request(api::CallRef(hand.api_ref(), arg.id())).await.unwrap(), }; - let val = Expr::from_api(&ret, PathSetBuilder::new(), ctx.clone()).await; + let val = Expr::from_api(ret, PathSetBuilder::new(), ExprFromApiCtx { + sys: owner.id(), + ctx: ctx.clone(), + }) + .await; ctx.exprs.take_expr(arg.id()); val } diff --git a/orchid-host/src/ctx.rs b/orchid-host/src/ctx.rs index 5269a3e..f1408a9 100644 --- a/orchid-host/src/ctx.rs +++ b/orchid-host/src/ctx.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::num::{NonZero, NonZeroU16}; use std::rc::{Rc, Weak}; +use std::time::Duration; use std::{fmt, ops}; use futures::future::LocalBoxFuture; @@ -14,12 +15,21 @@ use crate::system::{System, WeakSystem}; use crate::tree::WeakRoot; pub trait JoinHandle { + /// It is guaranteed that the future will never be polled after this is called fn abort(&self); + /// take the future out of the task. If the return value + /// is dropped, the spawned future is also dropped fn join(self: Box) -> LocalBoxFuture<'static, ()>; } pub trait Spawner { - fn spawn_obj(&self, fut: LocalBoxFuture<'static, ()>) -> Box; + /// Spawn a future off-task, but in the same thread. If all objects associated + /// with the owning [Ctx] are dropped (eg. expressions, extensions, systems) + /// and no extensions create permanent ref loops, then all tasks will + /// eventually settle, therefore the implementor of this interface should not + /// exit while there are pending tasks to allow external communication + /// channels to cleanly shut down. + fn spawn_obj(&self, delay: Duration, fut: LocalBoxFuture<'static, ()>) -> Box; } pub struct CtxData { @@ -58,12 +68,16 @@ impl Ctx { } /// Spawn a parallel future that you can join at any later time. /// - /// Don't use this for async Drop, use [orchid_base::stash::stash] instead. + /// Don't use this for async Drop, use [orchid_base::stash] instead. /// If you use this for an actor object, make sure to actually join the /// handle. #[must_use] - pub fn spawn(&self, fut: impl Future + 'static) -> Box { - self.spawner.spawn_obj(Box::pin(fut)) + pub fn spawn( + &self, + delay: Duration, + fut: impl Future + 'static, + ) -> Box { + self.spawner.spawn_obj(delay, Box::pin(fut)) } #[must_use] pub(crate) async fn system_inst(&self, id: api::SysId) -> Option { diff --git a/orchid-host/src/dealias.rs b/orchid-host/src/dealias.rs index fcd3368..e333811 100644 --- a/orchid-host/src/dealias.rs +++ b/orchid-host/src/dealias.rs @@ -1,9 +1,6 @@ use hashbrown::HashSet; use itertools::Itertools; -use orchid_base::error::{OrcErrv, OrcRes, mk_errv}; -use orchid_base::interner::{IStr, is}; -use orchid_base::location::Pos; -use orchid_base::name::VName; +use orchid_base::{IStr, OrcErrv, OrcRes, Pos, VName, is, mk_errv}; /// Errors produced by absolute_path #[derive(Clone, Debug, Hash, PartialEq, Eq)] diff --git a/orchid-host/src/dylib.rs b/orchid-host/src/dylib.rs index aed4145..91aae69 100644 --- a/orchid-host/src/dylib.rs +++ b/orchid-host/src/dylib.rs @@ -1,18 +1,19 @@ use std::io; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; +use std::time::Duration; use futures::io::BufReader; use futures::{AsyncBufReadExt, StreamExt}; use hashbrown::HashMap; use libloading::{Library, Symbol}; -use orchid_base::binary::vt_to_future; -use orchid_base::logging::log; +use orchid_base::{log, on_drop, vt_to_future}; use unsync_pipe::pipe; use crate::api; use crate::ctx::Ctx; use crate::extension::ExtPort; +use crate::task_set::TaskSet; static DYNAMIC_LIBRARIES: Mutex>>> = Mutex::new(None); fn load_dylib(path: &Path) -> Result, libloading::Error> { @@ -32,7 +33,7 @@ pub async fn ext_dylib(path: &Path, ctx: Ctx) -> Result Result = unsafe { library.get("orchid_extension_main") }?; - let data = Box::into_raw(Box::new(ctx)) as *const (); - extern "C" fn drop(data: *const ()) { std::mem::drop(unsafe { Box::from_raw(data as *mut Ctx) }) } - extern "C" fn spawn(data: *const (), vt: api::binary::FutureBin) { - let _ = unsafe { (data as *mut Ctx).as_mut().unwrap().spawn(vt_to_future(vt)) }; - } + + let data = Box::into_raw(Box::new(SpawnerState { ctx, tasks: tasks.clone() })) as *const (); let spawner = api::binary::SpawnerBin { data, drop, spawn }; let cx = api::binary::ExtensionContext { input, output, log: write_log, spawner }; unsafe { (entrypoint)(cx) }; - Ok(ExtPort { input: Box::pin(write_input), output: Box::pin(read_output) }) + Ok(ExtPort { + input: Box::pin(write_input), + output: Box::pin(read_output), + drop_trigger: Box::new(on_drop(move || tasks.abort_all())), + }) +} + +struct SpawnerState { + ctx: Ctx, + tasks: TaskSet, +} +extern "C" fn drop(data: *const ()) { + let state = unsafe { Box::from_raw(data as *mut SpawnerState) }; + state.tasks.abort_all(); +} +extern "C" fn spawn(data: *const (), delay: u64, vt: api::binary::FutureBin) { + let future = vt_to_future(vt); + // SAFETY: this is technically a Box but it can be used directly as a &mut + let state = unsafe { (data as *mut SpawnerState).as_mut() }.unwrap(); + state.tasks.with(|store| { + store.add_with(|id| { + state.ctx.spawn(Duration::from_millis(delay), async move { + future.await; + // SAFETY: We know this is live because the drop handle that frees it also + // aborts the future + let state = unsafe { (data as *mut SpawnerState).as_mut() }.unwrap(); + state.tasks.with(|store| store.remove(id)); + }) + }); + }); } diff --git a/orchid-host/src/execute.rs b/orchid-host/src/execute.rs index 356cf28..5965d30 100644 --- a/orchid-host/src/execute.rs +++ b/orchid-host/src/execute.rs @@ -3,10 +3,7 @@ use std::mem; use bound::Bound; use futures::FutureExt; use futures_locks::{RwLockWriteGuard, TryLockError}; -use orchid_base::error::OrcErrv; -use orchid_base::format::fmt; -use orchid_base::location::Pos; -use orchid_base::logging::log; +use orchid_base::{OrcErrv, Pos, fmt, log}; use crate::expr::{Expr, ExprKind, PathSet, Step}; use crate::tree::Root; diff --git a/orchid-host/src/expr.rs b/orchid-host/src/expr.rs index ae3ac6c..2e0db17 100644 --- a/orchid-host/src/expr.rs +++ b/orchid-host/src/expr.rs @@ -7,12 +7,10 @@ use std::{fmt, mem}; use futures::FutureExt; use futures_locks::RwLock; use itertools::Itertools; -use orchid_base::error::OrcErrv; -use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants}; -use orchid_base::location::{Pos, SrcRange}; -use orchid_base::name::Sym; -use orchid_base::tl_cache; -use orchid_base::tree::{AtomRepr, TokenVariant, indent}; +use orchid_base::{ + AtomRepr, FmtCtx, FmtUnit, Format, OrcErrv, Pos, SrcRange, Sym, TokenVariant, Variants, indent, + tl_cache, +}; use substack::Substack; use crate::api; @@ -26,6 +24,12 @@ pub struct ExprData { kind: RwLock, } +#[derive(Clone, Debug)] +pub struct ExprFromApiCtx { + pub sys: api::SysId, + pub ctx: Ctx, +} + #[derive(Clone, Debug)] pub struct Expr(Rc); impl Expr { @@ -54,35 +58,40 @@ impl Expr { ) } #[must_use] - pub async fn from_api(api: &api::Expression, psb: PathSetBuilder<'_, u64>, ctx: Ctx) -> Self { + pub async fn from_api( + api: api::Expression, + psb: PathSetBuilder<'_, u64>, + ctx: ExprFromApiCtx, + ) -> Self { let pos = Pos::from_api(&api.location).await; - let kind = match &api.kind { + let kind = match api.kind { api::ExpressionKind::Arg(n) => { - assert!(psb.register_arg(n), "Arguments must be enclosed in a matching lambda"); + assert!(psb.register_arg(&n), "Arguments must be enclosed in a matching lambda"); ExprKind::Arg }, api::ExpressionKind::Bottom(bot) => ExprKind::Bottom(OrcErrv::from_api(bot).await), api::ExpressionKind::Call(f, x) => { let (lpsb, rpsb) = psb.split(); ExprKind::Call( - Expr::from_api(f, lpsb, ctx.clone()).boxed_local().await, - Expr::from_api(x, rpsb, ctx).boxed_local().await, + Expr::from_api(*f, lpsb, ctx.clone()).boxed_local().await, + Expr::from_api(*x, rpsb, ctx).boxed_local().await, ) }, - api::ExpressionKind::Const(name) => ExprKind::Const(Sym::from_api(*name).await), + api::ExpressionKind::Const(name) => ExprKind::Const(Sym::from_api(name).await), api::ExpressionKind::Lambda(x, body) => { - let lbuilder = psb.lambda(x); - let body = Expr::from_api(body, lbuilder.stack(), ctx).boxed_local().await; + let lbuilder = psb.lambda(&x); + let body = Expr::from_api(*body, lbuilder.stack(), ctx).boxed_local().await; ExprKind::Lambda(lbuilder.collect(), body) }, - api::ExpressionKind::NewAtom(a) => - ExprKind::Atom(AtomHand::from_api(a, pos.clone(), &mut ctx.clone()).await), - api::ExpressionKind::Slot(tk) => return ctx.exprs.take_expr(*tk).expect("Invalid slot"), + api::ExpressionKind::NewAtom(a) => ExprKind::Atom( + AtomHand::from_api(&a.associate(ctx.sys), pos.clone(), &mut ctx.ctx.clone()).await, + ), + api::ExpressionKind::Slot(tk) => return ctx.ctx.exprs.take_expr(tk).expect("Invalid slot"), api::ExpressionKind::Seq(a, b) => { let (apsb, bpsb) = psb.split(); ExprKind::Seq( - Expr::from_api(a, apsb, ctx.clone()).boxed_local().await, - Expr::from_api(b, bpsb, ctx).boxed_local().await, + Expr::from_api(*a, apsb, ctx.clone()).boxed_local().await, + Expr::from_api(*b, bpsb, ctx).boxed_local().await, ) }, }; @@ -159,8 +168,10 @@ async fn print_exprkind<'a>( panic!("This variant is swapped into write guards, so a read can never see it") }, ExprKind::Atom(a) => a.print(c).await, - ExprKind::Bottom(e) if e.len() == 1 => format!("Bottom({e})").into(), - ExprKind::Bottom(e) => format!("Bottom(\n\t{}\n)", indent(&e.to_string())).into(), + ExprKind::Bottom(e) => match e.one() { + Some(e) => format!("Bottom({e})").into(), + None => format!("Bottom(\n\t{}\n)", indent(&e.to_string())).into(), + }, ExprKind::Call(f, x) => tl_cache!(Rc: Rc::new(Variants::default() .unbounded("{0b} {1l}") .bounded("({0b} {1})"))) @@ -333,8 +344,8 @@ impl WeakExpr { impl TokenVariant for Expr { type FromApiCtx<'a> = ExprStore; - async fn from_api(api: &api::ExprTicket, ctx: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { - ctx.get_expr(*api).expect("Invalid ticket") + async fn from_api(api: api::ExprTicket, ctx: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { + ctx.get_expr(api).expect("Invalid ticket") } type ToApiCtx<'a> = ExprStore; async fn into_api(self, ctx: &mut Self::ToApiCtx<'_>) -> api::ExprTicket { @@ -349,8 +360,8 @@ impl TokenVariant for Expr { pub struct ExprWillPanic; impl TokenVariant for Expr { - type FromApiCtx<'a> = Ctx; - async fn from_api(api: &api::Expression, ctx: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { + type FromApiCtx<'a> = ExprFromApiCtx; + async fn from_api(api: api::Expression, ctx: &mut Self::FromApiCtx<'_>, _: SrcRange) -> Self { Self::from_api(api, PathSetBuilder::new(), ctx.clone()).await } type ToApiCtx<'a> = ExprWillPanic; diff --git a/orchid-host/src/extension.rs b/orchid-host/src/extension.rs index 85615a0..e95025b 100644 --- a/orchid-host/src/extension.rs +++ b/orchid-host/src/extension.rs @@ -1,9 +1,11 @@ +use std::any::Any; use std::cell::RefCell; use std::future::Future; use std::io; use std::num::NonZeroU64; use std::pin::Pin; use std::rc::{Rc, Weak}; +use std::time::Duration; use async_fn_stream::stream; use derive_destructure::destructure; @@ -14,28 +16,23 @@ use futures::{AsyncRead, AsyncWrite, AsyncWriteExt, SinkExt, StreamExt}; use hashbrown::{HashMap, HashSet}; use itertools::Itertools; use orchid_api_traits::{Decode, Encode, Request}; -use orchid_base::format::{FmtCtxImpl, Format}; -use orchid_base::interner::{IStr, IStrv, es, ev, is, iv}; -use orchid_base::location::Pos; -use orchid_base::logging::log; -use orchid_base::name::Sym; -use orchid_base::reqnot::{ - Client, ClientExt, CommCtx, MsgReaderExt, ReqHandleExt, ReqReaderExt, io_comm, +use orchid_base::{ + AtomRepr, Client, ClientExt, CommCtx, FmtCtxImpl, Format, IStr, IStrv, MsgReaderExt, Pos, + ReqHandleExt, ReqReaderExt, Sym, Witness, es, ev, io_comm, is, iv, log, stash, with_stash, }; -use orchid_base::stash::{stash, with_stash}; -use orchid_base::tree::AtomRepr; use crate::api; use crate::atom::AtomHand; use crate::ctx::{Ctx, JoinHandle}; use crate::dealias::{ChildError, ChildErrorKind, walk}; -use crate::expr::{Expr, PathSetBuilder}; +use crate::expr::{Expr, ExprFromApiCtx, PathSetBuilder}; use crate::system::SystemCtor; use crate::tree::MemberKind; pub struct ExtPort { pub input: Pin>, pub output: Pin>, + pub drop_trigger: Box, } pub struct ReqPair(R, Sender); @@ -56,6 +53,8 @@ pub struct ExtensionData { lex_recur: Mutex>>>, strings: RefCell>, string_vecs: RefCell>, + /// Moved over from [ExtPort] to allow hooking to the extension's drop + _drop_trigger: Box, } impl Drop for ExtensionData { fn drop(&mut self) { @@ -85,7 +84,7 @@ impl Extension { let weak2 = weak; let weak = weak.clone(); let ctx2 = ctx.clone(); - let join_ext = ctx.clone().spawn(async move { + let join_ext = ctx.clone().spawn(Duration::ZERO, async move { comm .listen( async |reader| { @@ -192,11 +191,17 @@ impl Extension { }) .await }, - api::ExprReq::Create(ref cre @ api::Create(ref expr)) => { - let expr = Expr::from_api(expr, PathSetBuilder::new(), ctx.clone()).await; + api::ExprReq::Create(cre) => { + let req = Witness::of(&cre); + let api::Create(sys, expr) = cre; + let expr = Expr::from_api(expr, PathSetBuilder::new(), ExprFromApiCtx { + ctx: ctx.clone(), + sys, + }) + .await; let expr_id = expr.id(); ctx.exprs.give_expr(expr); - handle.reply(cre, &expr_id).await + handle.reply(req, &expr_id).await }, }, api::ExtHostReq::LsModule(ref ls @ api::LsModule(_sys, path)) => { @@ -286,6 +291,7 @@ impl Extension { client: Rc::new(client), strings: RefCell::default(), string_vecs: RefCell::default(), + _drop_trigger: init.drop_trigger, } }))) } @@ -336,10 +342,13 @@ impl Extension { } pub fn system_drop(&self, id: api::SysId) { let rc = self.clone(); - let _ = self.ctx().spawn(with_stash(async move { - rc.client().request(api::SystemDrop(id)).await.unwrap(); - rc.ctx().systems.write().await.remove(&id); - })); + let _ = self.ctx().spawn( + Duration::ZERO, + with_stash(async move { + rc.client().request(api::SystemDrop(id)).await.unwrap(); + rc.ctx().systems.write().await.remove(&id); + }), + ); } #[must_use] pub fn downgrade(&self) -> WeakExtension { WeakExtension(Rc::downgrade(&self.0)) } diff --git a/orchid-host/src/inline.rs b/orchid-host/src/inline.rs index cd41c1e..b8b6c89 100644 --- a/orchid-host/src/inline.rs +++ b/orchid-host/src/inline.rs @@ -1,10 +1,17 @@ #[cfg(feature = "orchid-extension")] +use std::time::Duration; + +#[cfg(feature = "orchid-extension")] +use orchid_base::on_drop; +#[cfg(feature = "orchid-extension")] use orchid_extension as ox; #[cfg(feature = "orchid-extension")] use crate::ctx::Ctx; #[cfg(feature = "orchid-extension")] use crate::extension::ExtPort; +#[cfg(feature = "orchid-extension")] +use crate::task_set::TaskSet; #[cfg(feature = "orchid-extension")] pub async fn ext_inline(builder: ox::entrypoint::ExtensionBuilder, ctx: Ctx) -> ExtPort { @@ -13,7 +20,7 @@ pub async fn ext_inline(builder: ox::entrypoint::ExtensionBuilder, ctx: Ctx) -> use futures::io::BufReader; use futures::{AsyncBufReadExt, StreamExt}; - use orchid_base::logging::log; + use orchid_base::log; use unsync_pipe::pipe; let (in_stdin, out_stdin) = pipe(1024); @@ -22,7 +29,7 @@ pub async fn ext_inline(builder: ox::entrypoint::ExtensionBuilder, ctx: Ctx) -> let name = builder.name; - std::mem::drop(ctx.spawn(async move { + std::mem::drop(ctx.spawn(Duration::ZERO, async move { let mut lines = BufReader::new(out_stderr).lines(); while let Some(line) = lines.next().await { match line { @@ -35,11 +42,35 @@ pub async fn ext_inline(builder: ox::entrypoint::ExtensionBuilder, ctx: Ctx) -> } })); - builder.build(ox::ext_port::ExtPort { - input: Box::pin(out_stdin), - output: Box::pin(in_stdout), - log: Box::pin(in_stderr), - spawn: Rc::new(move |fut| std::mem::drop(ctx.spawn(fut))), - }); - ExtPort { input: Box::pin(in_stdin), output: Box::pin(out_stdout) } + let task_set = TaskSet::default(); + let task_set1 = task_set.clone(); + + std::mem::drop(ctx.clone().spawn(Duration::ZERO, async move { + let task_set2 = task_set1.clone(); + builder + .run(ox::ext_port::ExtPort { + input: Box::pin(out_stdin), + output: Box::pin(in_stdout), + log: Box::pin(in_stderr), + spawn: Rc::new(move |delay, fut| { + let ctx1 = ctx.clone(); + let task_set2 = task_set1.clone(); + task_set1.with(move |store| { + store.add_with(move |id| { + ctx1.spawn(delay, async move { + fut.await; + task_set2.with(|store| store.remove(id)); + }) + }) + }); + }), + }) + .await; + task_set2.abort_all(); + })); + ExtPort { + input: Box::pin(in_stdin), + output: Box::pin(out_stdout), + drop_trigger: Box::new(on_drop(move || task_set.abort_all())), + } } diff --git a/orchid-host/src/lex.rs b/orchid-host/src/lex.rs index 2671061..de158ac 100644 --- a/orchid-host/src/lex.rs +++ b/orchid-host/src/lex.rs @@ -3,18 +3,14 @@ use std::ops::Range; use futures::FutureExt; use futures::lock::Mutex; -use orchid_base::clone; -use orchid_base::error::{OrcErrv, OrcRes, mk_errv, report}; -use orchid_base::interner::{IStr, is}; -use orchid_base::location::SrcRange; -use orchid_base::name::Sym; -use orchid_base::parse::{name_char, name_start, op_char, unrep_space}; -use orchid_base::tokens::PARENS; -use orchid_base::tree::recur; +use orchid_base::{ + IStr, OrcErrv, OrcRes, PARENS, SrcRange, Sym, clone, is, mk_errv, name_char, name_start, op_char, + report, unrep_space, +}; use crate::api; use crate::ctx::Ctx; -use crate::expr::Expr; +use crate::expr::{Expr, ExprFromApiCtx}; use crate::expr_store::ExprStore; use crate::parsed::{ParsTok, ParsTokTree, tt_to_api}; use crate::system::System; @@ -63,8 +59,14 @@ impl<'a> LexCtx<'a> { tt_to_api(&mut { exprs }, subtree).await } #[must_use] - pub async fn des_subtree(&mut self, tree: &api::TokenTree, exprs: ExprStore) -> ParsTokTree { - ParsTokTree::from_api(tree, &mut { exprs }, &mut self.ctx.clone(), self.path).await + pub async fn des_subtree( + &mut self, + tree: api::TokenTree, + sys: api::SysId, + exprs: ExprStore, + ) -> ParsTokTree { + let mut cx = ExprFromApiCtx { ctx: self.ctx.clone(), sys }; + ParsTokTree::from_api(tree, &mut { exprs }, &mut cx, self.path).await } #[must_use] pub fn strip_char(&mut self, tgt: char) -> bool { @@ -261,12 +263,13 @@ pub async fn sys_lex(ctx: &mut LexCtx<'_>) -> Option>> { .await; match lx { Err(e) => - return Some(Err(errors.into_iter().fold(OrcErrv::from_api(&e).await, |a, b| a + b))), + return Some(Err(errors.into_iter().fold(OrcErrv::from_api(e).await, |a, b| a + b))), Ok(Some(lexed)) => { ctx.set_pos(lexed.pos); let mut stable_trees = Vec::new(); for tok in lexed.expr { - stable_trees.push(recur(ctx.des_subtree(&tok, temp_store.clone()).await, &|tt, r| { + let tree = ctx.des_subtree(tok, sys.id(), temp_store.clone()).await; + stable_trees.push(tree.recur(&|tt, r| { if let ParsTok::NewExpr(expr) = tt.tok { return ParsTok::Handle(expr).at(tt.sr); } diff --git a/orchid-host/src/lib.rs b/orchid-host/src/lib.rs index 5b4cd0c..24633dd 100644 --- a/orchid-host/src/lib.rs +++ b/orchid-host/src/lib.rs @@ -18,4 +18,5 @@ pub mod parsed; pub mod subprocess; mod sys_parser; pub mod system; +mod task_set; pub mod tree; diff --git a/orchid-host/src/logger.rs b/orchid-host/src/logger.rs index 0bc4867..831396e 100644 --- a/orchid-host/src/logger.rs +++ b/orchid-host/src/logger.rs @@ -6,7 +6,7 @@ use std::rc::Rc; use futures::future::LocalBoxFuture; use hashbrown::HashMap; use itertools::Itertools; -use orchid_base::logging::{LogWriter, Logger}; +use orchid_base::{LogWriter, Logger}; use crate::api; diff --git a/orchid-host/src/parse.rs b/orchid-host/src/parse.rs index b90908c..d38a8bc 100644 --- a/orchid-host/src/parse.rs +++ b/orchid-host/src/parse.rs @@ -1,13 +1,9 @@ use futures::FutureExt; use itertools::Itertools; -use orchid_base::error::{OrcRes, mk_errv, report}; -use orchid_base::format::fmt; -use orchid_base::interner::{IStr, is}; -use orchid_base::name::Sym; -use orchid_base::parse::{ - Comment, Import, Parsed, Snippet, expect_end, line_items, parse_multiname, try_pop_no_fluff, +use orchid_base::{ + Comment, IStr, Import, OrcRes, Paren, Parsed, Snippet, Sym, TokTree, Token, expect_end, fmt, is, + line_items, mk_errv, parse_multiname, report, try_pop_no_fluff, }; -use orchid_base::tree::{Paren, TokTree, Token}; use substack::Substack; use crate::ctx::Ctx; @@ -60,7 +56,7 @@ pub async fn parse_item( comments: Vec, item: ParsSnippet<'_>, ) -> OrcRes> { - match item.pop_front() { + match item.split_first() { Some((TokTree { tok: Token::Name(n), .. }, postdisc)) => match n { n if *n == is("export").await => match try_pop_no_fluff(postdisc).await? { Parsed { output: TokTree { tok: Token::Name(n), .. }, tail } => diff --git a/orchid-host/src/parsed.rs b/orchid-host/src/parsed.rs index c66a1ee..5c01522 100644 --- a/orchid-host/src/parsed.rs +++ b/orchid-host/src/parsed.rs @@ -5,12 +5,10 @@ use futures::FutureExt; use futures::future::{LocalBoxFuture, join_all}; use hashbrown::HashSet; use itertools::Itertools; -use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants}; -use orchid_base::interner::{IStr, IStrv}; -use orchid_base::location::SrcRange; -use orchid_base::parse::{Comment, Import}; -use orchid_base::tl_cache; -use orchid_base::tree::{TokTree, Token, recur}; +use orchid_base::{ + Comment, FmtCtx, FmtUnit, Format, IStr, IStrv, Import, SrcRange, TokTree, Token, Variants, + tl_cache, +}; use crate::api; use crate::dealias::{ChildErrorKind, ChildResult, Tree}; @@ -182,7 +180,7 @@ impl Tree for ParsedModule { impl Format for ParsedModule { async fn print<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit { let head_str = format!("export ::({})\n", self.exports.iter().join(", ")); - Variants::default().sequence(self.items.len() + 1, "", "\n", "", None).units_own( + Variants::default().sequence(self.items.len() + 1, "", "\n", "", false).units_own( [head_str.into()].into_iter().chain(join_all(self.items.iter().map(|i| i.print(c))).await), ) } @@ -202,7 +200,7 @@ impl ConstPath { } pub async fn tt_to_api(exprs: &mut ExprStore, subtree: ParsTokTree) -> api::TokenTree { - let without_new_expr = recur(subtree, &|tt, r| { + let without_new_expr = subtree.recur(&|tt, r| { if let ParsTok::NewExpr(expr) = tt.tok { return ParsTok::Handle(expr).at(tt.sr); } diff --git a/orchid-host/src/subprocess.rs b/orchid-host/src/subprocess.rs index 413f5f2..642a999 100644 --- a/orchid-host/src/subprocess.rs +++ b/orchid-host/src/subprocess.rs @@ -1,36 +1,40 @@ -use std::{io, process}; +use std::time::Duration; use futures::io::BufReader; use futures::{self, AsyncBufReadExt, StreamExt}; -use orchid_base::logging::log; +use orchid_base::log; use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt}; use crate::ctx::Ctx; use crate::extension::ExtPort; -pub async fn ext_command(cmd: process::Command, ctx: Ctx) -> io::Result { +pub async fn ext_command(cmd: std::process::Command, ctx: Ctx) -> std::io::Result { let name = cmd.get_program().to_string_lossy().to_string(); let mut child = tokio::process::Command::from(cmd) - .stdin(process::Stdio::piped()) - .stdout(process::Stdio::piped()) - .stderr(process::Stdio::piped()) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) .spawn()?; std::thread::spawn(|| {}); let stdin = child.stdin.take().unwrap(); let stdout = child.stdout.take().unwrap(); let child_stderr = child.stderr.take().unwrap(); - let _ = ctx.spawn(Box::pin(async move { + std::mem::drop(ctx.spawn(Duration::ZERO, async move { let _ = child; let mut lines = BufReader::new(child_stderr.compat()).lines(); while let Some(line) = lines.next().await { match line { Ok(line) => writeln!(log("stderr"), "subproc {name} err> {line}").await, Err(e) => match e.kind() { - io::ErrorKind::BrokenPipe | io::ErrorKind::UnexpectedEof => break, + std::io::ErrorKind::BrokenPipe | std::io::ErrorKind::UnexpectedEof => break, _ => panic!("Error while reading stderr {e}"), }, } } })); - Ok(ExtPort { input: Box::pin(stdin.compat_write()), output: Box::pin(stdout.compat()) }) + Ok(ExtPort { + input: Box::pin(stdin.compat_write()), + output: Box::pin(stdout.compat()), + drop_trigger: Box::new(()), + }) } diff --git a/orchid-host/src/sys_parser.rs b/orchid-host/src/sys_parser.rs index 84e52bb..5d58334 100644 --- a/orchid-host/src/sys_parser.rs +++ b/orchid-host/src/sys_parser.rs @@ -1,16 +1,11 @@ use futures::FutureExt; use futures::future::join_all; use itertools::Itertools; -use orchid_base::error::{OrcErrv, OrcRes}; -use orchid_base::interner::{IStr, es}; -use orchid_base::location::SrcRange; -use orchid_base::name::Sym; -use orchid_base::parse::Comment; -use orchid_base::reqnot::ClientExt; -use orchid_base::tree::ttv_from_api; +use orchid_base::{ClientExt, Comment, IStr, OrcErrv, OrcRes, SrcRange, Sym, es, ttv_from_api}; use substack::Substack; use crate::api; +use crate::expr::ExprFromApiCtx; use crate::expr_store::ExprStore; use crate::parse::HostParseCtx; use crate::parsed::{ @@ -59,7 +54,7 @@ impl Parser { sys: &self.system, }) .await, - Err(e) => Err(OrcErrv::from_api(&e).await), + Err(e) => Err(OrcErrv::from_api(e).await), } } } @@ -82,8 +77,8 @@ async fn conv( api::ParsedLineKind::Member(api::ParsedMember { name, exported, kind }) => (name, exported, kind), api::ParsedLineKind::Recursive(rec) => { - let tokens = - ttv_from_api(rec, ctx.ext_exprs, &mut ctx.sys.ctx().clone(), ctx.src_path).await; + let mut cx = ExprFromApiCtx { ctx: ctx.sys.ctx().clone(), sys: ctx.sys.id() }; + let tokens = ttv_from_api(rec, ctx.ext_exprs, &mut cx, ctx.src_path).await; items.extend(callback(module.clone(), tokens).await?); continue; }, diff --git a/orchid-host/src/system.rs b/orchid-host/src/system.rs index 39e5579..cbc9457 100644 --- a/orchid-host/src/system.rs +++ b/orchid-host/src/system.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use std::fmt; use std::future::Future; use std::rc::{Rc, Weak}; +use std::time::Duration; use derive_destructure::destructure; use futures::future::join_all; @@ -9,15 +10,10 @@ use futures_locks::RwLock; use hashbrown::HashMap; use itertools::Itertools; use memo_map::MemoMap; -use orchid_base::char_filter::char_filter_match; -use orchid_base::error::{OrcRes, mk_errv_floating}; -use orchid_base::format::{FmtCtx, FmtUnit, Format}; -use orchid_base::interner::{IStr, es, is}; -use orchid_base::iter_utils::IteratorPrint; -use orchid_base::logging::log; -use orchid_base::name::{NameLike, Sym, VName, VPath}; -use orchid_base::reqnot::{Client, ClientExt}; -use orchid_base::stash::stash; +use orchid_base::{ + Client, ClientExt, FmtCtx, FmtUnit, Format, IStr, IteratorPrint, NameLike, OrcRes, Sym, VName, + VPath, char_filter_match, es, is, log, mk_errv_floating, stash, +}; use ordered_float::NotNan; use substack::{Stackframe, Substack}; @@ -129,10 +125,10 @@ impl System { } pub(crate) fn drop_atom(&self, dropped_atom_id: api::AtomId) { let this = self.0.clone(); - let _ = self.0.ctx.spawn(Box::pin(async move { + let _ = self.0.ctx.spawn(Duration::ZERO, async move { this.ext.client().request(api::AtomDrop(this.id, dropped_atom_id)).await.unwrap(); this.owned_atoms.write().await.remove(&dropped_atom_id); - })); + }); } #[must_use] pub fn downgrade(&self) -> WeakSystem { diff --git a/orchid-host/src/task_set.rs b/orchid-host/src/task_set.rs new file mode 100644 index 0000000..3be2073 --- /dev/null +++ b/orchid-host/src/task_set.rs @@ -0,0 +1,23 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use orchid_base::IdStore; + +use crate::ctx::JoinHandle; + +#[derive(Clone)] +pub struct TaskSet { + pub tasks: Rc>>>>, +} +impl TaskSet { + pub fn with(&self, f: impl FnOnce(&mut IdStore>) -> R) -> Option { + self.tasks.borrow_mut().as_mut().map(f) + } + pub fn abort_all(&self) { + let Some(tasks) = self.tasks.borrow_mut().take() else { return }; + tasks.into_iter().for_each(|(_, task)| task.abort()); + } +} +impl Default for TaskSet { + fn default() -> Self { Self { tasks: Rc::new(RefCell::new(Some(IdStore::default()))) } } +} diff --git a/orchid-host/src/tree.rs b/orchid-host/src/tree.rs index 093bf38..abcf877 100644 --- a/orchid-host/src/tree.rs +++ b/orchid-host/src/tree.rs @@ -12,17 +12,15 @@ use hashbrown::HashMap; use hashbrown::hash_map::Entry; use itertools::Itertools; use memo_map::MemoMap; -use orchid_base::clone; -use orchid_base::error::{OrcRes, mk_errv, report}; -use orchid_base::interner::{IStr, IStrv, es, is, iv}; -use orchid_base::location::{CodeGenInfo, Pos}; -use orchid_base::name::{NameLike, Sym, VPath}; -use orchid_base::reqnot::ClientExt; +use orchid_base::{ + ClientExt, CodeGenInfo, IStr, IStrv, NameLike, OrcRes, Pos, Sym, VPath, clone, es, is, iv, + mk_errv, report, +}; use crate::api; use crate::ctx::Ctx; use crate::dealias::{ChildErrorKind, Tree, absolute_path, resolv_glob, walk}; -use crate::expr::{Expr, PathSetBuilder}; +use crate::expr::{Expr, ExprFromApiCtx, PathSetBuilder}; use crate::parsed::{ItemKind, ParsedMemberKind, ParsedModule}; use crate::system::System; @@ -89,7 +87,8 @@ impl Root { for (path, sys_id, pc_id) in deferred_consts { let sys = this.ctx.system_inst(sys_id).await.expect("System dropped since parsing"); let api_expr = sys.client().request(api::FetchParsedConst(sys.id(), pc_id)).await.unwrap(); - let expr = Expr::from_api(&api_expr, PathSetBuilder::new(), this.ctx.clone()).await; + let cx = ExprFromApiCtx { sys: sys_id, ctx: this.ctx.clone() }; + let expr = Expr::from_api(api_expr, PathSetBuilder::new(), cx).await; new.0.write().await.consts.insert(path, expr); } new @@ -176,7 +175,8 @@ impl Module { api::MemberKind::Lazy(id) => (Some(LazyMemberHandle { id, sys: ctx.sys.id(), path: name.clone() }), None), api::MemberKind::Const(val) => { - let expr = Expr::from_api(&val, PathSetBuilder::new(), ctx.sys.ctx().clone()).await; + let cx = ExprFromApiCtx { ctx: ctx.sys.ctx().clone(), sys: ctx.sys.id() }; + let expr = Expr::from_api(val, PathSetBuilder::new(), cx).await; ctx.consts.insert(name.clone(), expr); (None, Some(MemberKind::Const)) }, @@ -456,7 +456,8 @@ impl LazyMemberHandle { let sys = ctx.system_inst(self.sys).await.expect("Missing system for lazy member"); match sys.get_tree(self.id).await { api::MemberKind::Const(c) => { - let expr = Expr::from_api(&c, PathSetBuilder::new(), ctx.clone()).await; + let ctx = ExprFromApiCtx { sys: sys.id(), ctx: ctx.clone() }; + let expr = Expr::from_api(c, PathSetBuilder::new(), ctx).await; let (.., path) = self.destructure(); consts.insert(path, expr); MemberKind::Const diff --git a/orchid-std/Cargo.toml b/orchid-std/Cargo.toml index 2be48d3..790d8c2 100644 --- a/orchid-std/Cargo.toml +++ b/orchid-std/Cargo.toml @@ -13,8 +13,10 @@ name = "orchid_std" path = "src/lib.rs" [dependencies] +async-event = "0.2.1" async-fn-stream = { version = "0.1.0", path = "../async-fn-stream" } async-once-cell = "0.5.4" +chrono = "0.4.43" futures = { version = "0.3.31", features = ["std"], default-features = false } hashbrown = "0.16.1" itertools = "0.14.0" diff --git a/orchid-std/src/lib.rs b/orchid-std/src/lib.rs index 2843f0a..67c0113 100644 --- a/orchid-std/src/lib.rs +++ b/orchid-std/src/lib.rs @@ -17,7 +17,7 @@ use orchid_extension::dylib_main; use orchid_extension::entrypoint::ExtensionBuilder; pub fn builder() -> ExtensionBuilder { - ExtensionBuilder::new("orchid-std::main").system(StdSystem).system(MacroSystem) + ExtensionBuilder::new("orchid-std::main").system(StdSystem::default()).system(MacroSystem) } dylib_main! { builder() } diff --git a/orchid-std/src/macros/instantiate_tpl.rs b/orchid-std/src/macros/instantiate_tpl.rs index 0acf61a..3810311 100644 --- a/orchid-std/src/macros/instantiate_tpl.rs +++ b/orchid-std/src/macros/instantiate_tpl.rs @@ -1,13 +1,12 @@ use std::borrow::Cow; use never::Never; -use orchid_base::format::fmt; -use orchid_extension::atom::{Atomic, TAtom}; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; +use orchid_base::fmt; use orchid_extension::conv::ToExpr; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::{GExpr, new_atom}; +use orchid_extension::gen_expr::new_atom; +use orchid_extension::{Atomic, OwnedAtom, OwnedVariant, TAtom}; use crate::macros::mactree::{MacTok, MacTree}; @@ -25,7 +24,7 @@ impl OwnedAtom for InstantiateTplCall { async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } type Refs = Never; // Technically must be supported but shouldn't actually ever be called - async fn call_ref(&self, arg: Expr) -> GExpr { + async fn call_ref(&self, arg: Expr) -> impl ToExpr { if !self.argv.is_empty() { eprintln!( "Copying partially applied instantiate_tpl call. This is an internal value.\ @@ -34,11 +33,11 @@ impl OwnedAtom for InstantiateTplCall { } self.clone().call(arg).await } - async fn call(mut self, arg: Expr) -> GExpr { + async fn call(mut self, arg: Expr) -> impl ToExpr { exec(async move |mut h| { match h.exec::>(arg.clone()).await { Err(_) => panic!("Expected a macro param, found {}", fmt(&arg).await), - Ok(t) => self.argv.push(own(&t).await), + Ok(t) => self.argv.push(t.own().await), }; if self.argv.len() < self.argc { return new_atom(self); @@ -52,7 +51,5 @@ impl OwnedAtom for InstantiateTplCall { new_atom(ret) }) .await - .to_gen() - .await } } diff --git a/orchid-std/src/macros/let_line.rs b/orchid-std/src/macros/let_line.rs index 3bb896f..e3b3c16 100644 --- a/orchid-std/src/macros/let_line.rs +++ b/orchid-std/src/macros/let_line.rs @@ -3,15 +3,13 @@ use std::pin::pin; use futures::{FutureExt, StreamExt, stream}; use hashbrown::HashMap; use itertools::Itertools; -use orchid_base::error::{OrcRes, report, with_reporter}; -use orchid_base::interner::is; -use orchid_base::name::Sym; -use orchid_base::parse::{Comment, Parsed, Snippet, expect_tok, token_errv, try_pop_no_fluff}; -use orchid_base::sym; -use orchid_base::tree::Paren; -use orchid_extension::atom::TAtom; +use orchid_base::{ + Comment, OrcRes, Paren, Parsed, Snippet, Sym, expect_tok, is, report, sym, token_errv, + try_pop_no_fluff, with_reporter, +}; +use orchid_extension::TAtom; use orchid_extension::conv::TryFromExpr; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::parser::{ConstCtx, PSnippet, PTok, PTokTree, ParsCtx, ParsedLine, Parser}; use crate::macros::mactree::{MacTok, MacTree, MacTreeSeq}; @@ -40,7 +38,7 @@ impl Parser for LetLine { Ok(vec![ParsedLine::cnst(&line.sr(), &comments, exported, name, async move |ctx| { let macro_input = MacTok::S(Paren::Round, with_reporter(dealias_mac_v(&aliased, &ctx)).await?).at(sr.pos()); - Ok(call(sym_ref(sym!(macros::resolve)), [new_atom(macro_input)])) + Ok(call(sym!(macros::resolve), new_atom(macro_input))) })]) } } @@ -66,7 +64,7 @@ pub async fn dealias_mac_v(aliased: &MacTreeSeq, ctx: &ConstCtx) -> MacTreeSeq { pub async fn parse_tokv(line: PSnippet<'_>) -> MacTreeSeq { if let Some((idx, arg)) = line.iter().enumerate().find_map(|(i, x)| Some((i, x.as_lambda()?))) { let (head, lambda) = line.split_at(idx as u32); - let (_, body) = lambda.pop_front().unwrap(); + let (_, body) = lambda.split_first().unwrap(); let body = parse_tokv(body).boxed_local().await; let mut all = parse_tokv_no_lambdas(&head).await; match parse_tok(arg).await { diff --git a/orchid-std/src/macros/macro_lib.rs b/orchid-std/src/macros/macro_lib.rs index adfa2d3..6ed97cf 100644 --- a/orchid-std/src/macros/macro_lib.rs +++ b/orchid-std/src/macros/macro_lib.rs @@ -1,8 +1,7 @@ use orchid_base::sym; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_extension::TAtom; use orchid_extension::coroutine_exec::exec; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::tree::{GenMember, fun, prefix}; use crate::macros::mactree::MacTree; @@ -12,37 +11,37 @@ use crate::{HomoTpl, UntypedTuple}; pub async fn gen_macro_lib() -> Vec { prefix("macros", [ - fun(true, "resolve", async |tpl: TAtom| resolve(own(&tpl).await).await), + fun(true, "resolve", async |tpl: TAtom| resolve(tpl.own().await).await), prefix("common", [ build_macro(None, ["..", "_", "="]).finish(), build_macro(Some(1), ["+"]) .rule(mactreev!("...$" lhs 1 macros::common::+ "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::add::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::add::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(Some(1), ["-"]) .rule(mactreev!("...$" lhs 1 macros::common::- "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::sub::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::sub::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(Some(2), ["*"]) .rule(mactreev!("...$" lhs 1 macros::common::* "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::mul::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::mul::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(Some(2), ["/"]) .rule(mactreev!("...$" lhs 1 macros::common::/ "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::div::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::div::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(Some(2), ["%"]) .rule(mactreev!("...$" lhs 1 macros::common::% "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::mod::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::mod::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(Some(3), ["."]) .rule(mactreev!("...$" lhs 1 macros::common::. "...$" rhs 0), [async |[lhs, rhs]| { - call(sym_ref(sym!(std::ops::get::resolve)), [resolve(lhs).await, resolve(rhs).await]) + call(sym!(std::ops::get::resolve), (resolve(lhs).await, resolve(rhs).await)).await }]) .finish(), build_macro(None, ["comma_list", ","]) diff --git a/orchid-std/src/macros/macro_line.rs b/orchid-std/src/macros/macro_line.rs index b299756..d36cf78 100644 --- a/orchid-std/src/macros/macro_line.rs +++ b/orchid-std/src/macros/macro_line.rs @@ -4,16 +4,13 @@ use async_fn_stream::stream; use async_once_cell::OnceCell; use futures::StreamExt; use itertools::Itertools; -use orchid_base::error::{OrcRes, mk_errv, report, with_reporter}; -use orchid_base::interner::is; -use orchid_base::parse::{ - Comment, Parsed, Snippet, expect_end, expect_tok, line_items, token_errv, try_pop_no_fluff, +use orchid_base::{ + Comment, OrcRes, Paren, Parsed, Snippet, Token, clone, expect_end, expect_tok, is, line_items, + mk_errv, report, sym, token_errv, try_pop_no_fluff, with_reporter, }; -use orchid_base::tree::{Paren, Token}; -use orchid_base::{clone, sym}; -use orchid_extension::atom::TAtom; +use orchid_extension::TAtom; use orchid_extension::conv::{ToExpr, TryFromExpr}; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::parser::{PSnippet, ParsCtx, ParsedLine, Parser}; use crate::macros::let_line::{dealias_mac_v, parse_tokv}; @@ -133,7 +130,7 @@ impl Parser for MacroLine { let macro_input = MacTok::S(Paren::Round, with_reporter(dealias_mac_v(&body_mactree, &ctx)).await?) .at(body_sr.pos()); - Ok(call(sym_ref(sym!(macros::resolve)), [new_atom(macro_input)])) + Ok(call(sym!(macros::resolve), new_atom(macro_input))) })) } let mac_cell = Rc::new(OnceCell::new()); diff --git a/orchid-std/src/macros/macro_system.rs b/orchid-std/src/macros/macro_system.rs index 0fba0b7..60925f4 100644 --- a/orchid-std/src/macros/macro_system.rs +++ b/orchid-std/src/macros/macro_system.rs @@ -1,8 +1,6 @@ use never::Never; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle}; -use orchid_base::sym; -use orchid_extension::atom::{AtomDynfo, AtomicFeatures}; +use orchid_base::{Receipt, ReqHandle, Sym, sym}; +use orchid_extension::{AtomOps, AtomicFeatures}; use orchid_extension::lexer::LexerObj; use orchid_extension::other_system::SystemHandle; use orchid_extension::parser::ParserObj; @@ -34,20 +32,22 @@ impl SystemCtor for MacroSystem { impl SystemCard for MacroSystem { type Ctor = Self; type Req = Never; - fn atoms() -> impl IntoIterator>> { + fn atoms() -> impl IntoIterator>> { [ - Some(InstantiateTplCall::dynfo()), - Some(MacTree::dynfo()), - Some(Macro::dynfo()), - Some(PhAtom::dynfo()), - Some(MacroBodyArgCollector::dynfo()), - Some(MatcherAtom::dynfo()), + Some(InstantiateTplCall::ops()), + Some(MacTree::ops()), + Some(Macro::ops()), + Some(PhAtom::ops()), + Some(MacroBodyArgCollector::ops()), + Some(MatcherAtom::ops()), ] } } impl System for MacroSystem { - async fn request<'a>(_: Box + 'a>, req: Never) -> Receipt<'a> { match req {} } - async fn prelude() -> Vec { + async fn request<'a>(&self, _: Box + 'a>, req: Never) -> Receipt<'a> { + match req {} + } + async fn prelude(&self) -> Vec { vec![ sym!(macros::common::+), sym!(macros::common::*), @@ -65,9 +65,9 @@ impl System for MacroSystem { sym!(std::fn::[|>]), ] } - fn lexers() -> Vec { vec![&MacTreeLexer, &PhLexer] } - fn parsers() -> Vec { vec![&LetLine, &MacroLine] } - async fn env() -> Vec { + fn lexers(&self) -> Vec { vec![&MacTreeLexer, &PhLexer] } + fn parsers(&self) -> Vec { vec![&LetLine, &MacroLine] } + async fn env(&self) -> Vec { merge_trivial([gen_macro_lib().await, gen_std_macro_lib().await, gen_match_macro_lib().await]) } } diff --git a/orchid-std/src/macros/macro_value.rs b/orchid-std/src/macros/macro_value.rs index a55a837..18337f4 100644 --- a/orchid-std/src/macros/macro_value.rs +++ b/orchid-std/src/macros/macro_value.rs @@ -2,10 +2,10 @@ use std::borrow::Cow; use std::rc::Rc; use never::Never; -use orchid_base::interner::IStr; -use orchid_base::name::Sym; -use orchid_extension::atom::Atomic; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant}; +use orchid_base::IStr; +use orchid_base::Sym; +use orchid_extension::Atomic; +use orchid_extension::{OwnedAtom, OwnedVariant}; use crate::macros::mactree::MacTreeSeq; use crate::macros::rule::matcher::Matcher; diff --git a/orchid-std/src/macros/mactree.rs b/orchid-std/src/macros/mactree.rs index f916779..044c983 100644 --- a/orchid-std/src/macros/mactree.rs +++ b/orchid-std/src/macros/mactree.rs @@ -6,16 +6,12 @@ use futures::FutureExt; use futures::future::join_all; use hashbrown::HashSet; use orchid_api_derive::Coding; -use orchid_base::error::OrcErrv; -use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants}; -use orchid_base::interner::IStr; -use orchid_base::location::Pos; -use orchid_base::name::Sym; -use orchid_base::tl_cache; -use orchid_base::tree::{Paren, indent}; -use orchid_extension::atom::Atomic; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant}; +use orchid_base::{ + FmtCtx, FmtUnit, Format, IStr, OrcErrv, Paren, Pos, Sym, Variants, indent, tl_cache, +}; +use orchid_extension::Atomic; use orchid_extension::expr::Expr; +use orchid_extension::{OwnedAtom, OwnedVariant}; fn union_rc_sets(seq: impl IntoIterator>>) -> Rc> { let mut acc = Rc::>::default(); @@ -190,8 +186,10 @@ impl Format for MacTok { } .units([body.print(c).await]), Self::Slot => "$SLOT".into(), - Self::Bottom(err) if err.len() == 1 => format!("Bottom({}) ", err.one().unwrap()).into(), - Self::Bottom(err) => format!("Botttom(\n{}) ", indent(&err.to_string())).into(), + Self::Bottom(err) => match err.one() { + Some(err) => format!("Bottom({err}) ").into(), + None => format!("Botttom(\n{}) ", indent(&err.to_string())).into(), + }, } } } @@ -200,7 +198,7 @@ pub async fn mtreev_fmt<'b>( v: impl IntoIterator, c: &(impl FmtCtx + ?Sized), ) -> FmtUnit { - FmtUnit::sequence("", " ", "", None, join_all(v.into_iter().map(|t| t.print(c))).await) + FmtUnit::sequence("", " ", "", true, join_all(v.into_iter().map(|t| t.print(c))).await) } #[derive(Clone, Debug, Hash, PartialEq, Eq)] diff --git a/orchid-std/src/macros/mactree_lexer.rs b/orchid-std/src/macros/mactree_lexer.rs index 66092d1..e201737 100644 --- a/orchid-std/src/macros/mactree_lexer.rs +++ b/orchid-std/src/macros/mactree_lexer.rs @@ -2,10 +2,8 @@ use std::ops::RangeInclusive; use futures::FutureExt; use itertools::chain; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::interner::is; -use orchid_base::tokens::PARENS; -use orchid_base::tree::Paren; +use orchid_base::Paren; +use orchid_base::{OrcRes, PARENS, is, mk_errv}; use orchid_extension::gen_expr::new_atom; use orchid_extension::lexer::{LexContext, Lexer, err_not_applicable}; use orchid_extension::parser::p_tree2gen; diff --git a/orchid-std/src/macros/match_macros.rs b/orchid-std/src/macros/match_macros.rs index 172ffef..edec506 100644 --- a/orchid-std/src/macros/match_macros.rs +++ b/orchid-std/src/macros/match_macros.rs @@ -5,18 +5,13 @@ use futures::future::join_all; use futures::{Stream, StreamExt, stream}; use never::Never; use orchid_api_derive::Coding; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::format::fmt; -use orchid_base::interner::is; -use orchid_base::name::Sym; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, TAtom}; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; +use orchid_base::{OrcRes, Sym, fmt, is, mk_errv, sym}; use orchid_extension::conv::ToExpr; use orchid_extension::coroutine_exec::{ExecHandle, exec}; use orchid_extension::expr::{Expr, ExprHandle}; -use orchid_extension::gen_expr::{GExpr, arg, bot, call, lambda, new_atom, sym_ref}; +use orchid_extension::gen_expr::{GExpr, arg, bot, call, call_v, lam, new_atom}; use orchid_extension::tree::{GenMember, fun, prefix}; +use orchid_extension::{Atomic, OwnedAtom, OwnedVariant, TAtom}; use crate::macros::resolve::resolve; use crate::macros::utils::{build_macro, mactree, mactreev}; @@ -35,8 +30,7 @@ impl MatcherData { h: &mut ExecHandle<'_>, val: impl ToExpr, ) -> OrcRes>> { - h.exec::>>(call(self.matcher().await.to_gen().await, [val.to_gen().await])) - .await + h.exec::>>(call(self.matcher().await, val)).await } pub fn keys(&self) -> impl Stream { stream(async |mut h| { @@ -75,8 +69,7 @@ pub async fn gen_match_macro_lib() -> Vec { "match_one", async |mat: TAtom, value: Expr, then: Expr, default: Expr| { exec(async move |mut h| match mat.run_matcher(&mut h, value).await? { - OrcOpt(Some(values)) => - Ok(call(then.to_gen().await, join_all(values.0.into_iter().map(|x| x.to_gen())).await)), + OrcOpt(Some(values)) => Ok(call_v(then, values.0).await), OrcOpt(None) => Ok(default.to_gen().await), }) .await @@ -93,35 +86,34 @@ pub async fn gen_match_macro_lib() -> Vec { async |[value, rules]| { exec(async move |mut h| { let rule_lines = h - .exec::>>(call(sym_ref(sym!(macros::resolve)), [new_atom( - mactree!(macros::common::semi_list "push" rules.clone();), - )])) + .exec::>>(call( + sym!(macros::resolve), + new_atom(mactree!(macros::common::semi_list "push" rules.clone();)), + )) .await?; let mut rule_atoms = Vec::<(TAtom, Expr)>::new(); for line_mac in rule_lines.0.iter() { let Tpl((matcher, body)) = h - .exec(call(sym_ref(sym!(macros::resolve)), [new_atom( - mactree!(pattern::_row "push" own(line_mac).await ;), - )])) + .exec(call( + sym!(macros::resolve), + new_atom(mactree!(pattern::_row "push" line_mac.own().await ;)), + )) .await?; rule_atoms.push((matcher, body)); } - let base_case = lambda(0, [bot(mk_errv( + let base_case = lam::<0>(bot(mk_errv( is("No branches match").await, "None of the patterns matches this value", [rules.pos()], - ))]); + ))) + .await; let match_expr = stream::iter(rule_atoms.into_iter().rev()) .fold(base_case, async |tail, (mat, body)| { - lambda(0, [call(sym_ref(sym!(pattern::match_one)), [ - mat.to_gen().await, - arg(0), - body.to_gen().await, - call(tail, [arg(0)]), - ])]) + lam::<0>(call(sym!(pattern::match_one), (mat, arg(0), body, call(tail, arg(0))))) + .await }) .await; - Ok(call(match_expr, [resolve(value).await])) + Ok(call(match_expr, resolve(value).await)) }) .await }, @@ -132,16 +124,17 @@ pub async fn gen_match_macro_lib() -> Vec { .rule(mactreev!(pattern::match_rule ( macros::common::_ )), [async |[]| { Ok(new_atom(MatcherAtom { keys: Vec::new(), - matcher: lambda(0, [OrcOpt(Some(Tpl(()))).to_gen().await]).create().await, + matcher: lam::<0>(OrcOpt(Some(Tpl(())))).await.create().await, })) }]) .rule(mactreev!(pattern::_row ( "...$" pattern 0 pattern::=> "...$" value 1 )), [ async |[pattern, mut value]| { exec(async move |mut h| -> OrcRes, GExpr)>> { let Ok(pat) = h - .exec::>(call(sym_ref(sym!(macros::resolve)), [new_atom( - mactree!(pattern::match_rule "push" pattern.clone();), - )])) + .exec::>(call( + sym!(macros::resolve), + new_atom(mactree!(pattern::match_rule "push" pattern.clone();)), + )) .await else { return Err(mk_errv( @@ -175,7 +168,7 @@ pub async fn gen_match_macro_lib() -> Vec { }; Ok(new_atom(MatcherAtom { keys: vec![name.clone()], - matcher: sym_ref(sym!(pattern::ref_body)).to_expr().await, + matcher: sym!(pattern::ref_body).to_expr().await, })) }]) .finish(), diff --git a/orchid-std/src/macros/ph_lexer.rs b/orchid-std/src/macros/ph_lexer.rs index 350718c..7678d29 100644 --- a/orchid-std/src/macros/ph_lexer.rs +++ b/orchid-std/src/macros/ph_lexer.rs @@ -1,13 +1,10 @@ use orchid_api_derive::Coding; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::format::FmtUnit; -use orchid_base::interner::{es, is}; -use orchid_base::parse::{name_char, name_start}; -use orchid_extension::atom::Atomic; -use orchid_extension::atom_thin::{ThinAtom, ThinVariant}; +use orchid_base::{FmtUnit, OrcRes, es, is, mk_errv, name_char, name_start}; +use orchid_extension::Atomic; use orchid_extension::gen_expr::new_atom; use orchid_extension::lexer::{LexContext, Lexer, err_not_applicable}; use orchid_extension::tree::{GenTokTree, x_tok}; +use orchid_extension::{ThinAtom, ThinVariant}; use crate::macros::mactree::{Ph, PhKind}; diff --git a/orchid-std/src/macros/resolve.rs b/orchid-std/src/macros/resolve.rs index 35f36d9..0d5c757 100644 --- a/orchid-std/src/macros/resolve.rs +++ b/orchid-std/src/macros/resolve.rs @@ -1,21 +1,15 @@ +use std::collections::VecDeque; use std::ops::{Add, Range}; use async_fn_stream::stream; -use futures::{FutureExt, StreamExt}; +use futures::{FutureExt, StreamExt, stream}; use hashbrown::{HashMap, HashSet}; use itertools::Itertools; -use orchid_base::error::mk_errv; -use orchid_base::format::fmt; -use orchid_base::interner::is; -use orchid_base::location::Pos; -use orchid_base::logging::log; -use orchid_base::name::{NameLike, Sym, VPath}; -use orchid_base::tree::Paren; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_base::{NameLike, Paren, Pos, Sym, VPath, fmt, is, log, mk_errv}; +use orchid_extension::TAtom; use orchid_extension::conv::ToExpr; -use orchid_extension::coroutine_exec::{ExecHandle, exec}; -use orchid_extension::gen_expr::{GExpr, arg, bot, call, lambda, new_atom, sym_ref}; +use orchid_extension::coroutine_exec::exec; +use orchid_extension::gen_expr::{GExpr, arg, bot, call, call_v, dyn_lambda, new_atom}; use orchid_extension::reflection::{ReflMemKind, refl}; use subslice_offset::SubsliceOffset; use substack::Substack; @@ -37,8 +31,8 @@ pub async fn resolve(val: MacTree) -> GExpr { .to_sym() .await; if let Ok(ReflMemKind::Const) = root.get_by_path(&new_name).await.map(|m| m.kind()) { - let Ok(mac) = h.exec::>(sym_ref(new_name)).await else { continue }; - let mac = own(&mac).await; + let Ok(mac) = h.exec::>(new_name).await else { continue }; + let mac = mac.own().await; macros.entry(mac.0.canonical_name.clone()).or_insert(mac); } } @@ -63,7 +57,7 @@ pub async fn resolve(val: MacTree) -> GExpr { } } } - let mut rctx = ResolveCtx { h, exclusive, priod }; + let mut rctx = ResolveCtx { exclusive, priod }; let gex = resolve_one(&mut rctx, Substack::Bottom, &val).await; writeln!( log("debug"), @@ -85,7 +79,6 @@ pub struct FilteredMacroRecord<'a> { } struct ResolveCtx<'a> { - pub h: ExecHandle<'a>, /// If these overlap, that's a compile-time error pub exclusive: Vec>, /// If these overlap, the priorities decide the order. In case of a tie, the @@ -104,7 +97,7 @@ async fn resolve_one( MacTok::Value(v) => v.clone().to_gen().await, MacTok::Name(n) => match arg_stk.iter().position(|arg| arg == n) { Some(de_bruijn) => arg((arg_stk.len() - 1 - de_bruijn).try_into().unwrap()), - None => sym_ref(n.clone()), + None => n.clone().to_gen().await, }, MacTok::Lambda(arg, body) => { let MacTok::Name(name) = &*arg.tok else { @@ -116,7 +109,7 @@ async fn resolve_one( }; let arg_pos = arg_stk.len() as u64; let arg_stk = arg_stk.push(name.clone()); - lambda(arg_pos, [resolve_seq(ctx, arg_stk, body.clone(), value.pos()).await]) + dyn_lambda(arg_pos, resolve_seq(ctx, arg_stk, body.clone(), value.pos()).await).await }, MacTok::S(Paren::Round, body) => resolve_seq(ctx, arg_stk, body.clone(), value.pos()).await, MacTok::S(..) => bot(mk_errv( @@ -243,7 +236,7 @@ async fn resolve_seq( // backwards so that the non-overlapping ranges remain valid let pos = (state.names().flat_map(|r| r.1).cloned().reduce(Pos::add)) .expect("All macro rules must contain at least one locally defined name"); - let subex = ctx.h.register(mk_body_call(mac, rule, &state, pos.clone()).await).await; + let subex = mk_body_call(mac, rule, &state, pos.clone()).await.to_expr().await; new_val.splice(range, [MacTok::Value(subex).at(pos)]); } }; @@ -261,22 +254,23 @@ async fn resolve_seq( let range = pre.len()..new_val.len() - suf.len(); let pos = (state.names().flat_map(|pair| pair.1).cloned().reduce(Pos::add)) .expect("All macro rules must contain at least one locally defined name"); - let subex = ctx.h.register(mk_body_call(mac, rule, &state, pos.clone()).await).await; + let subex = mk_body_call(mac, rule, &state, pos.clone()).await.to_expr().await; std::mem::drop(state); new_val.splice(range, [MacTok::Value(subex).at(pos)]); } } - let exprs = stream(async |mut h| { + let mut exprs = stream(async |mut h| { for mt in new_val { h.emit(resolve_one(ctx, arg_stk.clone(), &mt).await).await } }) - .collect::>() + .collect::>() .boxed_local() .await; - exprs.into_iter().reduce(|f, x| call(f, [x])).expect( + let first = exprs.pop_front().expect( "We checked first that it isn't empty, and named macros get replaced with their results", - ) + ); + stream::iter(exprs).fold(first, async |f, x| call(f, x).await).await } async fn mk_body_call(mac: &Macro, rule: &Rule, state: &MatchState<'_>, pos: Pos) -> GExpr { @@ -288,5 +282,5 @@ async fn mk_body_call(mac: &Macro, rule: &Rule, state: &MatchState<'_>, pos: Pos new_atom(MacTok::S(Paren::Round, MacTreeSeq::new(vec.iter().cloned())).at(Pos::None)), }); } - call(sym_ref(mac.0.module.suffix([rule.body.clone()]).await), call_args).at(pos.clone()) + call_v(mac.0.module.suffix([rule.body.clone()]).await, call_args).await.at(pos.clone()) } diff --git a/orchid-std/src/macros/rule/any_match.rs b/orchid-std/src/macros/rule/any_match.rs index 6a94576..61c93a9 100644 --- a/orchid-std/src/macros/rule/any_match.rs +++ b/orchid-std/src/macros/rule/any_match.rs @@ -1,4 +1,4 @@ -use orchid_base::name::Sym; +use orchid_base::Sym; use super::scal_match::scalv_match; use super::shared::AnyMatcher; diff --git a/orchid-std/src/macros/rule/build.rs b/orchid-std/src/macros/rule/build.rs index f3b86dc..45ee884 100644 --- a/orchid-std/src/macros/rule/build.rs +++ b/orchid-std/src/macros/rule/build.rs @@ -1,10 +1,7 @@ use futures::FutureExt; use futures::future::join_all; use itertools::Itertools; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::interner::{IStr, is}; -use orchid_base::join_ok; -use orchid_base::side::Side; +use orchid_base::{IStr, OrcRes, Side, is, join_ok, mk_errv}; use super::shared::{AnyMatcher, ScalMatcher, VecMatcher}; use super::vec_attrs::vec_attrs; @@ -137,11 +134,8 @@ async fn mk_scalar(pattern: &MacTree) -> OrcRes { #[cfg(test)] mod test { - use orchid_base::interner::local_interner::local_interner; - use orchid_base::interner::{is, with_interner}; - use orchid_base::location::SrcRange; - use orchid_base::sym; - use orchid_base::tokens::Paren; + use orchid_base::local_interner::local_interner; + use orchid_base::{Paren, SrcRange, is, sym, with_interner}; use test_executors::spin_on; use super::mk_any; diff --git a/orchid-std/src/macros/rule/matcher.rs b/orchid-std/src/macros/rule/matcher.rs index cf7a829..01d59db 100644 --- a/orchid-std/src/macros/rule/matcher.rs +++ b/orchid-std/src/macros/rule/matcher.rs @@ -1,9 +1,8 @@ use std::fmt; use std::rc::Rc; -use orchid_base::error::OrcRes; -use orchid_base::interner::is; -use orchid_base::name::Sym; +use orchid_base::Sym; +use orchid_base::{OrcRes, is}; use super::any_match::any_match; use super::build::mk_any; diff --git a/orchid-std/src/macros/rule/scal_match.rs b/orchid-std/src/macros/rule/scal_match.rs index ceb095b..c783fba 100644 --- a/orchid-std/src/macros/rule/scal_match.rs +++ b/orchid-std/src/macros/rule/scal_match.rs @@ -1,4 +1,4 @@ -use orchid_base::name::Sym; +use orchid_base::Sym; use super::any_match::any_match; use super::shared::ScalMatcher; diff --git a/orchid-std/src/macros/rule/shared.rs b/orchid-std/src/macros/rule/shared.rs index b942162..bf4e223 100644 --- a/orchid-std/src/macros/rule/shared.rs +++ b/orchid-std/src/macros/rule/shared.rs @@ -3,10 +3,8 @@ use std::fmt; use itertools::Itertools; -use orchid_base::interner::IStr; -use orchid_base::name::Sym; -use orchid_base::side::Side; -use orchid_base::tokens::{PARENS, Paren}; +use orchid_base::{PARENS, Paren}; +use orchid_base::{IStr, Side, Sym}; pub enum ScalMatcher { Name(Sym), diff --git a/orchid-std/src/macros/rule/state.rs b/orchid-std/src/macros/rule/state.rs index 1d79e96..8ef65f8 100644 --- a/orchid-std/src/macros/rule/state.rs +++ b/orchid-std/src/macros/rule/state.rs @@ -2,11 +2,9 @@ use std::any::Any; use hashbrown::HashMap; -use orchid_base::interner::IStr; -use orchid_base::join::join_maps; -use orchid_base::location::Pos; -use orchid_base::match_mapping; -use orchid_base::name::Sym; +use orchid_base::Pos; +use orchid_base::Sym; +use orchid_base::{IStr, join_maps, match_mapping}; use crate::macros::MacTree; diff --git a/orchid-std/src/macros/rule/vec_attrs.rs b/orchid-std/src/macros/rule/vec_attrs.rs index a440f8d..c10bbf9 100644 --- a/orchid-std/src/macros/rule/vec_attrs.rs +++ b/orchid-std/src/macros/rule/vec_attrs.rs @@ -1,4 +1,4 @@ -use orchid_base::interner::IStr; +use orchid_base::IStr; use crate::macros::mactree::{Ph, PhKind}; use crate::macros::{MacTok, MacTree}; diff --git a/orchid-std/src/macros/rule/vec_match.rs b/orchid-std/src/macros/rule/vec_match.rs index df17ec8..73bada4 100644 --- a/orchid-std/src/macros/rule/vec_match.rs +++ b/orchid-std/src/macros/rule/vec_match.rs @@ -1,7 +1,7 @@ use std::cmp::Ordering; use itertools::Itertools; -use orchid_base::name::Sym; +use orchid_base::Sym; use super::scal_match::scalv_match; use super::shared::VecMatcher; diff --git a/orchid-std/src/macros/stdlib/option.rs b/orchid-std/src/macros/stdlib/option.rs index 677a399..8d1d11c 100644 --- a/orchid-std/src/macros/stdlib/option.rs +++ b/orchid-std/src/macros/stdlib/option.rs @@ -1,10 +1,10 @@ use futures::StreamExt; use orchid_base::sym; -use orchid_extension::atom::TAtom; +use orchid_extension::TAtom; use orchid_extension::conv::ToExpr; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::tree::{GenMember, fun, prefix}; use crate::macros::match_macros::MatcherAtom; @@ -36,19 +36,15 @@ pub async fn gen_option_macro_lib() -> Vec { .await?; Ok(new_atom(MatcherAtom { keys: sub.keys().collect().await, - matcher: h - .register(call(sym_ref(sym!(std::option::is_some_body)), [sub.to_gen().await])) - .await, + matcher: call(sym!(std::option::is_some_body), sub).await.create().await, })) }) }, ]) - .rule(mactreev!(pattern::match_rule(std::option::none)), [|[]: [_; _]| { - exec(async |mut h| { - Ok(new_atom(MatcherAtom { - keys: vec![], - matcher: h.register(sym_ref(sym!(std::option::is_none_body))).await, - })) + .rule(mactreev!(pattern::match_rule(std::option::none)), [async |[]: [_; _]| { + new_atom(MatcherAtom { + keys: vec![], + matcher: sym!(std::option::is_none_body).to_expr().await, }) }]) .finish(), diff --git a/orchid-std/src/macros/stdlib/record.rs b/orchid-std/src/macros/stdlib/record.rs index 6e4ce71..ba18384 100644 --- a/orchid-std/src/macros/stdlib/record.rs +++ b/orchid-std/src/macros/stdlib/record.rs @@ -1,10 +1,9 @@ use orchid_base::sym; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_extension::TAtom; use orchid_extension::conv::ToExpr; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::tree::{GenMember, prefix}; use crate::macros::resolve::resolve; @@ -17,22 +16,19 @@ pub async fn gen_record_macro_lib() -> Vec { .rule(mactreev!(std::record::r[ "...$" elements 0 ]), [async |[elements]: [_; _]| { exec(async move |mut h| { let tup = h - .exec::>>(call(sym_ref(sym!(macros::resolve)), [new_atom( - mactree!((macros::common::comma_list "push" elements ;)), - )])) + .exec::>>(call( + sym!(macros::resolve), + new_atom(mactree!((macros::common::comma_list "push" elements ;))), + )) .await?; - let mut record = sym_ref(sym!(std::record::empty)); + let mut record = sym!(std::record::empty).to_gen().await; for item_exprh in tup.0 { let Tpl((key, value)) = h .exec::, Expr)>>( - resolve(mactree!(std::record::_row "push" own(&item_exprh).await ;)).await, + resolve(mactree!(std::record::_row "push" item_exprh.own().await ;)).await, ) .await?; - record = call(sym_ref(sym!(std::record::set)), [ - record.to_gen().await, - key.to_gen().await, - value.to_gen().await, - ]); + record = call(sym!(std::record::set), (record, key, value)).await; } Ok(record) }) diff --git a/orchid-std/src/macros/stdlib/tuple.rs b/orchid-std/src/macros/stdlib/tuple.rs index c8e5c7b..7efc692 100644 --- a/orchid-std/src/macros/stdlib/tuple.rs +++ b/orchid-std/src/macros/stdlib/tuple.rs @@ -1,12 +1,10 @@ use futures::{StreamExt, stream}; -use orchid_base::error::OrcRes; -use orchid_base::sym; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_base::{OrcRes, sym}; +use orchid_extension::TAtom; use orchid_extension::conv::ToExpr; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::{GExpr, call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{GExpr, call, new_atom}; use orchid_extension::tree::{GenMember, fun, prefix}; use crate::macros::match_macros::MatcherAtom; @@ -19,19 +17,19 @@ pub async fn gen_tuple_macro_lib() -> Vec { .rule(mactreev!(std::tuple::t [ "...$" elements 0 ]), [|[elements]: [_; _]| { exec(async move |mut h| { let tup = h - .exec::>>(call(sym_ref(sym!(macros::resolve)), [ + .exec::>>(call(sym!(macros::resolve), new_atom(mactree!((macros::common::comma_list "push" elements ;))), - ])) + )) .await?; let val = stream::iter(&tup.0[..]) - .fold(sym_ref(sym!(std::tuple::empty)), async |head, new| { - call(sym_ref(sym!(std::tuple::cat)), [ + .fold(sym!(std::tuple::empty).to_gen().await, async |head, new| { + call(sym!(std::tuple::cat), ( head, - call(sym_ref(sym!(std::tuple::one)), [call( - sym_ref(sym!(macros::resolve)), - [new.clone().to_gen().await], - )]), - ]) + call(sym!(std::tuple::one), call( + sym!(macros::resolve), + new.clone(), + )), + )).await }) .await; Ok(val) @@ -58,25 +56,24 @@ pub async fn gen_tuple_macro_lib() -> Vec { fn parse_tpl(elements: MacTree, tail_matcher: Option) -> impl Future { exec(async move |mut h| -> OrcRes { let tup = h - .exec::>>(call(sym_ref(sym!(macros::resolve)), [new_atom( + .exec::>>(call(sym!(macros::resolve), new_atom( mactree!((macros::common::comma_list "push" elements ;)), - )])) + ))) .await?; let mut subs = Vec::with_capacity(tup.0.len()); for mac_a in &tup.0[..] { - let mac = own(mac_a).await; let sub = h - .exec::>(call(sym_ref(sym!(macros::resolve)), [new_atom( - mactree!(pattern::match_rule ("push" mac ;)), - )])) + .exec::>(call(sym!(macros::resolve), new_atom( + mactree!(pattern::match_rule ("push" mac_a.own().await ;)), + ))) .await?; subs.push(sub); } let tail_matcher = match tail_matcher { Some(mac) => Some( - h.exec::>(call(sym_ref(sym!(macros::resolve)), [new_atom( + h.exec::>(call(sym!(macros::resolve), new_atom( mactree!(pattern::match_rule "push" mac ;), - )])) + ))) .await?, ), None => None, @@ -87,10 +84,10 @@ fn parse_tpl(elements: MacTree, tail_matcher: Option) -> impl Future (), Some(tail_mat) => { let tail_tpl = stream::iter(&value.0[children.0.len()..]) - .fold(sym_ref(sym!(std::tuple::empty)), async |prefix, new| { - call(sym_ref(sym!(std::tuple::cat)), [prefix, new.clone().to_gen().await]) + .fold(sym!(std::tuple::empty).to_gen().await, async |prefix, new| { + call(sym!(std::tuple::cat), (prefix, new.clone())).await }) .await; match tail_mat.run_matcher(&mut h, tail_tpl).await? { diff --git a/orchid-std/src/macros/utils.rs b/orchid-std/src/macros/utils.rs index 7205c87..3beafa2 100644 --- a/orchid-std/src/macros/utils.rs +++ b/orchid-std/src/macros/utils.rs @@ -6,13 +6,11 @@ use futures::StreamExt; use futures::future::LocalBoxFuture; use itertools::{Itertools, chain}; use never::Never; -use orchid_base::interner::is; -use orchid_base::name::{NameLike, Sym, VPath}; -use orchid_extension::atom::{Atomic, TAtom}; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; +use orchid_base::{NameLike, Sym, VPath, is}; use orchid_extension::conv::ToExpr; -use orchid_extension::gen_expr::{GExpr, new_atom, sym_ref}; +use orchid_extension::gen_expr::{GExpr, new_atom}; use orchid_extension::tree::{GenMember, MemKind, cnst, lazy}; +use orchid_extension::{Atomic, OwnedAtom, OwnedVariant, TAtom}; use crate::macros::macro_value::{Macro, MacroData, Rule}; use crate::macros::mactree::MacTreeSeq; @@ -41,10 +39,10 @@ impl OwnedAtom for MacroBodyArgCollector { self.clone().call(arg).await } async fn call(mut self, arg: orchid_extension::expr::Expr) -> GExpr { - let atom = (TAtom::downcast(arg.handle()).await).unwrap_or_else(|_| { + let atom = (TAtom::::downcast(arg.handle()).await).unwrap_or_else(|_| { panic!("This is an intermediary value, the argument types are known in advance") }); - self.args.push(own(&atom).await); + self.args.push(atom.own().await); if self.argc == self.args.len() { (self.cb)(self.args).await.to_gen().await } else { @@ -142,7 +140,7 @@ impl MacroBuilder { .name_with_suffix(is(&format!("__macro__{name}")).await) .to_sym() .await; - MemKind::Const(sym_ref(main_const_name)) + MemKind::Const(main_const_name.to_gen().await) }) }); chain!(main_const, kw_consts, body_consts).collect() @@ -159,29 +157,29 @@ macro_rules! mactreev_impl { (@RECUR $ret:ident) => {}; (@RECUR $ret:ident "..$" $name:ident $prio:literal $($tail:tt)*) => { $ret.push($crate::macros::mactree::MacTok::Ph($crate::macros::mactree::Ph{ - name: orchid_base::interner::is(stringify!($name)).await, + name: orchid_base::is(stringify!($name)).await, kind: $crate::macros::mactree::PhKind::Vector{ at_least_one: false, priority: $prio } - }).at(orchid_base::location::Pos::Inherit)); + }).at(orchid_base::Pos::Inherit)); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident "...$" $name:ident $prio:literal $($tail:tt)*) => { $ret.push($crate::macros::mactree::MacTok::Ph($crate::macros::mactree::Ph{ - name: orchid_base::interner::is(stringify!($name)).await, + name: orchid_base::is(stringify!($name)).await, kind: $crate::macros::mactree::PhKind::Vector{ at_least_one: true, priority: $prio } - }).at(orchid_base::location::Pos::Inherit)); + }).at(orchid_base::Pos::Inherit)); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident "$" $name:ident $($tail:tt)*) => { $ret.push($crate::macros::mactree::MacTok::Ph($crate::macros::mactree::Ph{ - name: orchid_base::interner::is(stringify!(name)).await, + name: orchid_base::is(stringify!(name)).await, kind: $crate::macros::mactree::PhKind::Scalar - }).at(orchid_base::location::Pos::Inherit)); + }).at(orchid_base::Pos::Inherit)); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident "Val" $arg:expr ; $($tail:tt)*) => { $ret.push( $crate::macros::mactree::MacTok::Value($arg) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; @@ -200,16 +198,16 @@ macro_rules! mactreev_impl { }; (@RECUR $ret:ident "l_" $arg:expr ; ($($body:tt)*) $($tail:tt)*) => { $ret.push(MacTok::Lambda( - MacTok::Name($arg).at(orchid_base::location::Pos::Inherit), + MacTok::Name($arg).at(orchid_base::Pos::Inherit), $crate::macros::utils::mactreev!($($body)*) - ).at(orchid_base::location::Pos::Inherit)); + ).at(orchid_base::Pos::Inherit)); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident "l" $argh:tt $(:: $arg:tt)+ ($($body:tt)*) $($tail:tt)*) => { $ret.push(MacTok::Lambda( - MacTok::Name(sym!($argh $(:: $arg)+).await).at(orchid_base::location::Pos::Inherit), + MacTok::Name(sym!($argh $(:: $arg)+).await).at(orchid_base::Pos::Inherit), $crate::macros::utils::mactreev!($($body)*) - ).at(orchid_base::location::Pos::Inherit)); + ).at(orchid_base::Pos::Inherit)); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident $name:literal $($tail:tt)*) => { @@ -218,42 +216,42 @@ macro_rules! mactreev_impl { "{} was treated as a name, but it doesn't have a namespace prefix", $name ); - let sym = orchid_base::name::Sym::parse( + let sym = orchid_base::Sym::parse( $name ).await.expect("Empty string in sym literal in Rust"); $ret.push( $crate::macros::mactree::MacTok::Name(sym) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident ( $($body:tt)* ) $($tail:tt)*) => { $ret.push( $crate::macros::mactree::MacTok::S( - orchid_base::tree::Paren::Round, + orchid_base::Paren::Round, $crate::macros::utils::mactreev!($($body)*) ) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident [ $($body:tt)* ] $($tail:tt)*) => { $ret.push( $crate::macros::mactree::MacTok::S( - orchid_base::tree::Paren::Square, + orchid_base::Paren::Square, $crate::macros::utils::mactreev!($($body)*) ) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; (@RECUR $ret:ident { $($body:tt)* } $($tail:tt)*) => { $ret.push( $crate::macros::mactree::MacTok::S( - orchid_base::tree::Paren::Curly, + orchid_base::Paren::Curly, $crate::macros::utils::mactreev!($($body)*) ) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; @@ -267,7 +265,7 @@ macro_rules! mactreev_impl { let sym = orchid_base::sym!($($munched)*); $ret.push( $crate::macros::mactree::MacTok::Name(sym) - .at(orchid_base::location::Pos::Inherit) + .at(orchid_base::Pos::Inherit) ); $crate::macros::utils::mactreev_impl!(@RECUR $ret $($tail)*); }; diff --git a/orchid-std/src/std/binary/binary_atom.rs b/orchid-std/src/std/binary/binary_atom.rs index d97d677..5e70379 100644 --- a/orchid-std/src/std/binary/binary_atom.rs +++ b/orchid-std/src/std/binary/binary_atom.rs @@ -4,8 +4,8 @@ use std::rc::Rc; use futures::AsyncWrite; use orchid_api_traits::Encode; -use orchid_extension::atom::Atomic; -use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant}; +use orchid_extension::Atomic; +use orchid_extension::{DeserializeCtx, OwnedAtom, OwnedVariant}; #[derive(Clone)] pub struct BlobAtom(pub(crate) Rc>); diff --git a/orchid-std/src/std/binary/binary_lib.rs b/orchid-std/src/std/binary/binary_lib.rs index d38a03b..f0ea6c5 100644 --- a/orchid-std/src/std/binary/binary_lib.rs +++ b/orchid-std/src/std/binary/binary_lib.rs @@ -1,9 +1,7 @@ use std::rc::Rc; -use orchid_base::error::{OrcErrv, mk_errv}; -use orchid_base::interner::is; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_base::{OrcErrv, is, mk_errv}; +use orchid_extension::TAtom; use orchid_extension::func_atom::get_arg_posv; use orchid_extension::gen_expr::new_atom; use orchid_extension::tree::{GenMember, comments, fun, prefix}; @@ -32,7 +30,7 @@ pub fn gen_binary_lib() -> Vec { ["Appends a binary blob to another", "|type: Blob -> Blob -> Blob|"], fun(true, "concat", async |a: TAtom, b: TAtom| { new_atom(BlobAtom(Rc::new( - own(&a).await.0.iter().chain(&own(&b).await.0[..]).copied().collect(), + a.own().await.0.iter().chain(&b.own().await.0[..]).copied().collect(), ))) }), ), @@ -43,7 +41,7 @@ pub fn gen_binary_lib() -> Vec { "|type: Blob -> Int -> Int -> Blob|", ], fun(true, "slice", async |a: TAtom, Int(start): Int, Int(len): Int| { - let blob = own(&a).await; + let blob = a.own().await; if start + len > blob.0.len() as i64 { return Err(bounds_error(format!("{start}+{len}"), &blob, 0..3).await); } @@ -57,8 +55,8 @@ pub fn gen_binary_lib() -> Vec { "|type: Blob -> Blob -> std::option Int|", ], fun(true, "find", async |haystack: TAtom, needle: TAtom| { - let haystack_vec = own(&haystack).await; - let needle_vec = own(&needle).await; + let haystack_vec = haystack.own().await; + let needle_vec = needle.own().await; for i in 0..haystack_vec.0.len() - needle_vec.0.len() { if haystack_vec.0[i..].starts_with(&needle_vec.0) { return OrcOpt(Some(Int(i as i64))); @@ -73,7 +71,7 @@ pub fn gen_binary_lib() -> Vec { "|type: Blob -> Int -> std::tuple Blob Blob|", ], fun(true, "split", async |a: TAtom, i: Int| { - let v = own(&a).await; + let v = a.own().await; if v.0.len() < i.0 as usize { return Err(bounds_error(i.0.to_string(), &v, 1..2).await); } @@ -95,7 +93,7 @@ pub fn gen_binary_lib() -> Vec { true, "get_int", async |bin: TAtom, Int(start): Int, Int(len): Int, Bool(le): Bool| { - let vec = own(&bin).await; + let vec = bin.own().await; if start + len > vec.0.len() as i64 { return Err(bounds_error(format!("{start}+{len}"), &vec, 1..3).await); } @@ -139,7 +137,7 @@ pub fn gen_binary_lib() -> Vec { ), comments( ["Returns the number of bytes in a binary", "|type: Blob -> Int|"], - fun(true, "size", async |blob: TAtom| Int(own(&blob).await.0.len() as i64)), + fun(true, "size", async |blob: TAtom| Int(blob.own().await.0.len() as i64)), ), ]), )]) diff --git a/orchid-std/src/std/boolean.rs b/orchid-std/src/std/boolean.rs index 8e1899a..90bf7e3 100644 --- a/orchid-std/src/std/boolean.rs +++ b/orchid-std/src/std/boolean.rs @@ -1,13 +1,10 @@ use orchid_api_derive::Coding; -use orchid_base::error::OrcRes; -use orchid_base::format::FmtUnit; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, TAtom}; -use orchid_extension::atom_thin::{ThinAtom, ThinVariant}; +use orchid_base::{FmtUnit, OrcRes, sym}; use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::{GExpr, sym_ref}; +use orchid_extension::gen_expr::GExpr; use orchid_extension::tree::{GenMember, cnst, comments, fun, prefix}; +use orchid_extension::{Atomic, TAtom, ThinAtom, ThinVariant}; #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Coding)] pub struct Bool(pub bool); @@ -28,7 +25,7 @@ impl TryFromExpr for Bool { } impl ToExpr for Bool { async fn to_gen(self) -> GExpr { - sym_ref(if self.0 { sym!(std::true) } else { sym!(std::false) }) + if self.0 { sym!(std::true) } else { sym!(std::false) }.to_gen().await } } diff --git a/orchid-std/src/std/future/future_lib.rs b/orchid-std/src/std/future/future_lib.rs new file mode 100644 index 0000000..3d1ab43 --- /dev/null +++ b/orchid-std/src/std/future/future_lib.rs @@ -0,0 +1,350 @@ +use std::borrow::Cow; +use std::cell::RefCell; +use std::cmp::{Ordering, Reverse}; +use std::collections::{BinaryHeap, VecDeque}; +use std::fmt::Debug; +use std::mem; +use std::num::NonZeroU64; +use std::pin::Pin; +use std::rc::Rc; +use std::task::{Context, Poll, Waker}; +use std::time::Instant; + +use async_event::Event; +use chrono::TimeDelta; +use futures::channel::{mpsc, oneshot}; +use futures::{FutureExt, select}; +use hashbrown::HashMap; +use never::Never; +use orchid_api_derive::{Coding, Hierarchy}; +use orchid_api_traits::Request; +use orchid_base::{FmtCtxImpl, OrcRes}; +use orchid_extension::conv::ToExpr; +use orchid_extension::entrypoint::spawn; +use orchid_extension::expr::Expr; +use orchid_extension::gen_expr::{GExpr, IntoGExprStream, call, new_atom}; +use orchid_extension::system::cted; +use orchid_extension::tree::{GenMember, cnst, comments, fun, prefix}; +use orchid_extension::{ + Atomic, CmdResult, Continuation, ForeignAtom, Next, OwnedAtom, OwnedVariant, err_not_callable, + err_not_command, +}; +use rust_decimal::prelude::Zero; +use tokio::task::{JoinHandle, spawn_local}; +use tokio::time::sleep; + +use crate::std::std_system::StdReq; +use crate::std::time::OrcDT; +use crate::{StdSystem, api}; + +#[derive(Clone, Copy, Coding, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct AsyncTaskId(NonZeroU64); + +/// Signals to the scheduler that some async work is in progress, and to take +/// ownership of this expression representing the progress of that work. This +/// doesn't have to be called before [FinishAsyncWork] if keeping the work and +/// thus the requesting system alive is not necessary +#[derive(Debug, Clone, Coding, Hierarchy)] +#[extends(FutureReq, StdReq)] +pub struct AddAsyncWork(pub api::ExprTicket); +impl Request for AddAsyncWork { + type Response = AsyncTaskId; +} + +/// Signals to the scheduler that some async work has been finished, and to +/// return this expression from a future `std::future::yield` call. +/// If [AddAsyncWork] was called before this, include the [AsyncTaskId] you +/// received to unlink the work from the scheduler so that cleanup is not +/// blocked. +#[derive(Debug, Clone, Coding, Hierarchy)] +#[extends(FutureReq, StdReq)] +pub struct FinishAsyncWork(pub Option, pub api::ExprTicket); +impl Request for FinishAsyncWork { + type Response = Result<(), SchedulerError>; +} + +#[derive(Debug, Clone, Coding)] +pub struct SchedulerError; + +#[derive(Debug, Clone, Coding, Hierarchy)] +#[extendable] +#[extends(StdReq)] +pub enum FutureReq { + AddAsyncWork(AddAsyncWork), + FinishAsyncWork(FinishAsyncWork), +} + +#[derive(Clone)] +struct Timer { + set_at: Instant, + delay: TimeDelta, + repetition: Option, + cancelled: Rc, + action: Expr, +} +impl Timer { + pub fn next_occurrence(&self) -> Instant { + let delay_mult = i32::try_from(self.repetition.unwrap_or(0) + 1).unwrap(); + self.set_at + (self.delay * delay_mult).to_std().unwrap() + } +} +impl PartialEq for Timer { + fn eq(&self, other: &Self) -> bool { self.next_occurrence().eq(&other.next_occurrence()) } +} +impl Eq for Timer {} +impl PartialOrd for Timer { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } +} +impl Ord for Timer { + fn cmp(&self, other: &Self) -> Ordering { self.next_occurrence().cmp(&other.next_occurrence()) } +} +impl Atomic for Timer { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for Timer { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn command(mut self) -> CmdResult { + let sleep_until = + self.set_at + (self.delay * self.repetition.unwrap_or(1) as i32).to_std().unwrap(); + let (timer_ready, on_timer_ready) = oneshot::channel(); + let task = spawn(self.delay.to_std().unwrap(), async move { mem::drop(timer_ready.send(())) }); + let res = + self.cancelled.wait_until_or_timeout(|| Some(()), on_timer_ready.map(mem::drop)).await; + task.abort(); + // cancelled + if let Some(()) = res { + return Continuation::default().into(); + } + // TODO: add binary API for sleep and + let mut ret = Continuation::default().into(); + let mut ret = vec![self.action.to_gen().await]; + if let Some(rep) = self.repetition.as_mut() { + *rep = *rep + 1; + ret.push(new_atom(self)); + } + Ok(ret) + } +} + +struct SchedulerState { + /// Waker to call when async work finishes + finish_waker: Waker, + timer_task: Option<(Instant, JoinHandle<()>)>, + id: NonZeroU64, + background: HashMap, + ready: VecDeque, + timers: BinaryHeap>, +} +impl SchedulerState { + fn activate_timers(&mut self, now: Instant) { + while let Some(t) = self.timers.peek() + && t.0.next_occurrence() < now + { + let mut timer = self.timers.pop().unwrap().0; + let work = timer.action.clone(); + self.ready.push_back(work); + if let Some(count) = timer.repetition { + timer.repetition = Some(count + 1); + self.timers.push(Reverse(timer)); + } + } + } +} +impl Debug for SchedulerState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SchedulerState").finish_non_exhaustive() + } +} +impl Default for SchedulerState { + fn default() -> Self { + SchedulerState { + background: HashMap::new(), + finish_waker: Waker::noop().clone(), + id: NonZeroU64::MIN, + timer_task: None, + ready: VecDeque::new(), + timers: BinaryHeap::new(), + } + } +} + +#[derive(Clone, Debug, Default)] +pub struct Scheduler(Rc>); +impl Scheduler { + pub(crate) async fn add(&self, req: &AddAsyncWork) -> ::Response { + let expr = Expr::deserialize(req.0).await; + let mut this = self.0.borrow_mut(); + let id = AsyncTaskId(this.id); + this.background.insert(id, expr); + this.id = this.id.checked_add(1).unwrap(); + id + } + pub(crate) async fn finish( + &self, + req: &FinishAsyncWork, + ) -> ::Response { + let expr = Expr::deserialize(req.1).await; + let mut g = self.0.borrow_mut(); + if let Some(id) = req.0 { + g.background.remove(&id); + } + g.ready.push_back(expr); + g.finish_waker.wake_by_ref(); + Ok(()) + } +} + +#[derive(Clone)] +struct Yield; +impl Atomic for Yield { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for Yield { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn command(self) -> OrcRes<()> { Ok(()) } +} + +#[derive(Clone)] +struct Spawn(ForeignAtom, ForeignAtom); +impl Atomic for Spawn { + type Variant = OwnedVariant; + type Data = [api::ExprTicket; 2]; +} +impl OwnedAtom for Spawn { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { + Cow::Owned([self.0.clone().ex().handle().ticket(), self.1.clone().ex().handle().ticket()]) + } + async fn command(self) -> OrcRes { Ok((self.1, self.0)) } +} + +#[derive(Clone)] +struct Canceller { + cont: Option, + cancel: Rc>>>, +} +impl Atomic for Canceller { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for Canceller { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn call_ref(&self, arg: Expr) -> impl ToExpr { + match &self.cont { + Some(_) => Err(err_not_callable(&self.print_atom(&FmtCtxImpl::default()).await).await), + None => Ok(new_atom(Self { cont: Some(arg), cancel: self.cancel.clone() })), + } + } + async fn command(self) -> OrcRes { + let Some(cont) = self.cont else { + return Err(err_not_command(&self.print_atom(&FmtCtxImpl::default()).await).await); + }; + if let Some(canceller) = self.cancel.borrow_mut().take() { + canceller.send(()); + } + Ok(cont) + } +} + +#[derive(Clone)] +struct SetTimer { + delay: TimeDelta, + recurring: bool, + action: Expr, + cont: Expr, +} +impl Atomic for SetTimer { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for SetTimer { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn command(self) -> OrcRes { + let (send, recv) = oneshot::channel(); + Ok(( + new_atom(Timer { + set_at: Instant::now(), + delay: self.delay, + cancelled: Rc::new(recv), + repetition: self.recurring.then_some(1), + action: self.action, + }), + call( + self.cont, + new_atom(Canceller { cont: None, cancel: Rc::new(RefCell::new(Some(send))) }), + ), + )) + } +} + +pub fn gen_future_lib() -> Vec { + prefix("std", [comments( + [ + "This library exposes a futures executor, and tools for timing and cooperative multitasking. \ + The use of these tools is only possible in a command trampoline, i.e. a caller that always \ + defers to the command implementation of an atom.", + "Any command that correctly integrates with this library should return `std::future::yield` \ + as its final value on all codepaths, which is the (re)entry point of the trampoline. \ + Returning any other command, especially the ones in `std::exit_code` causes the program to \ + immediately exit.", + "Cancellers take a continuation, stop whatever process they are associated with from \ + proceeding, and call the continuation with information about the cancelled work.", + "|type canceller: \\T ((T -> cmd) -> cmd)|", + ], + prefix("future", [ + comments( + [ + "A command without a continuation that defers control to the queued set of commands.", + "|type: cmd|", + ], + cnst(true, "yield", new_atom(Yield)), + ), + comments( + [ + "Takes two commands and queues both to be executed one after the other.", + "|type: cmd -> cmd -> cmd|", + ], + fun(true, "spawn", async |left: ForeignAtom, right: ForeignAtom| { + new_atom(Spawn(left, right)) + }), + ), + comments( + [ + "Takes a time amount to wait, the command to perform after waiting, and a continuation, \ + and returns a command that sets a single-fire timeout. The continuation will be \ + called with a canceller, which reports true if the task has not yet run.", + "|type: Duration -> cmd -> (canceller bool -> cmd) -> cmd|", + ], + fun(true, "timeout", async |OrcDT(delay): OrcDT, action: Expr, cont: Expr| { + new_atom(SetTimer { delay, action, cont, recurring: false }) + }), + ), + comments( + [ + "Takes a time amount to wait between repetitions, the command to perform periodically, \ + and a continuation, and returns a command. The continuation will be called with a \ + canceller, which reports how many times the interval has run.", + "|type: Duration -> cmd -> (canceller Int -> cmd) -> cmd|", + ], + fun(true, "interval", async |OrcDT(delay): OrcDT, action: Expr, cont: Expr| { + new_atom(SetTimer { delay, action, cont, recurring: true }) + }), + ), + ]), + )]) +} + +fn get_scheduler() -> Scheduler { + let cted = cted(); + let std = cted.as_any().downcast_ref::().unwrap(); + let sched = std.sched.get_or_init(Scheduler::default); + sched.clone() +} + +pub struct AsyncTaskAtom {} diff --git a/orchid-std/src/std/future/mod.rs b/orchid-std/src/std/future/mod.rs new file mode 100644 index 0000000..59e5c02 --- /dev/null +++ b/orchid-std/src/std/future/mod.rs @@ -0,0 +1 @@ +pub mod future_lib; diff --git a/orchid-std/src/std/mod.rs b/orchid-std/src/std/mod.rs index 2e1f16a..472a627 100644 --- a/orchid-std/src/std/mod.rs +++ b/orchid-std/src/std/mod.rs @@ -1,5 +1,6 @@ pub mod binary; pub mod boolean; +pub mod future; pub mod number; pub mod ops; pub mod option; @@ -7,5 +8,7 @@ pub mod protocol; pub mod record; pub mod reflection; pub mod std_system; +pub mod stream; pub mod string; +pub mod time; pub mod tuple; diff --git a/orchid-std/src/std/number/num_atom.rs b/orchid-std/src/std/number/num_atom.rs index 5957f2d..af7ae84 100644 --- a/orchid-std/src/std/number/num_atom.rs +++ b/orchid-std/src/std/number/num_atom.rs @@ -2,18 +2,11 @@ use std::io; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use orchid_base::error::OrcRes; -use orchid_base::format::FmtUnit; -use orchid_base::name::Sym; -use orchid_base::number::Numeric; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqHandleExt}; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, MethodSetBuilder, Supports, TAtom}; -use orchid_extension::atom_thin::{ThinAtom, ThinVariant}; +use orchid_base::{FmtUnit, Numeric, OrcRes, Receipt, ReqHandle, ReqHandleExt, Sym, sym}; use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::sym_ref; use orchid_extension::system::sys_req; +use orchid_extension::{Atomic, MethodSetBuilder, Supports, TAtom, ThinAtom, ThinVariant}; use ordered_float::NotNan; use rust_decimal::prelude::Zero; @@ -34,7 +27,7 @@ pub struct Int(pub i64); impl Atomic for Int { type Variant = ThinVariant; type Data = Self; - fn reg_reqs() -> MethodSetBuilder { + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new().handle::().handle::() } } @@ -62,19 +55,19 @@ impl Supports for Int { ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::add) { - sym_ref(sym!(std::number::add)) + sym!(std::number::add) } else if name == sym!(std::ops::sub) { - sym_ref(sym!(std::number::sub)) + sym!(std::number::sub) } else if name == sym!(std::ops::mul) { - sym_ref(sym!(std::number::mul)) + sym!(std::number::mul) } else if name == sym!(std::ops::div) { - sym_ref(sym!(std::number::idiv)) + sym!(std::number::idiv) } else if name == sym!(std::ops::mod) { - sym_ref(sym!(std::number::imod)) + sym!(std::number::imod) } else { return hand.reply(req, &None).await; }; - hand.reply(req, &Some(val.create().await.serialize().await)).await + hand.reply(req, &Some(val.to_expr().await.serialize().await)).await }, } } @@ -101,7 +94,7 @@ pub struct Float(pub NotNan); impl Atomic for Float { type Variant = ThinVariant; type Data = Self; - fn reg_reqs() -> MethodSetBuilder { + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new().handle::().handle::() } } @@ -129,19 +122,19 @@ impl Supports for Float { ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::add) { - sym_ref(sym!(std::number::add)) + sym!(std::number::add) } else if name == sym!(std::ops::sub) { - sym_ref(sym!(std::number::sub)) + sym!(std::number::sub) } else if name == sym!(std::ops::mul) { - sym_ref(sym!(std::number::mul)) + sym!(std::number::mul) } else if name == sym!(std::ops::div) { - sym_ref(sym!(std::number::fdiv)) + sym!(std::number::fdiv) } else if name == sym!(std::ops::mod) { - sym_ref(sym!(std::number::fmod)) + sym!(std::number::fmod) } else { return hand.reply(req, &None).await; }; - hand.reply(req, &Some(val.create().await.serialize().await)).await + hand.reply(req, &Some(val.to_expr().await.serialize().await)).await }, } } @@ -172,11 +165,9 @@ impl TryFromExpr for Num { impl ToExpr for Num { async fn to_gen(self) -> orchid_extension::gen_expr::GExpr { match self.0 { - Numeric::Float(f) => Float(f).to_expr().await, - Numeric::Int(i) => Int(i).to_expr().await, + Numeric::Float(f) => Float(f).to_gen().await, + Numeric::Int(i) => Int(i).to_gen().await, } - .to_gen() - .await } } diff --git a/orchid-std/src/std/number/num_lexer.rs b/orchid-std/src/std/number/num_lexer.rs index 2740a44..b5985f2 100644 --- a/orchid-std/src/std/number/num_lexer.rs +++ b/orchid-std/src/std/number/num_lexer.rs @@ -1,7 +1,6 @@ use std::ops::RangeInclusive; -use orchid_base::error::OrcRes; -use orchid_base::number::{num_to_errv, parse_num}; +use orchid_base::{OrcRes, num_to_errv, parse_num}; use orchid_extension::conv::ToExpr; use orchid_extension::lexer::{LexContext, Lexer}; use orchid_extension::tree::{GenTokTree, x_tok}; @@ -16,8 +15,7 @@ impl Lexer for NumLexer { let ends_at = all.find(|c: char| !c.is_ascii_hexdigit() && !"xX._pP".contains(c)); let (chars, tail) = all.split_at(ends_at.unwrap_or(all.len())); match parse_num(chars) { - Ok(numeric) => - Ok((tail, x_tok(Num(numeric).to_gen().await).await.at(lxcx.pos_lt(chars.len(), tail)))), + Ok(numeric) => Ok((tail, x_tok(Num(numeric)).await.at(lxcx.pos_lt(chars.len(), tail)))), Err(e) => Err(num_to_errv(e, lxcx.pos(all), lxcx.src()).await), } } diff --git a/orchid-std/src/std/number/num_lib.rs b/orchid-std/src/std/number/num_lib.rs index 7701684..1775c84 100644 --- a/orchid-std/src/std/number/num_lib.rs +++ b/orchid-std/src/std/number/num_lib.rs @@ -1,6 +1,4 @@ -use orchid_base::error::mk_errv; -use orchid_base::interner::is; -use orchid_base::number::Numeric; +use orchid_base::{Numeric, is, mk_errv}; use orchid_extension::func_atom::get_arg; use orchid_extension::tree::{GenMember, fun, prefix}; use ordered_float::NotNan; diff --git a/orchid-std/src/std/ops/subscript_lexer.rs b/orchid-std/src/std/ops/subscript_lexer.rs index 4335fca..eb52894 100644 --- a/orchid-std/src/std/ops/subscript_lexer.rs +++ b/orchid-std/src/std/ops/subscript_lexer.rs @@ -1,6 +1,5 @@ -use orchid_base::error::OrcRes; -use orchid_base::interner::is; -use orchid_base::parse::{name_char, name_start}; +use orchid_base::{name_char, name_start}; +use orchid_base::{OrcRes, is}; use orchid_extension::gen_expr::new_atom; use orchid_extension::lexer::{LexContext, LexedData, Lexer, err_not_applicable}; use orchid_extension::tree::GenTok; diff --git a/orchid-std/src/std/option.rs b/orchid-std/src/std/option.rs index d0bd2a0..cda3a5a 100644 --- a/orchid-std/src/std/option.rs +++ b/orchid-std/src/std/option.rs @@ -3,15 +3,12 @@ use std::pin::Pin; use futures::AsyncWrite; use orchid_api_traits::Encode; -use orchid_base::error::mk_errv; -use orchid_base::interner::is; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, ForeignAtom, TAtom}; -use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant}; +use orchid_base::{is, mk_errv, sym}; use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::{Expr, ExprHandle}; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::tree::{GenMember, cnst, fun, prefix}; +use orchid_extension::{Atomic, DeserializeCtx, ForeignAtom, OwnedAtom, OwnedVariant, TAtom}; use crate::{OrcString, api}; @@ -37,7 +34,7 @@ impl OwnedAtom for OptAtom { pub struct OrcOpt(pub Option); impl TryFromExpr for OrcOpt { - async fn try_from_expr(expr: Expr) -> orchid_base::error::OrcRes { + async fn try_from_expr(expr: Expr) -> orchid_base::OrcRes { let atom = TAtom::::try_from_expr(expr).await?; match atom.value { None => Ok(OrcOpt(None)), @@ -50,9 +47,9 @@ impl TryFromExpr for OrcOpt { impl ToExpr for OrcOpt { async fn to_gen(self) -> orchid_extension::gen_expr::GExpr { if let Some(val) = self.0 { - call(sym_ref(sym!(std::option::some)), [val.to_gen().await]) + call(sym!(std::option::some), val).await } else { - sym_ref(sym!(std::option::none)) + sym!(std::option::none).to_gen().await } } } diff --git a/orchid-std/src/std/protocol/parse_impls.rs b/orchid-std/src/std/protocol/parse_impls.rs index aeb0779..a948a44 100644 --- a/orchid-std/src/std/protocol/parse_impls.rs +++ b/orchid-std/src/std/protocol/parse_impls.rs @@ -1,11 +1,10 @@ use itertools::{Itertools, chain}; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::interner::{IStr, is}; -use orchid_base::name::Sym; -use orchid_base::parse::{ +use orchid_base::Sym; +use orchid_base::{ Import, Parsed, Snippet, expect_tok, line_items, parse_multiname, token_errv, }; -use orchid_base::tree::{Paren, Token}; +use orchid_base::{Paren, Token}; +use orchid_base::{IStr, OrcRes, is, mk_errv}; use orchid_extension::parser::{ PTokTree, ParsCtx, ParsedLine, ParsedLineKind, p_tree2gen, p_v2gen, }; diff --git a/orchid-std/src/std/protocol/proto_parser.rs b/orchid-std/src/std/protocol/proto_parser.rs index 08abd1e..c380c6a 100644 --- a/orchid-std/src/std/protocol/proto_parser.rs +++ b/orchid-std/src/std/protocol/proto_parser.rs @@ -1,13 +1,9 @@ use std::rc::Rc; use hashbrown::HashMap; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::interner::is; -use orchid_base::parse::{Comment, Parsed, expect_end, try_pop_no_fluff}; -use orchid_base::sym; -use orchid_base::tree::Token; -use orchid_extension::coroutine_exec::exec; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_base::{Comment, OrcRes, Parsed, Token, expect_end, is, mk_errv, sym, try_pop_no_fluff}; +use orchid_extension::conv::ToExpr; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::parser::{PSnippet, ParsCtx, ParsedLine, Parser}; use crate::std::protocol::parse_impls::parse_impls; @@ -39,17 +35,14 @@ impl Parser for AsProtoParser { let proto_tag_name = is("__protocol_tag__").await; let proto_tag_path = id.suffix([proto_tag_name.clone()]).await; lines.push(ParsedLine::cnst(&line.sr(), &cmts, true, proto_tag_name, async |_ccx| { - exec(async move |mut h| { - let mut new_impls = HashMap::new(); - for (k, v) in impls { - new_impls.insert(k.clone(), h.register(sym_ref(id.suffix([v]).await)).await); - } - new_atom(Tag { id, impls: Rc::new(new_impls) }) - }) - .await + let mut new_impls = HashMap::new(); + for (k, v) in impls { + new_impls.insert(k.clone(), id.suffix([v]).await.to_expr().await); + } + new_atom(Tag { id, impls: Rc::new(new_impls) }) })); lines.push(ParsedLine::cnst(&line.sr(), [], false, is("resolve").await, async move |_| { - call(sym_ref(sym!(std::protocol::resolve)), [sym_ref(proto_tag_path)]) + call(sym!(std::protocol::resolve), proto_tag_path).await })); Ok(lines) } diff --git a/orchid-std/src/std/protocol/type_parser.rs b/orchid-std/src/std/protocol/type_parser.rs index be0774d..a83ee86 100644 --- a/orchid-std/src/std/protocol/type_parser.rs +++ b/orchid-std/src/std/protocol/type_parser.rs @@ -1,13 +1,9 @@ use std::rc::Rc; use hashbrown::HashMap; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::interner::is; -use orchid_base::parse::{Comment, Parsed, expect_end, try_pop_no_fluff}; -use orchid_base::sym; -use orchid_base::tree::Token; -use orchid_extension::coroutine_exec::exec; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_base::{Comment, OrcRes, Parsed, Token, expect_end, is, mk_errv, sym, try_pop_no_fluff}; +use orchid_extension::conv::ToExpr; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::parser::{PSnippet, ParsCtx, ParsedLine, Parser}; use crate::std::protocol::parse_impls::parse_impls; @@ -39,22 +35,19 @@ impl Parser for AsTypeParser { let type_tag_name = is("__type_tag__").await; let type_tag_path = id.suffix([type_tag_name.clone()]).await; lines.push(ParsedLine::cnst(&line.sr(), &cmts, true, type_tag_name, async |_ccx| { - exec(async move |mut h| { - let mut new_impls = HashMap::new(); - for (k, v) in impls { - new_impls.insert(k.clone(), h.register(sym_ref(id.suffix([v]).await)).await); - } - new_atom(Tag { id, impls: Rc::new(new_impls) }) - }) - .await + let mut new_impls = HashMap::new(); + for (k, v) in impls { + new_impls.insert(k.clone(), id.suffix([v]).await.to_expr().await); + } + new_atom(Tag { id, impls: Rc::new(new_impls) }) })); let type_tag_path_1 = type_tag_path.clone(); lines.push(ParsedLine::cnst(&line.sr(), [], false, is("wrap").await, async move |_ccx| { - call(sym_ref(sym!(std::protocol::wrap)), [sym_ref(type_tag_path_1)]) + call(sym!(std::protocol::wrap), type_tag_path_1).await })); let type_tag_path_1 = type_tag_path.clone(); lines.push(ParsedLine::cnst(&line.sr(), [], false, is("unwrap").await, async move |_ccx| { - call(sym_ref(sym!(std::protocol::unwrap)), [sym_ref(type_tag_path_1)]) + call(sym!(std::protocol::unwrap), type_tag_path_1).await })); Ok(lines) } diff --git a/orchid-std/src/std/protocol/types.rs b/orchid-std/src/std/protocol/types.rs index dea2655..2044eaf 100644 --- a/orchid-std/src/std/protocol/types.rs +++ b/orchid-std/src/std/protocol/types.rs @@ -10,19 +10,16 @@ use itertools::Itertools; use never::Never; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::format::fmt; -use orchid_base::interner::{ev, is}; -use orchid_base::name::{NameLike, Sym, VName}; -use orchid_base::reqnot::ReqHandleExt; -use orchid_extension::atom::{AtomMethod, Atomic, ForeignAtom, MethodSetBuilder, Supports, TAtom}; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; +use orchid_base::{NameLike, OrcRes, ReqHandleExt, Sym, VName, ev, fmt, is, mk_errv}; use orchid_extension::conv::{ClonableToExprDyn, ToExpr}; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::{Expr, ExprHandle}; -use orchid_extension::gen_expr::{GExpr, call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{GExpr, call, new_atom}; use orchid_extension::system::sys_req; use orchid_extension::tree::{GenMember, MemKind, cnst, fun, lazy, prefix}; +use orchid_extension::{ + AtomMethod, Atomic, ForeignAtom, MethodSetBuilder, OwnedAtom, OwnedVariant, Supports, TAtom, +}; use crate::std::std_system::StdReq; use crate::{StdSystem, api}; @@ -35,7 +32,7 @@ pub struct Tag { impl Atomic for Tag { type Data = api::TStrv; type Variant = OwnedVariant; - fn reg_reqs() -> MethodSetBuilder { MethodSetBuilder::new().handle::() } + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new().handle::() } } impl OwnedAtom for Tag { type Refs = Never; @@ -44,9 +41,9 @@ impl OwnedAtom for Tag { impl Supports for Tag { async fn handle<'a>( &self, - hand: Box + '_>, + hand: Box + '_>, req: ProtocolMethod, - ) -> std::io::Result> { + ) -> std::io::Result> { match req { ProtocolMethod::GetTagId(req) => hand.reply(&req, &self.id.to_api()).await, ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => @@ -95,22 +92,22 @@ impl OwnedAtom for Tagged { impl Supports for Tagged { async fn handle<'a>( &self, - hand: Box + '_>, + hand: Box + '_>, req: ProtocolMethod, - ) -> io::Result> { + ) -> io::Result> { self.tag.handle(hand, req).await } } pub async fn get_impl(receiver: ForeignAtom, proto: ForeignAtom) -> OrcRes { - let Some(proto_id) = proto.request(GetTagId).await else { + let Some(proto_id) = proto.call(GetTagId).await else { return Err(mk_errv( is("Not a protocol").await, format!("Protocol ({}) does not have a tag ID", fmt(&proto).await), [proto.pos()], )); }; - let Some(impl_val_opt) = receiver.request(GetImpl(proto_id)).await else { + let Some(impl_val_opt) = receiver.call(GetImpl(proto_id)).await else { return Err(mk_errv( is("Receiver not tagged").await, format!("The receiver ({}) does not have a type tag", fmt(&receiver).await), @@ -120,14 +117,14 @@ pub async fn get_impl(receiver: ForeignAtom, proto: ForeignAtom) -> OrcRes if let Some(impl_val) = impl_val_opt { return Ok(Expr::deserialize(impl_val).await); } - let Some(type_id) = receiver.request(GetTagId).await else { + let Some(type_id) = receiver.call(GetTagId).await else { return Err(mk_errv( is("Incorrect protocols implementation in extension").await, format!("The receiver ({}) provides an impl table but no tag ID", fmt(&receiver).await), [receiver.pos()], )); }; - let Some(impl_val_opt) = proto.request(GetImpl(type_id)).await else { + let Some(impl_val_opt) = proto.call(GetImpl(type_id)).await else { return Err(mk_errv( is("Incorrect protocols implementation in extension").await, format!("Protocol ({}) provides a tag ID but no impl table", fmt(&proto).await), @@ -147,16 +144,16 @@ pub async fn get_impl(receiver: ForeignAtom, proto: ForeignAtom) -> OrcRes pub fn gen_protocol_lib() -> Vec { prefix("std::protocol", [ fun(false, "resolve", async |tag: ForeignAtom, value: ForeignAtom| { - Ok(call(get_impl(value.clone(), tag).await?.to_gen().await, [value.to_gen().await])) + Ok(call(get_impl(value.clone(), tag).await?, value)) }), fun(false, "wrap", async |tag: TAtom, value: Expr| { - new_atom(Tagged { tag: own(&tag).await, value }) + new_atom(Tagged { tag: tag.own().await, value }) }), fun(false, "unwrap", async |tag: TAtom, value: TAtom| { - let own_tag = own(&tag).await; - let own_val = own(&value).await; + let own_tag = tag.own().await; + let own_val = value.own().await; if own_val.tag.id == own_tag.id { - Ok(own_val.value.to_gen().await) + Ok(own_val.value) } else { Err(mk_errv( is("Type mismatch").await, @@ -259,14 +256,13 @@ pub fn resolver_for(proto: VName) -> impl AsyncFn(ForeignAtom) -> GExpr + Clone let proto = match cached_proto { Some(val) => val, None => { - let proto: ForeignAtom = h - .exec(sym_ref(proto.clone().suffix([is("__protocol_tag__").await]).to_sym().await)) - .await?; + let proto: ForeignAtom = + h.exec(proto.clone().suffix([is("__protocol_tag__").await]).to_sym().await).await?; *proto_cache.borrow_mut() = Some(proto.clone()); proto }, }; - Ok(call(get_impl(atom.clone(), proto).await?.to_gen().await, [atom.to_gen().await])) + Ok(call(get_impl(atom.clone(), proto).await?, atom)) }) .await } diff --git a/orchid-std/src/std/record/record_atom.rs b/orchid-std/src/std/record/record_atom.rs index 587d87f..acaf0d6 100644 --- a/orchid-std/src/std/record/record_atom.rs +++ b/orchid-std/src/std/record/record_atom.rs @@ -8,14 +8,12 @@ use futures::future::join_all; use hashbrown::HashMap; use orchid_api_derive::Coding; use orchid_api_traits::{Encode, Request}; -use orchid_base::interner::{IStr, es}; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqHandleExt}; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, MethodSetBuilder, Supports}; -use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant}; +use orchid_base::{IStr, Receipt, ReqHandle, ReqHandleExt, Sym, es, sym}; +use orchid_extension::conv::ToExpr; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::sym_ref; +use orchid_extension::{ + Atomic, DeserializeCtx, MethodSetBuilder, OwnedAtom, OwnedVariant, Supports, +}; use crate::api; use crate::std::protocol::types::{GetImpl, ProtocolMethod}; @@ -25,7 +23,7 @@ pub struct RecordAtom(pub Rc>); impl Atomic for RecordAtom { type Data = (); type Variant = OwnedVariant; - fn reg_reqs() -> MethodSetBuilder { MethodSetBuilder::new().handle::() } + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new().handle::() } } impl OwnedAtom for RecordAtom { type Refs = Vec; @@ -54,13 +52,13 @@ impl Supports for RecordAtom { ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::get) { - sym_ref(sym!(std::record::get)) + sym!(std::record::get) } else if name == sym!(std::ops::set) { - sym_ref(sym!(std::record::set)) + sym!(std::record::set) } else { return hand.reply(req, &None).await; }; - return hand.reply(req, &Some(val.create().await.serialize().await)).await; + return hand.reply(req, &Some(val.to_expr().await.serialize().await)).await; }, } } diff --git a/orchid-std/src/std/record/record_lib.rs b/orchid-std/src/std/record/record_lib.rs index 72a34e8..fa90f3c 100644 --- a/orchid-std/src/std/record/record_lib.rs +++ b/orchid-std/src/std/record/record_lib.rs @@ -2,10 +2,8 @@ use std::rc::Rc; use hashbrown::HashMap; use itertools::Itertools; -use orchid_base::error::mk_errv; -use orchid_base::interner::is; -use orchid_extension::atom::TAtom; -use orchid_extension::atom_owned::own; +use orchid_base::{is, mk_errv}; +use orchid_extension::TAtom; use orchid_extension::expr::Expr; use orchid_extension::gen_expr::{arg, new_atom}; use orchid_extension::tree::{GenMember, cnst, fun, prefix}; @@ -17,12 +15,12 @@ pub fn gen_record_lib() -> Vec { prefix("std::record", [ cnst(true, "empty", new_atom(RecordAtom(Rc::new(HashMap::new())))), fun(true, "set", async |map: TAtom, key: IntStrAtom, val: Expr| { - let mut map = own(&map).await.0.as_ref().clone(); + let mut map = map.own().await.0.as_ref().clone(); map.insert(key.0.clone(), val); new_atom(RecordAtom(Rc::new(map))) }), fun(true, "get", async |map: TAtom, key: IntStrAtom| { - let record = own(&map).await; + let record = map.own().await; match record.0.get(&key.0) { Some(val) => Ok(val.clone()), None => Err(mk_errv( @@ -33,7 +31,7 @@ pub fn gen_record_lib() -> Vec { } }), fun(true, "delete", async |map: TAtom, key: IntStrAtom| { - let mut map = own(&map).await.0.as_ref().clone(); + let mut map = map.own().await.0.as_ref().clone(); map.remove(&key.0); new_atom(RecordAtom(Rc::new(map))) }), diff --git a/orchid-std/src/std/reflection/sym_atom.rs b/orchid-std/src/std/reflection/sym_atom.rs index 2c31f1e..57633d6 100644 --- a/orchid-std/src/std/reflection/sym_atom.rs +++ b/orchid-std/src/std/reflection/sym_atom.rs @@ -2,12 +2,9 @@ use std::borrow::Cow; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use orchid_base::error::mk_errv; -use orchid_base::interner::{es, is}; -use orchid_base::name::{NameLike, Sym}; -use orchid_base::reqnot::ReqHandleExt; -use orchid_extension::atom::{Atomic, Supports, TAtom}; -use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own}; +use orchid_base::{NameLike, ReqHandleExt, Sym, es, is, mk_errv}; +use orchid_extension::{Atomic, Supports, TAtom}; +use orchid_extension::{OwnedAtom, OwnedVariant}; use orchid_extension::expr::{Expr, ExprHandle}; use orchid_extension::gen_expr::new_atom; use orchid_extension::system::sys_req; @@ -33,9 +30,9 @@ impl OwnedAtom for SymAtom { impl Supports for SymAtom { async fn handle<'a>( &self, - hand: Box + '_>, + hand: Box + '_>, req: ToStringMethod, - ) -> std::io::Result> { + ) -> std::io::Result> { hand.reply(&req, &self.0.to_string()).await } } @@ -66,7 +63,7 @@ pub async fn gen_sym_lib() -> Vec { } }), fun(true, "to_tpl", async move |sym: TAtom| { - HomoTpl(own(&sym).await.0.segs().map(|seg| new_atom(IntStrAtom(seg))).collect()) + HomoTpl(sym.own().await.0.segs().map(|seg| new_atom(IntStrAtom(seg))).collect()) }), ]) } diff --git a/orchid-std/src/std/std_system.rs b/orchid-std/src/std/std_system.rs index 8958261..7b149aa 100644 --- a/orchid-std/src/std/std_system.rs +++ b/orchid-std/src/std/std_system.rs @@ -1,12 +1,10 @@ +use std::cell::OnceCell; use std::rc::Rc; use futures::future::join_all; use orchid_api_derive::{Coding, Hierarchy}; -use orchid_base::interner::es; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqHandleExt}; -use orchid_base::sym; -use orchid_extension::atom::{AtomDynfo, AtomicFeatures}; +use orchid_base::{Receipt, ReqHandle, ReqHandleExt, Sym, es, sym}; +use orchid_extension::{AtomOps, AtomicFeatures}; use orchid_extension::conv::ToExpr; use orchid_extension::expr::Expr; use orchid_extension::gen_expr::new_atom; @@ -22,6 +20,7 @@ use super::string::str_lib::gen_str_lib; use crate::std::binary::binary_atom::BlobAtom; use crate::std::binary::binary_lib::gen_binary_lib; use crate::std::boolean::gen_bool_lib; +use crate::std::future::future_lib::{FutureReq, Scheduler, gen_future_lib}; use crate::std::number::num_atom::{CreateFloat, CreateInt}; use crate::std::number::num_lexer::NumLexer; use crate::std::ops::gen_ops_lib; @@ -33,7 +32,9 @@ use crate::std::protocol::types::{CreateTag, Tag, Tagged, gen_protocol_lib}; use crate::std::record::record_atom::{CreateRecord, RecordAtom}; use crate::std::record::record_lib::gen_record_lib; use crate::std::reflection::sym_atom::{CreateSymAtom, SymAtom, gen_sym_lib}; +use crate::std::stream::stream_lib::gen_stream_lib; use crate::std::string::str_lexer::StringLexer; +use crate::std::time::{CreateDT, gen_time_lib}; use crate::std::tuple::{CreateTuple, Tuple, TupleBuilder, gen_tuple_lib}; use crate::{Float, Int}; @@ -43,47 +44,61 @@ use crate::{Float, Int}; pub enum StdReq { CreateInt(CreateInt), CreateFloat(CreateFloat), + CreateDT(CreateDT), CreateTag(CreateTag), CreateTuple(CreateTuple), CreateRecord(CreateRecord), CreateSymAtom(CreateSymAtom), + FutureReq(FutureReq), } #[derive(Debug, Default)] -pub struct StdSystem; +pub struct StdSystem { + pub(crate) sched: OnceCell, +} impl SystemCtor for StdSystem { type Deps = (); type Instance = Self; const NAME: &'static str = "orchid::std"; const VERSION: f64 = 0.00_01; - fn inst(&self, _: ()) -> Self::Instance { Self } + fn inst(&self, _: ()) -> Self::Instance { Self::default() } } impl SystemCard for StdSystem { type Ctor = Self; type Req = StdReq; - fn atoms() -> impl IntoIterator>> { + fn atoms() -> impl IntoIterator>> { [ - Some(BlobAtom::dynfo()), - Some(Int::dynfo()), - Some(Float::dynfo()), - Some(StrAtom::dynfo()), - Some(IntStrAtom::dynfo()), - Some(OptAtom::dynfo()), - Some(RecordAtom::dynfo()), - Some(Tuple::dynfo()), - Some(TupleBuilder::dynfo()), - Some(Tag::dynfo()), - Some(Tagged::dynfo()), + Some(BlobAtom::ops()), + Some(Int::ops()), + Some(Float::ops()), + Some(StrAtom::ops()), + Some(IntStrAtom::ops()), + Some(OptAtom::ops()), + Some(RecordAtom::ops()), + Some(Tuple::ops()), + Some(TupleBuilder::ops()), + Some(Tag::ops()), + Some(Tagged::ops()), ] } } impl System for StdSystem { - async fn request<'a>(xreq: Box + 'a>, req: Self::Req) -> Receipt<'a> { + async fn request<'a>(&self, xreq: Box + 'a>, req: Self::Req) -> Receipt<'a> { match req { + StdReq::FutureReq(req) => { + let sched = self.sched.get_or_init(Scheduler::default); + match req { + FutureReq::AddAsyncWork(req) => xreq.reply(&req, &sched.add(&req).await).await.unwrap(), + FutureReq::FinishAsyncWork(req) => + xreq.reply(&req, &sched.finish(&req).await).await.unwrap(), + } + }, StdReq::CreateInt(ref req @ CreateInt(int)) => xreq.reply(req, &new_atom(int).to_expr().await.serialize().await).await.unwrap(), StdReq::CreateFloat(ref req @ CreateFloat(float)) => xreq.reply(req, &new_atom(float).to_expr().await.serialize().await).await.unwrap(), + StdReq::CreateDT(ref req @ CreateDT(dt)) => + xreq.reply(req, &new_atom(dt).to_expr().await.serialize().await).await.unwrap(), StdReq::CreateTuple(ref req @ CreateTuple(ref items)) => { let tpl = Tuple(Rc::new(join_all(items.iter().copied().map(Expr::deserialize)).await)); let tk = new_atom(tpl).to_expr().await.serialize().await; @@ -118,9 +133,11 @@ impl System for StdSystem { }, } } - fn lexers() -> Vec { vec![&StringLexer, &NumLexer, &SubscriptLexer] } - fn parsers() -> Vec { vec![&AsTypeParser, &TypeParser, &AsProtoParser, &ProtoParser] } - async fn env() -> Vec { + fn lexers(&self) -> Vec { vec![&StringLexer, &NumLexer, &SubscriptLexer] } + fn parsers(&self) -> Vec { + vec![&AsTypeParser, &TypeParser, &AsProtoParser, &ProtoParser] + } + async fn env(&self) -> Vec { merge_trivial([ gen_bool_lib(), gen_num_lib(), @@ -132,9 +149,12 @@ impl System for StdSystem { gen_sym_lib().await, gen_ops_lib(), gen_binary_lib(), + gen_stream_lib(), + gen_time_lib(), + gen_future_lib(), ]) } - async fn prelude() -> Vec { + async fn prelude(&self) -> Vec { vec![sym!(std), sym!(std::tuple), sym!(std::option), sym!(std::record), sym!(std::string)] } } diff --git a/orchid-std/src/std/stream/mod.rs b/orchid-std/src/std/stream/mod.rs new file mode 100644 index 0000000..5483319 --- /dev/null +++ b/orchid-std/src/std/stream/mod.rs @@ -0,0 +1,2 @@ +pub mod stream_cmds; +pub mod stream_lib; diff --git a/orchid-std/src/std/stream/stream_cmds.rs b/orchid-std/src/std/stream/stream_cmds.rs new file mode 100644 index 0000000..b5a3d9f --- /dev/null +++ b/orchid-std/src/std/stream/stream_cmds.rs @@ -0,0 +1,48 @@ +use std::borrow::Cow; +use std::rc::Rc; + +use never::Never; +use orchid_base::{OrcRes, fmt, is, mk_errv}; +use orchid_extension::expr::Expr; +use orchid_extension::gen_expr::{GExpr, bot, call, new_atom}; +use orchid_extension::stream_reqs::{ReadLimit, ReadReq}; +use orchid_extension::{Atomic, ForeignAtom, OwnedAtom, OwnedVariant}; + +use crate::std::binary::binary_atom::BlobAtom; + +#[derive(Clone, Debug)] +pub struct ReadStreamCmd { + pub hand: ForeignAtom, + pub limit: ReadLimit, + pub succ: Expr, + pub fail: Expr, +} +impl Atomic for ReadStreamCmd { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for ReadStreamCmd { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn command(self) -> OrcRes> { + match self.hand.call(ReadReq(self.limit)).await { + None => Err(mk_errv( + is("Atom is not readable").await, + format!("Expected a readable stream handle, found {}", fmt(&self.hand).await), + [self.hand.pos()], + )), + Some(Err(e)) => Ok(Some( + call( + self.fail, + bot(mk_errv( + is(e.kind.message()).await, + format!("An error occurred while reading: {}", e.message), + [self.hand.pos(), self.succ.pos().await], + )), + ) + .await, + )), + Some(Ok(v)) => Ok(Some(call(self.succ, new_atom(BlobAtom(Rc::new(v)))).await)), + } + } +} diff --git a/orchid-std/src/std/stream/stream_lib.rs b/orchid-std/src/std/stream/stream_lib.rs new file mode 100644 index 0000000..d29305d --- /dev/null +++ b/orchid-std/src/std/stream/stream_lib.rs @@ -0,0 +1,37 @@ +use orchid_base::{is, mk_errv}; +use orchid_extension::ForeignAtom; +use orchid_extension::expr::Expr; +use orchid_extension::func_atom::get_arg; +use orchid_extension::gen_expr::new_atom; +use orchid_extension::stream_reqs::ReadLimit; +use orchid_extension::tree::{GenMember, comments, fun, prefix}; + +use crate::Int; +use crate::std::stream::stream_cmds::ReadStreamCmd; + +pub fn gen_stream_lib() -> Vec { + prefix("std", [comments( + ["Read from and write to byte streams"], + prefix("stream", [ + fun(true, "read_bin", async |hand: ForeignAtom, succ: Expr, fail: Expr| { + new_atom(ReadStreamCmd { hand, succ, fail, limit: ReadLimit::End }) + }), + fun(true, "read_until", async |hand: ForeignAtom, delim: Int, succ: Expr, fail: Expr| { + let Ok(end) = delim.0.try_into() else { + return Err(mk_errv( + is("Byte out of range").await, + format!( + "{} doesn't fit into a byte and cannot be used as a delimiter for reading", + delim.0 + ), + [get_arg(1).pos().await], + )); + }; + Ok(new_atom(ReadStreamCmd { hand, succ, fail, limit: ReadLimit::Delimiter(end) })) + }), + fun(true, "read_bytes", async |hand: ForeignAtom, count: Int, succ: Expr, fail: Expr| { + Int(todo!()) + }), + ]), + )]) +} diff --git a/orchid-std/src/std/string/str_atom.rs b/orchid-std/src/std/string/str_atom.rs index 8314ebe..315508c 100644 --- a/orchid-std/src/std/string/str_atom.rs +++ b/orchid-std/src/std/string/str_atom.rs @@ -7,17 +7,14 @@ use std::rc::Rc; use futures::AsyncWrite; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::{Encode, Request}; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::format::{FmtCtx, FmtUnit}; -use orchid_base::interner::{IStr, es, is}; -use orchid_base::name::Sym; -use orchid_base::reqnot::{Receipt, ReqHandle, ReqHandleExt}; -use orchid_base::sym; -use orchid_extension::atom::{AtomMethod, Atomic, MethodSetBuilder, Supports, TAtom}; -use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant}; -use orchid_extension::conv::TryFromExpr; +use orchid_base::{ + FmtCtx, FmtUnit, IStr, OrcRes, Receipt, ReqHandle, ReqHandleExt, Sym, es, is, mk_errv, sym, +}; +use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::Expr; -use orchid_extension::gen_expr::sym_ref; +use orchid_extension::{ + AtomMethod, Atomic, DeserializeCtx, MethodSetBuilder, OwnedAtom, OwnedVariant, Supports, TAtom, +}; use crate::std::protocol::types::{GetImpl, ProtocolMethod}; use crate::std::string::to_string::ToStringMethod; @@ -36,7 +33,7 @@ pub struct StrAtom(Rc); impl Atomic for StrAtom { type Variant = OwnedVariant; type Data = (); - fn reg_reqs() -> MethodSetBuilder { + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new() .handle::() .handle::() @@ -92,11 +89,11 @@ impl Supports for StrAtom { ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::add) { - sym_ref(sym!(std::string::concat)) + sym!(std::string::concat) } else { return hand.reply(req, &None).await; }; - hand.reply(req, &Some(val.create().await.serialize().await)).await + hand.reply(req, &Some(val.to_expr().await.serialize().await)).await }, } } @@ -107,7 +104,7 @@ pub struct IntStrAtom(pub(crate) IStr); impl Atomic for IntStrAtom { type Variant = OwnedVariant; type Data = orchid_api::TStr; - fn reg_reqs() -> MethodSetBuilder { + fn reg_methods() -> MethodSetBuilder { MethodSetBuilder::new().handle::().handle::() } } @@ -154,11 +151,11 @@ impl Supports for IntStrAtom { ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::add) { - sym_ref(sym!(std::string::concat)) + sym!(std::string::concat) } else { return hand.reply(req, &None).await; }; - hand.reply(req, &Some(val.create().await.serialize().await)).await + hand.reply(req, &Some(val.to_expr().await.serialize().await)).await }, } } @@ -177,7 +174,7 @@ impl OrcString { pub async fn get_string(&self) -> Rc { match &self.kind { OrcStringKind::Int(tok) => es(**tok).await.rc(), - OrcStringKind::Val(atom) => atom.request(StringGetValMethod).await, + OrcStringKind::Val(atom) => atom.call(StringGetValMethod).await, } } } diff --git a/orchid-std/src/std/string/str_lexer.rs b/orchid-std/src/std/string/str_lexer.rs index 2ce2569..d3cf925 100644 --- a/orchid-std/src/std/string/str_lexer.rs +++ b/orchid-std/src/std/string/str_lexer.rs @@ -1,11 +1,7 @@ use itertools::Itertools; -use orchid_base::error::{OrcErr, OrcErrv, OrcRes, mk_errv}; -use orchid_base::interner::is; -use orchid_base::location::SrcRange; -use orchid_base::name::Sym; -use orchid_base::sym; -use orchid_base::tree::{Paren, wrap_tokv}; -use orchid_extension::gen_expr::{new_atom, sym_ref}; +use orchid_base::{OrcErr, OrcErrv, OrcRes, Paren, SrcRange, Sym, is, mk_errv, sym, wrap_tokv}; +use orchid_extension::conv::ToExpr; +use orchid_extension::gen_expr::new_atom; use orchid_extension::lexer::{LexContext, Lexer, err_not_applicable}; use orchid_extension::parser::p_tree2gen; use orchid_extension::tree::{GenTok, GenTokTree, ref_tok, x_tok}; @@ -136,7 +132,7 @@ impl Lexer for StringLexer { let (new_tail, tree) = lctx.recurse(rest).await?; tail = new_tail; // wrap the received token in a call to to_str - let to_str = sym_ref(sym!(std::string::to_str)); + let to_str = sym!(std::string::to_str).to_gen().await; let sr = tree.sr(); let inj_to_str_tok = GenTok::NewExpr(to_str).at(sr.map_range(|_| sr.start()..sr.start())); let to_str_call = GenTok::S(Paren::Round, vec![inj_to_str_tok, p_tree2gen(tree)]).at(sr); diff --git a/orchid-std/src/std/string/str_lib.rs b/orchid-std/src/std/string/str_lib.rs index dc3f7dd..57582da 100644 --- a/orchid-std/src/std/string/str_lib.rs +++ b/orchid-std/src/std/string/str_lib.rs @@ -1,15 +1,11 @@ use std::rc::Rc; -use orchid_base::error::mk_errv; -use orchid_base::format::fmt; -use orchid_base::interner::is; -use orchid_base::sym; -use orchid_extension::atom::ForeignAtom; -use orchid_extension::conv::ToExpr; +use orchid_base::{fmt, is, mk_errv, sym}; +use orchid_extension::ForeignAtom; use orchid_extension::coroutine_exec::exec; use orchid_extension::expr::Expr; use orchid_extension::func_atom::get_arg; -use orchid_extension::gen_expr::{call, new_atom, sym_ref}; +use orchid_extension::gen_expr::{call, new_atom}; use orchid_extension::tree::{GenMember, comments, fun, prefix}; use unicode_segmentation::UnicodeSegmentation; @@ -154,13 +150,13 @@ pub fn gen_str_lib() -> Vec { fun(true, "to_str", async |input: Expr| { exec(async move |mut h| { if let Ok(atom) = h.exec::(input.clone()).await { - if let Some(str) = atom.request(ToStringMethod).await { + if let Some(str) = atom.call(ToStringMethod).await { return new_atom(StrAtom::new(Rc::new(str))); } - let proto_ref = sym_ref(sym!(std::string::to_string::__protocol_tag__)); + let proto_ref = sym!(std::string::to_string::__protocol_tag__); let proto = h.exec(proto_ref).await.expect("This protocol is defined in this system"); if let Ok(cb) = get_impl(atom.clone(), proto).await { - return call(cb.to_gen().await, [atom.to_gen().await]).to_gen().await; + return call(cb, atom).await; } } return new_atom(StrAtom::new(Rc::new(fmt(&input).await))); diff --git a/orchid-std/src/std/string/to_string.rs b/orchid-std/src/std/string/to_string.rs index e498cfe..a38688a 100644 --- a/orchid-std/src/std/string/to_string.rs +++ b/orchid-std/src/std/string/to_string.rs @@ -1,6 +1,6 @@ use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use orchid_extension::atom::AtomMethod; +use orchid_extension::AtomMethod; /// Method version of std::string::to_string protocol for atoms #[derive(Coding, Clone, Debug, Hierarchy)] diff --git a/orchid-std/src/std/time.rs b/orchid-std/src/std/time.rs new file mode 100644 index 0000000..dee0cba --- /dev/null +++ b/orchid-std/src/std/time.rs @@ -0,0 +1,98 @@ +use std::borrow::Cow; +use std::time::Instant; + +use chrono::TimeDelta; +use never::Never; +use orchid_api::ExprTicket; +use orchid_api_derive::{Coding, Hierarchy}; +use orchid_api_traits::Request; +use orchid_base::{Numeric, OrcRes}; +use orchid_extension::conv::{ToExpr, TryFromExpr}; +use orchid_extension::expr::Expr; +use orchid_extension::gen_expr::{GExpr, call, new_atom}; +use orchid_extension::system::sys_req; +use orchid_extension::tree::{GenMember, fun, prefix}; +use orchid_extension::{Atomic, OwnedAtom, OwnedVariant, TAtom, ThinAtom, ThinVariant}; +use ordered_float::NotNan; + +use crate::std::std_system::StdReq; +use crate::{Float, Int, Num, StdSystem}; + +#[derive(Clone, Copy, Debug, Coding, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Hierarchy)] +#[extends(StdReq)] +pub struct CreateDT(pub OrcDT); +impl Request for CreateDT { + type Response = ExprTicket; +} + +#[derive(Clone, Copy, Debug, Coding, Default, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct OrcDT(pub TimeDelta); +impl Atomic for OrcDT { + type Variant = ThinVariant; + type Data = Self; +} +impl ThinAtom for OrcDT {} +impl ToExpr for OrcDT { + async fn to_gen(self) -> GExpr { + Expr::deserialize(sys_req::(CreateDT(self)).await).await.to_gen().await + } +} +impl TryFromExpr for OrcDT { + async fn try_from_expr(expr: Expr) -> OrcRes { + Ok(TAtom::::try_from_expr(expr).await?.value) + } +} + +#[derive(Clone)] +pub struct InstantAtom(Instant); +impl Atomic for InstantAtom { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for InstantAtom { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } +} + +#[derive(Clone)] +struct Now(Expr); +impl Atomic for Now { + type Variant = OwnedVariant; + type Data = (); +} +impl OwnedAtom for Now { + type Refs = Never; + async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) } + async fn command(self) -> OrcRes> { + Ok(Some(call(self.0, new_atom(InstantAtom(Instant::now()))))) + } +} + +pub fn gen_time_lib() -> Vec { + prefix("std::time", [ + fun(true, "weeks", async |amount: Int| new_atom(OrcDT(TimeDelta::weeks(amount.0)))), + fun(true, "num_weeks", async |amount: TAtom| Int(amount.0.num_weeks())), + fun(true, "days", async |amount: Int| new_atom(OrcDT(TimeDelta::days(amount.0)))), + fun(true, "num_days", async |amount: TAtom| Int(amount.0.num_days())), + fun(true, "hours", async |amount: Int| new_atom(OrcDT(TimeDelta::hours(amount.0)))), + fun(true, "num_hours", async |amount: TAtom| Int(amount.0.num_hours())), + fun(true, "minutes", async |amount: Int| new_atom(OrcDT(TimeDelta::minutes(amount.0)))), + fun(true, "num_minutes", async |amount: TAtom| Int(amount.0.num_minutes())), + fun(true, "secs", async |amount: Num| { + new_atom(OrcDT(match amount.0 { + Numeric::Int(i) => TimeDelta::seconds(i), + Numeric::Float(f) => + TimeDelta::new(f.floor() as i64, (f.fract() * 1_000_000_000_f64).floor() as u32).unwrap(), + })) + }), + fun(true, "num_secs", async |amount: TAtom| Int(amount.0.num_seconds())), + fun(true, "as_secs", async |amount: TAtom| { + Float(NotNan::new(amount.0.as_seconds_f64()).unwrap()) + }), + fun(true, "milis", async |amount: Int| new_atom(OrcDT(TimeDelta::milliseconds(amount.0)))), + fun(true, "num_millis", async |amount: TAtom| Int(amount.0.num_milliseconds())), + fun(true, "nanos", async |amount: Int| new_atom(OrcDT(TimeDelta::nanoseconds(amount.0)))), + fun(true, "num_nanos", async |amount: TAtom| Int(amount.0.num_nanoseconds().unwrap())), + fun(true, "now", async |cb: Expr| new_atom(Now(cb))), + ]) +} diff --git a/orchid-std/src/std/tuple.rs b/orchid-std/src/std/tuple.rs index f920737..9458e82 100644 --- a/orchid-std/src/std/tuple.rs +++ b/orchid-std/src/std/tuple.rs @@ -8,19 +8,15 @@ use futures::future::join_all; use never::Never; use orchid_api_derive::{Coding, Hierarchy}; use orchid_api_traits::Request; -use orchid_base::error::{OrcRes, mk_errv}; -use orchid_base::format::{FmtCtx, FmtUnit, Format, Variants}; -use orchid_base::interner::is; -use orchid_base::name::Sym; -use orchid_base::reqnot::ReqHandleExt; -use orchid_base::sym; -use orchid_extension::atom::{Atomic, MethodSetBuilder, Supports, TAtom}; -use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant, own}; +use orchid_base::{FmtCtx, FmtUnit, Format, OrcRes, ReqHandleExt, Sym, Variants, is, mk_errv, sym}; use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::{Expr, ExprHandle}; -use orchid_extension::gen_expr::{GExpr, new_atom, sym_ref}; +use orchid_extension::gen_expr::{GExpr, new_atom}; use orchid_extension::system::sys_req; use orchid_extension::tree::{GenMember, cnst, fun, prefix}; +use orchid_extension::{ + Atomic, DeserializeCtx, MethodSetBuilder, OwnedAtom, OwnedVariant, Supports, TAtom, +}; use crate::std::protocol::types::{GetImpl, ProtocolMethod}; use crate::std::std_system::StdReq; @@ -32,7 +28,7 @@ pub struct Tuple(pub(super) Rc>); impl Atomic for Tuple { type Data = Vec; type Variant = OwnedVariant; - fn reg_reqs() -> orchid_extension::atom::MethodSetBuilder { + fn reg_methods() -> orchid_extension::MethodSetBuilder { MethodSetBuilder::new().handle::() } } @@ -48,29 +44,29 @@ impl OwnedAtom for Tuple { async fn deserialize(_: impl DeserializeCtx, refs: Self::Refs) -> Self { Self(Rc::new(refs)) } async fn print_atom<'a>(&'a self, c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit { Variants::default() - .sequence(self.0.len(), "t[", ", ", "]", Some(true)) - .sequence(self.0.len(), "t[\n", ",\n", "\n]", Some(true)) + .sequence(self.0.len(), "t[", ", ", "]", true) + .sequence(self.0.len(), "t[\n", ",\n", "\n]", true) .units_own(join_all(self.0.iter().map(|x| x.print(c))).await) } } impl Supports for Tuple { async fn handle<'a>( &self, - hand: Box + '_>, + hand: Box + '_>, req: ProtocolMethod, - ) -> std::io::Result> { + ) -> std::io::Result> { match req { ProtocolMethod::GetTagId(req) => hand.reply(&req, &sym!(std::tuple).to_api()).await, ProtocolMethod::GetImpl(ref req @ GetImpl(key)) => { let name = Sym::from_api(key).await; let val = if name == sym!(std::ops::get) { - sym_ref(sym!(std::tuple::get)) + sym!(std::tuple::get) } else if name == sym!(std::ops::set) { - sym_ref(sym!(std::tuple::set)) + sym!(std::tuple::set) } else { return hand.reply(req, &None).await; }; - hand.reply(req, &Some(val.create().await.serialize().await)).await + hand.reply(req, &Some(val.to_expr().await.serialize().await)).await }, } } @@ -118,7 +114,7 @@ pub fn gen_tuple_lib() -> Vec { }), fun(true, "get", async |tup: TAtom, idx: TAtom| { if let Ok(idx) = usize::try_from(idx.0) - && let Some(val) = own(&tup).await.0.get(idx) + && let Some(val) = tup.own().await.0.get(idx) { return Ok(val.clone()); } @@ -130,7 +126,7 @@ pub fn gen_tuple_lib() -> Vec { }), fun(true, "set", async |tup: TAtom, idx: TAtom, val: Expr| { if let Ok(idx) = usize::try_from(idx.0) { - let mut new_vec = own(&tup).await.0.to_vec(); + let mut new_vec = tup.own().await.0.to_vec(); if let Some(slot) = new_vec.get_mut(idx) { *slot = val; return Ok(new_atom(Tuple(Rc::new(new_vec)))); @@ -147,7 +143,7 @@ pub fn gen_tuple_lib() -> Vec { }), fun(true, "cat", async |left: TAtom, right: TAtom| { new_atom(Tuple(Rc::new( - own(&left).await.0.iter().chain(own(&right).await.0.iter()).cloned().collect(), + left.own().await.0.iter().chain(right.own().await.0.iter()).cloned().collect(), ))) }), ]) @@ -174,8 +170,7 @@ pub struct Tpl(pub T); mod tpl_impls { use itertools::Itertools; - use orchid_base::error::{OrcRes, mk_errv}; - use orchid_base::interner::is; + use orchid_base::{OrcRes, is, mk_errv}; use orchid_extension::conv::{ToExpr, TryFromExpr}; use orchid_extension::expr::Expr; use orchid_extension::gen_expr::GExpr; diff --git a/orcx/src/main.rs b/orcx/src/main.rs index 0e58fe2..5289a90 100644 --- a/orcx/src/main.rs +++ b/orcx/src/main.rs @@ -1,4 +1,4 @@ -use orchid_base::logging::Logger; +use orchid_base::Logger; use orchid_host::dylib::ext_dylib; use tokio::time::Instant; pub mod parse_folder; @@ -18,17 +18,11 @@ use clap::{Parser, Subcommand}; use futures::future::LocalBoxFuture; use futures::{FutureExt, Stream, TryStreamExt, io}; use itertools::Itertools; -use orchid_base::error::{try_with_reporter, with_reporter}; -use orchid_base::format::{FmtCtxImpl, Format, fmt, fmt_v, take_first}; -use orchid_base::interner::local_interner::local_interner; -use orchid_base::interner::{is, with_interner}; -use orchid_base::location::SrcRange; -use orchid_base::logging::{log, with_logger}; -use orchid_base::name::{NameLike, VPath}; -use orchid_base::parse::{Import, Snippet}; -use orchid_base::stash::with_stash; -use orchid_base::sym; -use orchid_base::tree::{Token, ttv_fmt}; +use orchid_base::local_interner::local_interner; +use orchid_base::{ + FmtCtxImpl, Format, Import, NameLike, Snippet, SrcRange, Token, VPath, fmt, fmt_v, is, log, sym, + take_first, try_with_reporter, ttv_fmt, with_interner, with_logger, with_reporter, with_stash, +}; use orchid_host::ctx::{Ctx, JoinHandle, Spawner}; use orchid_host::execute::{ExecCtx, ExecResult}; use orchid_host::expr::ExprKind; diff --git a/orcx/src/parse_folder.rs b/orcx/src/parse_folder.rs index 6c57491..2ea3044 100644 --- a/orcx/src/parse_folder.rs +++ b/orcx/src/parse_folder.rs @@ -3,11 +3,10 @@ use std::path::{Path, PathBuf}; use futures::FutureExt; use itertools::Itertools; -use orchid_base::error::{OrcRes, async_io_err, os_str_to_string, report}; -use orchid_base::interner::is; -use orchid_base::location::SrcRange; -use orchid_base::name::Sym; -use orchid_base::parse::Snippet; +use orchid_base::SrcRange; +use orchid_base::Sym; +use orchid_base::Snippet; +use orchid_base::{OrcRes, async_io_err, is, os_str_to_string, report}; use orchid_host::ctx::Ctx; use orchid_host::lex::lex; use orchid_host::parse::{HostParseCtxImpl, parse_items};