Skip to content

Commit

Permalink
impl fastn_continuation::Continuation for fastn_compiler::Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 7, 2024
1 parent db6b9d4 commit 7cc0bbe
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 148 deletions.
1 change: 1 addition & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions v0.5/fastn-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ homepage.workspace = true
[dependencies]
fastn-builtins.workspace = true
fastn-resolved.workspace = true
fastn-continuation.workspace = true
fastn-section.workspace = true
fastn-unresolved.workspace = true
indexmap.workspace = true
116 changes: 57 additions & 59 deletions v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
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)
//
Expand All @@ -23,7 +15,7 @@ pub struct Compiler {
/// checkout resolve_document for why this is an Option
pub(crate) content: Option<Vec<fastn_unresolved::URCI>>,
pub(crate) document: fastn_unresolved::Document,
pub global_aliases: fastn_unresolved::AliasesSimple,
// pub global_aliases: fastn_unresolved::AliasesSimple,
iterations: usize,
}

Expand All @@ -32,14 +24,14 @@ impl Compiler {
source: &str,
package: &str,
module: Option<&str>,
global_aliases: fastn_unresolved::AliasesSimple,
// global_aliases: fastn_unresolved::AliasesSimple,
) -> Self {
let mut arena = fastn_unresolved::Arena::default();
let mut document = fastn_unresolved::parse(
fastn_unresolved::Module::new(package, module, &mut arena),
source,
&mut arena,
&global_aliases,
// &global_aliases,
);
let content = Some(document.content);
document.content = vec![];
Expand All @@ -50,7 +42,7 @@ impl Compiler {
modules: std::collections::HashMap::new(),
content,
document,
global_aliases,
// global_aliases,
definitions_used: Default::default(),
iterations: 0,
}
Expand Down Expand Up @@ -151,50 +143,6 @@ impl Compiler {
stuck_on_symbols
}

pub fn continue_with_definitions(
mut self,
definitions: Vec<fastn_unresolved::URD>,
) -> CompilerState {
self.iterations += 1;
if self.iterations > ITERATION_THRESHOLD {
panic!("iterations too high");
}

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));
}

// 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,
Expand Down Expand Up @@ -236,11 +184,61 @@ pub fn compile(
source: &str,
package: &str,
module: Option<&str>,
global_aliases: fastn_unresolved::AliasesSimple,
) -> CompilerState {
Compiler::new(source, package, module, global_aliases).continue_with_definitions(vec![])
) -> fastn_continuation::Result<Compiler> {
use fastn_continuation::Continuation;

Compiler::new(source, package, module).continue_after(vec![])
}

impl fastn_continuation::Continuation for Compiler {
type Output = Result<fastn_resolved::CompiledDocument, fastn_compiler::Error>;
type NeededInput = std::collections::HashSet<fastn_unresolved::Symbol>;
type NeededOutput = Vec<fastn_unresolved::URD>;

fn continue_after(
mut self,
definitions: Self::NeededOutput,
) -> fastn_continuation::Result<Self> {
self.iterations += 1;
if self.iterations > ITERATION_THRESHOLD {
panic!("iterations too high");
}

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 fastn_continuation::Result::Done(self.finalise(false));
}

// 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 fastn_continuation::Result::Done(self.finalise(true));
}

fastn_continuation::Result::Stuck(Box::new(self), r.need_more_symbols)
}
}
#[derive(Default)]
struct ResolveSymbolsResult {
need_more_symbols: std::collections::HashSet<fastn_unresolved::Symbol>,
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate self as fastn_compiler;
mod compiler;
mod utils;

pub use compiler::{compile, Compiler, CompilerState};
pub use compiler::{compile, Compiler};
pub use fastn_section::Result;

#[derive(Debug)]
Expand Down
17 changes: 17 additions & 0 deletions v0.5/fastn-continuation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ where
}
}

pub fn consume_with<C: Continuation, F>(mut c: Result<C>, f: F) -> C::Output
where
F: Fn(&mut C, C::NeededInput) -> C::NeededOutput,
{
loop {
match c {
Result::Stuck(mut ic, input) => {
let o = f(&mut ic, input);
c = ic.continue_after(o);
}
Result::Done(c) => {
return c;
}
}
}
}

pub async fn consume_async<C: Continuation, Fut>(
mut c: Result<C>,
f: impl Fn(C::NeededInput) -> Fut,
Expand Down
4 changes: 2 additions & 2 deletions v0.5/fastn-unresolved/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn parse(
module: fastn_unresolved::Module,
source: &str,
arena: &mut fastn_unresolved::Arena,
global_aliases: &fastn_unresolved::AliasesSimple,
// global_aliases: &fastn_unresolved::AliasesSimple,
) -> fastn_unresolved::Document {
let (mut document, sections) = fastn_unresolved::Document::new(
module,
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn parse(
}
}

document.add_definitions_to_scope(arena, global_aliases);
// document.add_definitions_to_scope(arena, global_aliases);
document
}

Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn-unresolved/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl fastn_unresolved::Document {
self.comments.extend(comments);
}

#[expect(unused)]
pub(crate) fn add_definitions_to_scope(
&mut self,
_arena: &mut fastn_unresolved::Arena,
Expand Down
38 changes: 9 additions & 29 deletions v0.5/fastn/src/commands/render.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
impl fastn::commands::Render {
pub async fn run(self, config: &mut fastn_package::Package, _router: fastn_router::Router) {
pub async fn run(self, _package: &mut fastn_package::Package, _router: fastn_router::Router) {
let route = fastn_continuation::consume(fastn_router::Router::reader(), fastn::full_filler)
.route("/", fastn_router::Method::Get, &[]);
match route {
fastn_router::Route::Document(path, data) => {
let html = fastn::commands::render::render_document(
config.auto_imports.clone(),
path.as_str(),
data,
self.strict,
)
.await;
let html =
fastn::commands::render::render_document(path.as_str(), data, self.strict)
.await;
std::fs::write(path.replace(".ftd", ".html"), html).unwrap();
}
_ => todo!(),
};
}
}

pub async fn render_document(
global_aliases: Vec<fastn_package::AutoImport>,
path: &str,
_data: serde_json::Value,
_strict: bool,
) -> String {
pub async fn render_document(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 mut cs = fastn_compiler::compile(&source, "main", None, todo!());
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 o = fastn_continuation::consume_with(
fastn_compiler::compile(&source, "main", None),
fastn::symbols::lookup,
);
let h = fastn_runtime::HtmlData::from_cd(o.unwrap());
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(
todo!(),
// global_aliases,
"index.ftd",
serde_json::Value::Null,
Expand Down
2 changes: 0 additions & 2 deletions v0.5/fastn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use tokio as _;
pub mod commands;
mod symbols;

pub use symbols::Symbols;

pub enum Action {
Read,
Write,
Expand Down
Loading

0 comments on commit 7cc0bbe

Please sign in to comment.