-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple namespace inference during function resolution
- Loading branch information
1 parent
77fda01
commit 7be20b7
Showing
4 changed files
with
67 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,67 @@ | ||
use crate::{name::ResolvedName, resolved}; | ||
use crate::{ | ||
name::{Name, ResolvedName}, | ||
resolved, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct FunctionSearchCtx { | ||
pub available: HashMap<ResolvedName, Vec<resolved::FunctionRef>>, | ||
pub imported_namespaces: Vec<Box<str>>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum FindFunctionError { | ||
NotDefined, | ||
Ambiguous, | ||
} | ||
|
||
impl FunctionSearchCtx { | ||
pub fn new() -> Self { | ||
Self { | ||
available: Default::default(), | ||
// TODO: Make this value user-specified | ||
imported_namespaces: vec!["io".to_string().into_boxed_str()], | ||
} | ||
} | ||
|
||
pub fn find_function(&self, name: &ResolvedName) -> Option<resolved::FunctionRef> { | ||
self.available | ||
.get(name) | ||
pub fn find_function(&self, name: &Name) -> Result<resolved::FunctionRef, FindFunctionError> { | ||
eprintln!("warning: function call name resolution not fully implemented yet"); | ||
|
||
let resolved_name = if !name.namespace.is_empty() { | ||
ResolvedName::Project(format!("{}{}", name.namespace, name.basename).into_boxed_str()) | ||
} else { | ||
ResolvedName::Project(name.basename.clone().into_boxed_str()) | ||
}; | ||
|
||
if let Some(found) = self | ||
.available | ||
.get(&resolved_name) | ||
.and_then(|list| list.first()) | ||
.copied() | ||
{ | ||
return Ok(found); | ||
} | ||
|
||
if name.namespace.is_empty() { | ||
let mut matches = self.imported_namespaces.iter().filter_map(|namespace| { | ||
self.available | ||
.get(&ResolvedName::Project( | ||
format!("{}/{}", namespace, name.basename).into_boxed_str(), | ||
)) | ||
.and_then(|list| list.first()) | ||
.copied() | ||
}); | ||
|
||
if let Some(found) = matches.next() { | ||
if matches.next().is_some() { | ||
return Err(FindFunctionError::Ambiguous); | ||
} else { | ||
return Ok(found); | ||
} | ||
} | ||
} | ||
|
||
Err(FindFunctionError::NotDefined) | ||
} | ||
} |