Skip to content

Commit

Permalink
some kind of delirious thoughts on resolve symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 30, 2024
1 parent 94f9f6d commit 0f79241
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
11 changes: 6 additions & 5 deletions fastn-resolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ pub type Map<T> = std::collections::BTreeMap<String, T>;
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Definition {
SymbolAlias {
symbol: String,
alias: String,
package: String,
module: String,
name: String,
line_number: usize,
},
ModuleAlias {
package: String,
module: String,
alias: String,
line_number: usize,
},
Record(fastn_resolved::Record),
Expand Down Expand Up @@ -74,8 +75,8 @@ impl Definition {
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 { alias, .. } => alias.to_string(),
Definition::ModuleAlias { alias, .. } => alias.to_string(),
Definition::SymbolAlias { name, .. } => name.clone(),
Definition::ModuleAlias { .. } => todo!(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions ftd/src/interpreter/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ 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!(),
};

(
Expand Down
3 changes: 2 additions & 1 deletion v0.5/fastn-unresolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ pub enum UR<U, R> {
///
/// what if we store the dependencies it failed on, so when any of them changes, we can
/// revalidate?
Invalid(Vec<fastn_section::Error>),
Invalid(fastn_section::Error),
InvalidN(Vec<fastn_section::Error>),
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
62 changes: 41 additions & 21 deletions v0.5/fastn-unresolved/src/resolver/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
/// some variables, each such nested block is passed as locals,
/// with the innermost block as the last entry.
pub fn resolve(
// foo.ftd (current_module = foo, package = foo, module = "")
current_module: &fastn_unresolved::Module,
// parent: Option<fastn_unresolved::Symbol>,
// S1_name="bar.x"
// S2_name="x"
// S3_name="bar#x"
name: &mut fastn_unresolved::URIS,
definitions: &std::collections::HashMap<String, fastn_unresolved::URD>,
arena: &mut fastn_unresolved::Arena,
_output: &mut fastn_unresolved::resolver::Output,
output: &mut fastn_unresolved::resolver::Output,
_locals: &[Vec<fastn_unresolved::UR<fastn_unresolved::Argument, fastn_resolved::Argument>>],
) {
let inner_name = if let fastn_unresolved::UR::UnResolved(name) = name {
Expand All @@ -25,20 +29,49 @@ pub fn resolve(
package,
module,
name,
} => fastn_unresolved::Symbol::new(package.str(), module.str(), name.str(), arena),
fastn_section::IdentifierReference::Imported { module, name } => {
} => {
// this is S3, S3_name="bar#x"
// we have to check if bar#x exists in definitions, and is resolved.
// if it is in resolved-state we have resolved, and we store bar#x.
// 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_section::IdentifierReference::Imported {
module,
name: dotted_name,
} => {
let target_symbol = current_module.symbol(module.str(), arena);
// current module is foo.ftd
// it has -- import: bar at the top
// the value of module and name is "bar" and "x" respectively
// current_module.symbol(module) should be a module alias, we should get the target
// module from the module alias.
// we combine the target module with the name, "x" to get the target symbol.

match definitions.get(&current_module.symbol(module.str(), arena).string(arena)) {
Some(fastn_unresolved::UR::UnResolved(fastn_unresolved::Definition {
inner: fastn_unresolved::InnerDefinition::ModuleAlias(target),
match definitions.get(target_symbol.str(arena)) {
Some(fastn_unresolved::UR::Resolved(fastn_resolved::Definition::ModuleAlias {
package,
module,
..
})) => target.symbol(name.str(), arena),
})) => {
// what we stored in resolved place, no further action is required.
*name = fastn_unresolved::UR::Resolved(fastn_unresolved::Symbol::new(
package,
module,
dotted_name.str(),
arena,
));
return;
}
Some(fastn_unresolved::UR::UnResolved(_)) => {
output.stuck_on.insert(target_symbol);
return;
}
Some(fastn_unresolved::UR::NotFound) => {
*name = fastn_unresolved::UR::Invalid(fastn_section::Error::InvalidIdentifier);
return;
}
_ => {
// we have
todo!()
Expand All @@ -51,18 +84,5 @@ pub fn resolve(
}
};

// we verify that target symbol exists in definitions and if so we resolve it.
// if the target symbol resolves into a module alias, it is an error.
// if the target symbol resolves into a symbol alias, we resolve that symbol, till we get a
// non-alias symbol.
// if the target-symbol does not exist in definitions, we add it to output.stuck_on symbol list
if !validate_target(&target_symbol) {
return;
}

*name = fastn_unresolved::UR::Resolved(target_symbol);
}

fn validate_target(_target: &fastn_unresolved::Symbol) -> bool {
true
}

0 comments on commit 0f79241

Please sign in to comment.