correct the copier's mishandling of cyclic lists
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use prolog::and_stack::*;
|
use prolog::and_stack::*;
|
||||||
use prolog::ast::*;
|
use prolog::ast::*;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::ops::IndexMut;
|
use std::ops::IndexMut;
|
||||||
|
|
||||||
pub trait CopierTarget
|
pub trait CopierTarget
|
||||||
@@ -21,6 +22,11 @@ pub trait CopierTarget
|
|||||||
let mut scan = self.source();
|
let mut scan = self.source();
|
||||||
let old_h = self.threshold();
|
let old_h = self.threshold();
|
||||||
|
|
||||||
|
// Lists have a flattened representation as structures,
|
||||||
|
// removing the need for a NamedStr variant, so we use a
|
||||||
|
// redirection table for reconstructing lists.
|
||||||
|
let mut list_redirect = HashMap::new();
|
||||||
|
|
||||||
self.push(HeapCellValue::Addr(a));
|
self.push(HeapCellValue::Addr(a));
|
||||||
|
|
||||||
while scan < self.threshold() {
|
while scan < self.threshold() {
|
||||||
@@ -30,6 +36,13 @@ pub trait CopierTarget
|
|||||||
HeapCellValue::Addr(a) =>
|
HeapCellValue::Addr(a) =>
|
||||||
match a.clone() {
|
match a.clone() {
|
||||||
Addr::Lis(a) => {
|
Addr::Lis(a) => {
|
||||||
|
if let Some(idx) = list_redirect.get(&a) {
|
||||||
|
self[scan] = HeapCellValue::Addr(Addr::Lis(*idx));
|
||||||
|
scan += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
list_redirect.insert(a, self.threshold());
|
||||||
self[scan] = HeapCellValue::Addr(Addr::Lis(self.threshold()));
|
self[scan] = HeapCellValue::Addr(Addr::Lis(self.threshold()));
|
||||||
|
|
||||||
let hcv = self[a].clone();
|
let hcv = self[a].clone();
|
||||||
|
|||||||
@@ -1416,7 +1416,8 @@ fn test_queries_on_builtins()
|
|||||||
[["Sorted = [1 - a, 1 - z, 1 - a, 2 - 99, 2 - 44, 3 - f(_7)]"]]);
|
[["Sorted = [1 - a, 1 - z, 1 - a, 2 - 99, 2 - 44, 3 - f(_7)]"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- keysort([X-1,1-1],[2-1,1-1]).",
|
assert_prolog_success!(&mut wam, "?- keysort([X-1,1-1],[2-1,1-1]).",
|
||||||
[["X = 2"]]);
|
[["X = 2"]]);
|
||||||
|
//TODO: enable the printer to print cyclic terms. Then run this test.
|
||||||
|
//assert_prolog_failure!(&mut wam, "?- Pairs = [a-a|Pairs], keysort(Pairs, _).");
|
||||||
assert_prolog_success!(&mut wam, "?- keysort([], L).",
|
assert_prolog_success!(&mut wam, "?- keysort([], L).",
|
||||||
[["L = []"]]);
|
[["L = []"]]);
|
||||||
assert_prolog_success!(&mut wam, "?- catch(keysort([a|_], _), error(E, _), true).",
|
assert_prolog_success!(&mut wam, "?- catch(keysort([a|_], _), error(E, _), true).",
|
||||||
|
|||||||
Reference in New Issue
Block a user