add findall/3

This commit is contained in:
Mark Thom
2019-02-22 00:56:04 -07:00
parent 0453435bcd
commit f41b5f465e
10 changed files with 255 additions and 114 deletions

71
src/prolog/heap.rs Normal file
View File

@@ -0,0 +1,71 @@
use prolog_parser::ast::*;
use prolog::instructions::*;
use std::ops::{Index, IndexMut};
pub struct Heap {
heap: Vec<HeapCellValue>,
pub h: usize,
}
impl Heap {
pub fn with_capacity(cap: usize) -> Self {
Heap { heap: Vec::with_capacity(cap),
h: 0 }
}
pub fn push(&mut self, val: HeapCellValue) {
self.heap.push(val);
self.h += 1;
}
pub fn truncate(&mut self, h: usize) {
self.h = h;
self.heap.truncate(h);
}
pub fn len(&self) -> usize {
self.heap.len()
}
pub fn append(&mut self, vals: Vec<HeapCellValue>) {
let n = vals.len();
self.heap.extend(vals.into_iter());
self.h += n;
}
pub fn clear(&mut self) {
self.heap.clear();
self.h = 0;
}
pub fn to_list<Iter: Iterator<Item=Addr>>(&mut self, values: Iter) -> usize {
let head_addr = self.h;
for value in values {
let h = self.h;
self.push(HeapCellValue::Addr(Addr::Lis(h+1)));
self.push(HeapCellValue::Addr(value));
}
self.push(HeapCellValue::Addr(Addr::Con(Constant::EmptyList)));
head_addr
}
}
impl Index<usize> for Heap {
type Output = HeapCellValue;
fn index(&self, index: usize) -> &Self::Output {
&self.heap[index]
}
}
impl IndexMut<usize> for Heap {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.heap[index]
}
}