add term variables

This commit is contained in:
Mark Thom
2018-09-24 23:33:47 -06:00
parent 5e30fa6264
commit 4cf1aa0d8f
6 changed files with 70 additions and 33 deletions

View File

@@ -120,7 +120,7 @@ impl<'a> SubModuleUser for MachineCodeIndices<'a> {
static LISTS: &str = include_str!("../lib/lists.pl");
static CONTROL: &str = include_str!("../lib/control.pl");
static QUEUES: &str = include_str!("../lib/queues.pl");
static NUMVARS: &str = include_str!("../lib/numbervars.pl");
static TERMS: &str = include_str!("../lib/terms.pl");
impl Machine {
pub fn new() -> Self {
@@ -144,7 +144,7 @@ impl Machine {
compile_user_module(&mut wam, LISTS.as_bytes());
compile_user_module(&mut wam, CONTROL.as_bytes());
compile_user_module(&mut wam, QUEUES.as_bytes());
compile_user_module(&mut wam, NUMVARS.as_bytes());
compile_user_module(&mut wam, TERMS.as_bytes());
wam
}

View File

@@ -1,13 +1,14 @@
use prolog_parser::ast::*;
use prolog::heap_iter::*;
use prolog::instructions::*;
use prolog::machine::MachineCodeIndices;
use prolog::machine::machine_errors::*;
use prolog::machine::machine_state::*;
use prolog::num::{ToPrimitive, Zero};
use prolog::num::bigint::{BigInt};
//use prolog::term_writer::*;
use std::collections::HashSet;
use std::rc::Rc;
struct BrentAlgState {
@@ -435,6 +436,47 @@ impl MachineState {
return Err(err);
},
&SystemClauseType::Succeed => {},
&SystemClauseType::TermVariables => {
let a1 = self[temp_v!(1)].clone();
let mut vars = Vec::new();
{
let iter = HCPreOrderIterator::new(self, a1);
for item in iter {
match item {
HeapCellValue::Addr(Addr::HeapCell(h)) =>
vars.push(Ref::HeapCell(h)),
HeapCellValue::Addr(Addr::StackCell(fr, sc)) =>
vars.push(Ref::StackCell(fr, sc)),
_ => {}
}
}
}
let mut h = self.heap.h;
let outcome = Addr::HeapCell(h);
let mut seen_vars = HashSet::new();
for r in vars {
if seen_vars.contains(&r) {
continue;
}
self.heap.push(HeapCellValue::Addr(Addr::Lis(h+1)));
self.heap.push(HeapCellValue::Addr(r.as_addr()));
h += 2;
seen_vars.insert(r);
}
self.heap.push(HeapCellValue::Addr(Addr::Con(Constant::EmptyList)));
let a2 = self[temp_v!(2)].clone();
self.unify(a2, outcome);
},
&SystemClauseType::UnwindStack => self.unwind_stack()
};