From ec2c5a9b2c3a98609e992eee54918cab6ea4c143 Mon Sep 17 00:00:00 2001 From: notoria Date: Sat, 19 Dec 2020 21:04:17 +0100 Subject: [PATCH] Simplified the code, removed first_arg, the Option --- src/codegen.rs | 47 ++++++++++++++++++++--------------------------- src/forms.rs | 7 ------- src/indexing.rs | 4 ++-- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index 56128edc..eb68ae8b 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -857,16 +857,14 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator { Ok(code) } - fn split_predicate(clauses: &Vec, optimal_index: Option) -> Vec<(usize, usize)> { + fn split_predicate(clauses: &Vec, optimal_index: usize) -> Vec<(usize, usize)> { let mut subseqs = Vec::new(); 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() { - if clause.args().is_none() { - continue; - } - if let Some(arg) = clause.args().unwrap().iter().nth(optimal_i) { + // Can unwrap safely. + if let Some(arg) = clause.args().unwrap().iter().nth(optimal_index) { match **arg { Term::Var(..) | Term::AnonVar => { if left_index < right_index { @@ -908,7 +906,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator { fn compile_pred_subseq<'b: 'a>( &mut self, clauses: &'b [PredicateClause], - optimal_index: Option, + optimal_index: usize, ) -> Result { let mut code_body = Vec::new(); let mut code_offsets = CodeOffsets::new(); @@ -933,18 +931,16 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator { code_body.push(Line::Choice(choice)); } - if let Some(optimal_i) = optimal_index { - let arg = match clause.args() { - Some(args) => match args.iter().nth(optimal_i) { - Some(term) => Some(term), - None => None, - }, + let arg = match clause.args() { + Some(args) => match args.iter().nth(optimal_index) { + Some(term) => Some(term), None => None, - }; - if let Some(arg) = arg { - let index = code_body.len(); - code_offsets.index_term(arg, index); - } + }, + None => None, + }; + if let Some(arg) = arg { + let index = code_body.len(); + code_offsets.index_term(arg, index); } code_body.append(&mut clause_code); @@ -952,9 +948,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator { let mut code = Vec::new(); - if let Some(optimal_i) = optimal_index { - code_offsets.add_indices(&mut code, code_body, optimal_i + 1); - } + code_offsets.add_indices(&mut code, code_body, optimal_index + 1); + Ok(code) } @@ -993,12 +988,10 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator { } } } - match optimal_index { - // No good index or no argument, default to 0. - // TODO: Why? - None => optimal_index = Some(0), - Some(_) => (), - } + let optimal_index = match optimal_index { + Some(optimal_index) => optimal_index, + None => 0, // Default to first argument indexing. + }; let split_pred = Self::split_predicate(&clauses, optimal_index); let multi_seq = split_pred.len() > 1; diff --git a/src/forms.rs b/src/forms.rs index 5a7fcd2b..be572bc4 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -332,13 +332,6 @@ pub enum 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`. pub fn args(&self) -> Option<&[Box]> { match *self { diff --git a/src/indexing.rs b/src/indexing.rs index ccd63717..bf5403bf 100644 --- a/src/indexing.rs +++ b/src/indexing.rs @@ -143,8 +143,8 @@ impl CodeOffsets { } } - pub fn index_term(&mut self, first_arg: &Term, index: usize) { - match first_arg { + pub fn index_term(&mut self, optimal_arg: &Term, index: usize) { + match optimal_arg { &Term::Clause(_, ref name, ref terms, _) => { let code = self .structures -- 2.54.0