]> Repositorios git - scryer-prolog.git/commitdiff
finalize transition to CutPolicy trait object.
authorMark Thom <[email protected]>
Thu, 8 Feb 2018 04:46:40 +0000 (21:46 -0700)
committerMark Thom <[email protected]>
Thu, 8 Feb 2018 04:46:40 +0000 (21:46 -0700)
src/prolog/ast.rs
src/prolog/builtins.rs
src/prolog/io.rs
src/prolog/machine/machine_state.rs
src/prolog/machine/machine_state_impl.rs
src/prolog/macros.rs

index 386c83b57c744787f45f4b521291653c1d81de6c..7fde3b6d546741e822216f1625f14a765010876f 100644 (file)
@@ -818,6 +818,7 @@ pub enum BuiltInInstruction {
     IsString(RegType),
     IsVar(RegType),
     ResetBlock,
+    RestoreCutPolicy,
     SetBall,
     SetCutPoint(RegType),
     Succeed,
index c6f1be05b6962e20d585958b5ffcc6260d0c1705..bd902e95143528bdae401655bea101de135cab96 100644 (file)
@@ -475,25 +475,27 @@ fn get_builtins(atom_tbl: TabledData<Atom>) -> Code {
          query![put_value!(perm_v!(3), 1)],
          call_n!(1),
          query![put_value!(perm_v!(2), 1),
-                put_unsafe_value!(1, 2)],
+                put_value!(perm_v!(1), 2)],
          deallocate!(),
          check_cp_execute!(),
-         retry_me_else!(10),
-         allocate!(1),
+         retry_me_else!(12),
+         allocate!(2),
          query![put_value!(temp_v!(3), 1)],
-         reset_block!(),
+         reset_block!(),         
          query![put_var!(perm_v!(1), 1)],
          get_ball!(),
-         goto_call!(340, 0), // goto run_cleaners_with_handling/0, 340.
-         query![put_value!(perm_v!(1), 1)],
+         get_level!(perm_v!(2)),
+         set_cp!(perm_v!(2)),
+         goto_call!(342, 0), // goto run_cleaners_with_handling/0, 342.
+         query![put_unsafe_value!(1, 1)],
          deallocate!(),
-         goto_execute!(59, 1),
+         goto_execute!(59, 1), // goto throw/1, 59.        
          trust_me!(),
          allocate!(0),
-         goto_call!(352, 0), // goto run_cleaners_without_handling/0, 352.
+         goto_call!(354, 0), // goto run_cleaners_without_handling/0, 354.
          deallocate!(),
          fail!(),
-         try_me_else!(10), // run_cleaners_with_handling/0, 340.
+         try_me_else!(10), // run_cleaners_with_handling/0, 342.
          allocate!(2),
          get_level!(perm_v!(1)),
          query![put_var!(perm_v!(2), 1)],
@@ -502,39 +504,41 @@ fn get_builtins(atom_tbl: TabledData<Atom>) -> Code {
                 put_var!(temp_v!(4), 2),
                 put_constant!(Level::Shallow, atom!("true", atom_tbl), temp_v!(3))],
          goto_call!(5, 3), // goto catch/3, 5.
-         cut!(perm_v!(1)),
+         set_cp!(perm_v!(1)),
          deallocate!(),
-         goto_execute!(340, 0), // goto run_cleaners_with_handling/0, 340.
+         goto_execute!(342, 0), // goto run_cleaners_with_handling/0, 342.
          trust_me!(),
-         proceed!(), // goto_execute!(380, 0), // goto restore_cut_points/0, 380.
-         try_me_else!(10), // run_cleaners_without_handling/1, 352.
+         goto_execute!(382, 0), // goto restore_cut_points/0, 382.
+         try_me_else!(10), // run_cleaners_without_handling/1, 354.
          allocate!(2),
          get_level!(perm_v!(1)),
          query![put_var!(perm_v!(2), 1)],
          get_cleaner_call!(),
          query![put_value!(perm_v!(2), 1)],
          call_n!(1),
-         cut!(perm_v!(1)),
+         set_cp!(perm_v!(1)),
          deallocate!(),
-         goto_execute!(352, 0), // goto run_cleaners_without_handling/0, 352.
+         goto_execute!(354, 0), // goto run_cleaners_without_handling/0, 354.
          trust_me!(),
-         proceed!(), // goto_execute!(380, 0), // goto restore_cut_points/0, 380.
-         allocate!(1), // sgc_on_success/2, 364.
+         goto_execute!(382, 0), // goto restore_cut_points/0, 382.
+         allocate!(1), // sgc_on_success/2, 366.
          fact![get_var_in_fact!(perm_v!(1), 2)],
          reset_block!(),
          cut!(perm_v!(1)),
          deallocate!(),
          proceed!(),
-         is_compound!(temp_v!(1)), // compound/1, 370.
+         is_compound!(temp_v!(1)), // compound/1, 372.
          proceed!(),
-         is_rational!(temp_v!(1)), // rational/1, 372.
+         is_rational!(temp_v!(1)), // rational/1, 374.
          proceed!(),
-         is_string!(temp_v!(1)), // string/1, 374.
+         is_string!(temp_v!(1)), // string/1, 376.
          proceed!(),
-         is_float!(temp_v!(1)), // float/1, 376.
+         is_float!(temp_v!(1)), // float/1, 378.
          proceed!(),
-         is_nonvar!(temp_v!(1)), // nonvar/1, 378.
+         is_nonvar!(temp_v!(1)), // nonvar/1, 380.
          proceed!(),
+         restore_cut_policy!(), // restore_cut_policy/0, 382.
+         proceed!()
     ]
 }
 
@@ -621,11 +625,11 @@ pub fn build_code_dir(atom_tbl: TabledData<Atom>) -> (Code, CodeDir, OpDir)
 
     code_dir.insert((tabled_rc!("length", atom_tbl), 2), (PredicateKeyType::BuiltIn, 261));
     code_dir.insert((tabled_rc!("setup_call_cleanup", atom_tbl), 3), (PredicateKeyType::BuiltIn, 294));
-    code_dir.insert((tabled_rc!("compound", atom_tbl), 1), (PredicateKeyType::BuiltIn, 370));
-    code_dir.insert((tabled_rc!("rational", atom_tbl), 1), (PredicateKeyType::BuiltIn, 372));
-    code_dir.insert((tabled_rc!("string", atom_tbl), 1), (PredicateKeyType::BuiltIn, 374));
-    code_dir.insert((tabled_rc!("float", atom_tbl), 1), (PredicateKeyType::BuiltIn, 376));
-    code_dir.insert((tabled_rc!("nonvar", atom_tbl), 1), (PredicateKeyType::BuiltIn, 378));
+    code_dir.insert((tabled_rc!("compound", atom_tbl), 1), (PredicateKeyType::BuiltIn, 372));
+    code_dir.insert((tabled_rc!("rational", atom_tbl), 1), (PredicateKeyType::BuiltIn, 374));
+    code_dir.insert((tabled_rc!("string", atom_tbl), 1), (PredicateKeyType::BuiltIn, 376));
+    code_dir.insert((tabled_rc!("float", atom_tbl), 1), (PredicateKeyType::BuiltIn, 378));
+    code_dir.insert((tabled_rc!("nonvar", atom_tbl), 1), (PredicateKeyType::BuiltIn, 380));
     
     (builtin_code, code_dir, op_dir)
 }
index 3119ad2dd2fa8a4152ff80562e3cf07d0df8b70f..c5f26212108580165c3fd6943985ab573d42c72a 100644 (file)
@@ -219,6 +219,8 @@ impl fmt::Display for BuiltInInstruction {
                 write!(f, "is_var {}", r),
             &BuiltInInstruction::ResetBlock =>
                 write!(f, "reset_block"),
+            &BuiltInInstruction::RestoreCutPolicy =>
+                write!(f, "restore_cut_point"),
             &BuiltInInstruction::SetBall =>
                 write!(f, "set_ball"),
             &BuiltInInstruction::SetCutPoint(r) =>
index 6bcdecfcb987e32200794add0096a11a10996c52..4189f9c2bc55d2270a5c9aa877dd9270a609c53b 100644 (file)
@@ -248,7 +248,7 @@ impl CutPolicy for SetupCallCleanupCutPolicy {
             machine_st.cp = machine_st.p;
             machine_st.num_of_args = 0;
             machine_st.b0 = machine_st.b;
-            machine_st.p  = CodePtr::DirEntry(352); // goto_call run_cleaners_without_handling/0.
+            machine_st.p  = CodePtr::DirEntry(354); // goto_call run_cleaners_without_handling/0, 354.
         }
     }
 }
index a60d7f230a86aafd34917081e6dbe9215898966f..2c584b6385df70ec28368144c503ab213cb73434 100644 (file)
@@ -1204,7 +1204,7 @@ impl MachineState {
                 let b = self.b;
                 let block = self.block;
 
-                if !cut_policy.is::<SetupCallCleanupCutPolicy>() {
+                if cut_policy.downcast_ref::<SetupCallCleanupCutPolicy>().is_err() {
                     *cut_policy = Box::new(SetupCallCleanupCutPolicy::new());
                 }
 
@@ -1217,6 +1217,20 @@ SetupCallCleanupCutPolicy.")
 
                 self.p += 1;
             },
+            &BuiltInInstruction::RestoreCutPolicy => {
+                let restore_default =
+                    if let Ok(cut_policy) = cut_policy.downcast_ref::<SetupCallCleanupCutPolicy>() {
+                        cut_policy.out_of_cont_pts()
+                    } else {
+                        false
+                    };
+
+                if restore_default {
+                    *cut_policy = Box::new(DefaultCutPolicy {});
+                }
+
+                self.p += 1;
+            },
             &BuiltInInstruction::SetBall => {
                 let addr = self[temp_v!(1)].clone();
                 self.ball.0 = self.heap.h;
@@ -1358,7 +1372,7 @@ SetupCallCleanupCutPolicy.")
 
                 self.unify(a1, a2);
                 self.p += 1;
-            }
+            },
         };
     }
 
@@ -1518,7 +1532,7 @@ SetupCallCleanupCutPolicy.")
                     _ => {
                         self.num_of_args = 2;
                         self.b0 = self.b;
-                        self.p = CodePtr::DirEntry(364); // goto sgc_on_success/2, 364.
+                        self.p = CodePtr::DirEntry(366); // goto sgc_on_success/2, 366.
                     }
                 };
             },
@@ -1576,21 +1590,21 @@ SetupCallCleanupCutPolicy.")
                 }),
             &ControlInstruction::GetCleanerCall => {
                 let dest = self[temp_v!(1)].clone();
-                
+
                 match cut_policy.downcast_mut::<SetupCallCleanupCutPolicy>().ok() {
                     Some(sgc_policy) =>
                         if let Some((addr, b_cutoff, prev_block)) = sgc_policy.pop_cont_pt()
                         {
                             self.p += 1;
-                        
+
                             if self.b <= b_cutoff + 1 {
                                 self.block = prev_block;
-                                
+
                                 if let Some(r) = dest.as_var() {
                                     self.bind(r, addr);
                                     return;
                                 }
-                            } else {                                
+                            } else {
                                 sgc_policy.push_cont_pt(addr, b_cutoff, prev_block);
                             }
                         },
index 2220a669b02f4ce805d1a4fe5e9de1c08bf93497..98220187eec4b7c6b41a5fed0ba30fb818254cbf 100644 (file)
@@ -556,3 +556,9 @@ macro_rules! get_cleaner_call {
         Line::Control(ControlInstruction::GetCleanerCall)
     )       
 }
+
+macro_rules! restore_cut_policy {
+    () => (
+        Line::BuiltIn(BuiltInInstruction::RestoreCutPolicy)
+    )
+}