Search in sources :

Example 16 with VarDec

use of ast.Ast.VarDec in project L42 by ElvisResearchGroup.

the class DesugarVars method findD2.

private int findD2(X x, int pos, List<VarDec> decs) {
    for (VarDec _dec : decs.subList(pos + 1, decs.size())) {
        pos += 1;
        Expression[] inner = { null };
        _dec.match(xe -> inner[0] = xe.getInner(), e -> inner[0] = e.getInner(), ce -> null);
        if (inner[0] == null) {
            continue;
        }
        boolean isAssigned = Exists.of(inner[0], e -> {
            if (!(e instanceof BinOp)) {
                return false;
            }
            BinOp bo = (BinOp) e;
            if (bo.getOp().kind != Ast.OpKind.EqOp) {
                return false;
            }
            return bo.getLeft().equals(x);
        });
        if (isAssigned) {
            return pos;
        }
    }
    return -1;
}
Also used : Expression(ast.Expression) VarDec(ast.Ast.VarDec) BinOp(ast.Expression.BinOp)

Example 17 with VarDec

use of ast.Ast.VarDec in project L42 by ElvisResearchGroup.

the class DesugarVars method visit.

public Expression visit(CurlyBlock s) {
    Expression inner;
    if (s.getContents().size() == 1 && s.getContents().get(0).get_catch().isEmpty() && s.getContents().get(0).getDecs().size() == 1 && s.getContents().get(0).getDecs().get(0) instanceof VarDecE) {
        inner = ((VarDecE) s.getContents().get(0).getDecs().get(0)).getInner();
    } else {
        inner = new RoundBlock(s.getP(), s.getDoc(), Expression._void.instance, s.getContents());
    }
    inner = inner.accept(this);
    List<VarDec> vd = Collections.singletonList((VarDec) new VarDecE(inner));
    BlockContent o = new BlockContent(vd, Collections.emptyList());
    return new CurlyBlock(s.getP(), Doc.empty(), Collections.singletonList(o));
}
Also used : Expression(ast.Expression) CurlyBlock(ast.Expression.CurlyBlock) VarDec(ast.Ast.VarDec) BlockContent(ast.Expression.BlockContent) RoundBlock(ast.Expression.RoundBlock) VarDecE(ast.Ast.VarDecE)

Example 18 with VarDec

use of ast.Ast.VarDec in project L42 by ElvisResearchGroup.

the class CheckTerminatingBlock method visit.

public Void visit(CurlyBlock s) {
    checkBlockContent(s.getContents());
    Expression.BlockContent bc = s.getContents().get(s.getContents().size() - 1);
    VarDec dec = bc.getDecs().get(bc.getDecs().size() - 1);
    if (!(dec instanceof Ast.VarDecE)) {
        return fail(s);
    }
    Ast.VarDecE decE = (Ast.VarDecE) dec;
    return decE.getInner().accept(this);
}
Also used : Ast(ast.Ast) Expression(ast.Expression) VarDec(ast.Ast.VarDec)

Example 19 with VarDec

use of ast.Ast.VarDec in project L42 by ElvisResearchGroup.

the class CheckVarUsedAreInScope method liftBC.

protected Expression.BlockContent liftBC(Expression.BlockContent c) {
    Set<String> aux = this.xs;
    Set<String> aux2 = new HashSet<String>(aux);
    for (VarDec vd : c.getDecs()) {
        vd.match(xe -> {
            aux2.add(xe.getX());
            return null;
        }, e -> {
            return null;
        }, ce -> {
            return null;
        });
    }
    this.xs = aux2;
    List<VarDec> liftVarDecs = liftVarDecs(c.getDecs());
    this.xs = aux;
    List<Catch> liftK;
    try {
        liftK = Map.of(this::liftK, c.get_catch());
    } catch (ErrorMessage.VariableUsedNotInScope nis) {
        Expression.X x = nis.getE();
        assert !aux.contains(x.getInner());
        if (aux2.contains(x.getInner())) {
            throw nis.withReason(nis.getReason() + "\nThe variable is in lexical scope, but is used in a catch of the declaring block. It may not be initialized at this stage");
        }
        throw nis;
    }
    //add again for later contents
    this.xs = aux2;
    return new Expression.BlockContent(liftVarDecs, liftK);
}
Also used : VarDec(ast.Ast.VarDec) ErrorMessage(ast.ErrorMessage) HashSet(java.util.HashSet)

Example 20 with VarDec

use of ast.Ast.VarDec 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

VarDec (ast.Ast.VarDec)25 ArrayList (java.util.ArrayList)17 Expression (ast.Expression)16 VarDecE (ast.Ast.VarDecE)10 VarDecXE (ast.Ast.VarDecXE)9 RoundBlock (ast.Expression.RoundBlock)9 BlockContent (ast.Expression.BlockContent)7 X (ast.Expression.X)6 Catch (ast.Expression.Catch)5 Type (ast.Ast.Type)4 Doc (ast.Ast.Doc)3 Parameters (ast.Ast.Parameters)3 MethodSelectorX (ast.Ast.MethodSelectorX)2 MethodType (ast.Ast.MethodType)2 MethodWithType (ast.Expression.ClassB.MethodWithType)2 Ast (ast.Ast)1 Position (ast.Ast.Position)1 VarDecCE (ast.Ast.VarDecCE)1 ErrorMessage (ast.ErrorMessage)1 BinOp (ast.Expression.BinOp)1