From: Mark Thom Date: Thu, 8 Feb 2018 04:46:40 +0000 (-0700) Subject: finalize transition to CutPolicy trait object. X-Git-Tag: v0.8.110~577 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=1b4d1c04c963f207b55a40c21be64dc9bfb84f24;p=scryer-prolog.git finalize transition to CutPolicy trait object. --- diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index 386c83b5..7fde3b6d 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -818,6 +818,7 @@ pub enum BuiltInInstruction { IsString(RegType), IsVar(RegType), ResetBlock, + RestoreCutPolicy, SetBall, SetCutPoint(RegType), Succeed, diff --git a/src/prolog/builtins.rs b/src/prolog/builtins.rs index c6f1be05..bd902e95 100644 --- a/src/prolog/builtins.rs +++ b/src/prolog/builtins.rs @@ -475,25 +475,27 @@ fn get_builtins(atom_tbl: TabledData) -> 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) -> 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) -> (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) } diff --git a/src/prolog/io.rs b/src/prolog/io.rs index 3119ad2d..c5f26212 100644 --- a/src/prolog/io.rs +++ b/src/prolog/io.rs @@ -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) => diff --git a/src/prolog/machine/machine_state.rs b/src/prolog/machine/machine_state.rs index 6bcdecfc..4189f9c2 100644 --- a/src/prolog/machine/machine_state.rs +++ b/src/prolog/machine/machine_state.rs @@ -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. } } } diff --git a/src/prolog/machine/machine_state_impl.rs b/src/prolog/machine/machine_state_impl.rs index a60d7f23..2c584b63 100644 --- a/src/prolog/machine/machine_state_impl.rs +++ b/src/prolog/machine/machine_state_impl.rs @@ -1204,7 +1204,7 @@ impl MachineState { let b = self.b; let block = self.block; - if !cut_policy.is::() { + if cut_policy.downcast_ref::().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::() { + 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::().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); } }, diff --git a/src/prolog/macros.rs b/src/prolog/macros.rs index 2220a669..98220187 100644 --- a/src/prolog/macros.rs +++ b/src/prolog/macros.rs @@ -556,3 +556,9 @@ macro_rules! get_cleaner_call { Line::Control(ControlInstruction::GetCleanerCall) ) } + +macro_rules! restore_cut_policy { + () => ( + Line::BuiltIn(BuiltInInstruction::RestoreCutPolicy) + ) +}