Remove unneeded indirection

This commit is contained in:
Patrick Lühne 2020-04-17 03:13:18 +02:00
parent abbc047dda
commit 8a9a7b9132
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 21 additions and 34 deletions

View File

@ -14,7 +14,7 @@ pub struct Declarations
{ {
function_declarations: std::cell::RefCell<crate::FunctionDeclarations>, function_declarations: std::cell::RefCell<crate::FunctionDeclarations>,
predicate_declarations: std::cell::RefCell<crate::PredicateDeclarations>, predicate_declarations: std::cell::RefCell<crate::PredicateDeclarations>,
free_variable_declarations: crate::FreeVariableDeclarations, free_variable_declarations: std::cell::RefCell<crate::VariableDeclarations>,
} }
impl Declarations impl Declarations
@ -25,7 +25,7 @@ impl Declarations
{ {
function_declarations: std::cell::RefCell::new(crate::FunctionDeclarations::new()), function_declarations: std::cell::RefCell::new(crate::FunctionDeclarations::new()),
predicate_declarations: std::cell::RefCell::new(crate::PredicateDeclarations::new()), predicate_declarations: std::cell::RefCell::new(crate::PredicateDeclarations::new()),
free_variable_declarations: crate::FreeVariableDeclarations::new(), free_variable_declarations: std::cell::RefCell::new(vec![]),
} }
} }
} }

View File

@ -15,22 +15,6 @@ pub trait FindVariableDeclaration
fn find_variable_declaration(&self, name: &str) -> std::rc::Rc<crate::VariableDeclaration>; fn find_variable_declaration(&self, name: &str) -> std::rc::Rc<crate::VariableDeclaration>;
} }
pub struct FreeVariableDeclarations
{
variable_declarations: std::cell::RefCell<crate::VariableDeclarations>,
}
impl FreeVariableDeclarations
{
pub fn new() -> Self
{
Self
{
variable_declarations: std::cell::RefCell::new(vec![]),
}
}
}
pub struct BoundVariableDeclarations<'p> pub struct BoundVariableDeclarations<'p>
{ {
parent: &'p VariableDeclarationStackLayer<'p>, parent: &'p VariableDeclarationStackLayer<'p>,
@ -52,7 +36,7 @@ impl<'p> BoundVariableDeclarations<'p>
pub enum VariableDeclarationStackLayer<'p> pub enum VariableDeclarationStackLayer<'p>
{ {
Free(FreeVariableDeclarations), Free(std::cell::RefCell<crate::VariableDeclarations>),
Bound(BoundVariableDeclarations<'p>), Bound(BoundVariableDeclarations<'p>),
} }
@ -60,7 +44,7 @@ impl<'p> VariableDeclarationStackLayer<'p>
{ {
pub fn free() -> Self pub fn free() -> Self
{ {
Self::Free(FreeVariableDeclarations::new()) Self::Free(std::cell::RefCell::new(vec![]))
} }
pub fn bound(parent: &'p VariableDeclarationStackLayer<'p>, pub fn bound(parent: &'p VariableDeclarationStackLayer<'p>,
@ -73,9 +57,9 @@ impl<'p> VariableDeclarationStackLayer<'p>
{ {
match self match self
{ {
VariableDeclarationStackLayer::Free(free) => VariableDeclarationStackLayer::Free(free_variable_declarations) =>
{ {
if let Some(variable_declaration) = free.variable_declarations.borrow().iter() if let Some(variable_declaration) = free_variable_declarations.borrow().iter()
.find(|x| x.name == variable_name) .find(|x| x.name == variable_name)
{ {
return Some(std::rc::Rc::clone(&variable_declaration)); return Some(std::rc::Rc::clone(&variable_declaration));
@ -83,15 +67,16 @@ impl<'p> VariableDeclarationStackLayer<'p>
None None
}, },
VariableDeclarationStackLayer::Bound(bound) => VariableDeclarationStackLayer::Bound(bound_variable_declarations) =>
{ {
if let Some(variable_declaration) = bound.variable_declarations.iter() if let Some(variable_declaration) = bound_variable_declarations
.variable_declarations.iter()
.find(|x| x.name == variable_name) .find(|x| x.name == variable_name)
{ {
return Some(std::rc::Rc::clone(&variable_declaration)); return Some(std::rc::Rc::clone(&variable_declaration));
} }
bound.parent.find(variable_name) bound_variable_declarations.parent.find(variable_name)
}, },
} }
} }
@ -100,9 +85,9 @@ impl<'p> VariableDeclarationStackLayer<'p>
{ {
match self match self
{ {
VariableDeclarationStackLayer::Free(free) => VariableDeclarationStackLayer::Free(free_variable_declarations) =>
{ {
if let Some(variable_declaration) = free.variable_declarations.borrow().iter() if let Some(variable_declaration) = free_variable_declarations.borrow().iter()
.find(|x| x.name == variable_name) .find(|x| x.name == variable_name)
{ {
return std::rc::Rc::clone(&variable_declaration); return std::rc::Rc::clone(&variable_declaration);
@ -114,20 +99,21 @@ impl<'p> VariableDeclarationStackLayer<'p>
}; };
let variable_declaration = std::rc::Rc::new(variable_declaration); let variable_declaration = std::rc::Rc::new(variable_declaration);
free.variable_declarations.borrow_mut() free_variable_declarations.borrow_mut()
.push(std::rc::Rc::clone(&variable_declaration)); .push(std::rc::Rc::clone(&variable_declaration));
variable_declaration variable_declaration
}, },
VariableDeclarationStackLayer::Bound(bound) => VariableDeclarationStackLayer::Bound(bound_variable_declarations) =>
{ {
if let Some(variable_declaration) = bound.variable_declarations.iter() if let Some(variable_declaration) = bound_variable_declarations
.variable_declarations.iter()
.find(|x| x.name == variable_name) .find(|x| x.name == variable_name)
{ {
return std::rc::Rc::clone(&variable_declaration); return std::rc::Rc::clone(&variable_declaration);
} }
bound.parent.find_or_create(variable_name) bound_variable_declarations.parent.find_or_create(variable_name)
}, },
} }
} }
@ -139,9 +125,10 @@ impl<'p> VariableDeclarationStackLayer<'p>
{ {
match self match self
{ {
VariableDeclarationStackLayer::Free(free) => f(&free.variable_declarations.borrow()), VariableDeclarationStackLayer::Free(free_variable_declarations)
VariableDeclarationStackLayer::Bound(bound) => f(&free_variable_declarations.borrow()),
=> bound.parent.free_variable_declarations_do(f), VariableDeclarationStackLayer::Bound(bound_variable_declarations)
=> bound_variable_declarations.parent.free_variable_declarations_do(f),
} }
} }
} }