Search in sources :

Example 11 with VarDec

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

the class InjectionOnSugar method visit.

@Override
public Expression visit(Block s) {
    Doc docs = s.getDoc();
    Expression inner = lift(s.getInner());
    List<VarDec> decs = new ArrayList<VarDec>();
    for (int i = 0; i < s.getDecs().size(); i++) {
        Optional<Type> t = s.getDecs().get(i).getT();
        String x = s.getDecs().get(i).getX();
        Expression e = lift(s.getDecs().get(i).getInner());
        decs.add(new VarDecXE(s.getDecs().get(i).isVar(), t, x, e));
    }
    List<Expression.Catch> _catch = injectionCatch(s.getOns());
    List<Expression.BlockContent> contents = new ArrayList<>();
    if (!decs.isEmpty() || !_catch.isEmpty()) {
        contents.add(new Expression.BlockContent(decs, _catch));
    }
    Expression.Position pos = s.getP();
    Expression.RoundBlock result = new Expression.RoundBlock(pos, docs, inner, contents);
    //assert WellFormedness.blockCheck(result);, no it can actually get wrong?
    return result;
}
Also used : ArrayList(java.util.ArrayList) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Expression(ast.Expression) VarDec(ast.Ast.VarDec) Doc(ast.Ast.Doc) VarDecXE(ast.Ast.VarDecXE)

Example 12 with VarDec

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

the class Desugar method getBlock.

static RoundBlock getBlock(Position p, Expression xe, List<Expression.Catch> ks, Expression inner) {
    List<Expression.BlockContent> bc = new ArrayList<>();
    List<VarDec> decs = new ArrayList<VarDec>();
    decs.add(new VarDecE(xe));
    bc.add(new Expression.BlockContent(decs, ks));
    return new RoundBlock(p, Doc.empty(), inner, bc);
}
Also used : Expression(ast.Expression) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) RoundBlock(ast.Expression.RoundBlock) VarDecE(ast.Ast.VarDecE)

Example 13 with VarDec

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

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

the class Desugar method blockInferVar.

private RoundBlock blockInferVar(RoundBlock s) {
    if (s.getContents().isEmpty()) {
        return s;
    }
    HashMap<String, Type> localVarEnv = new HashMap<String, Type>(this.varEnv);
    List<VarDec> newDecs = new ArrayList<VarDec>();
    for (VarDec _dec : s.getContents().get(0).getDecs()) {
        if (!(_dec instanceof VarDecXE)) {
            newDecs.add(_dec);
            continue;
        }
        VarDecXE dec = (VarDecXE) _dec;
        if (dec.getT().isPresent()) {
            Type ti = dec.getT().get();
            localVarEnv.put(dec.getX(), ti);
            newDecs.add(dec);
            continue;
        }
        localVarEnv.put(dec.getX(), null);
        newDecs.add(dec.withT(Optional.ofNullable(t)));
    }
    return blockWithDec(s, newDecs);
}
Also used : Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodType(ast.Ast.MethodType) HashMap(java.util.HashMap) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) VarDecXE(ast.Ast.VarDecXE)

Example 15 with VarDec

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

the class DesugarVars method _blockVarClass.

private RoundBlock _blockVarClass(RoundBlock s) {
    if (s.getContents().isEmpty()) {
        return s;
    }
    List<VarDec> varDecs = new ArrayList<VarDec>(s.getContents().get(0).getDecs());
    int pos = firstVar(varDecs);
    if (pos == -1) {
        return s;
    }
    VarDecXE varDec = (VarDecXE) varDecs.get(pos);
    varDecs.set(pos, varDec.withVar(false));
    VarDecCE ce = getClassBForVar(varDec);
    VarDecXE d = getDecForVar(ce.getInner().getName(), varDec);
    X x = new X(s.getP(), varDec.getX());
    X z = new X(s.getP(), d.getX());
    int d3First = findD2(x, pos, varDecs);
    RoundBlock fake = getFakeBlock(x, z, s, varDecs, d3First);
    List<VarDec> trueDecs = new ArrayList<VarDec>();
    trueDecs.add(ce);
    if (d3First != -1) {
        trueDecs.addAll(varDecs.subList(0, d3First));
    } else {
        trueDecs.addAll(varDecs);
    }
    trueDecs.add(d);
    trueDecs.addAll(fake.getContents().get(0).getDecs());
    List<Expression.BlockContent> trueContent = new ArrayList<>();
    trueContent.add(new Expression.BlockContent(trueDecs, fake.getContents().get(0).get_catch()));
    return fake.withContents(trueContent);
}
Also used : Expression(ast.Expression) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) X(ast.Expression.X) BlockContent(ast.Expression.BlockContent) VarDecCE(ast.Ast.VarDecCE) RoundBlock(ast.Expression.RoundBlock) VarDecXE(ast.Ast.VarDecXE) BlockContent(ast.Expression.BlockContent)

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