Search in sources :

Example 6 with NestedClass

use of ast.ExpCore.ClassB.NestedClass 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 7 with NestedClass

use of ast.ExpCore.ClassB.NestedClass in project L42 by ElvisResearchGroup.

the class CtxL method _split.

public static CtxL _split(ExpCore.ClassB l) {
    int pos = firstNotCompiled(l.getMs());
    if (pos == l.getMs().size()) {
        return null;
    }
    Member m = l.getMs().get(pos);
    ExpCore inner = m.getInner();
    boolean metaReady = m instanceof NestedClass && IsCompiled.of(m.getInner());
    CtxC innerSplit;
    if (metaReady) {
        innerSplit = CtxC.hole(inner);
    } else {
        innerSplit = CtxC.split(inner);
    }
    return new CtxL(l, pos, innerSplit);
}
Also used : ExpCore(ast.ExpCore) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member)

Example 8 with NestedClass

use of ast.ExpCore.ClassB.NestedClass in project L42 by ElvisResearchGroup.

the class RemoveCode method collectPaths.

private static void collectPaths(Set<List<Ast.C>> accumulator, List<Ast.C> path, ClassB cb) {
    accumulator.add(path);
    for (Member m : cb.getMs()) {
        if (!(m instanceof NestedClass)) {
            continue;
        }
        NestedClass nc = (NestedClass) m;
        List<Ast.C> pathi = new ArrayList<>(path);
        pathi.add(nc.getName());
        collectPaths(accumulator, pathi, (ClassB) nc.getInner());
    }
}
Also used : ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member)

Example 9 with NestedClass

use of ast.ExpCore.ClassB.NestedClass in project L42 by ElvisResearchGroup.

the class RemoveCode method collectDepNested.

private static ClassB collectDepNested(Set<List<Ast.C>> justAdded, ClassB originalCb, ClassB accumulator, ClassB depSource, List<Ast.C> origin) {
    List<List<Ast.C>> dep = collectDep(depSource, origin);
    for (List<Ast.C> pi : dep) {
        if (justAdded.contains(pi)) {
            continue;
        }
        justAdded.add(pi);
        accumulator = addDep(accumulator, pi, originalCb);
    }
    assert dep != null : "to break";
    for (Member m : depSource.getMs()) {
        if (!(m instanceof NestedClass)) {
            continue;
        }
        NestedClass nc = (NestedClass) m;
        ClassB cbi = (ClassB) nc.getInner();
        List<Ast.C> newOrigin = new ArrayList<>(origin);
        newOrigin.add(nc.getName());
        accumulator = collectDepNested(justAdded, originalCb, accumulator, cbi, newOrigin);
    }
    return accumulator;
}
Also used : Ast(ast.Ast) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB) EncodingHelper.ensureExtractClassB(auxiliaryGrammar.EncodingHelper.ensureExtractClassB)

Example 10 with NestedClass

use of ast.ExpCore.ClassB.NestedClass in project L42 by ElvisResearchGroup.

the class RemoveCode method removeAllButPath.

private static ClassB removeAllButPath(List<Ast.C> path, ClassB originalCb) {
    if (path.isEmpty()) {
        List<Member> ms = new ArrayList<>();
        for (Member m : originalCb.getMs()) {
            if (m instanceof NestedClass) {
                continue;
            }
            ms.add(m);
        }
        return originalCb.withMs(ms);
    }
    Ast.C firstName = path.get(0);
    List<Member> ms = new ArrayList<>();
    for (Member m : originalCb.getMs()) {
        if (!(m instanceof NestedClass)) {
            continue;
        }
        NestedClass nc = (NestedClass) m;
        if (!nc.getName().equals(firstName)) {
            continue;
        }
        ClassB newInner = removeAllButPath(path.subList(1, path.size()), (ClassB) nc.getInner());
        ms.add(nc.withInner(newInner));
    }
    return ClassB.membersClass(ms, Position.noInfo, originalCb.getPhase());
}
Also used : Ast(ast.Ast) ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB) EncodingHelper.ensureExtractClassB(auxiliaryGrammar.EncodingHelper.ensureExtractClassB)

Aggregations

NestedClass (ast.ExpCore.ClassB.NestedClass)24 Member (ast.ExpCore.ClassB.Member)18 ArrayList (java.util.ArrayList)16 ClassB (ast.ExpCore.ClassB)12 Ast (ast.Ast)10 MethodWithType (ast.ExpCore.ClassB.MethodWithType)7 Path (ast.Ast.Path)5 ExpCore (ast.ExpCore)4 MethodImplemented (ast.ExpCore.ClassB.MethodImplemented)3 PathPath (ast.Util.PathPath)3 EncodingHelper.ensureExtractClassB (auxiliaryGrammar.EncodingHelper.ensureExtractClassB)3 List (java.util.List)3 C (ast.Ast.C)2 Position (ast.Ast.Position)2 Type (ast.Ast.Type)2 Phase (ast.ExpCore.ClassB.Phase)2 CloneVisitor (coreVisitors.CloneVisitor)2 HashSet (java.util.HashSet)2 Doc (ast.Ast.Doc)1 MethodSelector (ast.Ast.MethodSelector)1