Harder, better, faster, stronger refactoring.

This commit is contained in:
elvis
2025-08-17 02:53:56 +02:00
parent 397cf20008
commit e409a5a6c2
3 changed files with 65 additions and 29 deletions

View File

@ -32,10 +32,17 @@ pub enum Variable<S> {
trait SpecialVariables<G>: std::fmt::Display + std::fmt::Debug + Sized + Eq
+ Copy + std::hash::Hash
{
/// Returns the type of the specific special variable.
fn type_of(&self) -> AssertionTypes;
/// Returns the type of the qualified special variable.
fn type_qualified(&self, q: &Qualifier) -> Result<AssertionTypes, String>;
/// Creates a new context.
fn new_context(input: HashMap<Self, G>)
-> HashMap<Self, AssertReturnValue>;
/// Returns true if
fn correct_type(&self, other: &AssertReturnValue) -> bool;
}
@ -68,12 +75,8 @@ pub enum Unary {
Empty,
Length,
ToStr,
Qualifier(Qualifier),
ToEl,
Source,
Target,
Neighbours,
System,
Qualifier(Qualifier),
}
#[derive(Debug, Clone, Copy)]
@ -100,11 +103,25 @@ pub enum QualifierSystem {
Context,
}
#[derive(Debug, Clone, Copy)]
pub enum QualifierEdge {
Source,
Target,
}
#[derive(Debug, Clone, Copy)]
pub enum QualifierNode {
Neighbours,
System,
}
#[derive(Debug, Clone, Copy)]
pub enum Qualifier {
System(QualifierSystem),
Label(QualifierLabel),
Restricted(QualifierRestricted),
Edge(QualifierEdge),
Node(QualifierNode),
}
#[derive(Debug, Clone, Copy)]
@ -254,11 +271,7 @@ impl Unary {
Self::Length |
Self::ToStr |
Self::Qualifier(_) |
Self::ToEl |
Self::Source |
Self::Target |
Self::Neighbours |
Self::System => false,
Self::ToEl => false,
}
}
@ -299,13 +312,11 @@ impl Unary {
(Self::ToEl, AssertionTypes::String) => {
Ok(AssertionTypes::Element)
},
(Self::Source, AssertionTypes::Edge) => {
(Self::Qualifier(Qualifier::Edge(_)), AssertionTypes::Edge) => {
Ok(AssertionTypes::Node)
},
(Self::Target, AssertionTypes::Edge) => {
Ok(AssertionTypes::Node)
},
(Self::Neighbours, AssertionTypes::Node) => {
(Self::Qualifier(Qualifier::Node(QualifierNode::Neighbours)),
AssertionTypes::Node) => {
Ok(AssertionTypes::RangeNeighbours)
},
(Self::Qualifier(
@ -318,7 +329,8 @@ impl Unary {
AssertionTypes::System) => {
Ok(AssertionTypes::Context)
},
(Self::System, AssertionTypes::Node) => {
(Self::Qualifier(Qualifier::Node(QualifierNode::System)),
AssertionTypes::Node) => {
Ok(AssertionTypes::System)
},
(op, type_exp) => {
@ -774,18 +786,22 @@ impl AssertReturnValue {
(AssertReturnValue::String(s), Unary::ToEl) => {
Ok(AssertReturnValue::Element(translator.encode(s)))
},
(AssertReturnValue::Edge(edge), Unary::Source) => {
(AssertReturnValue::Edge(edge),
Unary::Qualifier(Qualifier::Edge(QualifierEdge::Source))) => {
Ok(AssertReturnValue::Node(
graph.edge_endpoints(edge).unwrap().0))
},
(AssertReturnValue::Edge(edge), Unary::Target) => {
(AssertReturnValue::Edge(edge),
Unary::Qualifier(Qualifier::Edge(QualifierEdge::Target))) => {
Ok(AssertReturnValue::Node(
graph.edge_endpoints(edge).unwrap().1))
},
(AssertReturnValue::Node(node), Unary::Neighbours) => {
(AssertReturnValue::Node(node),
Unary::Qualifier(Qualifier::Node(QualifierNode::Neighbours))) => {
Ok(AssertReturnValue::Neighbours(node))
},
(AssertReturnValue::Node(node), Unary::System) => {
(AssertReturnValue::Node(node),
Unary::Qualifier(Qualifier::Node(QualifierNode::System))) => {
Ok(AssertReturnValue::System(
graph.node_weight(node).unwrap().clone()
))

View File

@ -92,12 +92,8 @@ impl fmt::Display for Unary {
Self::Empty => write!(f, ".empty"),
Self::Length => write!(f, ".length"),
Self::ToStr => write!(f, ".tostr"),
Self::Qualifier(q) => write!(f, ".{q}"),
Self::ToEl => write!(f, ".toel"),
Self::Source => write!(f, ".source"),
Self::Target => write!(f, ".target"),
Self::Neighbours => write!(f, ".neighbours"),
Self::System => write!(f, ".system"),
Self::Qualifier(q) => write!(f, ".{q}"),
}
}
}
@ -135,12 +131,32 @@ impl fmt::Display for QualifierSystem {
}
}
impl fmt::Display for QualifierEdge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Source => write!(f, "source"),
Self::Target => write!(f, "target"),
}
}
}
impl fmt::Display for QualifierNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Neighbours => write!(f, "neighbours"),
Self::System => write!(f, "system"),
}
}
}
impl fmt::Display for Qualifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Label(q) => write!(f, "{q}"),
Self::Restricted(q) => write!(f, "{q}"),
Self::System(q) => write!(f, "{q}"),
Self::Edge(q) => write!(f, "{q}"),
Self::Node(q) => write!(f, "{q}"),
}
}
}

View File

@ -1322,9 +1322,11 @@ fn assert_tycheck_for_8() {
Variable::Id("a".into()),
None,
Box::new(Expression::Unary(
Unary::Neighbours,
Unary::Qualifier(Qualifier::Node(
QualifierNode::Neighbours)),
Box::new(Expression::Unary(
Unary::Source,
Unary::Qualifier(Qualifier::Edge(
QualifierEdge::Source)),
Box::new(Expression::Var(
Variable::Special(EdgeRelablerInput::Edge)
))
@ -1404,9 +1406,11 @@ fn assert_tycheck_system() {
Qualifier::System(QualifierSystem::Entities)
),
Box::new(Expression::Unary(
Unary::System,
Unary::Qualifier(Qualifier::Node(
QualifierNode::System)),
Box::new(Expression::Unary(
Unary::Target,
Unary::Qualifier(Qualifier::Edge(
QualifierEdge::Target)),
Box::new(Expression::Var(
Variable::Special(EdgeRelablerInput::Edge)
))