Search in sources :

Example 31 with ExpCore

use of ast.ExpCore 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)

Example 32 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class TsBlock method kTypeCatchAny.

default default TOutK kTypeCatchAny(TIn in, Tr tr, On k) {
    Block e = (Block) k.getE();
    ExpCore e0 = e.getDecs().get(0).getInner();
    TIn in1 = in.removeG(k.getX());
    TOut _out = type(in1.withE(e0, Path.Void().toImmNT()));
    if (!_out.isOk()) {
        return _out.toError();
    }
    TOk ok = _out.toOk();
    if (!ok.exceptions.isEmpty() || !ok.returns.isEmpty()) {
        return new TErr(in, "", null, ErrorKind.UnsafeCatchAny);
    }
    ExpCore newE = e.withDeci(0, e.getDecs().get(0).withInner(ok.annotated));
    return new TOkK(tr, k.withE(newE), in.expected);
/*
   (catch and rethrow any)// could be sugared as "on throw doAndPropagate e"  
   Phase |p |G |Tr|-catch throw Any x (e0 throw x) ~> catch throw Any x (e0' throw x): T<=T | Tr
     where //Note: e0, e, e0',e' are using the sugar imm Void x=e == e
     e0=(e catch error Any z void void)
     e0'=(e' catch error Any z void void)
     Phase |p |G\x |- e ~> e':_ <=imm Void | empty
     catchRethrow(catch throw Any x(e0 throw x)) 
*/
}
Also used : ExpCore(ast.ExpCore) Block(ast.ExpCore.Block)

Example 33 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class ErrorFormatter method errorFormat.

public static String errorFormat(Object obj, ArrayList<Ast.Position> ps) {
    if (obj instanceof ast.Ast.HasPos) {
        ps.add(((ast.Ast.HasPos) obj).getP());
    }
    if (obj instanceof String) {
        return (String) obj;
    }
    if (obj instanceof Integer) {
        return obj.toString();
    }
    if (obj instanceof Expression) {
        Ast.Position p = CollapsePositions.of((Expression) obj);
        ps.add(p);
        return ToFormattedText.ofCompact((Expression) obj);
    }
    if (obj instanceof ExpCore) {
        //thanks to Path, this have to be after Expression
        Ast.Position p = CollapsePositions.of((ExpCore) obj);
        ps.add(p);
        ExpCore exp = (ExpCore) obj;
        Expression expression = exp.accept(new InjectionOnSugar());
        return errorFormat(expression, ps);
    }
    if (obj instanceof Ast.MethodSelector) {
        return formatSelectorCompact((Ast.MethodSelector) obj);
    }
    if (obj instanceof ExpCore.ClassB.MethodWithType) {
        return ToFormattedText.of((MethodWithType) obj).trim().replace("\n", " ");
    }
    //    }
    if (obj instanceof Reporter) {
        return ((Reporter) obj).toReport(ps);
    }
    if (obj instanceof Collection<?>) {
        Collection<?> c = (Collection<?>) obj;
        if (c.size() == 0) {
            return "[]";
        }
        if (c.size() == 1) {
            return "[" + errorFormat(c.iterator().next(), ps) + "]";
        }
        String res = "[\n";
        for (Object o : c) {
            res += "  " + errorFormat(o, ps) + "\n";
        }
        return res + "  ]\n";
    }
    if (obj instanceof Ast.Type) {
        return obj.toString();
    }
    if (obj instanceof ClassB.Member) {
        return ToFormattedText.of((ClassB.Member) obj);
    }
    //if(obj instanceof Expression){return ToFormattedText.of((Expression)obj);}
    if (obj instanceof Expression.ClassB.Member) {
        return ToFormattedText.of((Expression.ClassB.Member) obj);
    }
    if (obj instanceof java.nio.file.Path) {
        return obj.toString();
    }
    if (obj instanceof Ast.Position) {
        return obj.toString();
    }
    if (obj instanceof HashMap) {
        return obj.toString();
    }
    if (obj instanceof Collection) {
        return obj.toString();
    }
    if (obj instanceof ast.Util.PathMwt) {
        return obj.toString();
    }
    if (obj instanceof ast.Ast.C) {
        return obj.toString();
    }
    if (obj instanceof Boolean) {
        return obj.toString();
    }
    if (obj instanceof ast.Ast.Path) {
        return obj.toString();
    }
    return "unknown kind " + obj.getClass().getName();
}
Also used : Position(ast.Ast.Position) HashMap(java.util.HashMap) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB) Path(ast.Ast.Path) ExpCore(ast.ExpCore) Ast(ast.Ast) Position(ast.Ast.Position) MethodWithType(ast.ExpCore.ClassB.MethodWithType) InjectionOnSugar(coreVisitors.InjectionOnSugar) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Expression(ast.Expression) Collection(java.util.Collection)

Example 34 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class L42 method runSlow.

public static ErrorMessage.FinalResult runSlow(String fileName, String code) {
    try {
        L42.setExecutionStage(ExecutionStage.Parsing);
        Expression code1 = Parser.parse(fileName, code);
        assert code1 instanceof Expression.ClassB || code1 instanceof Expression.ClassReuse;
        L42.setExecutionStage(ExecutionStage.CheckingWellFormedness);
        auxiliaryGrammar.WellFormedness.checkAll(code1);
        L42.setExecutionStage(ExecutionStage.Desugaring);
        Expression code2 = Desugar.of(code1);
        assert auxiliaryGrammar.WellFormedness.checkAll(code2);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        assert coreVisitors.CheckNoVarDeclaredTwice.of(code3);
        // L42.usedNames.addAll(CollectDeclaredVarsAndCheckNoDeclaredTwice.of(code2));
        //L42.usedNames.addAll(CollectDeclaredClassNamesAndMethodNames.of(code2));
        L42.setExecutionStage(ExecutionStage.MetaExecution);
        //ClassB result= (ClassB)Executor.stepStar(exe,code3);
        //Refresh of position identities, it is used to generate correct Java code.
        code3 = (ClassB) code3.accept(new CloneVisitor() {

            @Override
            public ExpCore visit(ClassB cb) {
                Position p = cb.getP();
                cb = cb.withP(new Position(p.getFile(), p.getLine1(), p.getPos1(), p.getLine2(), p.getPos2(), p.get_next()));
                return super.visit(cb);
            }
        });
        //ClassB result= Configuration.reduction.of(code3);
        ClassB result = ProgramReduction.allSteps(code3);
        //System.out.println("--------------------------");
        return checkFinalError(result);
    } finally {
        L42.setExecutionStage(ExecutionStage.None);
    }
}
Also used : ExpCore(ast.ExpCore) Expression(ast.Expression) Position(ast.Ast.Position) CloneVisitor(coreVisitors.CloneVisitor) ClassB(ast.ExpCore.ClassB) ClassB(ast.ExpCore.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Example 35 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class TsMCall method tsMCall.

default default TOut tsMCall(TIn in, MCall s) {
    Type _rec = GuessTypeCore._of(in.p, in, s.getInner());
    if (_rec == null) {
        return new TErr(in, "", null, ErrorKind.SelectorNotFound);
    }
    Path rec = _rec.getPath();
    if (rec.isPrimitive()) {
        return new TErr(in, "MethodCall on primitive type", null, ErrorKind.SelectorNotFound);
    }
    MethodType mDec = AlternativeMethodTypes._mtDeclared(in.p, rec, s.getS());
    if (mDec == null) {
        return new TErr(in, "", null, ErrorKind.SelectorNotFound);
    }
    Type ret = mDec.getReturnType();
    ErrorKind kind = TypeSystem.subtype(in.p, ret.getPath(), in.expected.getPath());
    if (kind != null) {
        return new TErr(in, "", ret, kind);
    }
    List<MethodType> mTypes = AlternativeMethodTypes.types(mDec);
    MethodType mType = AlternativeMethodTypes._firstMatchReturn(in.p, in.expected, mTypes);
    if (mType == null) {
        return new TErr(in, "", ret, ErrorKind.NotSubtypeMdf);
    }
    //unachievable return type (T) for method (P.ms) [line numbers of expression and declaration]
    //2 type all the parameters with mutOnlyToLent(Ts) //we may include mutOnlyToLent in the computation of the MTypes, instead of in the loop below
    List<TOk> resp = new ArrayList<>();
    List<Type> computed = new ArrayList<>();
    List<ExpCore> annotated = new ArrayList<>();
    ExpCore e0 = s.getInner();
    Type t0 = new Type(mType.getMdf(), rec, Doc.empty());
    TOut _res0 = type(in.withE(e0, TypeManipulation.mutOnlyToLent(t0)));
    if (!_res0.isOk()) {
        return improveReceiverError(in, t0, _res0.toError());
    }
    TOk res0 = _res0.toOk();
    Mdf recMdf = _res0.toOk().computed.getMdf();
    {
        int i = -1;
        for (ExpCore ei : s.getEs()) {
            i += 1;
            Type ti = mType.getTs().get(i);
            TOut _resi = type(in.withE(ei, TypeManipulation.mutOnlyToLent(ti)));
            if (!_resi.isOk()) {
                return _resi.toError();
            }
            resp.add(_resi.toOk());
            computed.add(_resi.toOk().computed);
            annotated.add(_resi.toOk().annotated);
        }
    }
    MethodType computedMt = new MethodType(false, recMdf, computed, in.expected, Collections.emptyList());
    MethodType mTypeRev = AlternativeMethodTypes._bestMatchMtype(in.p, computedMt, mTypes);
    if (mTypeRev != null) {
        MCall resM = new MCall(res0.annotated, s.getS(), s.getDoc(), annotated, s.getP());
        TOk res = new TOk(in, resM, mTypeRev.getReturnType());
        // Trs[with r in resp (use[r.Tr])].collapse())
        res = res.trUnion(res0);
        for (TOk oki : resp) {
            res = res.trUnion(oki);
        }
        return res;
    }
    //3 if there is no matching method, we may need to retype some mut
    //in capsule caused by mvp:
    //it is  not over if there is a mathing method type with mutToCapsule(result param types)
    //tsToCaps=Ts[with r in resp (use[mutToCapsule(r.T)])]
    //mTypeMVP=_bestMatchMtype(tsToCaps,TSIn.T,mTypes)
    List<Type> tsToCaps = new ArrayList<>();
    for (TOk r : resp) {
        Mdf m = r.computed.getMdf();
        if (m == Mdf.MutableFwd || m == Mdf.MutablePFwd) {
            return new TErr(in, "impossible to search for mvp since mdf " + m, mTypes.get(0).getReturnType(), ErrorKind.NotSubtypeClass);
        }
        Type nt = TypeManipulation.mutToCapsule(r.computed);
        tsToCaps.add(nt);
    }
    computedMt = computedMt.withTs(tsToCaps).withMdf(TypeManipulation.mutToCapsule(computedMt.getMdf()));
    MethodType mTypeMVP = AlternativeMethodTypes._bestMatchMtype(in.p, computedMt, mTypes);
    if (mTypeMVP == null) {
        return new TErr(in, "mvp candidate notfound", mTypes.get(0).getReturnType(), ErrorKind.NotSubtypeClass);
    }
    //To be happy, we can retype the obtained mut parameters into expected capsule
    TOut _newRes0 = innerMVPRetype(res0, t0.withMdf(mTypeMVP.getMdf()));
    if (!_newRes0.isOk()) {
        return _newRes0;
    }
    TOk newRes0 = _newRes0.toOk();
    List<TOk> newResp = new ArrayList<>();
    {
        int i = -1;
        for (TOk ri : resp) {
            i += 1;
            Type ti = mTypeMVP.getTs().get(i);
            TOut outi = innerMVPRetype(ri, ti);
            if (!outi.isOk()) {
                return outi.toError();
            }
            newResp.add(outi.toOk());
        }
    }
    //return res=makeMCallOK(TSIn,respMVP,mTypeMVP)
    MCall resM = new MCall(newRes0.annotated, s.getS(), s.getDoc(), Map.of(r -> r.annotated, newResp), s.getP());
    TOk res = new TOk(in, resM, mTypeMVP.getReturnType());
    // Trs[with r in resp (use[r.Tr])].collapse())
    res = res.trUnion(newRes0);
    for (TOk oki : newResp) {
        res = res.trUnion(oki);
    }
    return res;
}
Also used : Path(ast.Ast.Path) Path(ast.Ast.Path) MCall(ast.ExpCore.MCall) Map(tools.Map) Doc(ast.Ast.Doc) Type(ast.Ast.Type) Assertions(tools.Assertions) MethodType(ast.Ast.MethodType) ExpCore(ast.ExpCore) Functions(auxiliaryGrammar.Functions) ArrayList(java.util.ArrayList) List(java.util.List) Collections(java.util.Collections) Mdf(ast.Ast.Mdf) MethodType(ast.Ast.MethodType) ExpCore(ast.ExpCore) ArrayList(java.util.ArrayList) Mdf(ast.Ast.Mdf) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) MCall(ast.ExpCore.MCall)

Aggregations

ExpCore (ast.ExpCore)62 ArrayList (java.util.ArrayList)25 ClassB (ast.ExpCore.ClassB)18 Member (ast.ExpCore.ClassB.Member)11 MethodWithType (ast.ExpCore.ClassB.MethodWithType)10 Expression (ast.Expression)10 Block (ast.ExpCore.Block)9 MethodType (ast.Ast.MethodType)8 Doc (ast.Ast.Doc)7 Path (ast.Ast.Path)7 Assertions (tools.Assertions)7 MethodSelector (ast.Ast.MethodSelector)6 Position (ast.Ast.Position)6 Program (programReduction.Program)6 InjectionOnCore (sugarVisitors.InjectionOnCore)6 Ast (ast.Ast)5 Type (ast.Ast.Type)5 ErrorMessage (ast.ErrorMessage)5 CloneVisitor (coreVisitors.CloneVisitor)5 List (java.util.List)5