Finished refactoring

This commit is contained in:
elvis
2025-08-24 03:35:32 +02:00
parent 3a4c4d43c2
commit 9a80044a89
11 changed files with 511 additions and 301 deletions

View File

@ -3,44 +3,47 @@
// -----------------------------------------------------------------------------
use std::fmt;
use super::dsl::*;
use super::super::translator::{Formatter, PrintableWithTranslator, Translator};
impl<S> fmt::Display for RSassert<S> where S: fmt::Display {
impl<S> fmt::Debug for Assert<S> where S: fmt::Debug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "label {{\n{}\n}}", self.tree)
write!(f, "label {{\n{:?}\n}}", self.tree)
}
}
impl<S> fmt::Display for Tree<S> where S: fmt::Display {
impl<S> fmt::Debug for Tree<S> where S: fmt::Debug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Concat(t1, t2) => {write!(f, "{t1};\n{t2}")},
Self::If(exp, t) => {write!(f, "if {exp} {{\n{t}\n}}")},
Self::Concat(t1, t2) => {write!(f, "{t1:?};\n{t2:?}")},
Self::If(exp, t) => {write!(f, "if {exp:?} {{\n{t:?}\n}}")},
Self::IfElse(exp, t1, t2) => {
write!(f, "if {exp} {{\n{t1}\n}} else {{\n{t2}\n}}")
write!(f, "if {exp:?} {{\n{t1:?}\n}} else {{\n{t2:?}\n}}")
},
Self::Assignment(v, q, exp) => {
if let Some(q) = q {
write!(f, "{v}.{q} = {exp}")
write!(f, "{v:?}.{q:?} = {exp:?}")
} else {
write!(f, "{v} = {exp}")
write!(f, "{v:?} = {exp:?}")
}
},
Self::Return(exp) => {write!(f, "return {exp}")},
Self::For(v, r, t) => {write!(f, "for {v} in {r} {{\n{t}\n}}")},
Self::Return(exp) => {write!(f, "return {exp:?}")},
Self::For(v, r, t) => {
write!(f, "for {v:?} in {r:?} {{\n{t:?}\n}}")
},
}
}
}
impl<S> fmt::Display for Variable<S> where S: fmt::Display {
impl<S> fmt::Debug for Variable<S> where S: fmt::Debug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Special(s) => {write!(f, "{s}")},
Self::Id(s) => {write!(f, "{s}")}
Self::Special(s) => {write!(f, "{s:?}")},
Self::Id(s) => {write!(f, "{s:?}")}
}
}
}
impl<S> fmt::Display for Expression<S> where S: fmt::Display {
impl<S> fmt::Debug for Expression<S> where S: fmt::Debug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::True => {write!(f, "True")},
@ -49,24 +52,24 @@ impl<S> fmt::Display for Expression<S> where S: fmt::Display {
Self::Label(rslabel) => {write!(f, "{{debug: {rslabel:?}}}")},
Self::Set(set) => {write!(f, "{{debug: {set:?}}}")},
Self::Element(el) => {write!(f, "'{{debug: {el:?}}}'")},
Self::String(s) => {write!(f, r#""{s}""#)},
Self::Var(v) => {write!(f, "{v}")},
Self::String(s) => {write!(f, r#""{s:?}""#)},
Self::Var(v) => {write!(f, "{v:?}")},
Self::Unary(u, exp) => {
if u.is_prefix() {
write!(f, "{u}({exp})")
write!(f, "{u:?}({exp:?})")
} else if u.is_suffix() {
write!(f, "{exp}{u}")
write!(f, "{exp:?}{u:?}")
} else {
unreachable!()
}
},
Self::Binary(b, exp1, exp2) => {
if b.is_prefix() {
write!(f, "{b}({exp1}, {exp2})")
write!(f, "{b:?}({exp1:?}, {exp2:?})")
} else if b.is_suffix() {
write!(f, "({exp1}, {exp2}){b}")
write!(f, "({exp1:?}, {exp2:?}){b:?}")
} else if b.is_infix() {
write!(f, "({exp1} {b} {exp2})")
write!(f, "({exp1:?} {b:?} {exp2:?})")
} else {
unreachable!()
}
@ -75,16 +78,16 @@ impl<S> fmt::Display for Expression<S> where S: fmt::Display {
}
}
impl<S> fmt::Display for Range<S> where S: fmt::Display {
impl<S> fmt::Debug for Range<S> where S: fmt::Debug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::IterateOverSet(exp) => {write!(f, "{{{exp}}}")},
Self::IterateInRange(exp1, exp2) => {write!(f, "{exp1}..{exp2}")}
Self::IterateOverSet(exp) => write!(f, "{{{exp:?}}}"),
Self::IterateInRange(exp1, exp2) => write!(f, "{exp1:?}..{exp2:?}")
}
}
}
impl fmt::Display for Unary {
impl fmt::Debug for Unary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Not => write!(f, "not"),
@ -93,12 +96,12 @@ impl fmt::Display for Unary {
Self::Length => write!(f, ".length"),
Self::ToStr => write!(f, ".tostr"),
Self::ToEl => write!(f, ".toel"),
Self::Qualifier(q) => write!(f, ".{q}"),
Self::Qualifier(q) => write!(f, ".{q:?}"),
}
}
}
impl fmt::Display for QualifierRestricted {
impl fmt::Debug for QualifierRestricted {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Entities => write!(f, "Entities"),
@ -112,7 +115,7 @@ impl fmt::Display for QualifierRestricted {
}
}
impl fmt::Display for QualifierLabel {
impl fmt::Debug for QualifierLabel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AvailableEntities => write!(f, "AvailableEntities"),
@ -122,7 +125,7 @@ impl fmt::Display for QualifierLabel {
}
}
impl fmt::Display for QualifierSystem {
impl fmt::Debug for QualifierSystem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Context => write!(f, "context"),
@ -131,7 +134,7 @@ impl fmt::Display for QualifierSystem {
}
}
impl fmt::Display for QualifierEdge {
impl fmt::Debug for QualifierEdge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Source => write!(f, "source"),
@ -140,7 +143,7 @@ impl fmt::Display for QualifierEdge {
}
}
impl fmt::Display for QualifierNode {
impl fmt::Debug for QualifierNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Neighbours => write!(f, "neighbours"),
@ -149,19 +152,19 @@ impl fmt::Display for QualifierNode {
}
}
impl fmt::Display for Qualifier {
impl fmt::Debug 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}"),
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:?}"),
}
}
}
impl fmt::Display for Binary {
impl fmt::Debug for Binary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::And => write!(f, "&&"),
@ -188,12 +191,12 @@ impl fmt::Display for Binary {
}
}
impl fmt::Display for AssertReturnValue {
impl fmt::Debug for AssertReturnValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Boolean(b) => write!(f, "{b}"),
Self::Integer(i) => write!(f, "{i}"),
Self::String(s) => write!(f, r#""{s}""#),
Self::Boolean(b) => write!(f, "{b:?}"),
Self::Integer(i) => write!(f, "{i:?}"),
Self::String(s) => write!(f, r#""{s:?}""#),
Self::Label(l) => write!(f, "{{debug: {l:?}}}"),
Self::Set(set) => write!(f, "{{debug: {set:?}}}"),
Self::Element(el) => write!(f, "{{debug: {el:?}}}"),
@ -207,7 +210,7 @@ impl fmt::Display for AssertReturnValue {
}
}
impl fmt::Display for AssertionTypes {
impl fmt::Debug for AssertionTypes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Boolean => write!(f, "boolean"),
@ -227,3 +230,245 @@ impl fmt::Display for AssertionTypes {
}
}
}
// -----------------------------------------------------------------------------
impl<S> PrintableWithTranslator for Assert<S>
where S: PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
write!(f, "label {{\n{}\n}}", Formatter::from(translator, &self.tree))
}
}
impl<S> PrintableWithTranslator for Tree<S>
where S: PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::Concat(t1, t2) =>
write!(f, "{};\n{}",
Formatter::from(translator, &**t1),
Formatter::from(translator, &**t2)),
Self::If(exp, t) =>
write!(f, "if {} {{\n{}\n}}",
Formatter::from(translator, &**exp),
Formatter::from(translator, &**t)),
Self::IfElse(exp, t1, t2) => {
write!(f, "if {} {{\n{}\n}} else {{\n{}\n}}",
Formatter::from(translator, &**exp),
Formatter::from(translator, &**t1),
Formatter::from(translator, &**t2))
},
Self::Assignment(v, q, exp) => {
if let Some(q) = q {
write!(f, "{}.{} = {}",
Formatter::from(translator, v),
Formatter::from(translator, q),
Formatter::from(translator, &**exp))
} else {
write!(f, "{} = {}",
Formatter::from(translator, v),
Formatter::from(translator, &**exp))
}
},
Self::Return(exp) =>
write!(f, "return {}", Formatter::from(translator, &**exp)),
Self::For(v, r, t) => {
write!(f, "for {} in {} {{\n{}\n}}",
Formatter::from(translator, v),
Formatter::from(translator, r),
Formatter::from(translator, &**t))
},
}
}
}
impl<S> PrintableWithTranslator for Variable<S>
where S: PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::Special(s) => write!(f, "{}", Formatter::from(translator, s)),
Self::Id(s) => write!(f, "{s}")
}
}
}
impl<S> PrintableWithTranslator for Expression<S>
where S: PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::True => write!(f, "True"),
Self::False => write!(f, "False"),
Self::Integer(i) => write!(f, "{i}"),
Self::Label(l) =>
write!(f, "{}", Formatter::from(translator, &**l)),
Self::Set(set) => write!(f, "{}", Formatter::from(translator, set)),
Self::Element(el) =>
write!(f, "'{}'",
translator.decode(*el).unwrap_or("Missing".into())),
Self::String(s) => write!(f, r#""{s}""#),
Self::Var(v) => write!(f, "{}", Formatter::from(translator, v)),
Self::Unary(u, exp) => {
if u.is_prefix() {
write!(f, "{}({})",
Formatter::from(translator, u),
Formatter::from(translator, &**exp))
} else if u.is_suffix() {
write!(f, "{}{}",
Formatter::from(translator, &**exp),
Formatter::from(translator, u))
} else {
unreachable!()
}
},
Self::Binary(b, exp1, exp2) => {
if b.is_prefix() {
write!(f, "{}({}, {})",
Formatter::from(translator, b),
Formatter::from(translator, &**exp1),
Formatter::from(translator, &**exp2))
} else if b.is_suffix() {
write!(f, "({}, {}){}",
Formatter::from(translator, &**exp1),
Formatter::from(translator, &**exp2),
Formatter::from(translator, b))
} else if b.is_infix() {
write!(f, "({} {} {})",
Formatter::from(translator, &**exp1),
Formatter::from(translator, b),
Formatter::from(translator, &**exp2))
} else {
unreachable!()
}
},
}
}
}
impl<S> PrintableWithTranslator for Range<S>
where S: PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::IterateOverSet(exp) =>
write!(f, "{}", Formatter::from(translator, &**exp)),
Self::IterateInRange(exp1, exp2) =>
write!(f, "{}..{}",
Formatter::from(translator, &**exp1),
Formatter::from(translator, &**exp2))
}
}
}
impl PrintableWithTranslator for Unary {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::Not => write!(f, "not"),
Self::Rand => write!(f, "rand"),
Self::Empty => write!(f, ".empty"),
Self::Length => write!(f, ".length"),
Self::ToStr => write!(f, ".tostr"),
Self::ToEl => write!(f, ".toel"),
Self::Qualifier(q) =>
write!(f, ".{}", Formatter::from(translator, q)),
}
}
}
impl PrintableWithTranslator for QualifierRestricted {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for QualifierLabel {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for QualifierSystem {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for QualifierEdge {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for QualifierNode {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for Qualifier {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::Label(q) =>
write!(f, "{}", Formatter::from(translator, q)),
Self::Restricted(q) =>
write!(f, "{}", Formatter::from(translator, q)),
Self::System(q) =>
write!(f, "{}", Formatter::from(translator, q)),
Self::Edge(q) =>
write!(f, "{}", Formatter::from(translator, q)),
Self::Node(q) =>
write!(f, "{}", Formatter::from(translator, q)),
}
}
}
impl PrintableWithTranslator for Binary {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}
impl PrintableWithTranslator for AssertReturnValue {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result {
match self {
Self::Boolean(b) => write!(f, "{b}"),
Self::Integer(i) => write!(f, "{i}"),
Self::String(s) => write!(f, r#""{s}""#),
Self::Label(l) =>
write!(f, "{}", Formatter::from(translator, l)),
Self::Set(set) =>
write!(f, "{}", Formatter::from(translator, set)),
Self::Element(el) =>
write!(f, "{}",
translator.decode(*el).unwrap_or("Missing".into())),
Self::Edge(edge) => write!(f, "{{edge: {edge:?}}}"),
Self::Node(node) => write!(f, "{{node: {node:?}}}"),
Self::Neighbours(node) =>
write!(f, "{{node: {node:?}}}.neighbours"),
Self::System(sys) =>
write!(f, "{}", Formatter::from(translator, sys)),
Self::Context(ctx) =>
write!(f, "{}", Formatter::from(translator, ctx)),
}
}
}
impl PrintableWithTranslator for AssertionTypes {
fn print(&self, f: &mut fmt::Formatter, _translator: &Translator)
-> fmt::Result {
write!(f, "{self:?}")
}
}