add term_expansion basics
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -86,7 +86,8 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "prolog_parser"
|
name = "prolog_parser"
|
||||||
version = "0.7.14"
|
version = "0.7.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@@ -107,12 +108,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rusty-wam"
|
name = "rusty-wam"
|
||||||
version = "0.7.13"
|
version = "0.7.14"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"downcast 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"downcast 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"prolog_parser 0.7.14",
|
"prolog_parser 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -151,6 +152,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070"
|
"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070"
|
||||||
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
|
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
|
||||||
"checksum ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "58d25b6c0e47b20d05226d288ff434940296e7e2f8b877975da32f862152241f"
|
"checksum ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "58d25b6c0e47b20d05226d288ff434940296e7e2f8b877975da32f862152241f"
|
||||||
|
"checksum prolog_parser 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)" = "11f378539616d1cd7b8fc006f309b5a4b483b82cbab76e898a9f1c99318b4dfa"
|
||||||
"checksum redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "ab105df655884ede59d45b7070c8a65002d921461ee813a024558ca16030eea0"
|
"checksum redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "ab105df655884ede59d45b7070c8a65002d921461ee813a024558ca16030eea0"
|
||||||
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
|
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
|
||||||
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
|
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rusty-wam"
|
name = "rusty-wam"
|
||||||
version = "0.7.13"
|
version = "0.7.14"
|
||||||
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
||||||
repository = "https://github.com/mthom/rusty-wam"
|
repository = "https://github.com/mthom/rusty-wam"
|
||||||
description = "The Warren Abstract Machine in Rust."
|
description = "The Warren Abstract Machine in Rust."
|
||||||
@@ -10,7 +10,7 @@ license = "BSD-3-Clause"
|
|||||||
downcast = "0.9.1"
|
downcast = "0.9.1"
|
||||||
num = "0.2"
|
num = "0.2"
|
||||||
ordered-float = "0.5.0"
|
ordered-float = "0.5.0"
|
||||||
prolog_parser = { path = "../prolog_parser", version = "0.7.14" }
|
prolog_parser = "0.7.15"
|
||||||
|
|
||||||
[dependencies.termion]
|
[dependencies.termion]
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@@ -94,7 +94,9 @@ impl Machine {
|
|||||||
self.reset();
|
self.reset();
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else {
|
} else {
|
||||||
Ok(Some(read_term_from_heap(&self.ms, Addr::HeapCell(h))?))
|
let term = read_term_from_heap(&self.ms, Addr::HeapCell(h))?;
|
||||||
|
self.reset();
|
||||||
|
Ok(Some(term))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use prolog_parser::ast::*;
|
use prolog_parser::ast::*;
|
||||||
use prolog_parser::parser::*;
|
|
||||||
use prolog_parser::tabled_rc::*;
|
use prolog_parser::tabled_rc::*;
|
||||||
|
|
||||||
use prolog::instructions::*;
|
use prolog::instructions::*;
|
||||||
@@ -623,12 +622,12 @@ impl RelationWorker {
|
|||||||
-> Result<(CompileTimeHook, PredicateClause), ParserError>
|
-> Result<(CompileTimeHook, PredicateClause), ParserError>
|
||||||
{
|
{
|
||||||
match term {
|
match term {
|
||||||
Term::Clause(r, name, terms, _) =>
|
Term::Clause(r, name, terms, _) =>
|
||||||
if name.as_str() == "term_expansion" && terms.len() == 2 {
|
if name.as_str() == "term_expansion" && terms.len() == 2 {
|
||||||
let term = Term::Clause(r, name, terms, None);
|
let term = Term::Clause(r, name, terms, None);
|
||||||
Ok((CompileTimeHook::TermExpansion, PredicateClause::Fact(term)))
|
Ok((CompileTimeHook::TermExpansion, PredicateClause::Fact(term)))
|
||||||
} else if name.as_str() == ":-" {
|
} else if name.as_str() == ":-" {
|
||||||
let rule = self.setup_rule(indices, terms, false)?;
|
let rule = self.setup_rule(indices, terms, false)?;
|
||||||
Ok((CompileTimeHook::TermExpansion, PredicateClause::Rule(rule)))
|
Ok((CompileTimeHook::TermExpansion, PredicateClause::Rule(rule)))
|
||||||
} else {
|
} else {
|
||||||
Err(ParserError::InvalidHook)
|
Err(ParserError::InvalidHook)
|
||||||
@@ -636,7 +635,7 @@ impl RelationWorker {
|
|||||||
_ => Err(ParserError::InvalidHook)
|
_ => Err(ParserError::InvalidHook)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_rule(&mut self, indices: &mut CompositeIndices, mut terms: Vec<Box<Term>>, blocks_cuts: bool)
|
fn setup_rule(&mut self, indices: &mut CompositeIndices, mut terms: Vec<Box<Term>>, blocks_cuts: bool)
|
||||||
-> Result<Rule, ParserError>
|
-> Result<Rule, ParserError>
|
||||||
{
|
{
|
||||||
@@ -662,7 +661,7 @@ impl RelationWorker {
|
|||||||
if is_term_expansion(&name, &terms) {
|
if is_term_expansion(&name, &terms) {
|
||||||
let term = Term::Clause(r, name, terms, fixity);
|
let term = Term::Clause(r, name, terms, fixity);
|
||||||
let (hook, clauses) = self.setup_hook(indices, term)?;
|
let (hook, clauses) = self.setup_hook(indices, term)?;
|
||||||
|
|
||||||
Ok(TopLevel::Declaration(Declaration::Hook(hook, clauses)))
|
Ok(TopLevel::Declaration(Declaration::Hook(hook, clauses)))
|
||||||
} else if name.as_str() == "?-" {
|
} else if name.as_str() == "?-" {
|
||||||
Ok(TopLevel::Query(try!(self.setup_query(indices, terms, blocks_cuts))))
|
Ok(TopLevel::Query(try!(self.setup_query(indices, terms, blocks_cuts))))
|
||||||
@@ -672,9 +671,9 @@ impl RelationWorker {
|
|||||||
let term = *terms.pop().unwrap();
|
let term = *terms.pop().unwrap();
|
||||||
Ok(TopLevel::Declaration(try!(setup_declaration(term))))
|
Ok(TopLevel::Declaration(try!(setup_declaration(term))))
|
||||||
} else {
|
} else {
|
||||||
let term = Term::Clause(r, name, terms, fixity);
|
let term = Term::Clause(r, name, terms, fixity);
|
||||||
Ok(TopLevel::Fact(try!(setup_fact(term))))
|
Ok(TopLevel::Fact(try!(setup_fact(term))))
|
||||||
},
|
},
|
||||||
term => Ok(TopLevel::Fact(try!(setup_fact(term))))
|
term => Ok(TopLevel::Fact(try!(setup_fact(term))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -699,7 +698,7 @@ impl RelationWorker {
|
|||||||
while let Some(terms) = self.queue.pop_front() {
|
while let Some(terms) = self.queue.pop_front() {
|
||||||
let clauses = merge_clauses(&mut self.try_terms_to_tls(indices, terms, false)?)?;
|
let clauses = merge_clauses(&mut self.try_terms_to_tls(indices, terms, false)?)?;
|
||||||
queue.push_back(clauses);
|
queue.push_back(clauses);
|
||||||
|
|
||||||
}
|
}
|
||||||
Ok(queue)
|
Ok(queue)
|
||||||
}
|
}
|
||||||
@@ -709,9 +708,12 @@ impl RelationWorker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// used to parse queries in test. mostly.
|
// used to parse queries in test.
|
||||||
|
#[cfg(test)]
|
||||||
pub fn parse_term<R: Read>(wam: &Machine, buf: R) -> Result<Term, ParserError>
|
pub fn parse_term<R: Read>(wam: &Machine, buf: R) -> Result<Term, ParserError>
|
||||||
{
|
{
|
||||||
|
use prolog_parser::parser::*;
|
||||||
|
|
||||||
let mut parser = Parser::new(buf, wam.atom_tbl(), wam.machine_flags());
|
let mut parser = Parser::new(buf, wam.atom_tbl(), wam.machine_flags());
|
||||||
parser.read_term(composite_op!(&wam.op_dir))
|
parser.read_term(composite_op!(&wam.op_dir))
|
||||||
}
|
}
|
||||||
@@ -723,7 +725,7 @@ fn consume_term<'a>(static_code_dir: Rc<RefCell<CodeDir>>, term: Term,
|
|||||||
{
|
{
|
||||||
let mut rel_worker = RelationWorker::new();
|
let mut rel_worker = RelationWorker::new();
|
||||||
let mut indices = composite_indices!(false, &mut indices, static_code_dir);
|
let mut indices = composite_indices!(false, &mut indices, static_code_dir);
|
||||||
|
|
||||||
let tl = rel_worker.try_term_to_tl(&mut indices, term, true)?;
|
let tl = rel_worker.try_term_to_tl(&mut indices, term, true)?;
|
||||||
let results = rel_worker.parse_queue(&mut indices)?;
|
let results = rel_worker.parse_queue(&mut indices)?;
|
||||||
|
|
||||||
@@ -762,8 +764,8 @@ impl<R: Read> TopLevelBatchWorker<R> {
|
|||||||
self.term_stream.empty_tokens(); // empty the parser stack of token descriptions.
|
self.term_stream.empty_tokens(); // empty the parser stack of token descriptions.
|
||||||
|
|
||||||
let mut new_rel_worker = RelationWorker::new();
|
let mut new_rel_worker = RelationWorker::new();
|
||||||
let term = self.term_stream.read_term(wam, &indices.local.op_dir)?;
|
let term = self.term_stream.read_term(wam, &indices.local.op_dir)?;
|
||||||
|
|
||||||
let tl = new_rel_worker.try_term_to_tl(&mut indices, term, true)?;
|
let tl = new_rel_worker.try_term_to_tl(&mut indices, term, true)?;
|
||||||
|
|
||||||
if !is_consistent(&tl, &preds) { // if is_consistent returns false, preds is non-empty.
|
if !is_consistent(&tl, &preds) { // if is_consistent returns false, preds is non-empty.
|
||||||
@@ -777,7 +779,7 @@ impl<R: Read> TopLevelBatchWorker<R> {
|
|||||||
TopLevel::Fact(fact) => preds.push(PredicateClause::Fact(fact)),
|
TopLevel::Fact(fact) => preds.push(PredicateClause::Fact(fact)),
|
||||||
TopLevel::Rule(rule) => preds.push(PredicateClause::Rule(rule)),
|
TopLevel::Rule(rule) => preds.push(PredicateClause::Rule(rule)),
|
||||||
TopLevel::Predicate(pred) => preds.extend(pred.0),
|
TopLevel::Predicate(pred) => preds.extend(pred.0),
|
||||||
TopLevel::Declaration(decl) => return Ok(Some(decl)),
|
TopLevel::Declaration(decl) => return Ok(Some(decl)),
|
||||||
TopLevel::Query(_) => return Err(SessionError::NamelessEntry)
|
TopLevel::Query(_) => return Err(SessionError::NamelessEntry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user