Search in sources :

Example 6 with X

use of ast.Expression.X 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 7 with X

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

the class Desugar method visit.

public Expression visit(BinOp s) {
    Op op = s.getOp();
    if (op == Op.ColonEqual) {
        //TODO: set the right expect type?
        return s.withRight(lift(s.getRight()));
    }
    if (op.kind == Ast.OpKind.EqOp) {
        //go from, for example a++=b into a:=a ++b
        Op op2 = Op.fromString(s.getOp().inner.substring(0, s.getOp().inner.length() - 1));
        BinOp s2 = s.withOp(op2);
        //NO!s2=s2.withLeft(getMCall(s.getP(),s.getLeft(),"#inner",getPs()));
        return visit(new BinOp(s.getP(), s.getLeft(), Op.ColonEqual, Doc.empty(), s2));
    }
    if (op.negated) {
        BinOp s2 = s.withOp(op.nonNegatedVersion());
        return visit(getMCall(s.getP(), s2, desugarName(Op.Bang.inner), getPs()));
    }
    if (op.normalized) {
        return visit(getMCall(s.getP(), s.getLeft(), desugarName(s.getOp().inner), getPs(s.getRight())));
    }
    String x = Functions.freshName("opNorm", usedVars);
    BinOp s2 = new BinOp(s.getP(), s.getRight(), op.normalizedVersion(), s.getDoc(), new Expression.X(s.getP(), x));
    return visit(getBlock(s.getP(), x, s.getLeft(), s2));
}
Also used : BinOp(ast.Expression.BinOp) Op(ast.Ast.Op) UnOp(ast.Expression.UnOp) Expression(ast.Expression) BinOp(ast.Expression.BinOp) X(ast.Expression.X)

Example 8 with X

use of ast.Expression.X 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 9 with X

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

the class Desugar method visit1Step.

public MCall visit1Step(Literal s) {
    //(b=r.builder() b.a() b.b() b.c() .... b)
    List<VarDec> vd = new ArrayList<>();
    Expression k = getMCall(s.getP(), s.getReceiver(), "#builder", getPs());
    String x = Functions.freshName("b", usedVars);
    X b = new X(s.getP(), x);
    vd.add(new VarDecXE(false, Optional.empty(), x, k));
    for (char ch : s.getInner().toCharArray()) {
        String name = Character.toString(ch);
        if (!Character.isAlphabetic(ch) && !Character.isDigit(ch)) {
            name = desugarSymbol(name);
        }
        vd.add(new VarDecE(getMCall(s.getP(), b, "#" + name, getPs())));
    }
    Expression inner = getBlock(s.getP(), vd, b);
    Parameters ps = new Parameters(Optional.empty(), Collections.singletonList("builder"), Collections.singletonList(inner));
    return getMCall(s.getP(), s.getReceiver(), "#from", ps);
}
Also used : Parameters(ast.Ast.Parameters) Expression(ast.Expression) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) X(ast.Expression.X) MethodSelectorX(ast.Ast.MethodSelectorX) VarDecE(ast.Ast.VarDecE) VarDecXE(ast.Ast.VarDecXE)

Example 10 with X

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

the class Desugar method visit.

public Expression visit(CurlyBlock s) {
    assert s.getContents().size() == 1;
    assert s.getContents().get(0).get_catch().isEmpty();
    assert s.getContents().get(0).getDecs().size() == 1;
    assert s.getContents().get(0).getDecs().get(0) instanceof VarDecE;
    Expression inner = ((VarDecE) s.getContents().get(0).getDecs().get(0)).getInner();
    String y = Functions.freshName("result", this.usedVars);
    Expression.Catch k = getK(s.getP(), SignalKind.Return, y, this.t, new X(s.getP(), y));
    Expression termination = Desugar.errorMsg("CurlyBlock-Should be unreachable code");
    RoundBlock outer = getBlock(s.getP(), inner, Collections.singletonList(k), termination);
    return visit(outer);
}
Also used : Expression(ast.Expression) X(ast.Expression.X) MethodSelectorX(ast.Ast.MethodSelectorX) RoundBlock(ast.Expression.RoundBlock) VarDecE(ast.Ast.VarDecE) Catch(ast.Expression.Catch)

Aggregations

Expression (ast.Expression)15 X (ast.Expression.X)15 ArrayList (java.util.ArrayList)10 VarDecXE (ast.Ast.VarDecXE)7 Catch (ast.Expression.Catch)7 MethodSelectorX (ast.Ast.MethodSelectorX)6 Type (ast.Ast.Type)6 VarDec (ast.Ast.VarDec)6 MethodWithType (ast.Expression.ClassB.MethodWithType)6 Parameters (ast.Ast.Parameters)5 VarDecE (ast.Ast.VarDecE)5 RoundBlock (ast.Expression.RoundBlock)5 BlockContent (ast.Expression.BlockContent)3 MCall (ast.Expression.MCall)3 Signal (ast.Expression.Signal)3 MethodType (ast.Ast.MethodType)2 Position (ast.Ast.Position)2 Catch1 (ast.Expression.Catch1)2 FieldDec (ast.Ast.FieldDec)1 MethodSelector (ast.Ast.MethodSelector)1