index inlined and builtin clausetypes inside a BTreeMap
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scryer-prolog"
|
name = "scryer-prolog"
|
||||||
version = "0.8.20"
|
version = "0.8.21"
|
||||||
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
authors = ["Mark Thom <markjordanthom@gmail.com>"]
|
||||||
repository = "https://github.com/mthom/scryer-prolog"
|
repository = "https://github.com/mthom/scryer-prolog"
|
||||||
description = "A modern Prolog implementation written mostly in Rust."
|
description = "A modern Prolog implementation written mostly in Rust."
|
||||||
@@ -14,8 +14,9 @@ cfg-if = "0.1.7"
|
|||||||
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 = "0.8.1"
|
prolog_parser = "0.8.2"
|
||||||
readline_rs_compat = { version = "0.1.7", optional = true }
|
readline_rs_compat = { version = "0.1.7", optional = true }
|
||||||
|
ref_thread_local = "0.0.0"
|
||||||
|
|
||||||
[dependencies.termion]
|
[dependencies.termion]
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#[macro_use] extern crate cfg_if;
|
#[macro_use] extern crate cfg_if;
|
||||||
#[macro_use] extern crate downcast;
|
#[macro_use] extern crate downcast;
|
||||||
#[macro_use] extern crate prolog_parser;
|
#[macro_use] extern crate prolog_parser;
|
||||||
|
#[macro_use] extern crate ref_thread_local;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(feature = "readline_rs_compat")] {
|
if #[cfg(feature = "readline_rs_compat")] {
|
||||||
|
|||||||
@@ -3,7 +3,11 @@ use prolog_parser::ast::*;
|
|||||||
use prolog::forms::OpDecl;
|
use prolog::forms::OpDecl;
|
||||||
use prolog::machine::machine_indices::*;
|
use prolog::machine::machine_indices::*;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
use ref_thread_local::RefThreadLocal;
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub enum CompareNumberQT {
|
pub enum CompareNumberQT {
|
||||||
GreaterThan,
|
GreaterThan,
|
||||||
LessThan,
|
LessThan,
|
||||||
@@ -26,7 +30,7 @@ impl CompareNumberQT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum CompareTermQT {
|
pub enum CompareTermQT {
|
||||||
LessThan,
|
LessThan,
|
||||||
LessThanOrEqual,
|
LessThanOrEqual,
|
||||||
@@ -49,7 +53,7 @@ impl CompareTermQT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum ArithmeticTerm {
|
pub enum ArithmeticTerm {
|
||||||
Reg(RegType),
|
Reg(RegType),
|
||||||
Interm(usize),
|
Interm(usize),
|
||||||
@@ -66,7 +70,7 @@ impl ArithmeticTerm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq)]
|
||||||
pub enum InlinedClauseType {
|
pub enum InlinedClauseType {
|
||||||
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
|
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
|
||||||
IsAtom(RegType),
|
IsAtom(RegType),
|
||||||
@@ -81,6 +85,58 @@ pub enum InlinedClauseType {
|
|||||||
IsVar(RegType)
|
IsVar(RegType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ref_thread_local! {
|
||||||
|
static managed CLAUSE_TYPE_FORMS: BTreeMap<(&'static str, usize), ClauseType> = {
|
||||||
|
let mut m = BTreeMap::new();
|
||||||
|
|
||||||
|
let r1 = temp_v!(1);
|
||||||
|
let r2 = temp_v!(2);
|
||||||
|
|
||||||
|
m.insert((">", 2),
|
||||||
|
ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::GreaterThan, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert(("<", 2),
|
||||||
|
ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::LessThan, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert((">=", 2), ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::GreaterThanOrEqual, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert(("=<", 2), ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::LessThanOrEqual, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert(("=:=", 2), ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::Equal, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert(("=\\=", 2), ClauseType::Inlined(InlinedClauseType::CompareNumber(CompareNumberQT::NotEqual, ar_reg!(r1), ar_reg!(r2))));
|
||||||
|
m.insert(("atom", 1), ClauseType::Inlined(InlinedClauseType::IsAtom(r1)));
|
||||||
|
m.insert(("atomic", 1), ClauseType::Inlined(InlinedClauseType::IsAtomic(r1)));
|
||||||
|
m.insert(("compound", 1), ClauseType::Inlined(InlinedClauseType::IsCompound(r1)));
|
||||||
|
m.insert(("integer", 1), ClauseType::Inlined(InlinedClauseType::IsInteger(r1)));
|
||||||
|
m.insert(("rational", 1), ClauseType::Inlined(InlinedClauseType::IsRational(r1)));
|
||||||
|
m.insert(("string", 1), ClauseType::Inlined(InlinedClauseType::IsString(r1)));
|
||||||
|
m.insert(("float", 1), ClauseType::Inlined(InlinedClauseType::IsFloat(r1)));
|
||||||
|
m.insert(("nonvar", 1), ClauseType::Inlined(InlinedClauseType::IsNonVar(r1)));
|
||||||
|
m.insert(("is_partial_string", 1), ClauseType::Inlined(InlinedClauseType::IsPartialString(r1)));
|
||||||
|
m.insert(("var", 1), ClauseType::Inlined(InlinedClauseType::IsVar(r1)));
|
||||||
|
m.insert(("acyclic_term", 1), ClauseType::BuiltIn(BuiltInClauseType::AcyclicTerm));
|
||||||
|
m.insert(("arg", 3), ClauseType::BuiltIn(BuiltInClauseType::Arg));
|
||||||
|
m.insert(("compare", 3), ClauseType::BuiltIn(BuiltInClauseType::Compare));
|
||||||
|
m.insert(("@>", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::GreaterThan)));
|
||||||
|
m.insert(("@<", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::LessThan)));
|
||||||
|
m.insert(("@>=", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::GreaterThanOrEqual)));
|
||||||
|
m.insert(("@=<", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::LessThanOrEqual)));
|
||||||
|
m.insert(("\\=@=", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::NotEqual)));
|
||||||
|
m.insert(("=@=", 2), ClauseType::BuiltIn(BuiltInClauseType::CompareTerm(CompareTermQT::Equal)));
|
||||||
|
m.insert(("copy_term", 2), ClauseType::BuiltIn(BuiltInClauseType::CopyTerm));
|
||||||
|
m.insert(("cyclic_term", 1), ClauseType::BuiltIn(BuiltInClauseType::CyclicTerm));
|
||||||
|
m.insert(("==", 2), ClauseType::BuiltIn(BuiltInClauseType::Eq));
|
||||||
|
m.insert(("functor", 3), ClauseType::BuiltIn(BuiltInClauseType::Functor));
|
||||||
|
m.insert(("ground", 1), ClauseType::BuiltIn(BuiltInClauseType::Ground));
|
||||||
|
m.insert(("is", 2), ClauseType::BuiltIn(BuiltInClauseType::Is(r1, ar_reg!(r2))));
|
||||||
|
m.insert(("keysort", 2), ClauseType::BuiltIn(BuiltInClauseType::KeySort));
|
||||||
|
m.insert(("nl", 0), ClauseType::BuiltIn(BuiltInClauseType::Nl));
|
||||||
|
m.insert(("\\==", 2), ClauseType::BuiltIn(BuiltInClauseType::NotEq));
|
||||||
|
m.insert(("partial_string", 2), ClauseType::BuiltIn(BuiltInClauseType::PartialString));
|
||||||
|
m.insert(("read", 1), ClauseType::BuiltIn(BuiltInClauseType::Read));
|
||||||
|
m.insert(("$reify_switch", 3), ClauseType::BuiltIn(BuiltInClauseType::ReifySwitch));
|
||||||
|
m.insert(("sort", 2), ClauseType::BuiltIn(BuiltInClauseType::Sort));
|
||||||
|
|
||||||
|
m
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl InlinedClauseType {
|
impl InlinedClauseType {
|
||||||
pub fn name(&self) -> &'static str {
|
pub fn name(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
@@ -97,43 +153,9 @@ impl InlinedClauseType {
|
|||||||
&InlinedClauseType::IsVar(..) => "var",
|
&InlinedClauseType::IsVar(..) => "var",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from(name: &str, arity: usize) -> Option<Self> {
|
|
||||||
let r1 = temp_v!(1);
|
|
||||||
let r2 = temp_v!(2);
|
|
||||||
|
|
||||||
let a1 = ArithmeticTerm::Reg(r1);
|
|
||||||
let a2 = ArithmeticTerm::Reg(r2);
|
|
||||||
|
|
||||||
match (name, arity) {
|
|
||||||
(">", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::GreaterThan, a1, a2)),
|
|
||||||
("<", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::LessThan, a1, a2)),
|
|
||||||
(">=", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::GreaterThanOrEqual,a1, a2)),
|
|
||||||
("=<", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::LessThanOrEqual, a1, a2)),
|
|
||||||
("=\\=", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::NotEqual, a1, a2)),
|
|
||||||
("=:=", 2) =>
|
|
||||||
Some(InlinedClauseType::CompareNumber(CompareNumberQT::Equal, a1, a2)),
|
|
||||||
("atom", 1) => Some(InlinedClauseType::IsAtom(r1)),
|
|
||||||
("atomic", 1) => Some(InlinedClauseType::IsAtomic(r1)),
|
|
||||||
("compound", 1) => Some(InlinedClauseType::IsCompound(r1)),
|
|
||||||
("integer", 1) => Some(InlinedClauseType::IsInteger(r1)),
|
|
||||||
("rational", 1) => Some(InlinedClauseType::IsRational(r1)),
|
|
||||||
("string", 1) => Some(InlinedClauseType::IsString(r1)),
|
|
||||||
("float", 1) => Some(InlinedClauseType::IsFloat(r1)),
|
|
||||||
("nonvar", 1) => Some(InlinedClauseType::IsNonVar(r1)),
|
|
||||||
("var", 1) => Some(InlinedClauseType::IsVar(r1)),
|
|
||||||
("is_partial_string", 1) => Some(InlinedClauseType::IsPartialString(r1)),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub enum SystemClauseType {
|
pub enum SystemClauseType {
|
||||||
AbolishClause,
|
AbolishClause,
|
||||||
AbolishModuleClause,
|
AbolishModuleClause,
|
||||||
@@ -274,7 +296,7 @@ impl SystemClauseType {
|
|||||||
&SystemClauseType::WriteTerm => clause_name!("$write_term"),
|
&SystemClauseType::WriteTerm => clause_name!("$write_term"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from(name: &str, arity: usize) -> Option<SystemClauseType> {
|
pub fn from(name: &str, arity: usize) -> Option<SystemClauseType> {
|
||||||
match (name, arity) {
|
match (name, arity) {
|
||||||
("$abolish_clause", 2) => Some(SystemClauseType::AbolishClause),
|
("$abolish_clause", 2) => Some(SystemClauseType::AbolishClause),
|
||||||
@@ -347,7 +369,7 @@ impl SystemClauseType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub enum BuiltInClauseType {
|
pub enum BuiltInClauseType {
|
||||||
AcyclicTerm,
|
AcyclicTerm,
|
||||||
Arg,
|
Arg,
|
||||||
@@ -368,7 +390,7 @@ pub enum BuiltInClauseType {
|
|||||||
Sort,
|
Sort,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd)]
|
||||||
pub enum ClauseType {
|
pub enum ClauseType {
|
||||||
BuiltIn(BuiltInClauseType),
|
BuiltIn(BuiltInClauseType),
|
||||||
CallN,
|
CallN,
|
||||||
@@ -423,34 +445,6 @@ impl BuiltInClauseType {
|
|||||||
&BuiltInClauseType::Sort => 2,
|
&BuiltInClauseType::Sort => 2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from(name: &str, arity: usize) -> Option<Self> {
|
|
||||||
match (name, arity) {
|
|
||||||
("acyclic_term", 1) => Some(BuiltInClauseType::AcyclicTerm),
|
|
||||||
("arg", 3) => Some(BuiltInClauseType::Arg),
|
|
||||||
("compare", 3) => Some(BuiltInClauseType::Compare),
|
|
||||||
("cyclic_term", 1) => Some(BuiltInClauseType::CyclicTerm),
|
|
||||||
("@>", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::GreaterThan)),
|
|
||||||
("@<", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::LessThan)),
|
|
||||||
("@>=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::GreaterThanOrEqual)),
|
|
||||||
("@=<", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::LessThanOrEqual)),
|
|
||||||
("\\=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::NotEqual)),
|
|
||||||
("=@=", 2) => Some(BuiltInClauseType::CompareTerm(CompareTermQT::Equal)),
|
|
||||||
("copy_term", 2) => Some(BuiltInClauseType::CopyTerm),
|
|
||||||
("==", 2) => Some(BuiltInClauseType::Eq),
|
|
||||||
("functor", 3) => Some(BuiltInClauseType::Functor),
|
|
||||||
("ground", 1) => Some(BuiltInClauseType::Ground),
|
|
||||||
("is", 2) => Some(BuiltInClauseType::Is(temp_v!(1), ArithmeticTerm::Reg(temp_v!(2)))),
|
|
||||||
("keysort", 2) => Some(BuiltInClauseType::KeySort),
|
|
||||||
("nl", 0) => Some(BuiltInClauseType::Nl),
|
|
||||||
("\\==", 2) => Some(BuiltInClauseType::NotEq),
|
|
||||||
("partial_string", 2) => Some(BuiltInClauseType::PartialString),
|
|
||||||
("read", 1) => Some(BuiltInClauseType::Read),
|
|
||||||
("$reify_switch", 3) => Some(BuiltInClauseType::ReifySwitch),
|
|
||||||
("sort", 2) => Some(BuiltInClauseType::Sort),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClauseType {
|
impl ClauseType {
|
||||||
@@ -481,26 +475,19 @@ impl ClauseType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from(name: ClauseName, arity: usize, spec: Option<(usize, Specifier)>) -> Self {
|
pub fn from(name: ClauseName, arity: usize, spec: Option<(usize, Specifier)>) -> Self {
|
||||||
InlinedClauseType::from(name.as_str(), arity)
|
CLAUSE_TYPE_FORMS.borrow().get(&(name.as_str(), arity)).cloned()
|
||||||
.map(ClauseType::Inlined)
|
.unwrap_or_else(||
|
||||||
.unwrap_or_else(|| {
|
SystemClauseType::from(name.as_str(), arity)
|
||||||
BuiltInClauseType::from(name.as_str(), arity)
|
.map(ClauseType::System)
|
||||||
.map(ClauseType::BuiltIn)
|
.unwrap_or_else(||
|
||||||
.unwrap_or_else(|| {
|
if let Some(spec) = spec {
|
||||||
SystemClauseType::from(name.as_str(), arity)
|
let op_decl = OpDecl(spec.0, spec.1, name);
|
||||||
.map(ClauseType::System)
|
ClauseType::Op(op_decl, CodeIndex::default())
|
||||||
.unwrap_or_else(|| {
|
} else if name.as_str() == "call" {
|
||||||
if let Some(spec) = spec {
|
ClauseType::CallN
|
||||||
let op_decl = OpDecl(spec.0, spec.1, name);
|
} else {
|
||||||
ClauseType::Op(op_decl, CodeIndex::default())
|
ClauseType::Named(name, arity, CodeIndex::default())
|
||||||
} else if name.as_str() == "call" {
|
}))
|
||||||
ClauseType::CallN
|
|
||||||
} else {
|
|
||||||
ClauseType::Named(name, arity, CodeIndex::default())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ impl Declaration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct OpDecl(pub usize, pub Specifier, pub ClauseName);
|
pub struct OpDecl(pub usize, pub Specifier, pub ClauseName);
|
||||||
|
|
||||||
impl OpDecl {
|
impl OpDecl {
|
||||||
|
|||||||
@@ -168,13 +168,13 @@ impl HeapCellValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub enum IndexPtr {
|
pub enum IndexPtr {
|
||||||
Undefined,
|
Undefined,
|
||||||
Index(usize),
|
Index(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
pub struct CodeIndex(pub Rc<RefCell<(IndexPtr, ClauseName)>>);
|
pub struct CodeIndex(pub Rc<RefCell<(IndexPtr, ClauseName)>>);
|
||||||
|
|
||||||
impl CodeIndex {
|
impl CodeIndex {
|
||||||
@@ -505,7 +505,7 @@ impl IndexStore {
|
|||||||
pub type CodeDir = HashMap<PredicateKey, CodeIndex>;
|
pub type CodeDir = HashMap<PredicateKey, CodeIndex>;
|
||||||
pub type TermDir = HashMap<PredicateKey, (Predicate, VecDeque<TopLevel>)>;
|
pub type TermDir = HashMap<PredicateKey, (Predicate, VecDeque<TopLevel>)>;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
||||||
pub enum CompileTimeHook {
|
pub enum CompileTimeHook {
|
||||||
GoalExpansion,
|
GoalExpansion,
|
||||||
TermExpansion,
|
TermExpansion,
|
||||||
|
|||||||
@@ -261,3 +261,9 @@ macro_rules! discard_result {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! ar_reg {
|
||||||
|
($r: expr) => (
|
||||||
|
ArithmeticTerm::Reg($r)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ extern crate ordered_float;
|
|||||||
extern crate prolog_parser;
|
extern crate prolog_parser;
|
||||||
|
|
||||||
pub mod instructions;
|
pub mod instructions;
|
||||||
mod clause_types;
|
|
||||||
#[macro_use] mod macros;
|
#[macro_use] mod macros;
|
||||||
|
mod clause_types;
|
||||||
#[macro_use] mod allocator;
|
#[macro_use] mod allocator;
|
||||||
mod fixtures;
|
mod fixtures;
|
||||||
pub mod machine;
|
pub mod machine;
|
||||||
|
|||||||
Reference in New Issue
Block a user