Expose variable stack
This commit is contained in:
parent
f19f1a3eb1
commit
6b10cced7c
59
src/ast.rs
59
src/ast.rs
@ -40,11 +40,70 @@ pub struct PredicateDeclaration
|
||||
pub arity: usize,
|
||||
}
|
||||
|
||||
#[derive(Eq, Hash, PartialEq)]
|
||||
pub struct VariableDeclaration
|
||||
{
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub type VariableDeclarations = std::collections::HashSet<std::rc::Rc<VariableDeclaration>>;
|
||||
|
||||
pub struct VariableDeclarationStack
|
||||
{
|
||||
pub free_variable_declarations: VariableDeclarations,
|
||||
bound_variable_declaration_stack: Vec<std::rc::Rc<VariableDeclarations>>,
|
||||
}
|
||||
|
||||
impl VariableDeclarationStack
|
||||
{
|
||||
pub fn find(&self, variable_name: &str) -> Option<std::rc::Rc<VariableDeclaration>>
|
||||
{
|
||||
for variable_declarations in self.bound_variable_declaration_stack.iter().rev()
|
||||
{
|
||||
if let Some(variable_declaration) = variable_declarations.iter().find(|x| x.name == variable_name)
|
||||
{
|
||||
return Some(std::rc::Rc::clone(&variable_declaration));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(variable_declaration) = self.free_variable_declarations.iter().find(|x| x.name == variable_name)
|
||||
{
|
||||
return Some(std::rc::Rc::clone(&variable_declaration));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn find_or_create(&mut self, variable_name: &str) -> std::rc::Rc<VariableDeclaration>
|
||||
{
|
||||
if let Some(variable_declaration) = self.find(variable_name)
|
||||
{
|
||||
return variable_declaration;
|
||||
}
|
||||
|
||||
let variable_declaration = VariableDeclaration
|
||||
{
|
||||
name: variable_name.to_owned(),
|
||||
};
|
||||
let variable_declaration = std::rc::Rc::new(variable_declaration);
|
||||
|
||||
self.free_variable_declarations.insert(std::rc::Rc::clone(&variable_declaration));
|
||||
|
||||
variable_declaration
|
||||
}
|
||||
|
||||
pub fn push(&mut self, bound_variable_declarations: std::rc::Rc<VariableDeclarations>)
|
||||
{
|
||||
self.bound_variable_declaration_stack.push(bound_variable_declarations);
|
||||
}
|
||||
|
||||
pub fn pop(&mut self)
|
||||
{
|
||||
// TODO: return error instead
|
||||
self.bound_variable_declaration_stack.pop().expect("bound variable is empty, cannot pop last element");
|
||||
}
|
||||
}
|
||||
|
||||
// Terms
|
||||
|
||||
pub struct BinaryOperation
|
||||
|
47
src/parse.rs
47
src/parse.rs
@ -10,53 +10,6 @@ use nom::
|
||||
bytes::complete::tag,
|
||||
};
|
||||
|
||||
type VariableDeclarations = std::collections::HashSet<std::rc::Rc<crate::VariableDeclaration>>;
|
||||
|
||||
struct VariableDeclarationStack
|
||||
{
|
||||
free_variable_declarations: VariableDeclarations,
|
||||
bound_variable_declarations: Vec<std::rc::Rc<VariableDeclarations>>,
|
||||
}
|
||||
|
||||
impl VariableDeclarationStack
|
||||
{
|
||||
fn find(&self, variable_name: &str) -> Option<std::rc::Rc<crate::VariableDeclaration>>
|
||||
{
|
||||
for variable_declarations in self.iter().rev()
|
||||
{
|
||||
if let Some(variable_declaration) = variable_declarations.iter().find(|x| x.name == variable_name)
|
||||
{
|
||||
return Some(std::rc::Rc::clone(&variable_declaration));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(variable_declaration) = self.free_variable_declarations.iter().find(|x| x.name == variable_name)
|
||||
{
|
||||
return Some(std::rc::Rc::clone(&variable_declaration));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn find_or_create(&mut self, variable_name: &str) -> std::rc::Rc<crate::VariableDeclaration>
|
||||
{
|
||||
if let Some(variable_declaration) = self.find(variable_name)
|
||||
{
|
||||
return variable_declaration;
|
||||
}
|
||||
|
||||
let variable_declaration = crate::VariableDeclaration
|
||||
{
|
||||
name: variable_name.to_owned(),
|
||||
};
|
||||
let variable_declaration = std::rc::Rc::new(variable_declaration);
|
||||
|
||||
self.free_variable_declarations.insert(std::rc::Rc::clone(&variable_declaration));
|
||||
|
||||
variable_declaration
|
||||
}
|
||||
}
|
||||
|
||||
struct Declarations
|
||||
{
|
||||
//function_declarations:,
|
||||
|
Loading…
Reference in New Issue
Block a user