Skip to content

Commit

Permalink
We do not store ModuleAlias but Module
Browse files Browse the repository at this point in the history
In earlier design we did not have a concept of a Module, meaning in
Definition we did not have a clause for Module. We had module alias and
symbol alias instead.

The aliases are not things, where as module is a thing. This commit
fixes that mistake.

Further, we are going to add alias as a id-arena map on every
definition, which will contain the merged value of aliases from globals,
auto imports and imports designed in current module.
  • Loading branch information
amitu committed Dec 1, 2024
1 parent 3c4ca32 commit 03d403d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 55 deletions.
28 changes: 14 additions & 14 deletions fastn-resolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,10 @@ pub type Map<T> = std::collections::BTreeMap<String, T>;

#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Definition {
SymbolAlias {
/// every module is a "thing" and can be referred to etc., so we need to keep track of them
Module {
package: String,
module: String,
name: String,
line_number: usize,
},
ModuleAlias {
package: String,
module: String,
line_number: usize,
module: Option<String>,
},
Record(fastn_resolved::Record),
OrType(fastn_resolved::OrType),
Expand Down Expand Up @@ -74,9 +68,14 @@ impl Definition {
fastn_resolved::Definition::Function(f) => f.name.to_string(),
fastn_resolved::Definition::WebComponent(w) => w.name.to_string(),
fastn_resolved::Definition::Export { to, .. } => to.to_string(),
// TODO: check if the following two are valid
Definition::SymbolAlias { name, .. } => name.clone(),
Definition::ModuleAlias { .. } => todo!(),
Definition::Module {
package,
module: None,
} => package.clone(),
Definition::Module {
package,
module: Some(module),
} => format!("{}/{}", package, module),
}
}

Expand All @@ -90,8 +89,9 @@ impl Definition {
Definition::OrTypeWithVariant { variant, .. } => variant.line_number(),
Definition::WebComponent(w) => w.line_number,
Definition::Export { line_number, .. } => *line_number,
Definition::SymbolAlias { line_number, .. } => *line_number,
Definition::ModuleAlias { line_number, .. } => *line_number,
// module is not defined on any given line, unless it is defined using the future
// module proposal, till now we return 0
Definition::Module { .. } => 0,
}
}

Expand Down
3 changes: 1 addition & 2 deletions ftd/src/interpreter/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,7 @@ impl<'a> TDoc<'a> {
),
ftd::interpreter::Thing::Function(f) => (f.return_kind, false),
ftd::interpreter::Thing::Export { .. } => unreachable!(),
ftd::interpreter::Thing::ModuleAlias { .. }
| ftd::interpreter::Thing::SymbolAlias { .. } => todo!(),
ftd::interpreter::Thing::Module { .. } => todo!(),
};

(
Expand Down
4 changes: 2 additions & 2 deletions v0.5/fastn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Compiler {
symbols: Box<dyn fastn_compiler::SymbolStore>,
source: &str,
package: &str,
module: &str,
module: Option<&str>,
// auto_import_scope: fastn_unresolved::SFId,
mut arena: fastn_unresolved::Arena,
) -> Self {
Expand Down Expand Up @@ -224,7 +224,7 @@ pub async fn compile(
symbols: Box<dyn fastn_compiler::SymbolStore>,
source: &str,
package: &str,
module: &str,
module: Option<&str>,
// auto_import_scope: fastn_unresolved::SFId,
arena: fastn_unresolved::Arena,
) -> Result<fastn_resolved::CompiledDocument, fastn_compiler::Error> {
Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn-unresolved/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl<U: fastn_unresolved::JIDebug, R: fastn_unresolved::JIDebug> fastn_unresolve
crate::UR::UnResolved(u) => u.idebug(arena),
crate::UR::NotFound => unimplemented!(),
crate::UR::Invalid(_) => unimplemented!(),
crate::UR::InvalidN(_) => unimplemented!(),
}
}
}
7 changes: 4 additions & 3 deletions v0.5/fastn-unresolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod utils;
#[cfg(test)]
pub(crate) use debug::JIDebug;
pub use parser::parse;
use std::num::NonZeroU16;

pub type URD = fastn_unresolved::UR<fastn_unresolved::Definition, fastn_resolved::Definition>;
pub type URCI = fastn_unresolved::UR<
Expand All @@ -34,9 +35,9 @@ pub struct Symbol {
/// this store the <package>/<module>#<name> of the symbol
interned: string_interner::DefaultSymbol, // u32
/// length of the <package> part of the symbol
package_len: u16,
package_len: NonZeroU16,
/// length of the <module> part of the symbol
module_len: u16,
module_len: Option<NonZeroU16>,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand All @@ -45,7 +46,7 @@ pub struct Module {
/// this store the <package>/<module>#<name> of the symbol
interned: string_interner::DefaultSymbol, // u32
/// length of the <package> part of the symbol
package_len: u16,
package_len: NonZeroU16,
}

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-unresolved/src/parser/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ fn parse_import(

Some(Import {
module: fastn_unresolved::Module::new(
caption.inner_str(module).str(),
caption.inner_str(package).str(),
Some(caption.inner_str(module).str()),
arena,
),
alias: alias.map(|v| fastn_section::Identifier {
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-unresolved/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ where
println!("--------- testing -----------\n{source}\n--------- source ------------");

let mut arena = fastn_unresolved::Arena::default();
let module = fastn_unresolved::Module::new("main", "", &mut arena);
let module = fastn_unresolved::Module::new("main", None, &mut arena);

let (mut document, sections) = fastn_unresolved::Document::new(
module,
Expand Down
6 changes: 3 additions & 3 deletions v0.5/fastn-unresolved/src/resolver/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn resolve(
// if it is not in unresolved-state, or if it is missing in definitions, we add "bar#x"
// to output.stuck_on.
// if it is in error state, or not found state, we resolve ourselves as them.
fastn_unresolved::Symbol::new(package.str(), module.str(), name.str(), arena)
fastn_unresolved::Symbol::new(package.str(), Some(module.str()), name.str(), arena)
}
fastn_section::IdentifierReference::Imported {
module,
Expand All @@ -50,15 +50,15 @@ pub fn resolve(
// module from the module alias.
// we combine the target module with the name, "x" to get the target symbol.
match definitions.get(target_symbol.str(arena)) {
Some(fastn_unresolved::UR::Resolved(fastn_resolved::Definition::ModuleAlias {
Some(fastn_unresolved::UR::Resolved(fastn_resolved::Definition::Module {
package,
module,
..
})) => {
// what we stored in resolved place, no further action is required.
*name = fastn_unresolved::UR::Resolved(fastn_unresolved::Symbol::new(
package,
module,
module.as_deref(),
dotted_name.str(),
arena,
));
Expand Down
66 changes: 39 additions & 27 deletions v0.5/fastn-unresolved/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::num::NonZeroU16;

impl fastn_unresolved::Document {
pub(crate) fn new(
module: fastn_unresolved::Module,
Expand Down Expand Up @@ -47,6 +49,7 @@ impl fastn_unresolved::Definition {
fastn_unresolved::UR::Resolved(ref r) => r.str(),
fastn_unresolved::UR::NotFound => unreachable!(),
fastn_unresolved::UR::Invalid(_) => unreachable!(),
fastn_unresolved::UR::InvalidN(_) => unreachable!(),
}
}

Expand Down Expand Up @@ -136,27 +139,25 @@ impl<U, V> fastn_unresolved::UR<U, V> {
impl fastn_unresolved::Symbol {
pub fn new(
package: &str,
module: &str,
module: Option<&str>,
name: &str,
arena: &mut fastn_unresolved::Arena,
) -> fastn_unresolved::Symbol {
let v = if module.is_empty() {
format!("{package}#{name}")
} else {
format!("{package}/{module}#{name}")
let v = match module {
Some(module) => format!("{package}/{module}#{name}"),
None => format!("{package}#{name}"),
};
fastn_unresolved::Symbol {
package_len: package.len() as u16,
module_len: module.len() as u16,
package_len: NonZeroU16::new(package.len() as u16).unwrap(),
module_len: module.map(|v| NonZeroU16::new(v.len() as u16).unwrap()),
interned: arena.interner.get_or_intern(v),
}
}

pub fn parent(&self, arena: &mut fastn_unresolved::Arena) -> fastn_unresolved::Module {
let v = if self.module_len == 0 {
self.package(arena).to_string()
} else {
format!("{}/{}", self.package(arena), self.module(arena))
let v = match self.module_len {
None => format!("{}/{}", self.package(arena), self.module(arena).unwrap()),
Some(_) => self.package(arena).to_string(),
};
fastn_unresolved::Module {
package_len: self.package_len,
Expand All @@ -173,32 +174,36 @@ impl fastn_unresolved::Symbol {
}

pub fn package<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str {
&self.str(arena)[..self.package_len as usize]
&self.str(arena)[..self.package_len.get() as usize]
}

pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str {
&self.str(arena)[self.package_len as usize + 1
..self.package_len as usize + 1 + self.module_len as usize]
pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> Option<&'a str> {
self.module_len.map(|module_len| {
&self.str(arena)[self.package_len.get() as usize + 1
..self.package_len.get() as usize + 1 + module_len.get() as usize]
})
}

pub fn name<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str {
&self.str(arena)[self.package_len as usize + 1 + self.module_len as usize + 1..]
&self.str(arena)[self.package_len.get() as usize
+ 1
+ self.module_len.map(|v| v.get()).unwrap_or_default() as usize
+ 1..]
}
}

impl fastn_unresolved::Module {
pub fn new(
package: &str,
module: &str,
module: Option<&str>,
arena: &mut fastn_unresolved::Arena,
) -> fastn_unresolved::Module {
let v = if module.is_empty() {
package.to_string()
} else {
format!("{package}/{module}")
let v = match module {
None => package.to_string(),
Some(module) => format!("{package}/{module}"),
};
fastn_unresolved::Module {
package_len: package.len() as u16,
package_len: NonZeroU16::new(package.len() as u16).unwrap(),
interned: arena.interner.get_or_intern(v),
}
}
Expand All @@ -208,21 +213,28 @@ impl fastn_unresolved::Module {
}

pub fn package<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str {
&self.str(arena)[..self.package_len as usize]
&self.str(arena)[..self.package_len.get() as usize]
}

pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str {
&self.str(arena)[self.package_len as usize + 1..]
&self.str(arena)[self.package_len.get() as usize + 1..]
}

pub fn symbol(
&self,
name: &str,
arena: &mut fastn_unresolved::Arena,
) -> fastn_unresolved::Symbol {
let module_len =
arena.interner.resolve(self.interned).unwrap().len() as u16 - self.package_len;
let v = if module_len == 0 {
let module_len = {
let len = arena.interner.resolve(self.interned).unwrap().len() as u16
- self.package_len.get();
if len > 0 {
Some(NonZeroU16::new(len).unwrap())
} else {
None
}
};
let v = if module_len.is_none() {
format!("{}#{name}", self.package(arena))
} else {
format!("{}/{}#{name}", self.package(arena), self.module(arena))
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn/src/commands/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn render_document(
let source = std::fs::File::open(path)
.and_then(std::io::read_to_string)
.unwrap();
let o = fastn_compiler::compile(Box::new(fastn::Symbols {}), &source, "main", "", arena)
let o = fastn_compiler::compile(Box::new(fastn::Symbols {}), &source, "main", None, arena)
.await
.unwrap();
let h = fastn_runtime::HtmlData::from_cd(o);
Expand Down
6 changes: 5 additions & 1 deletion v0.5/fastn/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ fn file_for_symbol(
arena: &mut fastn_unresolved::Arena,
) -> (String, fastn_unresolved::Module) {
(
format!("{}/{}.ftd", symbol.package(arena), symbol.module(arena)),
// this code is non sense right now
match symbol.module(arena) {
Some(module) => format!("{}/{}.ftd", symbol.package(arena), module),
None => format!("{}/index.ftd", symbol.package(arena)),
},
symbol.parent(arena),
)
}

0 comments on commit 03d403d

Please sign in to comment.