Search in sources :

Example 1 with Dec

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

the class IsValue method nestedGarbage.

public static Redex.Garbage nestedGarbage(Block b) {
    //b is a right value!
    Block b2 = Functions.garbage(b, b.getDecs().size());
    if (!b2.equals(b)) {
        return new Redex.Garbage(b2);
    }
    //else, try nested!
    {
        int i = -1;
        for (Dec di : b.getDecs()) {
            i += 1;
            if (!(di.getInner() instanceof Block)) {
                continue;
            }
            Block bi = (Block) di.getInner();
            Redex.Garbage ngi = nestedGarbage(bi);
            if (ngi == null) {
                continue;
            }
            List<Block.Dec> ds = new ArrayList<Block.Dec>(b.getDecs());
            ds.set(i, di.withInner(ngi.getThatLessGarbage()));
            return new Redex.Garbage(b.withDecs(ds));
        }
    }
    if (!(b.getInner() instanceof Block)) {
        return null;
    }
    Block bIn = (Block) b.getInner();
    Redex.Garbage ngIn = nestedGarbage(bIn);
    if (ngIn == null) {
        return null;
    }
    return new Redex.Garbage(b.withInner(ngIn.getThatLessGarbage()));
}
Also used : Garbage(ast.Redex.Garbage) Dec(ast.ExpCore.Block.Dec) Redex(ast.Redex) Garbage(ast.Redex.Garbage) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Dec

use of ast.ExpCore.Block.Dec 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 3 with Dec

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

the class GuessTypeCore method guessedDs.

public static List<Dec> guessedDs(Program p, G in, List<Dec> toGuess) {
    //G'
    List<Dec> res = new ArrayList<>();
    for (Dec di : toGuess) {
        if (di.getT().isPresent()) {
            res.add(di);
            continue;
        }
        Type nti = _guessDecType(p, in, di);
        assert nti != null;
        res.add(di.withT(Optional.of(nti)));
    }
    return res;
}
Also used : Type(ast.Ast.Type) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Dec(ast.ExpCore.Block.Dec) ArrayList(java.util.ArrayList)

Example 4 with Dec

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

the class TsBlock method dsType.

default default TOutDs dsType(TIn in, List<Dec> _ds) {
    if (_ds.isEmpty()) {
        return new TOkDs(Tr.instance, _ds, G.instance.addGG(in));
    }
    int i = splitDs(in, _ds);
    assert i + 1 <= _ds.size();
    List<Dec> ds = _ds.subList(i + 1, _ds.size());
    //G'
    List<Dec> ds0n = GuessTypeCore.guessedDs(in.p, in, _ds.subList(0, i + 1));
    List<String> fve0n = new ArrayList<>();
    for (Dec di : _ds.subList(0, i + 1)) {
        fve0n.addAll(FreeVariables.of(di.getInner()));
    }
    assert !fve0n.stream().anyMatch(x -> ds.stream().anyMatch(d -> d.getX().equals(x)));
    List<Dec> dsFiltered = ds0n.stream().filter(d -> {
        Mdf m = d.getT().get().getMdf();
        return m == Mdf.Immutable || m == Mdf.Mutable;
    }).map(d -> d.withVar(false).withT(Optional.of(TypeManipulation.fwd(d.getT().get())))).collect(Collectors.toList());
    //G1
    TIn in1 = in.addGds(in.p, dsFiltered);
    Tr trAcc = Tr.instance;
    List<Dec> ds1 = new ArrayList<>();
    List<Dec> ds1FwdP = new ArrayList<>();
    for (Dec di : ds0n) {
        Type nt = di.getT().get();
        Type ntFwdP = TypeManipulation.fwdP(nt);
        TOut _out = type(in1.withE(di.getInner(), ntFwdP));
        if (!_out.isOk()) {
            return _out.toError();
        }
        TOk ok = _out.toOk();
        trAcc = trAcc.trUnion(ok);
        Dec di1 = di.withInner(ok.annotated);
        if (TypeManipulation.fwd_or_fwdP_in(nt.getMdf())) {
            //building G2
            ds1.add(di1.withT(Optional.of(ok.computed)));
        } else {
            ds1.add(di1.withT(Optional.of(TypeManipulation.noFwd(ok.computed))));
        }
        ds1FwdP.add(di1.withVar(false).withT(Optional.of(TypeManipulation.fwdP(ok.computed))));
    }
    if (TypeManipulation.fwd_or_fwdP_in(trAcc.returns)) {
        boolean xInCommon = fve0n.stream().anyMatch(x -> ds0n.stream().anyMatch(d -> d.getX().equals(x)));
        if (xInCommon) {
            return new TErr(in, "", null, ErrorKind.AttemptReturnFwd);
        }
    }
    List<Type> _nts = new ArrayList<>();
    for (String x : fve0n) {
        Type t = in._g(x);
        if (t != null) {
            _nts.add(t);
        }
    }
    TIn inG0;
    if (TypeManipulation.fwd_or_fwdP_in(_nts)) {
        inG0 = in.addGds(in.p, ds1FwdP);
    } else {
        inG0 = in.addGds(in.p, ds1);
    }
    TOutDs _res = dsType(inG0, ds);
    if (!_res.isOk()) {
        return _res.toError();
    }
    TOkDs res = _res.toOkDs();
    //safe? locally created, not leaked yet.
    ds1.addAll(res.ds);
    if (res.trAcc != null) {
        trAcc = trAcc.trUnion(res.trAcc);
    }
    return new TOkDs(trAcc, ds1, res.g);
}
Also used : On(ast.ExpCore.Block.On) Path(ast.Ast.Path) Map(tools.Map) Doc(ast.Ast.Doc) Type(ast.Ast.Type) Set(java.util.Set) FreeVariables(coreVisitors.FreeVariables) Assertions(tools.Assertions) ExpCore(ast.ExpCore) Collectors(java.util.stream.Collectors) Block(ast.ExpCore.Block) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) SignalKind(ast.Ast.SignalKind) Optional(java.util.Optional) Collector(java.util.stream.Collector) Dec(ast.ExpCore.Block.Dec) Norm(programReduction.Norm) Mdf(ast.Ast.Mdf) Dec(ast.ExpCore.Block.Dec) ArrayList(java.util.ArrayList) Mdf(ast.Ast.Mdf) Type(ast.Ast.Type)

Example 5 with Dec

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

the class NormalizeBlocks method isTxEx.

private Dec isTxEx(Block s) {
    if (!s.getOns().isEmpty()) {
        return null;
    }
    if (s.getDecs().size() != 1) {
        return null;
    }
    Dec dec = s.getDecs().get(0);
    if (!(s.getInner() instanceof ExpCore.X)) {
        return null;
    }
    String x = ((ExpCore.X) s.getInner()).getInner();
    if (!x.equals(dec.getX())) {
        return null;
    }
    return dec;
}
Also used : Dec(ast.ExpCore.Block.Dec)

Aggregations

Dec (ast.ExpCore.Block.Dec)10 ArrayList (java.util.ArrayList)7 Type (ast.Ast.Type)4 Block (ast.ExpCore.Block)4 On (ast.ExpCore.Block.On)3 MethodWithType (ast.ExpCore.ClassB.MethodWithType)3 List (java.util.List)3 ExpCore (ast.ExpCore)2 Redex (ast.Redex)2 HashSet (java.util.HashSet)2 Doc (ast.Ast.Doc)1 Mdf (ast.Ast.Mdf)1 MethodType (ast.Ast.MethodType)1 Path (ast.Ast.Path)1 SignalKind (ast.Ast.SignalKind)1 MCall (ast.ExpCore.MCall)1 Signal (ast.ExpCore.Signal)1 Using (ast.ExpCore.Using)1 WalkBy (ast.ExpCore.WalkBy)1 Garbage (ast.Redex.Garbage)1