Commit 9eeca458 authored by Jeff Smits's avatar Jeff Smits

Added another example to show that strj just allows rebinding in <+ rhs, but...

Added another example to show that strj just allows rebinding in <+ rhs, but otherwise keeps the bindings from the failing lhs
parent 0ff4b12c
Specification([Signature([Constructors([ExtOpDecl("Nil",ConstType(Sort("List",[SortVar("a")])))])]),Strategies([SDefT("main_0_0",[],[],Scope(["c_0","d_0"],Seq(Match(Var("c_0")),Seq(Build(Anno(Str("Expected behaviour: failure"),Op("Nil",[]))),Seq(CallT(SVar("debug_0_0"),[],[]),Seq(Build(Var("c_0")),CallT(SVar("if_3_0"),[Seq(Match(Var("d_0")),Fail),Id,Seq(Build(Var("d_0")),CallT(SVar("debug_1_0"),[Build(Anno(Str("Well, it succeeded.. b was: "),Op("Nil",[])))],[]))],[]))))))),ExtSDef("debug_0_0",[],[]),ExtSDef("debug_1_0",[VarDec("r_13",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[]),ExtSDef("if_3_0",[VarDec("c_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("d_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("e_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[])])])
\ No newline at end of file
module bt_if
imports libstratego-lib
strategies
// EXPECTED BEHAVIOUR: program fails at runtime because of unbound b
// f always fails, but it binds b before that. In a guarded choice, the binding to b needs to be undone. But if you
// compile this program with -O 0 (no optimisations), f stays a separate strategy and becomes a closure inside the
// execution environment. Interaction between closures and backtracking can be trick, hence this "test".
main = where(<debug> "Expected behaviour: failure"); {b: let f = ?b; fail in if(f,id,!b;debug(!"Well, it succeeded.. b was: ")) end }
Specification([Signature([Constructors([ExtOpDecl("Nil",ConstType(Sort("List",[SortVar("a")])))])]),Strategies([SDefT("main_0_0",[],[],Scope(["c_0","d_0"],Seq(Match(Var("d_0")),Seq(CallT(SVar("if_3_0"),[Seq(Match(Var("c_0")),Fail),Id,Seq(Build(Var("c_0")),CallT(SVar("debug_1_0"),[Build(Anno(Str("hi"),Op("Nil",[])))],[]))],[]),Build(Var("d_0")))))),ExtSDef("debug_1_0",[VarDec("r_13",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[]),ExtSDef("if_3_0",[VarDec("c_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("d_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[])))),VarDec("e_23",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[])])])
\ No newline at end of file
module bt_if2
imports libstratego-lib
strategies
main = where(let g = ?b; fail in if(g,id,!b; debug(!"hi")) end)
Specification([Signature([Constructors([ExtOpDecl("Nil",ConstType(Sort("List",[SortVar("a")])))])]),Strategies([SDefT("main_0_0",[],[],Scope(["b_0","c_0"],Seq(GuardedLChoice(Seq(Build(Anno(Int("1"),Op("Nil",[]))),Seq(Match(Var("b_0")),Seq(Match(Var("c_0")),Fail))),Id,Seq(Build(Anno(Int("2"),Op("Nil",[]))),Match(Var("b_0")))),Seq(Build(Var("b_0")),Seq(CallT(SVar("debug_1_0"),[Build(Anno(Str("success. a is now: "),Op("Nil",[])))],[]),Seq(Build(Var("c_0")),CallT(SVar("debug_1_0"),[Build(Anno(Str("c is bound to: "),Op("Nil",[])))],[]))))))),ExtSDef("debug_1_0",[VarDec("p_13",FunType([ConstType(Sort("ATerm",[]))],ConstType(Sort("ATerm",[]))))],[])])])
\ No newline at end of file
import org.strategoxt.stratego_lib.*;
import org.strategoxt.lang.*;
import org.spoofax.interpreter.terms.*;
import static org.strategoxt.lang.Term.*;
import org.spoofax.interpreter.library.AbstractPrimitive;
import java.util.ArrayList;
import java.lang.ref.WeakReference;
@SuppressWarnings("all") public class choice_success2
{
protected static final boolean TRACES_ENABLED = true;
protected static ITermFactory constantFactory;
private static WeakReference<Context> initedContext;
private static boolean isIniting;
protected static IStrategoTerm const3;
protected static IStrategoTerm const2;
protected static IStrategoTerm const1;
protected static IStrategoTerm const0;
public static IStrategoConstructor _consConc_2;
public static IStrategoConstructor _consNone_0;
public static IStrategoConstructor _consSome_1;
public static Context init(Context context)
{
synchronized(choice_success2.class)
{
if(isIniting)
return null;
try
{
isIniting = true;
ITermFactory termFactory = context.getFactory();
if(constantFactory == null)
{
initConstructors(termFactory);
initConstants(termFactory);
}
if(initedContext == null || initedContext.get() != context)
{
org.strategoxt.stratego_lib.Main.init(context);
context.registerComponent("choice_success2");
}
initedContext = new WeakReference<Context>(context);
constantFactory = termFactory;
}
finally
{
isIniting = false;
}
return context;
}
}
public static Context init()
{
return init(new Context());
}
public static void main(String args[])
{
Context context = init();
context.setStandAlone(true);
try
{
IStrategoTerm result;
try
{
result = context.invokeStrategyCLI(main_0_0.instance, "choice_success2", args);
}
finally
{
context.getIOAgent().closeAllFiles();
}
if(result == null)
{
System.err.println("choice_success2" + (TRACES_ENABLED ? ": rewriting failed, trace:" : ": rewriting failed"));
context.printStackTrace();
context.setStandAlone(false);
System.exit(1);
}
else
{
System.out.println(result);
context.setStandAlone(false);
System.exit(0);
}
}
catch(StrategoErrorExit exit)
{
context.setStandAlone(false);
System.err.println(exit.getLocalizedMessage());
System.exit(exit.getValue());
}
catch(StrategoExit exit)
{
context.setStandAlone(false);
System.exit(exit.getValue());
}
}
public static IStrategoTerm mainNoExit(String ... args) throws StrategoExit
{
return mainNoExit(new Context(), args);
}
public static IStrategoTerm mainNoExit(Context context, String ... args) throws StrategoExit
{
try
{
init(context);
return context.invokeStrategyCLI(main_0_0.instance, "choice_success2", args);
}
finally
{
context.getIOAgent().closeAllFiles();
}
}
public static Strategy getMainStrategy()
{
return main_0_0.instance;
}
public static void initConstructors(ITermFactory termFactory)
{
_consConc_2 = termFactory.makeConstructor("Conc", 2);
_consNone_0 = termFactory.makeConstructor("None", 0);
_consSome_1 = termFactory.makeConstructor("Some", 1);
}
public static void initConstants(ITermFactory termFactory)
{
const0 = termFactory.makeString("c is bound to: ");
const1 = termFactory.makeInt(1);
const2 = termFactory.makeInt(2);
const3 = termFactory.makeString("success. a is now: ");
}
@SuppressWarnings("all") public static class main_0_0 extends Strategy
{
public static main_0_0 instance = new main_0_0();
@Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
{
context.push("main_0_0");
Fail0:
{
IStrategoTerm b_0 = null;
IStrategoTerm c_0 = null;
Success0:
{
Fail1:
{
term = choice_success2.const1;
b_0 = choice_success2.const1;
c_0 = term;
if(true)
break Fail1;
if(true)
break Success0;
}
term = choice_success2.const2;
b_0 = choice_success2.const2;
}
term = debug_1_0.instance.invoke(context, b_0, lifted0.instance);
if(term == null)
break Fail0;
if(c_0 == null)
break Fail0;
term = debug_1_0.instance.invoke(context, c_0, lifted1.instance);
if(term == null)
break Fail0;
context.popOnSuccess();
if(true)
return term;
}
context.popOnFailure();
return null;
}
}
@SuppressWarnings("all") private static final class lifted0 extends Strategy
{
public static final lifted0 instance = new lifted0();
@Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
{
Fail2:
{
term = choice_success2.const3;
if(true)
return term;
}
return null;
}
}
@SuppressWarnings("all") private static final class lifted1 extends Strategy
{
public static final lifted1 instance = new lifted1();
@Override public IStrategoTerm invoke(Context context, IStrategoTerm term)
{
Fail3:
{
term = choice_success2.const0;
if(true)
return term;
}
return null;
}
}
public static void registerInterop(org.spoofax.interpreter.core.IContext context, Context compiledContext)
{
new InteropRegisterer().registerLazy(context, compiledContext, InteropRegisterer.class.getClassLoader());
}
@SuppressWarnings("unused") public static class InteropRegisterer extends org.strategoxt.lang.InteropRegisterer
{
@Override public void register(org.spoofax.interpreter.core.IContext context, Context compiledContext)
{
register(context, compiledContext, context.getVarScope());
}
@Override public void registerLazy(org.spoofax.interpreter.core.IContext context, Context compiledContext, ClassLoader classLoader)
{
registerLazy(context, compiledContext, classLoader, context.getVarScope());
}
private void register(org.spoofax.interpreter.core.IContext context, Context compiledContext, org.spoofax.interpreter.core.VarScope varScope)
{
compiledContext.registerComponent("choice_success2");
choice_success2.init(compiledContext);
varScope.addSVar("main_0_0", new InteropSDefT(main_0_0.instance, context));
}
private void registerLazy(org.spoofax.interpreter.core.IContext context, Context compiledContext, ClassLoader classLoader, org.spoofax.interpreter.core.VarScope varScope)
{
compiledContext.registerComponent("choice_success2");
choice_success2.init(compiledContext);
varScope.addSVar("main_0_0", new InteropSDefT(classLoader, "choice_success2$main_0_0", context));
}
}
}
\ No newline at end of file
module choice_success2
imports libstratego-lib
strategies
main = (!1; ?a; ?c; fail <+ !2; ?a); !a; debug(!"success. a is now: "); !c; debug(!"c is bound to: ")
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