Simplified the code, removed first_arg, the Option

This commit is contained in:
notoria
2020-12-19 21:04:17 +01:00
parent 657e4f12bb
commit ec2c5a9b2c
3 changed files with 22 additions and 36 deletions

View File

@@ -857,16 +857,14 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker> {
Ok(code) Ok(code)
} }
fn split_predicate(clauses: &Vec<PredicateClause>, optimal_index: Option<usize>) -> Vec<(usize, usize)> { fn split_predicate(clauses: &Vec<PredicateClause>, optimal_index: usize) -> Vec<(usize, usize)> {
let mut subseqs = Vec::new(); let mut subseqs = Vec::new();
let mut left_index = 0; let mut left_index = 0;
if let Some(optimal_i) = optimal_index { if clauses.first().unwrap().args().is_some() {
for (right_index, clause) in clauses.iter().enumerate() { for (right_index, clause) in clauses.iter().enumerate() {
if clause.args().is_none() { // Can unwrap safely.
continue; if let Some(arg) = clause.args().unwrap().iter().nth(optimal_index) {
}
if let Some(arg) = clause.args().unwrap().iter().nth(optimal_i) {
match **arg { match **arg {
Term::Var(..) | Term::AnonVar => { Term::Var(..) | Term::AnonVar => {
if left_index < right_index { if left_index < right_index {
@@ -908,7 +906,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker> {
fn compile_pred_subseq<'b: 'a>( fn compile_pred_subseq<'b: 'a>(
&mut self, &mut self,
clauses: &'b [PredicateClause], clauses: &'b [PredicateClause],
optimal_index: Option<usize>, optimal_index: usize,
) -> Result<Code, ParserError> { ) -> Result<Code, ParserError> {
let mut code_body = Vec::new(); let mut code_body = Vec::new();
let mut code_offsets = CodeOffsets::new(); let mut code_offsets = CodeOffsets::new();
@@ -933,9 +931,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker> {
code_body.push(Line::Choice(choice)); code_body.push(Line::Choice(choice));
} }
if let Some(optimal_i) = optimal_index {
let arg = match clause.args() { let arg = match clause.args() {
Some(args) => match args.iter().nth(optimal_i) { Some(args) => match args.iter().nth(optimal_index) {
Some(term) => Some(term), Some(term) => Some(term),
None => None, None => None,
}, },
@@ -945,16 +942,14 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker> {
let index = code_body.len(); let index = code_body.len();
code_offsets.index_term(arg, index); code_offsets.index_term(arg, index);
} }
}
code_body.append(&mut clause_code); code_body.append(&mut clause_code);
} }
let mut code = Vec::new(); let mut code = Vec::new();
if let Some(optimal_i) = optimal_index { code_offsets.add_indices(&mut code, code_body, optimal_index + 1);
code_offsets.add_indices(&mut code, code_body, optimal_i + 1);
}
Ok(code) Ok(code)
} }
@@ -993,12 +988,10 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker> {
} }
} }
} }
match optimal_index { let optimal_index = match optimal_index {
// No good index or no argument, default to 0. Some(optimal_index) => optimal_index,
// TODO: Why? None => 0, // Default to first argument indexing.
None => optimal_index = Some(0), };
Some(_) => (),
}
let split_pred = Self::split_predicate(&clauses, optimal_index); let split_pred = Self::split_predicate(&clauses, optimal_index);
let multi_seq = split_pred.len() > 1; let multi_seq = split_pred.len() > 1;

View File

@@ -332,13 +332,6 @@ pub enum PredicateClause {
} }
impl PredicateClause { impl PredicateClause {
pub fn first_arg(&self) -> Option<&Term> {
match self {
&PredicateClause::Fact(ref term, ..) => term.first_arg(),
&PredicateClause::Rule(ref rule, ..) => rule.head.1.first().map(|bt| bt.as_ref()),
}
}
// TODO: add this to `Term` in `prolog_parser` like `first_arg`. // TODO: add this to `Term` in `prolog_parser` like `first_arg`.
pub fn args(&self) -> Option<&[Box<Term>]> { pub fn args(&self) -> Option<&[Box<Term>]> {
match *self { match *self {

View File

@@ -143,8 +143,8 @@ impl CodeOffsets {
} }
} }
pub fn index_term(&mut self, first_arg: &Term, index: usize) { pub fn index_term(&mut self, optimal_arg: &Term, index: usize) {
match first_arg { match optimal_arg {
&Term::Clause(_, ref name, ref terms, _) => { &Term::Clause(_, ref name, ref terms, _) => {
let code = self let code = self
.structures .structures