Skip to content

Commit

Permalink
remoed the fastn_compiler::Symbols trait
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 6, 2024
1 parent 8efa4ac commit a90354a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 516 deletions.
445 changes: 5 additions & 440 deletions v0.5/Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion v0.5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ homepage = "https://fastn.com"
# using the latest dependency, and what is the plan to move to the latest version.

arcstr = "1"
async-trait = "0.1"
fastn-compiler = { path = "fastn-compiler", default-features = false }
fastn-core = { path = "fastn-core" }
fastn-section = { path = "fastn-section" }
Expand Down
1 change: 0 additions & 1 deletion v0.5/fastn-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ homepage.workspace = true
owned-tdoc = ["fastn-resolved/owned-tdoc"]

[dependencies]
async-trait.workspace = true
fastn-section.workspace = true
fastn-resolved.workspace = true
fastn-builtins.workspace = true
Expand Down
74 changes: 40 additions & 34 deletions v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
const ITERATION_THRESHOLD: usize = 100;

pub enum CompilerState {
StuckOnSymbols(
Box<Compiler>,
std::collections::HashSet<fastn_unresolved::Symbol>,
),
Done(Result<fastn_resolved::CompiledDocument, fastn_compiler::Error>),
}

// foo.ftd
// -- import: foo as f (f => foo)
//
// -- import: bar (bar => Module<bar>, x => Symbol<bar.y>) (bar => bar, x => bar.y)
// exposing: y as x
//
pub(crate) struct Compiler {
symbols: Box<dyn fastn_compiler::SymbolStore>,
pub struct Compiler {
pub(crate) definitions_used: std::collections::HashSet<fastn_unresolved::Symbol>,
pub(crate) arena: fastn_unresolved::Arena,
pub(crate) definitions: std::collections::HashMap<String, fastn_unresolved::URD>,
/// 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.
pub(crate) modules: std::collections::HashMap<fastn_unresolved::Module, bool>,
/// checkout resolve_document for why this is an Option
content: Option<Vec<fastn_unresolved::URCI>>,
pub(crate) content: Option<Vec<fastn_unresolved::URCI>>,
pub(crate) document: fastn_unresolved::Document,
global_aliases: fastn_unresolved::AliasesSimple,
#[expect(unused)]
pub(crate) global_aliases: fastn_unresolved::AliasesSimple,
}

impl Compiler {
fn new(
symbols: Box<dyn fastn_compiler::SymbolStore>,
source: &str,
package: &str,
module: Option<&str>,
Expand All @@ -39,7 +46,6 @@ impl Compiler {

Self {
arena,
symbols,
definitions: std::collections::HashMap::new(),
modules: std::collections::HashMap::new(),
content,
Expand All @@ -51,28 +57,29 @@ impl Compiler {

async fn fetch_unresolved_symbols(
&mut self,
symbols_to_fetch: &std::collections::HashSet<fastn_unresolved::Symbol>,
_symbols_to_fetch: &std::collections::HashSet<fastn_unresolved::Symbol>,
) {
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,
);
}
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.
Expand Down Expand Up @@ -170,7 +177,7 @@ impl Compiler {
stuck_on_symbols
}

async fn compile(mut self) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
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();
Expand Down Expand Up @@ -222,14 +229,14 @@ impl Compiler {
}

// there were no errors, etc.
Ok(fastn_resolved::CompiledDocument {
CompilerState::Done(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,
),
})
}))
}
}

Expand All @@ -243,13 +250,12 @@ 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(
symbols: Box<dyn fastn_compiler::SymbolStore + Send>,
source: &str,
package: &str,
module: Option<&str>,
global_aliases: fastn_unresolved::AliasesSimple,
) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
Compiler::new(symbols, source, package, module, global_aliases)
) -> CompilerState {
Compiler::new(source, package, module, global_aliases)
.compile()
.await
}
Expand Down
2 changes: 0 additions & 2 deletions v0.5/fastn-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
extern crate self as fastn_compiler;

mod compiler;
mod symbols;
mod tdoc;
mod utils;
pub use tdoc::CompiledDocument;

pub use compiler::compile;
pub use fastn_section::Result;
pub use symbols::SymbolStore;

#[derive(Debug)]
pub struct Error {
Expand Down
19 changes: 0 additions & 19 deletions v0.5/fastn-compiler/src/symbols.rs

This file was deleted.

5 changes: 2 additions & 3 deletions v0.5/fastn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ repository.workspace = true
homepage.workspace = true

[dependencies]
async-trait.workspace = true
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
Expand Down
25 changes: 12 additions & 13 deletions v0.5/fastn/src/commands/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ impl fastn::commands::Render {
match route {
fastn_core::Route::Document(path, data) => {
let html = fastn::commands::render::render_document(
Box::new(fastn::Symbols {}),
config.auto_imports.clone(),
path.as_str(),
data,
Expand All @@ -19,19 +18,19 @@ impl fastn::commands::Render {
}

pub async fn render_document(
symbols: Box<dyn fastn_compiler::SymbolStore>,
global_aliases: fastn_unresolved::AliasesSimple,
path: &str,
_global_aliases: fastn_unresolved::AliasesSimple,
_path: &str,
_data: serde_json::Value,
_strict: bool,
) -> String {
let source = std::fs::File::open(path)
.and_then(std::io::read_to_string)
.unwrap();
let o = fastn_compiler::compile(symbols, &source, "main", None, global_aliases)
.await
.unwrap();

let h = fastn_runtime::HtmlData::from_cd(o);
h.to_test_html()
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()
}
1 change: 0 additions & 1 deletion v0.5/fastn/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async fn render(
Ok(hyper::Response::new(http_body_util::Full::new(
hyper::body::Bytes::from(
fastn::commands::render::render_document(
Box::new(fastn::Symbols {}),
global_aliases,
"index.ftd",
serde_json::Value::Null,
Expand Down
4 changes: 2 additions & 2 deletions v0.5/fastn/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl Symbols {
}
}

#[async_trait::async_trait]
impl fastn_compiler::SymbolStore for Symbols {
impl Symbols {
#[expect(unused)]
async fn lookup(
&mut self,
arena: &mut fastn_unresolved::Arena,
Expand Down

0 comments on commit a90354a

Please sign in to comment.