]> Repositorios git - scryer-prolog.git/commitdiff
discard CodeIndex literals from unfolded control operators in preprocessor (#1791)
authorMark <[email protected]>
Sat, 22 Apr 2023 23:14:00 +0000 (17:14 -0600)
committerMark <[email protected]>
Sun, 23 Apr 2023 00:38:50 +0000 (18:38 -0600)
src/machine/preprocessor.rs
src/parser/ast.rs

index 37871bb0525fee8fdc4007991262b1ef1e0052c3..7f0cc2649671fc01e56575fa2d3c65f70a050bf2 100644 (file)
@@ -399,7 +399,7 @@ fn check_for_internal_if_then(terms: &mut Vec<Term>) {
     }
 
     if let Some(Term::Clause(_, name, ref subterms)) = terms.last() {
-        if *name != atom!("->") || subterms.len() != 2 {
+        if *name != atom!("->") || source_arity(subterms) != 2 {
             return;
         }
     } else {
@@ -770,7 +770,7 @@ impl Preprocessor {
             Term::Var(_, ref v) if v.as_str() == "!" => {
                 Ok(QueryTerm::UnblockedCut(Cell::default()))
             }
-            Term::Clause(r, name, mut terms) => match (name, terms.len()) {
+            Term::Clause(r, name, mut terms) => match (name, source_arity(&terms)) {
                 (atom!(";"), 2) => {
                     let term = Term::Clause(r, name, terms);
 
@@ -900,7 +900,7 @@ impl Preprocessor {
             let mut term = term;
 
             if let Term::Clause(cell, name, terms) = term {
-                if name == atom!(",") && terms.len() == 2 {
+                if name == atom!(",") && source_arity(&terms) == 2 {
                     let term = Term::Clause(cell, name, terms);
                     let mut subterms = unfold_by_str(term, atom!(","));
 
index b1c0f5d9e51dc7ab9f5c480713b1cae0dd2b75f1..0933b11c0c3d9f5ca911ff9f67df005a32330e05 100644 (file)
@@ -626,8 +626,25 @@ impl Term {
     }
 }
 
+#[inline]
+pub fn source_arity(terms: &[Term]) -> usize {
+    if let Some(last_arg) = terms.last() {
+        if let Term::Literal(_, Literal::CodeIndex(_)) = last_arg {
+            return terms.len() - 1;
+        }
+    }
+
+    terms.len()
+}
+
 fn unfold_by_str_once(term: &mut Term, s: Atom) -> Option<(Term, Term)> {
     if let Term::Clause(_, ref name, ref mut subterms) = term {
+        if let Some(last_arg) = subterms.last() {
+            if let Term::Literal(_, Literal::CodeIndex(_)) = last_arg {
+                subterms.pop();
+            }
+        }
+
         if name == &s && subterms.len() == 2 {
             let snd = subterms.pop().unwrap();
             let fst = subterms.pop().unwrap();