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

View File

@ -92,12 +92,8 @@ impl fmt::Display for Unary {
Self::Empty => write!(f, ".empty"), Self::Empty => write!(f, ".empty"),
Self::Length => write!(f, ".length"), Self::Length => write!(f, ".length"),
Self::ToStr => write!(f, ".tostr"), Self::ToStr => write!(f, ".tostr"),
Self::Qualifier(q) => write!(f, ".{q}"),
Self::ToEl => write!(f, ".toel"), Self::ToEl => write!(f, ".toel"),
Self::Source => write!(f, ".source"), Self::Qualifier(q) => write!(f, ".{q}"),
Self::Target => write!(f, ".target"),
Self::Neighbours => write!(f, ".neighbours"),
Self::System => write!(f, ".system"),
} }
} }
} }
@ -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 { impl fmt::Display for Qualifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::Label(q) => write!(f, "{q}"), Self::Label(q) => write!(f, "{q}"),
Self::Restricted(q) => write!(f, "{q}"), Self::Restricted(q) => write!(f, "{q}"),
Self::System(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()), Variable::Id("a".into()),
None, None,
Box::new(Expression::Unary( Box::new(Expression::Unary(
Unary::Neighbours, Unary::Qualifier(Qualifier::Node(
QualifierNode::Neighbours)),
Box::new(Expression::Unary( Box::new(Expression::Unary(
Unary::Source, Unary::Qualifier(Qualifier::Edge(
QualifierEdge::Source)),
Box::new(Expression::Var( Box::new(Expression::Var(
Variable::Special(EdgeRelablerInput::Edge) Variable::Special(EdgeRelablerInput::Edge)
)) ))
@ -1404,9 +1406,11 @@ fn assert_tycheck_system() {
Qualifier::System(QualifierSystem::Entities) Qualifier::System(QualifierSystem::Entities)
), ),
Box::new(Expression::Unary( Box::new(Expression::Unary(
Unary::System, Unary::Qualifier(Qualifier::Node(
QualifierNode::System)),
Box::new(Expression::Unary( Box::new(Expression::Unary(
Unary::Target, Unary::Qualifier(Qualifier::Edge(
QualifierEdge::Target)),
Box::new(Expression::Var( Box::new(Expression::Var(
Variable::Special(EdgeRelablerInput::Edge) Variable::Special(EdgeRelablerInput::Edge)
)) ))