From: Mark Thom Date: Sat, 25 Aug 2018 05:14:41 +0000 (-0600) Subject: equate chars with atoms consisting of chars X-Git-Tag: v0.8.110~423 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=9759c523b68eb527867a863418db94cd3bb4c9fc;p=scryer-prolog.git equate chars with atoms consisting of chars --- diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index 23425e9a..f8e0a385 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -527,7 +527,7 @@ pub enum Fixity { In, Post, Pre } -#[derive(Clone, Eq, Hash, PartialEq)] +#[derive(Clone, Hash)] pub enum Constant { Atom(ClauseName), Char(char), @@ -537,6 +537,37 @@ pub enum Constant { EmptyList } +impl PartialEq for Constant { + fn eq(&self, other: &Constant) -> bool { + match (self, other) { + (&Constant::Atom(ref atom), &Constant::Char(c)) + | (&Constant::Char(c), &Constant::Atom(ref atom)) => { + let s = atom.as_str(); + if c.len_utf8() != s.len() || Some(c) != s.chars().next() { + false + } else { + true + } + }, + (&Constant::Atom(ref a1), &Constant::Atom(ref a2)) => + a1.as_str() == a2.as_str(), + (&Constant::Char(c1), &Constant::Char(c2)) => + c1 == c2, + (&Constant::Number(ref n1), &Constant::Number(ref n2)) => + n1 == n2, + (&Constant::String(ref s1), &Constant::String(ref s2)) => + s1 == s2, + (&Constant::EmptyList, &Constant::EmptyList) => + true, + (&Constant::Usize(u1), &Constant::Usize(u2)) => + u1 == u2, + _ => false + } + } +} + +impl Eq for Constant {} + impl Constant { pub fn to_atom(self) -> Option { match self { diff --git a/src/prolog/machine/machine_state_impl.rs b/src/prolog/machine/machine_state_impl.rs index 12db9d2a..850b0068 100644 --- a/src/prolog/machine/machine_state_impl.rs +++ b/src/prolog/machine/machine_state_impl.rs @@ -190,13 +190,6 @@ impl MachineState { self.fail = true; }, - (Addr::Con(Constant::Char(c)), Addr::Con(Constant::Atom(atom))) - | (Addr::Con(Constant::Atom(atom)), Addr::Con(Constant::Char(c))) => { - let s = atom.as_str(); - if c.len_utf8() != s.len() || Some(c) != s.chars().next() { - self.fail = true; - } - }, (Addr::Lis(a1), Addr::Con(Constant::String(ref s))) | (Addr::Con(Constant::String(ref s)), Addr::Lis(a1)) if self.flags.double_quotes.is_chars() => @@ -208,7 +201,7 @@ impl MachineState { pdl.push(Addr::HeapCell(a1)); } else { self.fail = true; - }, + }, (Addr::Con(Constant::EmptyList), Addr::Con(Constant::String(ref s))) | (Addr::Con(Constant::String(ref s)), Addr::Con(Constant::EmptyList)) if self.flags.double_quotes.is_chars() => { @@ -221,7 +214,7 @@ impl MachineState { pdl.push(Addr::HeapCell(a1 + 1)); pdl.push(Addr::HeapCell(a2 + 1)); }, - (Addr::Con(c1), Addr::Con(c2)) => + (Addr::Con(ref c1), Addr::Con(ref c2)) => if c1 != c2 { self.fail = true; },