Commit 519dd1ac authored by Jeff Smits's avatar Jeff Smits

Found case where we need scoping of let-defined strategies, as predicted in TODO comment

parent c128b076
Specification([Signature([Constructors([ExtOpDecl("Nil",ConstType(Sort("List",[SortVar("a")]))),ExtOpDecl("Cons",FunType([ConstType(SortVar("a")),ConstType(Sort("List",[SortVar("a")]))],ConstType(Sort("List",[SortVar("a")])))),OpDecl("DR_DUMMY",ConstType(Sort("ATerm",[])))])]),Strategies([SDefT("test1_0_0",[],[],Scope(["d_2","e_2"],Seq(Match(Var("e_2")),Seq(Build(Anno(Op("Cons",[Anno(Str("a"),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("d_2")),Seq(Build(Var("e_2")),Seq(CallT(SVar("dr_fork_and_intersect_2_1"),[Scope(["b_2","f_2","g_2","h_2","i_2","j_2","k_2"],Seq(Match(Var("b_2")),Seq(Match(Var("i_2")),Seq(Build(Anno(Str("a"),Op("Nil",[]))),Seq(Match(Var("f_2")),Seq(Build(Var("i_2")),Seq(Match(Var("j_2")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("g_2")),Seq(Build(Var("j_2")),Seq(Match(Var("k_2")),Seq(Build(Anno(Str("-515232"),Op("Nil",[]))),Seq(Match(Var("h_2")),Seq(Build(Var("k_2")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("f_2"),Var("g_2"),Var("h_2")]),Build(Var("b_2"))))))))))))))))),Scope(["c_2","l_2","m_2","n_2","o_2","p_2","q_2"],Seq(Match(Var("c_2")),Seq(Match(Var("o_2")),Seq(Build(Anno(Str("a"),Op("Nil",[]))),Seq(Match(Var("l_2")),Seq(Build(Var("o_2")),Seq(Match(Var("p_2")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("m_2")),Seq(Build(Var("p_2")),Seq(Match(Var("q_2")),Seq(Build(Anno(Str("-235771"),Op("Nil",[]))),Seq(Match(Var("n_2")),Seq(Build(Var("q_2")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("l_2"),Var("m_2"),Var("n_2")]),Build(Var("c_2")))))))))))))))))],[Var("d_2")]),CallT(SVar("a_0_0"),[],[])))))))),SDefT("test2_0_0",[],[],Scope(["v_2","w_2"],Seq(Match(Var("w_2")),Seq(Build(Anno(Op("Cons",[Anno(Str("b"),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("v_2")),Seq(Build(Var("w_2")),Seq(CallT(SVar("dr_fork_and_intersect_2_1"),[Scope(["t_2","x_2","y_2","z_2","a_3","b_3","c_3"],Seq(Match(Var("t_2")),Seq(Match(Var("a_3")),Seq(Build(Anno(Str("b"),Op("Nil",[]))),Seq(Match(Var("x_2")),Seq(Build(Var("a_3")),Seq(Match(Var("b_3")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("y_2")),Seq(Build(Var("b_3")),Seq(Match(Var("c_3")),Seq(Build(Anno(Str("-514980"),Op("Nil",[]))),Seq(Match(Var("z_2")),Seq(Build(Var("c_3")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("x_2"),Var("y_2"),Var("z_2")]),Build(Var("t_2"))))))))))))))))),Scope(["u_2","d_3","e_3","f_3","g_3","h_3","i_3"],Seq(Match(Var("u_2")),Seq(Match(Var("g_3")),Seq(Build(Anno(Str("b"),Op("Nil",[]))),Seq(Match(Var("d_3")),Seq(Build(Var("g_3")),Seq(Match(Var("h_3")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("e_3")),Seq(Build(Var("h_3")),Seq(Match(Var("i_3")),Seq(Build(Anno(Str("-514980"),Op("Nil",[]))),Seq(Match(Var("f_3")),Seq(Build(Var("i_3")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("d_3"),Var("e_3"),Var("f_3")]),Build(Var("u_2")))))))))))))))))],[Var("v_2")]),CallT(SVar("b_0_0"),[],[])))))))),SDefT("test3_0_0",[],[],Scope(["n_3","o_3"],Seq(Match(Var("o_3")),Seq(Build(Anno(Op("Cons",[Anno(Str("c"),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("n_3")),Seq(Build(Var("o_3")),Seq(CallT(SVar("dr_fork_and_intersect_2_1"),[Scope(["l_3","p_3","q_3","r_3","s_3","t_3","u_3"],Seq(Match(Var("l_3")),Seq(Match(Var("s_3")),Seq(Build(Anno(Str("c"),Op("Nil",[]))),Seq(Match(Var("p_3")),Seq(Build(Var("s_3")),Seq(Match(Var("t_3")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("q_3")),Seq(Build(Var("t_3")),Seq(Match(Var("u_3")),Seq(Build(Anno(Str("-514727"),Op("Nil",[]))),Seq(Match(Var("r_3")),Seq(Build(Var("u_3")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("p_3"),Var("q_3"),Var("r_3")]),Build(Var("l_3"))))))))))))))))),Scope(["m_3","v_3","w_3","x_3","y_3","z_3","a_4"],Seq(Match(Var("m_3")),Seq(Match(Var("y_3")),Seq(Build(Anno(Str("c"),Op("Nil",[]))),Seq(Match(Var("v_3")),Seq(Build(Var("y_3")),Seq(Match(Var("z_3")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("w_3")),Seq(Build(Var("z_3")),Seq(Match(Var("a_4")),Seq(Build(Anno(Str("-235266"),Op("Nil",[]))),Seq(Match(Var("x_3")),Seq(Build(Var("a_4")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("v_3"),Var("w_3"),Var("x_3")]),Build(Var("m_3")))))))))))))))))],[Var("n_3")]),CallT(SVar("c_0_0"),[],[])))))))),SDefT("test4_0_0",[],[],Scope(["f_4","g_4"],Seq(Match(Var("g_4")),Seq(Build(Anno(Op("Cons",[Anno(Str("d"),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("f_4")),Seq(Build(Var("g_4")),Seq(CallT(SVar("dr_fork_and_intersect_2_1"),[Scope(["d_4","h_4","i_4","j_4","k_4","l_4","m_4"],Seq(Match(Var("d_4")),Seq(Match(Var("k_4")),Seq(Build(Anno(Str("d"),Op("Nil",[]))),Seq(Match(Var("h_4")),Seq(Build(Var("k_4")),Seq(Match(Var("l_4")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("i_4")),Seq(Build(Var("l_4")),Seq(Match(Var("m_4")),Seq(Build(Anno(Str("-514576"),Op("Nil",[]))),Seq(Match(Var("j_4")),Seq(Build(Var("m_4")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("h_4"),Var("i_4"),Var("j_4")]),Build(Var("d_4"))))))))))))))))),Scope(["e_4","n_4","o_4","p_4","q_4","r_4","s_4"],Seq(Match(Var("e_4")),Seq(Match(Var("q_4")),Seq(Build(Anno(Str("d"),Op("Nil",[]))),Seq(Match(Var("n_4")),Seq(Build(Var("q_4")),Seq(Match(Var("r_4")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("o_4")),Seq(Build(Var("r_4")),Seq(Match(Var("s_4")),Seq(Build(Anno(Str("-514576"),Op("Nil",[]))),Seq(Match(Var("p_4")),Seq(Build(Var("s_4")),Seq(CallT(SVar("dr_set_rule_0_3"),[],[Var("n_4"),Var("o_4"),Var("p_4")]),Build(Var("e_4")))))))))))))))))],[Var("f_4")]),CallT(SVar("d_0_0"),[],[])))))))),SDefT("main_0_0",[],[],CallT(SVar("test_suite_2_0"),[Build(Anno(Str("intersection with rulenames"),Op("Nil",[]))),CallT(SVar("test_it_now_0_0"),[],[])],[])),SDefT("test_it_now_0_0",[],[],Seq(CallT(SVar("apply_and_fail_3_0"),[Build(Anno(Str("intersection with rulenames 1"),Op("Nil",[]))),CallT(SVar("test1_0_0"),[],[]),Build(Anno(Op("Nil",[]),Op("Nil",[])))],[]),Seq(CallT(SVar("apply_test_4_0"),[Build(Anno(Str("intersection with rulenames 2"),Op("Nil",[]))),CallT(SVar("test2_0_0"),[],[]),Build(Anno(Op("Nil",[]),Op("Nil",[]))),Build(Anno(Int("1"),Op("Nil",[])))],[]),Seq(CallT(SVar("apply_and_fail_3_0"),[Build(Anno(Str("normal intersection 1"),Op("Nil",[]))),CallT(SVar("test3_0_0"),[],[]),Build(Anno(Op("Nil",[]),Op("Nil",[])))],[]),CallT(SVar("apply_test_4_0"),[Build(Anno(Str("normal intersection 2"),Op("Nil",[]))),CallT(SVar("test4_0_0"),[],[]),Build(Anno(Op("Nil",[]),Op("Nil",[]))),Build(Anno(Int("1"),Op("Nil",[])))],[]))))),SDefT("aux_a_0_1",[],[VarDec("d_22",ConstType(Sort("ATerm",[])))],GuardedLChoice(Scope(["w_4"],Seq(Match(Anno(Str("-235771"),Wld)),Seq(Match(Var("w_4")),Seq(Build(Var("d_22")),Seq(Build(Var("w_4")),Build(Anno(Int("0"),Op("Nil",[])))))))),Id,Scope(["u_4"],Seq(Match(Anno(Str("-515232"),Wld)),Seq(Match(Var("u_4")),Seq(Build(Var("d_22")),Seq(Build(Var("u_4")),Build(Anno(Int("1"),Op("Nil",[])))))))))),SDefT("a_0_0",[],[],Scope(["c_9","d_9","e_9","f_9","g_9","h_9","i_9"],Seq(Match(As(Var("c_9"),Wld)),Seq(Match(Var("e_9")),Seq(Match(Var("h_9")),Seq(Build(Anno(Str("a"),Op("Nil",[]))),Seq(Match(Var("f_9")),Seq(Build(Var("h_9")),Seq(Match(Var("i_9")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("g_9")),Seq(Build(Var("i_9")),Seq(CallT(SVar("dr_lookup_rule_0_2"),[],[Var("f_9"),Var("g_9")]),Seq(Match(Var("d_9")),Seq(Build(Var("e_9")),Seq(Build(Var("d_9")),CallT(SVar("fetch_elem_1_0"),[CallT(SVar("aux_a_0_1"),[],[Var("c_9")])],[]))))))))))))))))),SDefT("b_0_0",[],[],Scope(["g_13","h_13","i_13","j_13","k_13","l_13","m_13"],Seq(Match(As(Var("g_13"),Wld)),Seq(Match(Var("i_13")),Seq(Match(Var("l_13")),Seq(Build(Anno(Str("b"),Op("Nil",[]))),Seq(Match(Var("j_13")),Seq(Build(Var("l_13")),Seq(Match(Var("m_13")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("k_13")),Seq(Build(Var("m_13")),Seq(CallT(SVar("dr_lookup_rule_0_2"),[],[Var("j_13"),Var("k_13")]),Seq(Match(Var("h_13")),Seq(Build(Var("i_13")),Seq(Build(Var("h_13")),CallT(SVar("fetch_elem_1_0"),[CallT(SVar("aux_b_0_1"),[],[Var("g_13")])],[]))))))))))))))))),SDefT("aux_b_0_1",[],[VarDec("n_13",ConstType(Sort("ATerm",[])))],Scope(["o_13"],Seq(Match(Anno(Str("-514980"),Wld)),Seq(Match(Var("o_13")),Seq(Build(Var("n_13")),Seq(Build(Var("o_13")),Build(Anno(Int("1"),Op("Nil",[]))))))))),SDefT("aux_c_0_1",[],[VarDec("e_22",ConstType(Sort("ATerm",[])))],GuardedLChoice(Scope(["s_13"],Seq(Match(Anno(Str("-235266"),Wld)),Seq(Match(Var("s_13")),Seq(Build(Var("e_22")),Seq(Build(Var("s_13")),Build(Anno(Int("0"),Op("Nil",[])))))))),Id,Scope(["q_13"],Seq(Match(Anno(Str("-514727"),Wld)),Seq(Match(Var("q_13")),Seq(Build(Var("e_22")),Seq(Build(Var("q_13")),Build(Anno(Int("1"),Op("Nil",[])))))))))),SDefT("c_0_0",[],[],Scope(["q_17","r_17","s_17","t_17","u_17","v_17","w_17"],Seq(Match(As(Var("q_17"),Wld)),Seq(Match(Var("s_17")),Seq(Match(Var("v_17")),Seq(Build(Anno(Str("c"),Op("Nil",[]))),Seq(Match(Var("t_17")),Seq(Build(Var("v_17")),Seq(Match(Var("w_17")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("u_17")),Seq(Build(Var("w_17")),Seq(CallT(SVar("dr_lookup_rule_0_2"),[],[Var("t_17"),Var("u_17")]),Seq(Match(Var("r_17")),Seq(Build(Var("s_17")),Seq(Build(Var("r_17")),CallT(SVar("fetch_elem_1_0"),[CallT(SVar("aux_c_0_1"),[],[Var("q_17")])],[]))))))))))))))))),SDefT("d_0_0",[],[],Scope(["u_21","v_21","w_21","x_21","y_21","z_21","a_22"],Seq(Match(As(Var("u_21"),Wld)),Seq(Match(Var("w_21")),Seq(Match(Var("z_21")),Seq(Build(Anno(Str("d"),Op("Nil",[]))),Seq(Match(Var("x_21")),Seq(Build(Var("z_21")),Seq(Match(Var("a_22")),Seq(Build(Anno(Op("Cons",[Anno(Op("DR_DUMMY",[]),Op("Nil",[])),Anno(Op("Nil",[]),Op("Nil",[]))]),Op("Nil",[]))),Seq(Match(Var("y_21")),Seq(Build(Var("a_22")),Seq(CallT(SVar("dr_lookup_rule_0_2"),[],[Var("x_21"),Var("y_21")]),Seq(Match(Var("v_21")),Seq(Build(Var("w_21")),Seq(Build(Var("v_21")),CallT(SVar("fetch_elem_1_0"),[CallT(SVar("aux_d_0_1"),[],[Var("u_21")])],[]))))))))))))))))),SDefT("aux_d_0_1",[],[VarDec("b_22",ConstType(Sort("ATerm",[])))],Scope(["c_22"],Seq(Match(Anno(Str("-514576"),Wld)),Seq(Match(Var("c_22")),Seq(Build(Var("b_22")),Seq(Build(Var("c_22")),Build(Anno(Int("1"),Op("Nil",[]))))))))),ExtSDef("test_suite_2_0",[VarDec("t_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("u_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[]),ExtSDef("apply_test_4_0",[VarDec("v_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("w_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("x_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("y_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[]),ExtSDef("apply_and_fail_3_0",[VarDec("g_24",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("h_24",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("i_24",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[]),ExtSDef("dr_lookup_rule_0_2",[],[VarDec("g_48",ConstType(Sort("ATerm",[]))),VarDec("h_48",ConstType(Sort("ATerm",[])))]),ExtSDef("dr_set_rule_0_3",[],[VarDec("n_48",ConstType(Sort("ATerm",[]))),VarDec("o_48",ConstType(Sort("ATerm",[]))),VarDec("p_48",ConstType(Sort("ATerm",[])))]),ExtSDef("dr_fork_and_intersect_2_1",[VarDec("e_56",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("f_56",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[VarDec("d_56",ConstType(Sort("ATerm",[])))]),ExtSDef("fetch_elem_1_0",[VarDec("q_63",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[])])])
\ No newline at end of file
module test73
imports libstratego-lib
strategies
test1 = ( rules( a : _ -> 1 ) /~["a"]\ rules( a : _ -> 0 ) ) ; a
test2 = ( rules( b : _ -> 1 ) /~["b"]\ rules( b : _ -> 1 ) ) ; b
test3 = ( rules( c : _ -> 1 ) /c\ rules( c : _ -> 0 ) ) ; c
test4 = ( rules( d : _ -> 1 ) /d\ rules( d : _ -> 1 ) ) ; d
strategies
main =
test-suite(!"intersection with rulenames",
test-it-now
)
test-it-now =
apply-and-fail(!"intersection with rulenames 1",test1,![])
; apply-test(!"intersection with rulenames 2",test2,![],!1)
; apply-and-fail(!"normal intersection 1",test3,![])
; apply-test(!"normal intersection 2",test4,![],!1)
......@@ -84,7 +84,7 @@ impl<'a> MutContext<'a> {
pub fn get_term(&self, term_name: &str) -> Result<ATermRef> {
self.get_term_option(term_name)
.and_then(|o| o.ok_or(Error::UninitializedVariable))
.and_then(|o| o.ok_or(Error::UninitializedVariable(String::from(term_name))))
}
pub fn match_term(&self, term_name: &str, current: &ATermRef) -> Result<()> {
......@@ -211,6 +211,18 @@ impl<ATerm> Scope<ATerm> {
}
}
pub fn from_let_defs<I>(no_of_scopes: usize, defs: I) -> Scope<ATerm>
where I: IntoIterator<Item = StrategyDef>
{
Scope {
term: HashMap::default(),
strategy: defs.into_iter()
.map(|sdeft| (sdeft.name(), sdeft.set_scope_on_let_sdef(no_of_scopes)))
.collect(),
overlay: false,
}
}
pub fn from_fresh_variables<I>(fresh_vars: I) -> Scope<ATerm>
where I: IntoIterator<Item = String>
{
......
......@@ -16,7 +16,7 @@ pub enum Error {
UndefinedStrategy(String),
UndefinedVariable(String),
UndefinedPrimitive(String),
UninitializedVariable,
UninitializedVariable(String),
StrategyFailed,
UnknownBehaviour(&'static str),
InterpreterExit(i32),
......@@ -32,7 +32,7 @@ impl<'t> error::Error for Error {
Error::UndefinedStrategy(_) => "couldn't find strategy",
Error::UndefinedVariable(_) => "couldn't find variable",
Error::UndefinedPrimitive(_) => "couldn't find primitive",
Error::UninitializedVariable => "variable was used but not bound",
Error::UninitializedVariable(_) => "variable was used but not bound",
Error::StrategyFailed => "strategy failed",
Error::UnknownBehaviour(_) => "unknown behaviour",
Error::InterpreterExit(_) => "interpreter exit",
......@@ -48,7 +48,7 @@ impl<'t> error::Error for Error {
Error::UndefinedStrategy(_) |
Error::UndefinedVariable(_) |
Error::UndefinedPrimitive(_) |
Error::UninitializedVariable |
Error::UninitializedVariable(_) |
Error::StrategyFailed |
Error::UnknownBehaviour(_) |
Error::InterpreterExit(_) => None,
......@@ -66,7 +66,7 @@ impl<'t> fmt::Display for Error {
Error::UndefinedStrategy(ref s) => write!(f, "UndefinedStrategyError: {}", s),
Error::UndefinedVariable(ref s) => write!(f, "UndefinedVariableError: {}", s),
Error::UndefinedPrimitive(ref s) => write!(f, "UndefinedPrimitiveError: {}", s),
Error::UninitializedVariable => write!(f, "Uninitialized variable"),
Error::UninitializedVariable(ref s) => write!(f, "Uninitialized variable: {}", s),
Error::StrategyFailed => write!(f, "Strategy failed"),
Error::UnknownBehaviour(s) => write!(f, "Unknown behaviour for {}", s),
Error::InterpreterExit(i) => write!(f, "Interpreter exited with code {}", i),
......
......@@ -135,8 +135,9 @@ impl<'a> Eval<'a> for preprocess::Strategy {
use context;
match *self {
Let(ref defs, ref body) => {
// TODO: Figure out if sdefts in let need to save a scopes-stack pointer thingy
context.push_scope(context::Scope::from_defs(defs.clone()));
// note the + 1, because lets allow recursive definitions
let no_of_scopes = context.scopes.borrow().len() + 1;
context.push_scope(context::Scope::from_let_defs(no_of_scopes, defs.clone()));
// Don't unwind scopes stack when fatal error occurred? Then again, why make such a
// failure case faster at the cost of many extra branches for the happy path...
let result = body.eval(context, current);
......@@ -144,14 +145,17 @@ impl<'a> Eval<'a> for preprocess::Strategy {
result
}
CallT(ref name, ref sargs, ref targs) => {
println!("Debug. CallT of {}", name);
let sdeft = context.get_strategy(name)?;
let no_of_scopes = context.scopes.borrow().len();
if "z_1480" == name {
println!("Debug. {:?}", sdeft);
}
let sargs = sargs
.into_iter()
.cloned()
.enumerate()
.map(|(n, s)| {
StrategyDef::from_strategy(context, name.clone(), n, s, no_of_scopes)
StrategyDef::from_strategy(context, name.clone(), n, s)
})
.collect::<Result<_>>()?;
let targs = targs
......@@ -162,7 +166,6 @@ impl<'a> Eval<'a> for preprocess::Strategy {
}
CallDynamic(ref term_name, ref sargs, ref targs) => {
let strategy_name = context.get_term(term_name)?;
let no_of_scopes = context.scopes.borrow().len();
let strategy_name = b(&strategy_name)
.get_string()
.ok_or_else(|| {
......@@ -179,8 +182,7 @@ impl<'a> Eval<'a> for preprocess::Strategy {
StrategyDef::from_strategy(context,
strategy_name.clone(),
n,
s,
no_of_scopes)
s)
})
.collect::<Result<_>>()?;
let targs = targs
......@@ -221,13 +223,12 @@ impl<'a> Eval<'a> for preprocess::Strategy {
}
}
PrimT(ref name, ref sargs, ref targs) => {
let no_of_scopes = context.scopes.borrow().len();
let sargs = sargs
.into_iter()
.cloned()
.enumerate()
.map(|(n, s)| {
StrategyDef::from_strategy(context, name.clone(), n, s, no_of_scopes)
StrategyDef::from_strategy(context, name.clone(), n, s)
})
.collect::<Result<_>>()?;
let targs = targs
......
......@@ -45,6 +45,7 @@ pub enum StrategyDef {
sargs: Vec<String>,
targs: Vec<String>,
body: Strategy,
no_of_scopes: Option<usize>,
},
Anonymous {
argument_of: String,
......@@ -67,9 +68,27 @@ impl StrategyDef {
ref sargs,
ref targs,
ref body,
no_of_scopes,
} => {
if actual_sargs.len() != sargs.len() || actual_targs.len() != targs.len() {
Err(Error::UndefinedStrategy(name.clone()))
} else if let Some(no_of_scopes) = no_of_scopes {
context.stack_tracer.borrow_mut().push(name.clone());
let popped_scopes = context.scopes.borrow_mut().split_off(no_of_scopes);
context.push_scope(Scope::new(targs.iter().cloned().zip(actual_targs),
sargs.iter().cloned().zip(actual_sargs)));
let result = body.eval(context, current);
context.pop_scope();
context
.scopes
.borrow_mut()
.extend_from_slice(&popped_scopes);
if result.is_ok() {
context.stack_tracer.borrow_mut().pop_on_success();
} else {
context.stack_tracer.borrow_mut().pop_on_failure();
}
result
} else {
context.stack_tracer.borrow_mut().push(name.clone());
context.push_scope(Scope::new(targs.iter().cloned().zip(actual_targs),
......@@ -114,9 +133,9 @@ impl StrategyDef {
pub fn from_strategy(context: &MutContext,
name: String,
number: usize,
body: Strategy,
no_of_scopes: usize)
body: Strategy)
-> Result<Self> {
let no_of_scopes = context.scopes.borrow().len();
// NOTE: this is not an optimisation but a necessary semantic distinction to allow
// strategies that take arguments to be passed as strategy arguments!
match body {
......@@ -135,6 +154,22 @@ impl StrategyDef {
}
}
pub fn set_scope_on_let_sdef(self, no_of_scopes: usize) -> Self {
match self {
StrategyDef::Predefined { name, sargs, targs, body, no_of_scopes: None } => {
StrategyDef::Predefined {
name: name,
sargs: sargs,
targs: targs,
body: body,
no_of_scopes: Some(no_of_scopes),
}
}
_ => panic!("set_scope_on_let_sdef called on already scoped predefined strategy or \
anonymous strategy")
}
}
pub fn name(&self) -> String {
match *self {
StrategyDef::Predefined { ref name, .. } => name.clone(),
......@@ -160,6 +195,7 @@ impl TryFrom<ctree::Def> for StrategyDef {
sargs: sargs.into_iter().map(|vardec| vardec.0).collect(),
targs: targs.into_iter().map(|vardec| vardec.0).collect(),
body: body,
no_of_scopes: None,
}
})
}
......
......@@ -142,6 +142,8 @@ pub fn indexedSet_getIndex<'a>(context: &MutContext<'a>,
Ok(context.factory.int(result as i32))
}
// TODO: libstratego-lib uses a primitive SSL_indexedSet_getElem, which does not exist in the Java implementation!
pub fn indexedSet_elements<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment