[fine] Fully untested compiler

This commit is contained in:
John Doty 2024-01-08 22:30:34 -08:00
parent f2b9eae339
commit d14c9a72df
3 changed files with 286 additions and 31 deletions

View file

@ -121,23 +121,48 @@ impl fmt::Display for Type {
}
}
#[derive(Clone, Copy)]
pub enum Location {
Argument,
Local,
Module,
}
pub struct Declaration {
pub declaration_type: Type,
pub location: Location,
pub index: usize,
}
pub struct Environment {
pub parent: Option<EnvironmentRef>,
pub location: Location,
pub base_index: usize,
pub declarations: HashMap<Box<str>, Declaration>,
}
impl Environment {
pub fn new(parent: Option<EnvironmentRef>) -> Self {
pub fn new(parent: Option<EnvironmentRef>, location: Location, base_index: usize) -> Self {
Environment {
parent,
location,
base_index,
declarations: HashMap::new(),
}
}
pub fn insert(&mut self, token: &Token, t: Type) {
let index = self.base_index + self.declarations.len();
self.declarations.insert(
token.as_str().into(),
Declaration {
declaration_type: t,
location: self.location,
index,
},
);
}
pub fn bind(&self, token: &Token) -> Option<&Declaration> {
if let Some(decl) = self.declarations.get(token.as_str()) {
return Some(decl);
@ -260,7 +285,7 @@ impl<'a> Semantics<'a> {
errors: RefCell::new(vec![]),
types: RefCell::new(vec![Incremental::None; tree.len()]),
environments: RefCell::new(vec![Incremental::None; tree.len()]),
empty_environment: EnvironmentRef::new(Environment::new(None)),
empty_environment: EnvironmentRef::new(Environment::new(None, Location::Module, 0)),
};
// NOTE: We ensure all the known errors are reported before we move
@ -397,10 +422,13 @@ impl<'a> Semantics<'a> {
None => Type::Error,
};
let mut environment = Environment::new(Some(parent));
environment
.declarations
.insert(name.as_str().into(), Declaration { declaration_type });
let base_index = match parent.location {
Location::Local => parent.base_index + parent.declarations.len(),
_ => 0,
};
let mut environment = Environment::new(Some(parent), Location::Local, base_index);
environment.insert(name, declaration_type);
EnvironmentRef::new(environment)
}
@ -414,7 +442,7 @@ impl<'a> Semantics<'a> {
return parent; // SE
}
let mut environment = Environment::new(Some(parent));
let mut environment = Environment::new(Some(parent), Location::Argument, 0);
for child in param_list.children.iter() {
let Child::Tree(ct) = child else {
continue;
@ -434,9 +462,7 @@ impl<'a> Semantics<'a> {
Type::Error
};
environment
.declarations
.insert(param_name.as_str().into(), Declaration { declaration_type });
environment.insert(param_name, declaration_type);
}
EnvironmentRef::new(environment)