Format code using 'cargo fmt'

This commit is contained in:
Atul Bhosale
2019-09-23 19:35:37 +07:00
parent 8207fdea40
commit 1273e2d52d
36 changed files with 8738 additions and 6375 deletions

View File

@@ -22,7 +22,13 @@ pub type OssifiedOpDir = BTreeMap<OrderedOpDirKey, (usize, Specifier)>;
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum DBRef {
NamedPred(ClauseName, usize, Option<SharedOpDesc>),
Op(usize, Specifier, ClauseName, Rc<OssifiedOpDir>, SharedOpDesc)
Op(
usize,
Specifier,
ClauseName,
Rc<OssifiedOpDir>,
SharedOpDesc,
),
}
#[derive(Clone, PartialEq, Eq, Hash)]
@@ -33,22 +39,22 @@ pub enum Addr {
Lis(usize),
HeapCell(usize),
StackCell(usize, usize),
Str(usize)
Str(usize),
}
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
pub enum Ref {
AttrVar(usize),
HeapCell(usize),
StackCell(usize, usize)
StackCell(usize, usize),
}
impl Ref {
pub fn as_addr(self) -> Addr {
match self {
Ref::AttrVar(h) => Addr::AttrVar(h),
Ref::HeapCell(h) => Addr::HeapCell(h),
Ref::StackCell(fr, sc) => Addr::StackCell(fr, sc)
Ref::AttrVar(h) => Addr::AttrVar(h),
Ref::HeapCell(h) => Addr::HeapCell(h),
Ref::StackCell(fr, sc) => Addr::StackCell(fr, sc),
}
}
}
@@ -63,25 +69,23 @@ impl PartialEq<Ref> for Addr {
impl PartialOrd<Ref> for Addr {
fn partial_cmp(&self, r: &Ref) -> Option<Ordering> {
match self {
&Addr::StackCell(fr, sc) =>
match *r {
Ref::AttrVar(_) | Ref::HeapCell(_) =>
Some(Ordering::Greater),
Ref::StackCell(fr1, sc1) =>
if fr1 < fr || (fr1 == fr && sc1 < sc) {
Some(Ordering::Greater)
} else if fr1 == fr && sc1 == sc {
Some(Ordering::Equal)
} else {
Some(Ordering::Less)
}
},
&Addr::HeapCell(h) | &Addr::AttrVar(h) =>
match r {
&Ref::StackCell(..) => Some(Ordering::Less),
&Ref::AttrVar(h1) | &Ref::HeapCell(h1) => h.partial_cmp(&h1)
},
_ => None
&Addr::StackCell(fr, sc) => match *r {
Ref::AttrVar(_) | Ref::HeapCell(_) => Some(Ordering::Greater),
Ref::StackCell(fr1, sc1) => {
if fr1 < fr || (fr1 == fr && sc1 < sc) {
Some(Ordering::Greater)
} else if fr1 == fr && sc1 == sc {
Some(Ordering::Equal)
} else {
Some(Ordering::Less)
}
}
},
&Addr::HeapCell(h) | &Addr::AttrVar(h) => match r {
&Ref::StackCell(..) => Some(Ordering::Less),
&Ref::AttrVar(h1) | &Ref::HeapCell(h1) => h.partial_cmp(&h1),
},
_ => None,
}
}
}
@@ -90,7 +94,7 @@ impl Addr {
pub fn is_ref(&self) -> bool {
match self {
&Addr::AttrVar(_) | &Addr::HeapCell(_) | &Addr::StackCell(_, _) => true,
_ => false
_ => false,
}
}
@@ -99,14 +103,14 @@ impl Addr {
&Addr::AttrVar(h) => Some(Ref::AttrVar(h)),
&Addr::HeapCell(h) => Some(Ref::HeapCell(h)),
&Addr::StackCell(fr, sc) => Some(Ref::StackCell(fr, sc)),
_ => None
_ => None,
}
}
pub fn is_protected(&self, e: usize) -> bool {
match self {
&Addr::StackCell(addr, _) if addr >= e => false,
_ => true
_ => true,
}
}
}
@@ -120,7 +124,7 @@ impl Add<usize> for Addr {
Addr::AttrVar(h) => Addr::AttrVar(h + rhs),
Addr::HeapCell(h) => Addr::HeapCell(h + rhs),
Addr::Str(s) => Addr::Str(s + rhs),
_ => self
_ => self,
}
}
}
@@ -135,7 +139,7 @@ impl Sub<i64> for Addr {
Addr::AttrVar(h) => Addr::AttrVar(h + rhs.abs() as usize),
Addr::HeapCell(h) => Addr::HeapCell(h + rhs.abs() as usize),
Addr::Str(s) => Addr::Str(s + rhs.abs() as usize),
_ => self
_ => self,
}
} else {
self.sub(rhs as usize)
@@ -152,7 +156,7 @@ impl Sub<usize> for Addr {
Addr::AttrVar(h) => Addr::AttrVar(h - rhs),
Addr::HeapCell(h) => Addr::HeapCell(h - rhs),
Addr::Str(s) => Addr::Str(s - rhs),
_ => self
_ => self,
}
}
}
@@ -166,9 +170,9 @@ impl SubAssign<usize> for Addr {
impl From<Ref> for Addr {
fn from(r: Ref) -> Self {
match r {
Ref::AttrVar(h) => Addr::AttrVar(h),
Ref::HeapCell(h) => Addr::HeapCell(h),
Ref::StackCell(fr, sc) => Addr::StackCell(fr, sc)
Ref::AttrVar(h) => Addr::AttrVar(h),
Ref::HeapCell(h) => Addr::HeapCell(h),
Ref::StackCell(fr, sc) => Addr::StackCell(fr, sc),
}
}
}
@@ -176,7 +180,7 @@ impl From<Ref> for Addr {
#[derive(Clone)]
pub enum TrailRef {
Ref(Ref),
AttrVarLink(usize, Addr)
AttrVarLink(usize, Addr),
}
impl From<Ref> for TrailRef {
@@ -195,7 +199,7 @@ impl HeapCellValue {
pub fn as_addr(&self, focus: usize) -> Addr {
match self {
&HeapCellValue::Addr(ref a) => a.clone(),
&HeapCellValue::NamedStr(_, _, _) => Addr::Str(focus)
&HeapCellValue::NamedStr(_, _, _) => Addr::Str(focus),
}
}
}
@@ -229,14 +233,17 @@ impl CodeIndex {
pub fn local(&self) -> Option<usize> {
match self.0.borrow().0 {
IndexPtr::Index(i) => Some(i),
_ => None
_ => None,
}
}
}
impl Default for CodeIndex {
fn default() -> Self {
CodeIndex(Rc::new(RefCell::new((IndexPtr::Undefined, clause_name!("")))))
CodeIndex(Rc::new(RefCell::new((
IndexPtr::Undefined,
clause_name!(""),
))))
}
}
@@ -248,23 +255,24 @@ impl From<(usize, ClauseName)> for CodeIndex {
#[derive(Clone, Copy, PartialEq)]
pub enum DynamicAssertPlace {
Back, Front
Back,
Front,
}
impl DynamicAssertPlace {
#[inline]
pub fn predicate_name(self) -> ClauseName {
match self {
DynamicAssertPlace::Back => clause_name!("assertz"),
DynamicAssertPlace::Front => clause_name!("asserta")
DynamicAssertPlace::Back => clause_name!("assertz"),
DynamicAssertPlace::Front => clause_name!("asserta"),
}
}
#[inline]
pub fn push_to_queue(self, addrs: &mut VecDeque<Addr>, new_addr: Addr) {
match self {
DynamicAssertPlace::Back => addrs.push_back(new_addr),
DynamicAssertPlace::Front => addrs.push_front(new_addr)
DynamicAssertPlace::Back => addrs.push_back(new_addr),
DynamicAssertPlace::Front => addrs.push_front(new_addr),
}
}
}
@@ -276,34 +284,33 @@ pub enum DynamicTransactionType {
ModuleAbolish,
ModuleAssert(DynamicAssertPlace),
ModuleRetract,
Retract // dynamic index of the clause to remove.
Retract, // dynamic index of the clause to remove.
}
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
pub enum REPLCodePtr {
CompileBatch,
SubmitQueryAndPrintResults
SubmitQueryAndPrintResults,
}
#[derive(Clone, PartialEq)]
pub enum CodePtr {
BuiltInClause(BuiltInClauseType, LocalCodePtr), // local is the successor call.
CallN(usize, LocalCodePtr), // arity, local.
CallN(usize, LocalCodePtr), // arity, local.
Local(LocalCodePtr),
DynamicTransaction(DynamicTransactionType, LocalCodePtr), // the type of transaction, the return pointer.
REPL(REPLCodePtr, LocalCodePtr), // the REPL code, the return pointer.
VerifyAttrInterrupt(usize) // location of the verify attribute interrupt code in the CodeDir.
REPL(REPLCodePtr, LocalCodePtr), // the REPL code, the return pointer.
VerifyAttrInterrupt(usize), // location of the verify attribute interrupt code in the CodeDir.
}
impl CodePtr {
pub fn local(&self) -> LocalCodePtr {
match self {
&CodePtr::BuiltInClause(_, ref local)
| &CodePtr::CallN(_, ref local)
| &CodePtr::Local(ref local) => local.clone(),
| &CodePtr::CallN(_, ref local)
| &CodePtr::Local(ref local) => local.clone(),
&CodePtr::VerifyAttrInterrupt(p) => LocalCodePtr::DirEntry(p),
&CodePtr::REPL(_, p)
| &CodePtr::DynamicTransaction(_, p) => p
&CodePtr::REPL(_, p) | &CodePtr::DynamicTransaction(_, p) => p,
}
}
}
@@ -314,7 +321,7 @@ pub enum LocalCodePtr {
InSituDirEntry(usize),
TopLevel(usize, usize), // chunk_num, offset.
UserGoalExpansion(usize),
UserTermExpansion(usize)
UserTermExpansion(usize),
}
impl LocalCodePtr {
@@ -330,7 +337,7 @@ impl PartialOrd<CodePtr> for CodePtr {
fn partial_cmp(&self, other: &CodePtr) -> Option<Ordering> {
match (self, other) {
(&CodePtr::Local(ref l1), &CodePtr::Local(ref l2)) => l1.partial_cmp(l2),
_ => Some(Ordering::Greater)
_ => Some(Ordering::Greater),
}
}
}
@@ -339,14 +346,14 @@ impl PartialOrd<LocalCodePtr> for LocalCodePtr {
fn partial_cmp(&self, other: &LocalCodePtr) -> Option<Ordering> {
match (self, other) {
(&LocalCodePtr::InSituDirEntry(p1), &LocalCodePtr::InSituDirEntry(ref p2))
| (&LocalCodePtr::DirEntry(p1), &LocalCodePtr::DirEntry(ref p2))
| (&LocalCodePtr::UserTermExpansion(p1), &LocalCodePtr::UserTermExpansion(ref p2))
| (&LocalCodePtr::UserGoalExpansion(p1), &LocalCodePtr::UserGoalExpansion(ref p2))
| (&LocalCodePtr::TopLevel(_, p1), &LocalCodePtr::TopLevel(_, ref p2)) =>
p1.partial_cmp(p2),
(_, &LocalCodePtr::TopLevel(_, _)) =>
Some(Ordering::Less),
_ => Some(Ordering::Greater)
| (&LocalCodePtr::DirEntry(p1), &LocalCodePtr::DirEntry(ref p2))
| (&LocalCodePtr::UserTermExpansion(p1), &LocalCodePtr::UserTermExpansion(ref p2))
| (&LocalCodePtr::UserGoalExpansion(p1), &LocalCodePtr::UserGoalExpansion(ref p2))
| (&LocalCodePtr::TopLevel(_, p1), &LocalCodePtr::TopLevel(_, ref p2)) => {
p1.partial_cmp(p2)
}
(_, &LocalCodePtr::TopLevel(_, _)) => Some(Ordering::Less),
_ => Some(Ordering::Greater),
}
}
}
@@ -381,10 +388,10 @@ impl AddAssign<usize> for LocalCodePtr {
fn add_assign(&mut self, rhs: usize) {
match self {
&mut LocalCodePtr::InSituDirEntry(ref mut p)
| &mut LocalCodePtr::UserGoalExpansion(ref mut p)
| &mut LocalCodePtr::UserTermExpansion(ref mut p)
| &mut LocalCodePtr::DirEntry(ref mut p)
| &mut LocalCodePtr::TopLevel(_, ref mut p) => *p += rhs
| &mut LocalCodePtr::UserGoalExpansion(ref mut p)
| &mut LocalCodePtr::UserTermExpansion(ref mut p)
| &mut LocalCodePtr::DirEntry(ref mut p)
| &mut LocalCodePtr::TopLevel(_, ref mut p) => *p += rhs,
}
}
}
@@ -395,10 +402,12 @@ impl Add<usize> for CodePtr {
fn add(self, rhs: usize) -> Self::Output {
match self {
p @ CodePtr::REPL(..)
| p @ CodePtr::VerifyAttrInterrupt(_)
| p @ CodePtr::DynamicTransaction(..) => p,
| p @ CodePtr::VerifyAttrInterrupt(_)
| p @ CodePtr::DynamicTransaction(..) => p,
CodePtr::Local(local) => CodePtr::Local(local + rhs),
CodePtr::CallN(_, local) | CodePtr::BuiltInClause(_, local) => CodePtr::Local(local + rhs)
CodePtr::CallN(_, local) | CodePtr::BuiltInClause(_, local) => {
CodePtr::Local(local + rhs)
}
}
}
}
@@ -406,14 +415,14 @@ impl Add<usize> for CodePtr {
impl AddAssign<usize> for CodePtr {
fn add_assign(&mut self, rhs: usize) {
match self {
&mut CodePtr::VerifyAttrInterrupt(_) => {},
&mut CodePtr::VerifyAttrInterrupt(_) => {}
&mut CodePtr::Local(ref mut local) => *local += rhs,
_ => *self = CodePtr::Local(self.local() + rhs)
_ => *self = CodePtr::Local(self.local() + rhs),
}
}
}
pub type HeapVarDict = IndexMap<Rc<Var>, Addr>;
pub type HeapVarDict = IndexMap<Rc<Var>, Addr>;
pub type AllocVarDict = IndexMap<Rc<Var>, VarData>;
#[derive(Clone)]
@@ -423,11 +432,13 @@ pub struct DynamicPredicateInfo {
impl Default for DynamicPredicateInfo {
fn default() -> Self {
DynamicPredicateInfo { clauses_subsection_p: 0 }
DynamicPredicateInfo {
clauses_subsection_p: 0,
}
}
}
pub type InSituCodeDir = IndexMap<PredicateKey, usize>;
pub type InSituCodeDir = IndexMap<PredicateKey, usize>;
// key type: module name, predicate indicator.
pub type DynamicCodeDir = IndexMap<(ClauseName, ClauseName, usize), DynamicPredicateInfo>;
@@ -444,42 +455,41 @@ pub struct IndexStore {
}
impl IndexStore {
pub fn predicate_exists(&self, name: ClauseName, module: ClauseName, arity: usize,
op_spec: Option<SharedOpDesc>)
-> bool
{
pub fn predicate_exists(
&self,
name: ClauseName,
module: ClauseName,
arity: usize,
op_spec: Option<SharedOpDesc>,
) -> bool {
match self.modules.get(&module) {
Some(module) =>
match ClauseType::from(name, arity, op_spec) {
ClauseType::Named(name, arity, _) =>
module.code_dir.contains_key(&(name, arity)),
ClauseType::Op(name, spec, ..) =>
module.code_dir.contains_key(&(name, spec.arity())),
_ =>
true
},
None =>
match ClauseType::from(name, arity, op_spec) {
ClauseType::Named(name, arity, _) =>
self.code_dir.contains_key(&(name, arity)),
ClauseType::Op(name, spec, ..) =>
self.code_dir.contains_key(&(name, spec.arity())),
_ =>
true
Some(module) => match ClauseType::from(name, arity, op_spec) {
ClauseType::Named(name, arity, _) => module.code_dir.contains_key(&(name, arity)),
ClauseType::Op(name, spec, ..) => {
module.code_dir.contains_key(&(name, spec.arity()))
}
_ => true,
},
None => match ClauseType::from(name, arity, op_spec) {
ClauseType::Named(name, arity, _) => self.code_dir.contains_key(&(name, arity)),
ClauseType::Op(name, spec, ..) => self.code_dir.contains_key(&(name, spec.arity())),
_ => true,
},
}
}
#[inline]
pub fn remove_clause_subsection(&mut self, module: ClauseName, name: ClauseName, arity: usize)
{
pub fn remove_clause_subsection(&mut self, module: ClauseName, name: ClauseName, arity: usize) {
self.dynamic_code_dir.remove(&(module, name, arity));
}
#[inline]
pub fn get_clause_subsection(&self, module: ClauseName, name: ClauseName, arity: usize)
-> Option<DynamicPredicateInfo>
{
pub fn get_clause_subsection(
&self,
module: ClauseName,
name: ClauseName,
arity: usize,
) -> Option<DynamicPredicateInfo> {
self.dynamic_code_dir.get(&(module, name, arity)).cloned()
}
@@ -503,7 +513,7 @@ impl IndexStore {
in_situ_code_dir: InSituCodeDir::new(),
op_dir: default_op_dir(),
modules: ModuleDir::new(),
// parsing_stream: readline::parsing_stream(String::new())
// parsing_stream: readline::parsing_stream(String::new())
}
}
@@ -518,21 +528,30 @@ impl IndexStore {
}
#[inline]
fn get_internal(&self, name: ClauseName, arity: usize, in_mod: ClauseName) -> Option<CodeIndex>
{
self.modules.get(&in_mod)
fn get_internal(
&self,
name: ClauseName,
arity: usize,
in_mod: ClauseName,
) -> Option<CodeIndex> {
self.modules
.get(&in_mod)
.and_then(|ref module| module.code_dir.get(&(name, arity)))
.cloned()
}
pub(super) fn get_cleaner_sites(&self) -> (usize, usize) {
let r_w_h = clause_name!("run_cleaners_with_handling");
let r_w_h = clause_name!("run_cleaners_with_handling");
let r_wo_h = clause_name!("run_cleaners_without_handling");
let non_iso = clause_name!("non_iso");
let r_w_h = self.get_internal(r_w_h, 0, non_iso.clone()).and_then(|item| item.local());
let r_wo_h = self.get_internal(r_wo_h, 1, non_iso).and_then(|item| item.local());
let r_w_h = self
.get_internal(r_w_h, 0, non_iso.clone())
.and_then(|item| item.local());
let r_wo_h = self
.get_internal(r_wo_h, 1, non_iso)
.and_then(|item| item.local());
if let Some(r_w_h) = r_w_h {
if let Some(r_wo_h) = r_wo_h {
@@ -552,36 +571,38 @@ pub enum CompileTimeHook {
GoalExpansion,
TermExpansion,
UserGoalExpansion,
UserTermExpansion
UserTermExpansion,
}
impl CompileTimeHook {
pub fn name(self) -> ClauseName {
match self {
CompileTimeHook::UserGoalExpansion
| CompileTimeHook::GoalExpansion => clause_name!("goal_expansion"),
CompileTimeHook::UserTermExpansion
| CompileTimeHook::TermExpansion => clause_name!("term_expansion")
CompileTimeHook::UserGoalExpansion | CompileTimeHook::GoalExpansion => {
clause_name!("goal_expansion")
}
CompileTimeHook::UserTermExpansion | CompileTimeHook::TermExpansion => {
clause_name!("term_expansion")
}
}
}
#[inline]
pub fn arity(self) -> usize {
match self {
CompileTimeHook::UserGoalExpansion
| CompileTimeHook::GoalExpansion => 2,
CompileTimeHook::UserTermExpansion
| CompileTimeHook::TermExpansion => 2
CompileTimeHook::UserGoalExpansion | CompileTimeHook::GoalExpansion => 2,
CompileTimeHook::UserTermExpansion | CompileTimeHook::TermExpansion => 2,
}
}
#[inline]
pub fn user_scope(self) -> Self {
match self {
CompileTimeHook::UserGoalExpansion | CompileTimeHook::GoalExpansion =>
CompileTimeHook::UserGoalExpansion,
CompileTimeHook::UserTermExpansion | CompileTimeHook::TermExpansion =>
CompileTimeHook::UserTermExpansion,
CompileTimeHook::UserGoalExpansion | CompileTimeHook::GoalExpansion => {
CompileTimeHook::UserGoalExpansion
}
CompileTimeHook::UserTermExpansion | CompileTimeHook::TermExpansion => {
CompileTimeHook::UserTermExpansion
}
}
}
@@ -589,30 +610,31 @@ impl CompileTimeHook {
pub fn has_module_scope(self) -> bool {
match self {
CompileTimeHook::UserTermExpansion | CompileTimeHook::UserGoalExpansion => false,
_ => true
_ => true,
}
}
}
pub enum RefOrOwned<'a, T: 'a> {
Borrowed(&'a T),
Owned(T)
Owned(T),
}
impl<'a, T> RefOrOwned<'a, T> {
pub fn as_ref(&'a self) -> &'a T {
match self {
&RefOrOwned::Borrowed(r) => r,
&RefOrOwned::Owned(ref r) => r
&RefOrOwned::Owned(ref r) => r,
}
}
pub fn to_owned(self) -> T
where T: Clone
where
T: Clone,
{
match self {
RefOrOwned::Borrowed(item) => item.clone(),
RefOrOwned::Owned(item) => item
RefOrOwned::Owned(item) => item,
}
}
}