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()));
}
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));
}
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;
}
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);
}
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;
}
Aggregations