use std::collections::BTreeSet;
+use crate::machine::BREAK_FROM_DISPATCH_LOOP_LOC;
+use crate::machine::mock_wam::{CompositeOpDir, Term};
+use crate::parser::parser::{Parser, Tokens};
+
use super::{
Machine, MachineConfig, QueryResult, QueryResolutionLine,
Atom, AtomCell, HeapCellValue, HeapCellValueTag,
streams::Stream
};
+use ref_thread_local::__Deref;
+fn print_term(term: &Term) {
+ match term {
+ Term::Clause(clause, atom, terms) => {
+ println!("clause: {:?}", clause);
+ println!("atom: {:?}", atom.as_str());
+ println!("terms: {:?}", terms);
+
+ for term in terms {
+ print_term(term);
+ }
+ },
+ Term::Cons(cons, term1, term2) => {
+ println!("constant: {:?}", cons);
+ println!("term1: {:?}", term1);
+ println!("term2: {:?}", term2);
+ },
+ Term::Literal(cell, literal) => {
+ println!("Cell: {:?}", cell);
+ println!("Literal: {:?}", literal);
+ },
+ Term::Var(var_reg, var_ptr) => {
+ println!("Var: {:?}, {:?}", var_reg.get(), var_ptr.deref());
+ },
+ Term::CompleteString(cell, atom) => {
+ println!("Cell: {:?}", cell);
+ println!("Atom: {:?}", atom.as_str());
+ },
+ _ => {
+ println!("Parsed query: {:?}", term);
+ }
+ }
+
+}
impl Machine {
pub fn new_lib() -> Self {
}
pub fn run_query(&mut self, query: String) -> QueryResult {
- let input = format!("{}", query);
+ //let input = format!("{}", query);
//println!("Running query: {}", input);
- self.set_user_input(input);
- self.run_top_level(atom!("$toplevel"), (atom!("run_input_once"), 0));
- self.parse_output()
+
+ // Create a new Stream from the user input
+ let stream = Stream::from_owned_string(query.clone(), &mut self.machine_st.arena);
+ // Read the term from the user input
+ let _result = self.machine_st.read_term_from_user_input(stream, &mut self.indices);
+
+
+ // Parse the query so we can analyze and then call the term
+ let mut parser = Parser::new(
+ Stream::from_owned_string(query, &mut self.machine_st.arena),
+ &mut self.machine_st
+ );
+ let op_dir = CompositeOpDir::new(&self.indices.op_dir, None);
+ let term = parser.read_term(&op_dir, Tokens::Default).expect("Failed to parse query");
+
+ // ... trying to understand what's going on here
+ print_term(&term);
+
+ // Extract the atom from the term which we need to find the code index
+ let term_atom = if let Term::Clause(_, atom, _) = term {
+ atom
+ } else {
+ panic!("Expected a clause");
+ };
+ println!("term_atom: {:?}", term_atom.as_str());
+ //println!("code_dir: {:?}", self.indices.code_dir);
+ let code_index = self.indices.code_dir.get(&(term_atom, term.arity()));
+ println!("code_index: {:?}", code_index);
+
+ // Ok, we have a code_index, so we can set the program counter to the code index:
+
+ self.machine_st.cp = BREAK_FROM_DISPATCH_LOOP_LOC;
+ self.machine_st.p = code_index.expect("couldn't get code index").local().unwrap();
+
+ println!("running dispatch loop");
+ self.dispatch_loop();
+ println!("done");
+
+ // If we don't set this register, we get an error in write_term.
+ // It seems to be the register that holds max_depth
+ self.machine_st.registers[7] = 50.into();
+
+ let op_dir = &self.indices.op_dir;
+ let printer = self.machine_st.write_term(op_dir)
+ .expect("Couldn't get printer from write_term")
+ .expect("Couldn't get printer from write_term");
+
+ println!("Varnames: {:?}", printer.var_names);
+ println!("Printer: {:?}", printer);
+
+ Err("not implementend".to_string())
}
pub fn parse_output(&self) -> QueryResult {