Search in sources :

Example 1 with Block

use of ast.ExpCore.Block in project L42 by ElvisResearchGroup.

the class Executor method subst.

public static ExpCore subst(ExpCore ctxVal, Redex.Subst r) {
    Block e1 = r.getThat();
    int i = r.getSubstIndex();
    ExpCore val = e1.getDecs().get(i).getInner();
    String x = e1.getDecs().get(i).getX();
    ArrayList<Block.Dec> decs = new ArrayList<Block.Dec>(e1.getDecs());
    decs.remove(i);
    Block e2 = new Block(e1.getDoc(), decs, e1.getInner(), e1.getOns(), e1.getP());
    ExpCore result = ReplaceX.of(e2, val, x);
    return ReplaceCtx.of(ctxVal, result);
}
Also used : ExpCore(ast.ExpCore) Dec(ast.ExpCore.Block.Dec) ArrayList(java.util.ArrayList) Block(ast.ExpCore.Block)

Example 2 with Block

use of ast.ExpCore.Block in project L42 by ElvisResearchGroup.

the class RenameVars method visit.

public ExpCore visit(Block s) {
    List<On> k = Map.of(this::liftO, s.getOns());
    List<On> newK = new ArrayList<>();
    for (On on : k) {
        String altK = toRename.get(on.getX());
        if (altK != null) {
            newK.add(on.withX(altK));
        } else {
            newK.add(on);
        }
    }
    List<Block.Dec> decs = new ArrayList<>();
    for (Block.Dec dec : s.getDecs()) {
        String altxi = toRename.get(dec.getX());
        if (altxi == null) {
            decs.add(dec);
        } else {
            decs.add(dec.withX(altxi));
        }
    }
    return new Block(s.getDoc(), Map.of(this::liftDec, decs), lift(s.getInner()), newK, s.getP());
}
Also used : ArrayList(java.util.ArrayList) Block(ast.ExpCore.Block) On(ast.ExpCore.Block.On)

Example 3 with Block

use of ast.ExpCore.Block in project L42 by ElvisResearchGroup.

the class ExtractCtxVal method visit.

public Ctx<Redex> visit(Block s) {
    Ctx<Redex> res = checkRedex(s);
    if (res != null) {
        return res;
    }
    for (int i = 0; i < s.getDecs().size(); i++) {
        //final restrictions
        int ii = i;
        Dec di = s.getDecs().get(i);
        boolean isDv = new IsValue(p).validDv(di);
        Redex isGarbage = null;
        if (isDv && di.getInner() instanceof Block) {
            Block diB = (Block) di.getInner();
            isGarbage = IsValue.nestedGarbage(diB);
        }
        if (isGarbage != null) {
            List<Block.Dec> ds = new ArrayList<Block.Dec>(s.getDecs());
            ds.set(ii, di.withInner(new WalkBy()));
            return new Ctx<Redex>(s.withDecs(ds), isGarbage);
        }
        if (isDv) {
            continue;
        }
        //otherwise, i is the first non dv
        return lift(di.getInner().accept(this), ctx -> {
            List<Block.Dec> es = new ArrayList<Block.Dec>(s.getDecs());
            es.set(ii, es.get(ii).withInner(ctx));
            return s.withDecs(es);
        });
    }
    if (!s.getOns().isEmpty()) {
        return null;
    }
    return lift(s.getInner().accept(this), ctx -> s.withInner(ctx));
}
Also used : Dec(ast.ExpCore.Block.Dec) Redex(ast.Redex) Ctx(auxiliaryGrammar.Ctx) ArrayList(java.util.ArrayList) Block(ast.ExpCore.Block) WalkBy(ast.ExpCore.WalkBy)

Example 4 with Block

use of ast.ExpCore.Block in project L42 by ElvisResearchGroup.

the class ExtractThrow method visit.

public ExpCore visit(Block s) {
    if (!s.getOns().isEmpty()) {
        return new ExpCore.WalkBy();
    }
    for (int i = 0; i < s.getDecs().size(); i++) {
        if (new IsValue(p).validDv(s.getDecs().get(i))) {
            continue;
        }
        //otherwise, i is the first non dv
        ExpCore res = s.getDecs().get(i).getInner().accept(this);
        if (res instanceof ExpCore.WalkBy) {
            return res;
        }
        Signal res_ = (Signal) res;
        List<Block.Dec> decs = s.getDecs().subList(0, i);
        Block newInner = new Block(s.getDoc(), decs, res_.getInner(), Collections.emptyList(), s.getP());
        newInner = Functions.garbage(newInner, i);
        return res_.withInner(newInner);
    }
    ExpCore res = s.getInner().accept(this);
    if (res instanceof ExpCore.WalkBy) {
        return res;
    }
    Signal res_ = (Signal) res;
    Block newInner = s.withInner(res_.getInner());
    return res_.withInner(newInner);
}
Also used : ExpCore(ast.ExpCore) Signal(ast.ExpCore.Signal) Block(ast.ExpCore.Block) WalkBy(ast.ExpCore.WalkBy)

Example 5 with Block

use of ast.ExpCore.Block in project L42 by ElvisResearchGroup.

the class HB method visit.

@Override
public ExpCore visit(Block s) {
    ExpCore result = null;
    for (Block.Dec di : s.getDecs()) {
        result = acc(result, di.getInner().accept(this));
    }
    result = acc(result, s.getInner().accept(this));
    if (result == null && onlyAroundHole) {
        return null;
    }
    assert !onlyAroundHole || result instanceof WalkBy : onlyAroundHole + " " + result;
    for (Block.Dec dec : s.getDecs()) {
        if (xs.contains(dec.getX())) {
            throw new ErrorMessage.VariableDeclaredMultipleTimes(dec.getX(), s.getP());
        }
        this.xs.add(dec.getX());
        this.xsRes.add(dec.getX());
    }
    try {
        if (!onlyAroundHole && !s.getOns().isEmpty()) {
            for (On on : s.getOns()) {
                if (xs.contains(on.getX())) {
                    throw new ErrorMessage.VariableDeclaredMultipleTimes(on.getX(), s.getP());
                }
                xs.add(on.getX());
                xsRes.add(on.getX());
                on.getInner().accept(this);
                xs.remove(on.getX());
            }
        }
        return result;
    } finally {
        for (Block.Dec dec : s.getDecs()) {
            this.xs.remove(dec.getX());
        }
    }
}
Also used : ExpCore(ast.ExpCore) Block(ast.ExpCore.Block) WalkBy(ast.ExpCore.WalkBy) On(ast.ExpCore.Block.On)

Aggregations

Block (ast.ExpCore.Block)11 ArrayList (java.util.ArrayList)7 ExpCore (ast.ExpCore)6 Dec (ast.ExpCore.Block.Dec)5 On (ast.ExpCore.Block.On)5 Type (ast.Ast.Type)4 WalkBy (ast.ExpCore.WalkBy)4 MethodType (ast.Ast.MethodType)2 MethodWithType (ast.ExpCore.ClassB.MethodWithType)2 Signal (ast.ExpCore.Signal)2 Ctx (auxiliaryGrammar.Ctx)2 Ast (ast.Ast)1 Doc (ast.Ast.Doc)1 Mdf (ast.Ast.Mdf)1 SignalKind (ast.Ast.SignalKind)1 MCall (ast.ExpCore.MCall)1 Using (ast.ExpCore.Using)1 Expression (ast.Expression)1 Catch (ast.Expression.Catch)1 Redex (ast.Redex)1