Search in sources :

Example 1 with Position

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

the class Desugar method visit.

public Expression visit(ClassB s) {
    Position pos = s.getP();
    assert !(s.getH() instanceof ConcreteHeader);
    if (!s.getFields().isEmpty()) {
        List<Member> ms = s.getFields().stream().flatMap(f -> Desugar.field(pos, f)).collect(Collectors.toList());
        ms.addAll(s.getMs());
        s = s.withMs(ms).withH(new Ast.TraitHeader());
    }
    Set<String> oldUsedVars = this.usedVars;
    HashMap<String, Type> oldVarEnv = this.varEnv;
    try {
        s = (ClassB) super.visit(s);
        s = FlatFirstLevelLocalNestedClasses.of(s, this);
        s = DesugarCatchDefault.of(s);
        return s;
    } finally {
        this.usedVars = oldUsedVars;
        this.varEnv = oldVarEnv;
    }
}
Also used : Stage(ast.Ast.Stage) VarDecXE(ast.Ast.VarDecXE) X(ast.Expression.X) Ast(ast.Ast) FieldDec(ast.Ast.FieldDec) Parameters(ast.Ast.Parameters) Catch1(ast.Expression.Catch1) Catch(ast.Expression.Catch) SquareWithCall(ast.Expression.SquareWithCall) ErrorMessage(ast.ErrorMessage) Type(ast.Ast.Type) Configuration(facade.Configuration) FCall(ast.Expression.FCall) SquareCall(ast.Expression.SquareCall) ConcreteHeader(ast.Ast.ConcreteHeader) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodSelector(ast.Ast.MethodSelector) Visitor(coreVisitors.Visitor) BinOp(ast.Expression.BinOp) InjectionOnSugar(coreVisitors.InjectionOnSugar) Op(ast.Ast.Op) SignalKind(ast.Ast.SignalKind) Loop(ast.Expression.Loop) OnLineCode(platformSpecific.fakeInternet.OnLineCode) RoundBlock(ast.Expression.RoundBlock) TypeManipulation(newTypeSystem.TypeManipulation) Set(java.util.Set) MethodSelectorX(ast.Ast.MethodSelectorX) With(ast.Expression.With) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) UseSquare(ast.Expression.UseSquare) While(ast.Expression.While) ClassReuse(ast.Expression.ClassReuse) VarDecCE(ast.Ast.VarDecCE) EncodingHelper(auxiliaryGrammar.EncodingHelper) Optional(java.util.Optional) Timer(profiling.Timer) L42(facade.L42) Expression._void(ast.Expression._void) Program(programReduction.Program) Header(ast.Ast.Header) Using(ast.Expression.Using) Member(ast.Expression.ClassB.Member) HashMap(java.util.HashMap) Assertions(tools.Assertions) MethodType(ast.Ast.MethodType) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PathMxMx(ast.Util.PathMxMx) RefreshUniqueNames(privateMangling.RefreshUniqueNames) MCall(ast.Expression.MCall) If(ast.Expression.If) Literal(ast.Expression.Literal) UnOp(ast.Expression.UnOp) IsCompiled(coreVisitors.IsCompiled) Mdf(ast.Ast.Mdf) VarDec(ast.Ast.VarDec) Path(ast.Ast.Path) DocE(ast.Expression.DocE) Doc(ast.Ast.Doc) VarDecE(ast.Ast.VarDecE) ExpCore(ast.ExpCore) Expression(ast.Expression) ClassB(ast.Expression.ClassB) Functions(auxiliaryGrammar.Functions) CatchMany(ast.Expression.CatchMany) CurlyBlock(ast.Expression.CurlyBlock) Position(ast.Ast.Position) MethodImplemented(ast.Expression.ClassB.MethodImplemented) PathAux(ast.PathAux) Collections(java.util.Collections) NestedClass(ast.Expression.ClassB.NestedClass) Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodType(ast.Ast.MethodType) Position(ast.Ast.Position) Member(ast.Expression.ClassB.Member) ConcreteHeader(ast.Ast.ConcreteHeader)

Example 2 with Position

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

the class UsedPaths method usedPathsL.

//- usedPathsL(L, Cs1..Csn)=usedInnerL(L(Cs1),Cs1) U ... U usedInnerL(L(Csn),Csn)
private static Paths usedPathsL(Program pForError, ClassB l, List<List<Ast.C>> css, Phase phase0) {
    Paths result = Paths.empty();
    for (List<Ast.C> csi : css) {
        assert !csi.isEmpty();
        ClassB li;
        try {
            li = l.getClassB(csi);
        } catch (ErrorMessage.PathMetaOrNonExistant pne) {
            //.withWherePathWasWritten(p);
            throw pne.withListOfNodeNames(csi).withCb(l);
        }
        ClassB liTop = li;
        if (csi.size() != 0) {
            liTop = l.getClassB(Collections.singletonList(csi.get(0)));
        }
        assert IsCompiled.of(liTop);
        //checked after for newPaths: when the offending value is produced, so we have more context for error message
        //  throw new ErrorMessage.PathMetaOrNonExistant(true, Collections.singletonList(csi.get(0)), l, null,null);
        Paths newPaths = usedInnerL(li, csi, phase0);
        try {
            newPaths.checkAllDefined(pForError);
        } catch (ErrorMessage.PathMetaOrNonExistant pne) {
            Position p = FindPathUsage._locate(pForError, li, Path.outer(csi.size() + 1, /*TODO we need to fix this crap for real*/
            pne.getListOfNodeNames()));
            throw pne.withWherePathWasWritten(p);
        }
        result = result.union(newPaths);
    }
    return result;
}
Also used : PathMetaOrNonExistant(ast.ErrorMessage.PathMetaOrNonExistant) C(ast.Ast.C) Position(ast.Ast.Position) ErrorMessage(ast.ErrorMessage) ClassB(ast.ExpCore.ClassB)

Example 3 with Position

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

the class ReplState method add.

public ReplState add(String code) {
    Expression.ClassB cbEmpty = new ClassB(Doc.empty(), new ast.Ast.InterfaceHeader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Position.noInfo);
    try {
        //parse
        Expression.ClassB codeTmp = (ClassB) Parser.parse("Repl", "{" + code + "}");
        //new original
        ClassReuse newOriginal = this.originalL;
        List<ast.Expression.ClassB.Member> newOriginalMs = newOriginal.getInner().getMs();
        newOriginalMs.addAll(codeTmp.getMs());
        newOriginal.withInner(newOriginal.getInner().withMs(newOriginalMs));
        //new src to desugar
        List<ClassB.Member> newMs = new ArrayList<>();
        int nestedAdded = 0;
        for (Member m : this.desugaredL.getMs()) {
            if (!(m instanceof NestedClass)) {
                continue;
            }
            NestedClass nc = (NestedClass) m;
            newMs.add(new ClassB.NestedClass(Doc.empty(), nc.getName(), cbEmpty, nc.getP()));
            nestedAdded += 1;
        }
        newMs.addAll(codeTmp.getMs());
        codeTmp = codeTmp.withMs(newMs);
        Expression code2 = Desugar.of(codeTmp);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        // TODO: will die after new reduction Refresh of position identities, it is used to generate correct Java code.
        code3 = (ExpCore.ClassB) code3.accept(new CloneVisitor() {

            @Override
            public ExpCore visit(ExpCore.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);
            }
        });
        //integrate new desugared src with old desugared code
        List<Member> resultMs = new ArrayList<>(this.desugaredL.getMs());
        for (int i = nestedAdded; i < code3.getMs().size(); i++) {
            resultMs.add(code3.getMs().get(i));
        }
        code3 = code3.withMs(resultMs);
        //call the repl and return
        ExpCore.ClassB result = ProgramReduction.allSteps(code3);
        return new ReplState(this.originalS + "\n" + code, newOriginal, result);
    } catch (ParseCancellationException parser) {
        System.out.println(parser.getMessage());
        return null;
    } catch (ErrorMessage msg) {
        ErrorFormatter.topFormatErrorMessage(msg);
        return null;
    }
}
Also used : ExpCore(ast.ExpCore) Position(ast.Ast.Position) ClassB(ast.Expression.ClassB) ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) CloneVisitor(coreVisitors.CloneVisitor) NestedClass(ast.ExpCore.ClassB.NestedClass) Expression(ast.Expression) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ClassReuse(ast.Expression.ClassReuse) ErrorMessage(ast.ErrorMessage) Member(ast.ExpCore.ClassB.Member) ClassB(ast.Expression.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Example 4 with Position

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

the class DesugarVars method getFakeBlock.

private RoundBlock getFakeBlock(X x, X z, RoundBlock s, List<VarDec> varDecs, int d3First) {
    Position pos = Desugar.getPosition(s);
    List<Expression.BlockContent> fakeContent = new ArrayList<>();
    List<VarDec> fakeDecs = new ArrayList<VarDec>();
    if (d3First != -1) {
        //d3 not empty
        fakeDecs.addAll(varDecs.subList(d3First, varDecs.size()));
    }
    fakeContent.add(new Expression.BlockContent(fakeDecs, s.getContents().get(0).get_catch()));
    RoundBlock fake = s.withContents(fakeContent);
    fake = (RoundBlock) XEqOpInZEqOp.of(x, z, fake);
    fake = (RoundBlock) XInE.of(x, Desugar.getMCall(pos, z, "#inner", Desugar.getPs()), fake);
    return fake;
}
Also used : Position(ast.Ast.Position) Expression(ast.Expression) VarDec(ast.Ast.VarDec) ArrayList(java.util.ArrayList) BlockContent(ast.Expression.BlockContent) RoundBlock(ast.Expression.RoundBlock) BlockContent(ast.Expression.BlockContent)

Example 5 with Position

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

the class ErrorFormatter method formatError.

public static ErrorMessage.UserLevelError formatError(Program p, ErrorMessage msg, Class<?> c) throws IllegalAccessException, NoSuchFieldException, SecurityException {
    String errorStart = "\n\n\n------------------------------------\n";
    ArrayList<Ast.Position> ps = new ArrayList<Ast.Position>();
    String errorTxt = "";
    errorTxt += infoFields(msg, c, ps);
    try {
        errorTxt += "Surrounding context:\n";
        errorTxt += envs(msg, c, ps);
        ArrayList<Position> ps2 = ps;
        if (!ps.isEmpty()) {
            ps2 = new ArrayList<>();
        }
        try {
            errorTxt += ctxP(msg, c, ps2);
        } catch (NoSuchFieldException ignored) {
        }
    } catch (NoSuchFieldException ignored) {
    }
    pos(msg, c, ps);
    Position pos = positionsFilter(ps);
    if (c == ErrorMessage.DotDotDotCanNotBeResolved.class) {
    //        ErrorMessage.DotDotDotCanNotBeResolved ddd=(ErrorMessage.DotDotDotCanNotBeResolved)msg;
    }
    errorTxt = "Error kind: " + c.getSimpleName() + "\nPosition:" + ((pos == null) ? "unknown" : pos.toString()) + "\n" + errorTxt;
    //ps+"\n"+errorTxt;
    ErrorMessage.UserLevelError.Kind kind = findKind(msg);
    switch(kind) {
        case TypeError:
            errorTxt = errorStart + "runStatus: " + kind.name() + "\n" + errorTxt;
            break;
        case MetaError:
            errorTxt = errorStart + "runStatus: " + kind.name() + "\n" + "Error in generating the following class: \n" + reportPlaceOfMetaError(p, msg) + /*.replace(".\n","\n")*/
            "\n----------\n" + errorTxt;
        default:
            break;
    }
    Throwable cause = null;
    if (msg.getCause() != null) {
        if (msg.getCause() instanceof ErrorMessage) {
            ErrorMessage.UserLevelError uleCause = formatError(p, (ErrorMessage) msg.getCause());
            cause = uleCause;
            errorTxt += "\n-------- caused by -----\n" + uleCause.getErrorTxt();
        } else {
            cause = msg.getCause();
        }
    }
    ErrorMessage.UserLevelError result = new ErrorMessage.UserLevelError(kind, pos, msg, errorTxt);
    result.initCause(cause);
    return result;
}
Also used : Ast(ast.Ast) Position(ast.Ast.Position) ArrayList(java.util.ArrayList) Kind(ast.ErrorMessage.UserLevelError.Kind) ErrorMessage(ast.ErrorMessage)

Aggregations

Position (ast.Ast.Position)14 Expression (ast.Expression)9 ErrorMessage (ast.ErrorMessage)5 ExpCore (ast.ExpCore)5 ArrayList (java.util.ArrayList)5 Ast (ast.Ast)4 ClassB (ast.ExpCore.ClassB)3 ClassB (ast.Expression.ClassB)3 ClassReuse (ast.Expression.ClassReuse)3 C (ast.Ast.C)2 Doc (ast.Ast.Doc)2 MethodSelectorX (ast.Ast.MethodSelectorX)2 Path (ast.Ast.Path)2 Type (ast.Ast.Type)2 VarDec (ast.Ast.VarDec)2 VarDecXE (ast.Ast.VarDecXE)2 Catch (ast.Expression.Catch)2 MethodWithType (ast.Expression.ClassB.MethodWithType)2 MCall (ast.Expression.MCall)2 RoundBlock (ast.Expression.RoundBlock)2