]> Repositorios git - scryer-prolog.git/commitdiff
correct faulty removal of clauses in abolish/1
authorMark Thom <[email protected]>
Tue, 9 Feb 2021 01:46:33 +0000 (18:46 -0700)
committerMark Thom <[email protected]>
Tue, 9 Feb 2021 02:02:26 +0000 (19:02 -0700)
src/forms.rs
src/machine/compile.rs
src/machine/loader.rs
src/machine/machine_indices.rs
src/machine/system_calls.rs

index 110906017eba419e0820b08860636fe36ae4f465..408d691dc945f8500ce9243769da7848be6c3d91 100644 (file)
@@ -711,5 +711,21 @@ impl PredicateSkeleton {
             clauses: self.clauses,
         }
     }
-    */
+     */
+
+    pub fn target_pos_of_clause_clause_loc(
+        &self,
+        clause_clause_loc: usize,
+        clause_assert_margin: usize,
+    ) -> usize {
+        let search_result = self.clause_clause_locs[0..clause_assert_margin]
+            .binary_search_by(|loc| clause_clause_loc.cmp(&loc));
+
+        search_result.unwrap_or_else(|_| {
+            self.clause_clause_locs[clause_assert_margin..]
+                .binary_search_by(|loc| loc.cmp(&clause_clause_loc))
+                .unwrap()
+                + clause_assert_margin
+        })
+    }
 }
index af25b3e8930e1c630e2a54dd0fad7a0fe1587aa9..faac9421c093c36284653e2efbfc161868c96682 100644 (file)
@@ -1047,7 +1047,7 @@ impl<'a> LoadState<'a> {
             match self
                 .wam
                 .indices
-                .get_predicate_skeleton(&self.compilation_target, &key)
+                .get_predicate_skeleton_mut(&self.compilation_target, &key)
             {
                 Some(skeleton) => {
                     self.retraction_info
@@ -1120,7 +1120,7 @@ impl<'a> LoadState<'a> {
         let skeleton = match self
             .wam
             .indices
-            .get_predicate_skeleton(&self.compilation_target, &key)
+            .get_predicate_skeleton_mut(&self.compilation_target, &key)
         {
             Some(skeleton) if !skeleton.clauses.is_empty() => skeleton,
             _ => {
@@ -1212,7 +1212,7 @@ impl<'a> LoadState<'a> {
         let skeleton = match self
             .wam
             .indices
-            .get_predicate_skeleton(&self.compilation_target, &key)
+            .get_predicate_skeleton_mut(&self.compilation_target, &key)
         {
             Some(skeleton) => skeleton,
             None => {
@@ -1540,7 +1540,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> {
             }
         }
 
-        match self.load_state.wam.indices.get_predicate_skeleton(
+        match self.load_state.wam.indices.get_predicate_skeleton_mut(
             &self.load_state.compilation_target,
             &(clause_name!("$clause"), 2),
         ) {
@@ -1563,7 +1563,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> {
             .load_state
             .wam
             .indices
-            .get_predicate_skeleton(&compilation_target, &key)
+            .get_predicate_skeleton_mut(&compilation_target, &key)
         {
             Some(skeleton) if append_or_prepend.is_append() => {
                 self.load_state.retraction_info.push_record(
index e713916f63d0c5606a01ad7a8353c13a97ca6628..90364ce76426b84f45478f0dd1a9d7142a912597 100644 (file)
@@ -199,7 +199,7 @@ impl<'a> Drop for LoadState<'a> {
                 RetractionRecord::AppendedModuleExtensiblePredicate(module_name, key) => {
                     self.wam
                         .indices
-                        .get_predicate_skeleton(&CompilationTarget::Module(module_name), &key)
+                        .get_predicate_skeleton_mut(&CompilationTarget::Module(module_name), &key)
                         .map(|skeleton| {
                             skeleton.clauses.pop_back();
                         });
@@ -207,7 +207,7 @@ impl<'a> Drop for LoadState<'a> {
                 RetractionRecord::PrependedModuleExtensiblePredicate(module_name, key) => {
                     self.wam
                         .indices
-                        .get_predicate_skeleton(&CompilationTarget::Module(module_name), &key)
+                        .get_predicate_skeleton_mut(&CompilationTarget::Module(module_name), &key)
                         .map(|skeleton| {
                             skeleton.clauses.pop_front();
                         });
@@ -266,7 +266,7 @@ impl<'a> Drop for LoadState<'a> {
                 RetractionRecord::AppendedUserExtensiblePredicate(key) => {
                     self.wam
                         .indices
-                        .get_predicate_skeleton(&CompilationTarget::User, &key)
+                        .get_predicate_skeleton_mut(&CompilationTarget::User, &key)
                         .map(|skeleton| {
                             skeleton.clauses.pop_back();
                         });
@@ -274,7 +274,7 @@ impl<'a> Drop for LoadState<'a> {
                 RetractionRecord::PrependedUserExtensiblePredicate(key) => {
                     self.wam
                         .indices
-                        .get_predicate_skeleton(&CompilationTarget::User, &key)
+                        .get_predicate_skeleton_mut(&CompilationTarget::User, &key)
                         .map(|skeleton| {
                             skeleton.clauses.pop_front();
                         });
@@ -412,7 +412,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clause_clause_locs.truncate_front(len);
@@ -430,7 +430,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &(clause_name!("$clause"), 2))
+                        .get_predicate_skeleton_mut(&compilation_target, &(clause_name!("$clause"), 2))
                     {
                         Some(skeleton) => {
                             skeleton.clause_clause_locs.truncate_front(len);
@@ -446,7 +446,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clause_clause_locs.truncate_back(len);
@@ -464,7 +464,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &(clause_name!("$clause"), 2))
+                        .get_predicate_skeleton_mut(&compilation_target, &(clause_name!("$clause"), 2))
                     {
                         Some(skeleton) => {
                             skeleton.clause_clause_locs.truncate_back(len);
@@ -476,7 +476,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clauses.pop_back();
@@ -488,7 +488,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clauses.pop_front();
@@ -500,7 +500,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clauses.truncate_back(len);
@@ -517,7 +517,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton.clauses[target_pos].clause_start = clause_start;
@@ -535,7 +535,7 @@ impl<'a> Drop for LoadState<'a> {
                     match self
                         .wam
                         .indices
-                        .get_predicate_skeleton(&compilation_target, &key)
+                        .get_predicate_skeleton_mut(&compilation_target, &key)
                     {
                         Some(skeleton) => {
                             skeleton
@@ -1365,7 +1365,7 @@ impl Machine {
                 .load_state
                 .wam
                 .indices
-                .get_predicate_skeleton(&compilation_target, &key)
+                .get_predicate_skeleton_mut(&compilation_target, &key)
                 .map(|skeleton| skeleton.is_dynamic = true);
 
             loader.compile_clause_clauses(
@@ -1407,53 +1407,98 @@ impl Machine {
             _ => CompilationTarget::Module(module_name),
         };
 
-        let mut loader = Loader::new(LiveTermStream::new(ListingSource::User), self);
-        loader.load_state.compilation_target = compilation_target;
+        let abolish_clause = || {
+            let mut loader = Loader::new(LiveTermStream::new(ListingSource::User), self);
+            loader.load_state.compilation_target = compilation_target;
 
-        match loader
-            .load_state
-            .wam
-            .indices
-            .get_predicate_skeleton(&loader.load_state.compilation_target, &key)
-        {
-            Some(skeleton) => {
-                skeleton.clauses.clear();
-                skeleton.clause_clause_locs.clear();
-            }
-            _ => {
-                unreachable!();
-            }
-        }
+            let clause_clause_compilation_target =
+                match &loader.load_state.compilation_target {
+                    CompilationTarget::User => {
+                        CompilationTarget::Module(clause_name!("builtins"))
+                    }
+                    module => {
+                        module.clone()
+                    }
+                };
+
+            let mut clause_assert_margin = loader
+                .load_state
+                .wam
+                .indices
+                .modules
+                .get(&clause_clause_compilation_target.module_name())
+                .map(|module| module.clause_assert_margin)
+                .unwrap();
 
-        let code_index = loader.load_state.get_or_insert_code_index(key);
-        code_index.set(IndexPtr::DynamicUndefined);
+            let mut clause_clause_target_poses: Vec<_> = loader
+                .load_state
+                .wam
+                .indices
+                .get_predicate_skeleton(&loader.load_state.compilation_target, &key)
+                .map(|skeleton| {
+                    loader
+                        .load_state
+                        .wam
+                        .indices
+                        .get_predicate_skeleton(
+                            &clause_clause_compilation_target,
+                            &(clause_name!("$clause"), 2),
+                        )
+                        .map(|clause_clause_skeleton| {
+                            skeleton.clause_clause_locs
+                                .iter()
+                                .map(|clause_clause_loc| {
+                                    clause_clause_skeleton.target_pos_of_clause_clause_loc(
+                                        *clause_clause_loc,
+                                        clause_assert_margin,
+                                    )
+                                })
+                                .collect()
+                        })
+                        .unwrap()
+                })
+                .unwrap();
 
-        match loader.load_state.compilation_target {
-            CompilationTarget::User => {
-                loader.load_state.compilation_target =
-                    CompilationTarget::Module(clause_name!("builtins"));
+            loader
+                .load_state
+                .wam
+                .indices
+                .get_predicate_skeleton_mut(&loader.load_state.compilation_target, &key)
+                .map(|skeleton| {
+                    skeleton.clauses.clear();
+                    skeleton.clause_clause_locs.clear();
+                });
+
+            let code_index = loader.load_state.get_or_insert_code_index(key);
+            code_index.set(IndexPtr::DynamicUndefined);
+
+            loader.load_state.compilation_target = clause_clause_compilation_target;
+
+            while let Some(target_pos) = clause_clause_target_poses.pop() {
+                loader.load_state.retract_clause((clause_name!("$clause"), 2), target_pos);
+
+                if target_pos < clause_assert_margin {
+                    clause_assert_margin -= 1;
+                }
             }
-            _ => {}
+
+            loader
+                .load_state
+                .wam
+                .indices
+                .modules
+                .get_mut(&loader.load_state.compilation_target.module_name())
+                .map(|module| module.clause_assert_margin = clause_assert_margin);
+
+            LiveTermStream::evacuate(loader)
         };
 
-        match loader.load_state.wam.indices.get_predicate_skeleton(
-            &loader.load_state.compilation_target,
-            &(clause_name!("$clause"), 2),
-        ) {
-            Some(skeleton) => {
-                skeleton.clauses.clear();
-                skeleton.clause_clause_locs.clear();
-            }
-            _ => {
-                unreachable!();
+        match abolish_clause() {
+            Ok(_) => {}
+            Err(e) => {
+                self.throw_session_error(e, (clause_name!("abolish"), 1));
             }
         }
-
-        let clause_clause_code_index = loader
-            .load_state
-            .get_or_insert_code_index((clause_name!("$clause"), 2));
-
-        clause_clause_code_index.set(IndexPtr::DynamicUndefined);
     }
 
     pub(crate) fn retract_clause(&mut self) {
@@ -1507,17 +1552,12 @@ impl Machine {
                 &(clause_name!("$clause"), 2),
             ) {
                 Some(skeleton) => {
-                    let search_result = skeleton.clause_clause_locs[0..clause_assert_margin]
-                        .binary_search_by(|loc| clause_clause_loc.cmp(&loc));
-
-                    let result = search_result.unwrap_or_else(|_| {
-                        skeleton.clause_clause_locs[clause_assert_margin..]
-                            .binary_search_by(|loc| loc.cmp(&clause_clause_loc))
-                            .unwrap()
-                            + clause_assert_margin
-                    });
+                    let target_pos = skeleton.target_pos_of_clause_clause_loc(
+                        clause_clause_loc,
+                        clause_assert_margin,
+                    );
 
-                    if result < clause_assert_margin {
+                    if target_pos < clause_assert_margin {
                         loader
                             .load_state
                             .wam
@@ -1527,7 +1567,7 @@ impl Machine {
                             .map(|module| module.clause_assert_margin -= 1);
                     }
 
-                    result
+                    target_pos
                 }
                 None => {
                     unreachable!();
index 6bb9dffdd861aceef711e4b868f7b191194281be..cb39fede16159703ef31ed5c3544d7c02e466382 100644 (file)
@@ -720,7 +720,7 @@ impl Default for IndexStore {
 }
 
 impl IndexStore {
-    pub fn get_predicate_skeleton(
+    pub fn get_predicate_skeleton_mut(
         &mut self,
         compilation_target: &CompilationTarget,
         key: &PredicateKey,
@@ -740,6 +740,26 @@ impl IndexStore {
         }
     }
 
+    pub fn get_predicate_skeleton(
+        &self,
+        compilation_target: &CompilationTarget,
+        key: &PredicateKey,
+    ) -> Option<&PredicateSkeleton> {
+        match (key.0.as_str(), key.1) {
+            ("term_expansion", 2) => self.extensible_predicates.get(key),
+            _ => match compilation_target {
+                CompilationTarget::User => self.extensible_predicates.get(key),
+                CompilationTarget::Module(ref module_name) => {
+                    if let Some(module) = self.modules.get(module_name) {
+                        module.extensible_predicates.get(key)
+                    } else {
+                        None
+                    }
+                }
+            },
+        }
+    }
+
     pub fn remove_predicate_skeleton(
         &mut self,
         compilation_target: &CompilationTarget,
index a75743332aac9971576ab4a041d97eaf0b68befe..8251c3fe3c293abf6b406ff6f11a778c2985a776 100644 (file)
@@ -4631,7 +4631,7 @@ impl MachineState {
 
                 copy_term(
                     CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut ball.stub),
-                    value.clone(),
+                    value,
                     AttrVarPolicy::DeepCopy,
                 );