From fda31f8ff079c369e109f5cedee37ef147b8ac73 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Fri, 6 Dec 2024 16:19:41 +0530 Subject: [PATCH] continuation based compiler, buggy yet --- v0.5/Cargo.lock | 417 +++++++++++++++++++++++++++- v0.5/fastn-compiler/Cargo.toml | 3 - v0.5/fastn-compiler/src/compiler.rs | 125 ++++----- v0.5/fastn-compiler/src/lib.rs | 4 +- v0.5/fastn-compiler/src/tdoc.rs | 92 ------ v0.5/fastn/Cargo.toml | 4 +- v0.5/fastn/src/commands/render.rs | 34 ++- v0.5/fastn/src/symbols.rs | 25 +- 8 files changed, 508 insertions(+), 196 deletions(-) delete mode 100644 v0.5/fastn-compiler/src/tdoc.rs diff --git a/v0.5/Cargo.lock b/v0.5/Cargo.lock index 38ddb2721..ecf1c63ae 100644 --- a/v0.5/Cargo.lock +++ b/v0.5/Cargo.lock @@ -32,6 +32,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "backtrace" version = "0.3.74" @@ -47,18 +53,83 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bytes" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" +[[package]] +name = "cc" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "copy_dir" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "543d1dd138ef086e2ff05e3a48cf9da045da2033d16f8538fd76b86cd49b2ca3" +dependencies = [ + "walkdir", +] + +[[package]] +name = "cpufeatures" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "either" version = "1.13.0" @@ -75,7 +146,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" name = "fastn" version = "0.1.0" dependencies = [ + "fastn-compiler", "fastn-core", + "fastn-runtime", "fastn-unresolved", "http-body-util", "hyper", @@ -114,6 +187,20 @@ dependencies = [ "serde_json", ] +[[package]] +name = "fastn-js" +version = "0.1.0" +dependencies = [ + "fastn-resolved", + "indoc", + "itertools", + "prettify-js", + "pretty", + "quick-js", + "rquickjs", + "thiserror", +] + [[package]] name = "fastn-resolved" version = "0.1.1" @@ -122,6 +209,21 @@ dependencies = [ "serde", ] +[[package]] +name = "fastn-runtime" +version = "0.1.0" +dependencies = [ + "fastn-builtins", + "fastn-js", + "fastn-resolved", + "indexmap", + "indoc", + "itertools", + "once_cell", + "serde", + "sha2", +] + [[package]] name = "fastn-section" version = "0.1.0" @@ -197,6 +299,16 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gimli" version = "0.31.1" @@ -298,6 +410,12 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "indexmap" version = "2.7.0" @@ -309,6 +427,12 @@ dependencies = [ "serde", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "itertools" version = "0.13.0" @@ -330,6 +454,22 @@ version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" +[[package]] +name = "libquickjs-sys" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f0b24e9bd171b75ae0295bd428fb8fe58410fb23156e5f34a4657a70c3cee96" +dependencies = [ + "cc", + "copy_dir", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + [[package]] name = "memchr" version = "2.7.4" @@ -365,6 +505,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "pin-project-lite" version = "0.2.15" @@ -377,6 +523,63 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "prettify-js" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7ff18ec712649921186715eeee7d880c3c5fb41c86a5a3524057da346e27771" +dependencies = [ + "log", + "ress", + "serde", + "serde_json", +] + +[[package]] +name = "pretty" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55c4d17d994b637e2f4daf6e5dc5d660d209d5642377d675d7a1c3ab69fa579" +dependencies = [ + "arrayvec", + "typed-arena", + "unicode-width", +] + +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.92" @@ -386,6 +589,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-js" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19cb4cefcb00f4ab9b332664d06005a74f582ac16aa959c6ad5912957bd83e5f" +dependencies = [ + "libquickjs-sys", + "once_cell", +] + [[package]] name = "quote" version = "1.0.37" @@ -424,6 +637,69 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + +[[package]] +name = "ress" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fb1cedc0fd02351568b53be91c4ba2f94beb31ff730bd7df1e54e9b19169b9b" +dependencies = [ + "log", + "unicode-xid", +] + +[[package]] +name = "rquickjs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cbd33e0b668aea0ab238b9164523aca929096f9f40834700d71d91dd4888882" +dependencies = [ + "rquickjs-core", + "rquickjs-macro", +] + +[[package]] +name = "rquickjs-core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9129d69b7b8f7ee8ad1da5b12c7f4a8a8acd45f2e6dd9cb2ee1bc5a1f2fa3d" +dependencies = [ + "relative-path", + "rquickjs-sys", +] + +[[package]] +name = "rquickjs-macro" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d2ecaf7c9eda262e02a91e9541989a9dd18984d17d0d97f99f33b464318057" +dependencies = [ + "convert_case", + "fnv", + "ident_case", + "indexmap", + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "rquickjs-core", + "syn 2.0.90", +] + +[[package]] +name = "rquickjs-sys" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf6f2288d8e7fbb5130f62cf720451641e99d55f6fde9db86aa2914ecb553fd2" +dependencies = [ + "cc", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -436,6 +712,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "serde" version = "1.0.215" @@ -453,7 +738,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", ] [[package]] @@ -468,6 +753,23 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "smallvec" version = "1.13.2" @@ -494,6 +796,16 @@ dependencies = [ "serde", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.90" @@ -505,6 +817,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + [[package]] name = "tokio" version = "1.41.1" @@ -528,21 +860,93 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.90", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -615,3 +1019,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] diff --git a/v0.5/fastn-compiler/Cargo.toml b/v0.5/fastn-compiler/Cargo.toml index 9a34f1deb..c5f0d5236 100644 --- a/v0.5/fastn-compiler/Cargo.toml +++ b/v0.5/fastn-compiler/Cargo.toml @@ -8,9 +8,6 @@ license.workspace = true repository.workspace = true homepage.workspace = true -[features] -owned-tdoc = ["fastn-resolved/owned-tdoc"] - [dependencies] fastn-section.workspace = true fastn-resolved.workspace = true diff --git a/v0.5/fastn-compiler/src/compiler.rs b/v0.5/fastn-compiler/src/compiler.rs index adeb4d09c..afc3ab43f 100644 --- a/v0.5/fastn-compiler/src/compiler.rs +++ b/v0.5/fastn-compiler/src/compiler.rs @@ -15,7 +15,7 @@ pub enum CompilerState { // exposing: y as x pub struct Compiler { pub(crate) definitions_used: std::collections::HashSet, - pub(crate) arena: fastn_unresolved::Arena, + pub arena: fastn_unresolved::Arena, pub(crate) definitions: std::collections::HashMap, /// we keep track of every module found (or not found), if not in dict we don't know /// if module exists, if in dict bool tells if it exists. @@ -23,8 +23,8 @@ pub struct Compiler { /// checkout resolve_document for why this is an Option pub(crate) content: Option>, pub(crate) document: fastn_unresolved::Document, - #[expect(unused)] - pub(crate) global_aliases: fastn_unresolved::AliasesSimple, + pub global_aliases: fastn_unresolved::AliasesSimple, + iterations: usize, } impl Compiler { @@ -52,36 +52,10 @@ impl Compiler { document, global_aliases, definitions_used: Default::default(), + iterations: 0, } } - async fn fetch_unresolved_symbols( - &mut self, - _symbols_to_fetch: &std::collections::HashSet, - ) { - todo!() - // self.definitions_used - // .extend(symbols_to_fetch.iter().cloned()); - // let definitions = self - // .symbols - // .lookup(&mut self.arena, &self.global_aliases, symbols_to_fetch) - // .await; - // for definition in definitions { - // // the following is only okay if our symbol store only returns unresolved definitions, - // // some other store might return resolved definitions, and we need to handle that. - // self.definitions.insert( - // definition - // .unresolved() - // .unwrap() - // .symbol - // .clone() - // .unwrap() - // .string(&self.arena), - // definition, - // ); - // } - } - /// try to resolve as many symbols as possible, and return the ones that we made any progress on. /// /// this function should be called in a loop, until the list of symbols is empty. @@ -177,48 +151,57 @@ impl Compiler { stuck_on_symbols } - async fn compile(mut self) -> CompilerState { - // we only make 10 attempts to resolve the document: we need a warning if we are not able to - // resolve the document in 10 attempts. - let mut unresolvable = std::collections::HashSet::new(); - // let mut ever_used = std::collections::HashSet::new(); - let mut iterations = 0; - while iterations < ITERATION_THRESHOLD { - // resolve_document can internally run in parallel. - // TODO: pass unresolvable to self.resolve_document() and make sure they don't come back - let unresolved_symbols = self.resolve_document(); - if unresolved_symbols.is_empty() { - break; - } - // ever_used.extend(&unresolved_symbols); - self.fetch_unresolved_symbols(&unresolved_symbols).await; - // this itself has to happen in a loop. we need a warning if we are not able to resolve all - // symbols in 10 attempts. - let mut r = ResolveSymbolsResult::default(); - r.need_more_symbols.extend(unresolved_symbols); + pub fn continue_with_definitions( + mut self, + definitions: Vec, + ) -> CompilerState { + self.iterations += 1; + if self.iterations > ITERATION_THRESHOLD { + panic!("iterations too high"); + } - while iterations < ITERATION_THRESHOLD { - // resolve_document can internally run in parallel. - // TODO: pass unresolvable to self.resolve_symbols() and make sure they don't come back - r = self.resolve_symbols(r.need_more_symbols); - unresolvable.extend(r.unresolvable); - if r.need_more_symbols.is_empty() { - break; - } - // ever_used.extend(r.need_more_symbols); - self.fetch_unresolved_symbols(&r.need_more_symbols).await; - iterations += 1; - } + for definition in definitions { + // the following is only okay if our symbol store only returns unresolved definitions, + // some other store might return resolved definitions, and we need to handle that. + self.definitions.insert( + definition + .unresolved() + .unwrap() + .symbol + .clone() + .unwrap() + .string(&self.arena), + definition, + ); + } + + let unresolved_symbols = self.resolve_document(); + if unresolved_symbols.is_empty() { + return CompilerState::Done(self.finalise(false)); + } - iterations += 1; + // this itself has to happen in a loop. we need a warning if we are not able to resolve all + // symbols in 10 attempts. + let mut r = ResolveSymbolsResult { + need_more_symbols: unresolved_symbols, + unresolvable: Default::default(), + }; + r = self.resolve_symbols(r.need_more_symbols); + + if r.need_more_symbols.is_empty() { + return CompilerState::Done(self.finalise(true)); } + CompilerState::StuckOnSymbols(Box::new(self), r.need_more_symbols) + } + + fn finalise( + self, + some_symbols_are_unresolved: bool, + ) -> Result { // we are here means ideally we are done. // we could have some unresolvable symbols or self.document.errors may not be empty. - if !unresolvable.is_empty() - || !self.document.errors.is_empty() - || iterations == ITERATION_THRESHOLD - { + if some_symbols_are_unresolved || !self.document.errors.is_empty() { // we were not able to resolve all symbols or there were errors // return Err(fastn_compiler::Error { // messages: todo!(), @@ -229,14 +212,14 @@ impl Compiler { } // there were no errors, etc. - CompilerState::Done(Ok(fastn_resolved::CompiledDocument { + Ok(fastn_resolved::CompiledDocument { content: fastn_compiler::utils::resolved_content(self.content.unwrap()), definitions: fastn_compiler::utils::used_definitions( self.definitions, self.definitions_used, self.arena, ), - })) + }) } } @@ -249,15 +232,13 @@ impl Compiler { /// /// earlier we had strict mode here, but to simplify things, now we let the caller convert non-empty /// warnings from OK part as error, and discard the generated JS. -pub async fn compile( +pub fn compile( source: &str, package: &str, module: Option<&str>, global_aliases: fastn_unresolved::AliasesSimple, ) -> CompilerState { - Compiler::new(source, package, module, global_aliases) - .compile() - .await + Compiler::new(source, package, module, global_aliases).continue_with_definitions(vec![]) } #[derive(Default)] diff --git a/v0.5/fastn-compiler/src/lib.rs b/v0.5/fastn-compiler/src/lib.rs index 85a8a9e5a..316ddb648 100644 --- a/v0.5/fastn-compiler/src/lib.rs +++ b/v0.5/fastn-compiler/src/lib.rs @@ -5,11 +5,9 @@ extern crate self as fastn_compiler; mod compiler; -mod tdoc; mod utils; -pub use tdoc::CompiledDocument; -pub use compiler::compile; +pub use compiler::{compile, Compiler, CompilerState}; pub use fastn_section::Result; #[derive(Debug)] diff --git a/v0.5/fastn-compiler/src/tdoc.rs b/v0.5/fastn-compiler/src/tdoc.rs deleted file mode 100644 index 7aec61490..000000000 --- a/v0.5/fastn-compiler/src/tdoc.rs +++ /dev/null @@ -1,92 +0,0 @@ -pub struct CompiledDocument { - pub name: String, - pub definitions: indexmap::IndexMap, -} - -impl CompiledDocument { - fn get(&self, name: &str) -> Option<&fastn_resolved::Definition> { - if let Some(definition) = self.definitions.get(name) { - return Some(definition); - } - - if let Some(definition) = fastn_builtins::builtins().get(name) { - return Some(definition); - } - - None - } -} - -impl fastn_resolved::tdoc::TDoc for CompiledDocument { - #[cfg(not(feature = "owned-tdoc"))] - fn get_opt_function(&self, name: &str) -> Option<&fastn_resolved::Function> { - match self.get(name) { - Some(fastn_resolved::Definition::Function(f)) => Some(f), - _ => None, - } - } - - #[cfg(feature = "owned-tdoc")] - fn get_opt_function(&self, name: &str) -> Option { - match self.get(name) { - Some(fastn_resolved::Definition::Function(f)) => Some(f.clone()), - _ => None, - } - } - - #[cfg(not(feature = "owned-tdoc"))] - fn get_opt_record(&self, name: &str) -> Option<&fastn_resolved::Record> { - match self.get(name) { - Some(fastn_resolved::Definition::Record(f)) => Some(f), - _ => None, - } - } - - #[cfg(feature = "owned-tdoc")] - fn get_opt_record(&self, name: &str) -> Option { - match self.get(name) { - Some(fastn_resolved::Definition::Record(f)) => Some(f.clone()), - _ => None, - } - } - - fn name(&self) -> &str { - self.name.as_str() - } - - #[cfg(not(feature = "owned-tdoc"))] - fn get_opt_component(&self, name: &str) -> Option<&fastn_resolved::ComponentDefinition> { - match self.get(name) { - Some(fastn_resolved::Definition::Component(f)) => Some(f), - _ => None, - } - } - - #[cfg(feature = "owned-tdoc")] - fn get_opt_component(&self, name: &str) -> Option { - match self.get(name) { - Some(fastn_resolved::Definition::Component(f)) => Some(f.clone()), - _ => None, - } - } - - #[cfg(not(feature = "owned-tdoc"))] - fn get_opt_web_component(&self, name: &str) -> Option<&fastn_resolved::WebComponentDefinition> { - match self.get(name) { - Some(fastn_resolved::Definition::WebComponent(f)) => Some(f), - _ => None, - } - } - - #[cfg(feature = "owned-tdoc")] - fn get_opt_web_component(&self, name: &str) -> Option { - match self.get(name) { - Some(fastn_resolved::Definition::WebComponent(f)) => Some(f.clone()), - _ => None, - } - } - - fn definitions(&self) -> &indexmap::IndexMap { - &self.definitions - } -} diff --git a/v0.5/fastn/Cargo.toml b/v0.5/fastn/Cargo.toml index f034c5c04..826bc4c64 100644 --- a/v0.5/fastn/Cargo.toml +++ b/v0.5/fastn/Cargo.toml @@ -10,8 +10,8 @@ homepage.workspace = true [dependencies] fastn-core.workspace = true -#fastn-compiler.workspace = true -#fastn-runtime.workspace = true +fastn-compiler.workspace = true +fastn-runtime.workspace = true fastn-unresolved.workspace = true hyper.workspace = true http-body-util.workspace = true diff --git a/v0.5/fastn/src/commands/render.rs b/v0.5/fastn/src/commands/render.rs index f2d7f45a6..72aa32ef3 100644 --- a/v0.5/fastn/src/commands/render.rs +++ b/v0.5/fastn/src/commands/render.rs @@ -18,19 +18,29 @@ impl fastn::commands::Render { } pub async fn render_document( - _global_aliases: fastn_unresolved::AliasesSimple, - _path: &str, + global_aliases: fastn_unresolved::AliasesSimple, + path: &str, _data: serde_json::Value, _strict: bool, ) -> String { - todo!() - // let source = std::fs::File::open(path) - // .and_then(std::io::read_to_string) - // .unwrap(); - // let o = fastn_compiler::compile(&source, "main", None, global_aliases) - // .await - // .unwrap(); - // - // let h = fastn_runtime::HtmlData::from_cd(o); - // h.to_test_html() + let source = std::fs::File::open(path) + .and_then(std::io::read_to_string) + .unwrap(); + let mut cs = fastn_compiler::compile(&source, "main", None, global_aliases); + let mut symbol_store = fastn::Symbols {}; + + let o = loop { + match cs { + fastn_compiler::CompilerState::StuckOnSymbols(mut c, symbols) => { + let o = symbol_store.lookup(&mut c, &symbols).await; + cs = c.continue_with_definitions(o); + } + fastn_compiler::CompilerState::Done(c) => { + break c; + } + } + }; + + let h = fastn_runtime::HtmlData::from_cd(o.unwrap()); + h.to_test_html() } diff --git a/v0.5/fastn/src/symbols.rs b/v0.5/fastn/src/symbols.rs index 008a648f4..63ca7254b 100644 --- a/v0.5/fastn/src/symbols.rs +++ b/v0.5/fastn/src/symbols.rs @@ -4,8 +4,7 @@ pub struct Symbols {} impl Symbols { fn find_all_definitions_in_a_module( &mut self, - arena: &mut fastn_unresolved::Arena, - global_aliases: &fastn_unresolved::AliasesSimple, + compiler: &mut fastn_compiler::Compiler, (file, module): (String, fastn_unresolved::Module), ) -> Vec { // we need to fetch the symbol from the store @@ -17,13 +16,21 @@ impl Symbols { } }; - let d = fastn_unresolved::parse(module.clone(), &source, arena, global_aliases); + let d = fastn_unresolved::parse( + module.clone(), + &source, + &mut compiler.arena, + &Default::default(), // TODO + // compiler.global_aliases, + ); d.definitions .into_iter() .map(|d| match d { fastn_unresolved::UR::UnResolved(mut v) => { - v.symbol = Some(module.symbol(v.name.unresolved().unwrap().str(), arena)); + v.symbol = Some( + module.symbol(v.name.unresolved().unwrap().str(), &mut compiler.arena), + ); fastn_unresolved::UR::UnResolved(v) } _ => { @@ -35,21 +42,19 @@ impl Symbols { } impl Symbols { - #[expect(unused)] - async fn lookup( + pub async fn lookup( &mut self, - arena: &mut fastn_unresolved::Arena, - global_aliases: &fastn_unresolved::AliasesSimple, + compiler: &mut fastn_compiler::Compiler, symbols: &std::collections::HashSet, ) -> Vec { let unique_modules = symbols .iter() - .map(|s| file_for_symbol(s, arena)) + .map(|s| file_for_symbol(s, &mut compiler.arena)) .collect::>(); unique_modules .into_iter() - .flat_map(|m| self.find_all_definitions_in_a_module(arena, global_aliases, m)) + .flat_map(|m| self.find_all_definitions_in_a_module(compiler, m)) .collect() } }