Search in sources :

Example 1 with Catch

use of ast.Expression.Catch in project L42 by ElvisResearchGroup.

the class DesugarW method castT.

private VarDecXE castT(Position pos, Type t, String y, String x) {
    assert t instanceof Type;
    Type nt = t;
    String z = Functions.freshName("casted", usedVars);
    List<Catch> ks = new ArrayList<>();
    Type t2 = new Type(nt.getMdf(), Path.Any(), Doc.empty());
    ks.add(new //case return captured
    Expression.Catch1(//case return captured
    pos, //case return captured
    SignalKind.Return, //case return captured
    t, //case return captured
    z, //return it
    new X(pos, z)));
    ks.add(new //else
    Expression.Catch1(//else
    pos, //else
    SignalKind.Return, //else
    t2, //else
    z, // exception void
    new Signal(SignalKind.Exception, Expression._void.instance)));
    RoundBlock block = Desugar.getBlock(pos, new Signal(SignalKind.Return, new X(pos, x)), ks, Desugar.errorMsg("CastT-Should be unreachable code"));
    return new VarDecXE(false, Optional.of(t), y, block);
}
Also used : Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) Signal(ast.Expression.Signal) Expression(ast.Expression) Catch(ast.Expression.Catch) ArrayList(java.util.ArrayList) X(ast.Expression.X) RoundBlock(ast.Expression.RoundBlock) VarDecXE(ast.Ast.VarDecXE)

Example 2 with Catch

use of ast.Expression.Catch in project L42 by ElvisResearchGroup.

the class DesugarW method with_E_handleIs.

private Expression with_E_handleIs(Position pos, List<VarDecXE> is, Expression b) {
    //case e in 6 pages
    //xs is dom(Is)
    List<String> xs = new ArrayList<>();
    for (VarDecXE i : is) {
        xs.add(i.getX());
    }
    //di ki is block content =nexti(xs)
    List<BlockContent> bc = new ArrayList<>();
    for (int i = 0; i < xs.size(); i++) {
        bc.add(withNext(pos, i, xs));
    }
    //inner =di ki s b[xs:=xs.#inner()]
    for (String xi : xs) {
        b = XInE.of(new X(pos, xi), Desugar.getMCall(pos, new X(pos, xi), "#inner", Desugar.getPs()), b);
    }
    RoundBlock inner = new RoundBlock(pos, Doc.empty(), b, bc);
    Catch k = Desugar.getK(pos, SignalKind.Exception, "", Type.immVoid, Expression._void.instance);
    inner = Desugar.getBlock(pos, new Loop(inner), Collections.singletonList(k), Expression._void.instance);
    Expression result = withDeclareIts(is, inner);
    //accept
    return result;
}
Also used : Loop(ast.Expression.Loop) Expression(ast.Expression) Catch(ast.Expression.Catch) ArrayList(java.util.ArrayList) BlockContent(ast.Expression.BlockContent) X(ast.Expression.X) RoundBlock(ast.Expression.RoundBlock) VarDecXE(ast.Ast.VarDecXE)

Example 3 with Catch

use of ast.Expression.Catch in project L42 by ElvisResearchGroup.

the class DesugarCatchDefault method liftBC.

protected BlockContent liftBC(BlockContent c) {
    if (c.get_catch().isEmpty()) {
        return super.liftBC(c);
    }
    List<Catch> ks = Map.of(this::liftK, c.get_catch());
    Type oldR = this.lastReturn;
    this.lastReturn = newR(ks);
    try {
        List<VarDec> liftVarDecs = liftVarDecs(c.getDecs());
        return new BlockContent(liftVarDecs, ks);
    } finally {
        this.lastReturn = oldR;
    }
}
Also used : Type(ast.Ast.Type) Catch(ast.Expression.Catch) VarDec(ast.Ast.VarDec) BlockContent(ast.Expression.BlockContent)

Example 4 with Catch

use of ast.Expression.Catch in project L42 by ElvisResearchGroup.

the class Desugar method liftKs.

protected List<Catch> liftKs(List<Catch> ks) {
    List<Catch> result = new ArrayList<>();
    String x = Functions.freshName("catched", usedVars);
    for (Catch k : ks) {
        if (k instanceof DesugarCatchDefault.CatchToComplete) {
            Catch k2 = this.liftK(((DesugarCatchDefault.CatchToComplete) k).catch1);
            result.add(new DesugarCatchDefault.CatchToComplete((Catch1) k2));
            continue;
        }
        k.match(k1 -> result.add(liftK(k1)), kM -> {
            for (Type t : kM.getTs()) {
                result.add(liftK(new Expression.Catch1(kM.getP(), kM.getKind(), t, x, kM.getInner())));
            }
            return false;
        }, kP -> {
            for (Type t : kP.getTs()) {
                Expression inner = kP.getInner();
                inner = new Expression.FCall(kP.getP(), inner, Doc.empty(), new ast.Ast.Parameters(Optional.of(new X(kP.getP(), x)), Collections.emptyList(), Collections.emptyList()));
                inner = new Expression.Signal(kP.getKind(), inner);
                result.add(liftK(new Expression.Catch1(kP.getP(), SignalKind.Exception, t, x, inner)));
            }
            return false;
        });
    }
    return result;
}
Also used : Parameters(ast.Ast.Parameters) Catch(ast.Expression.Catch) ArrayList(java.util.ArrayList) Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodType(ast.Ast.MethodType) Expression(ast.Expression) Catch1(ast.Expression.Catch1) FCall(ast.Expression.FCall) X(ast.Expression.X) MethodSelectorX(ast.Ast.MethodSelectorX)

Example 5 with Catch

use of ast.Expression.Catch in project L42 by ElvisResearchGroup.

the class DesugarW method with_C_A.

private Expression with_C_A(Position pos, List<String> xs, With.On on0, Expression continuation) {
    List<String> ys = new ArrayList<String>();
    for (String x : xs) {
        ys.add(Functions.freshName(x, usedVars));
    }
    //(
    List<VarDec> decs = new ArrayList<>();
    //TODO:?? now is very strange and requires the mdf to propagate it to the locally introduced var?
    //casts: every cast is a block content e+catch
    {
        int i = -1;
        for (Type ti : on0.getTs()) {
            i += 1;
            String xi = xs.get(i);
            String yi = ys.get(i);
            decs.add(castT(pos, ti, yi, xi));
        }
    }
    //catch exception Void recursive call
    Catch k = Desugar.getK(pos, SignalKind.Exception, "", Type.immVoid, continuation);
    //main expression with 'T' renaming
    Expression e0 = on0.getInner();
    {
        int i = -1;
        for (Type ti : on0.getTs()) {
            i += 1;
            String xi = xs.get(i);
            String yi = ys.get(i);
            e0 = renameT(e0, new Expression.X(pos, xi), ti, new Expression.X(pos, yi));
        }
    }
    BlockContent content = new BlockContent(decs, Collections.singletonList(k));
    List<BlockContent> contents = new ArrayList<BlockContent>();
    contents.add(content);
    contents.add(Desugar.getBlockContent(e0));
    //void)
    return new RoundBlock(pos, Doc.empty(), Expression._void.instance, contents);
}
Also used : Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) Expression(ast.Expression) Catch(ast.Expression.Catch) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) BlockContent(ast.Expression.BlockContent) RoundBlock(ast.Expression.RoundBlock) X(ast.Expression.X)

Aggregations

Catch (ast.Expression.Catch)8 Expression (ast.Expression)6 Type (ast.Ast.Type)5 BlockContent (ast.Expression.BlockContent)5 ArrayList (java.util.ArrayList)5 VarDec (ast.Ast.VarDec)4 X (ast.Expression.X)4 Doc (ast.Ast.Doc)3 MethodWithType (ast.Expression.ClassB.MethodWithType)3 RoundBlock (ast.Expression.RoundBlock)3 MethodType (ast.Ast.MethodType)2 VarDecXE (ast.Ast.VarDecXE)2 Ast (ast.Ast)1 MethodSelectorX (ast.Ast.MethodSelectorX)1 Parameters (ast.Ast.Parameters)1 SignalKind (ast.Ast.SignalKind)1 ExpCore (ast.ExpCore)1 Block (ast.ExpCore.Block)1 Catch1 (ast.Expression.Catch1)1 FCall (ast.Expression.FCall)1