Value::from_heapcell()
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::atom_table;
|
use crate::atom_table;
|
||||||
use crate::heap_print::{HCPrinter, HCValueOutputter, PrinterOutputter};
|
|
||||||
use crate::machine::machine_indices::VarKey;
|
use crate::machine::machine_indices::VarKey;
|
||||||
use crate::machine::mock_wam::CompositeOpDir;
|
use crate::machine::mock_wam::CompositeOpDir;
|
||||||
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
|
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
|
||||||
@@ -96,34 +94,18 @@ impl Iterator for QueryState<'_> {
|
|||||||
if var_key.to_string().starts_with('_') {
|
if var_key.to_string().starts_with('_') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut printer = HCPrinter::new(
|
|
||||||
&mut machine.machine_st.heap,
|
|
||||||
Arc::clone(&machine.machine_st.atom_tbl),
|
|
||||||
&mut machine.machine_st.stack,
|
|
||||||
&machine.indices.op_dir,
|
|
||||||
PrinterOutputter::new(),
|
|
||||||
*term_to_be_printed,
|
|
||||||
);
|
|
||||||
|
|
||||||
printer.ignore_ops = false;
|
let term = Value::from_heapcell(machine, term_to_be_printed, var_names);
|
||||||
printer.numbervars = true;
|
|
||||||
printer.quoted = true;
|
|
||||||
printer.max_depth = 1000; // NOTE: set this to 0 for unbounded depth
|
|
||||||
printer.double_quotes = true;
|
|
||||||
printer.var_names.clone_from(var_names);
|
|
||||||
|
|
||||||
let outputter = printer.print();
|
if let Value::String(ref term_str) = term {
|
||||||
|
if *term_str == var_key.to_string() {
|
||||||
let output: String = outputter.result();
|
continue;
|
||||||
|
|
||||||
if var_key.to_string() != output {
|
|
||||||
bindings.insert(
|
|
||||||
var_key.to_string(),
|
|
||||||
Value::try_from(output).expect("Couldn't convert Houtput to Value"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindings.insert(var_key.to_string(), term);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: there are outstanding choicepoints, backtrack
|
// NOTE: there are outstanding choicepoints, backtrack
|
||||||
// through them for further solutions. if
|
// through them for further solutions. if
|
||||||
// self.machine_st.b == stub_b we've backtracked to the stub
|
// self.machine_st.b == stub_b we've backtracked to the stub
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
use crate::atom_table::*;
|
use crate::atom_table::*;
|
||||||
|
use crate::heap_print::PrinterOutputter;
|
||||||
|
use crate::heap_print::{HCPrinter, HCValueOutputter};
|
||||||
|
use crate::parser::ast;
|
||||||
use dashu::*;
|
use dashu::*;
|
||||||
|
use indexmap::IndexMap;
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::HeapCellValue;
|
||||||
|
use super::Machine;
|
||||||
|
|
||||||
pub type QueryResult = Result<QueryResolution, String>;
|
pub type QueryResult = Result<QueryResolution, String>;
|
||||||
|
|
||||||
@@ -133,6 +141,35 @@ pub enum Value {
|
|||||||
Var,
|
Var,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
pub(crate) fn from_heapcell(
|
||||||
|
machine: &mut Machine,
|
||||||
|
heap_cell: &HeapCellValue,
|
||||||
|
var_names: &IndexMap<HeapCellValue, ast::VarPtr>,
|
||||||
|
) -> Self {
|
||||||
|
let mut printer = HCPrinter::new(
|
||||||
|
&mut machine.machine_st.heap,
|
||||||
|
Arc::clone(&machine.machine_st.atom_tbl),
|
||||||
|
&mut machine.machine_st.stack,
|
||||||
|
&machine.indices.op_dir,
|
||||||
|
PrinterOutputter::new(),
|
||||||
|
*heap_cell,
|
||||||
|
);
|
||||||
|
|
||||||
|
printer.ignore_ops = false;
|
||||||
|
printer.numbervars = true;
|
||||||
|
printer.quoted = true;
|
||||||
|
printer.max_depth = 1000; // NOTE: set this to 0 for unbounded depth
|
||||||
|
printer.double_quotes = true;
|
||||||
|
printer.var_names.clone_from(var_names);
|
||||||
|
|
||||||
|
let outputter = printer.print();
|
||||||
|
|
||||||
|
let output: String = outputter.result();
|
||||||
|
Value::try_from(output).expect("Couldn't convert Houtput to Value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<BTreeMap<&str, Value>> for QueryMatch {
|
impl From<BTreeMap<&str, Value>> for QueryMatch {
|
||||||
fn from(bindings: BTreeMap<&str, Value>) -> Self {
|
fn from(bindings: BTreeMap<&str, Value>) -> Self {
|
||||||
QueryMatch {
|
QueryMatch {
|
||||||
|
|||||||
Reference in New Issue
Block a user