Search in sources :

Example 1 with ClassReuse

use of ast.Expression.ClassReuse 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 2 with ClassReuse

use of ast.Expression.ClassReuse in project L42 by ElvisResearchGroup.

the class Desugar method visit.

public Expression visit(ClassReuse s) {
    ClassB res = lift(s.getInner());
    //ClassB reused2=OnLineCode.getCode(s.getUrl());
    ExpCore.ClassB _reused = this.importedLibs.get(s.getUrl());
    assert _reused != null : s.getUrl() + " " + this.importedLibs.keySet() + this.importedLibs.get(s.getUrl()) + this.importedLibs;
    ExpCore.ClassB reused = RefreshUniqueNames.refreshTopLevel(_reused);
    for (Member m2 : res.getMs()) {
        m2.match(nc2 -> {
            for (ast.ExpCore.ClassB.NestedClass nc1 : reused.ns()) {
                if (nc1.getName().equals(nc2.getName())) {
                    throw new ast.ErrorMessage.NotWellFormedMsk(s, s, "Nested class \"" + nc1.getName() + "\" already present in reused library " + s.getUrl());
                }
            }
            return null;
        }, mi -> {
            return null;
        }, mwt2 -> {
            for (ast.ExpCore.ClassB.MethodWithType mwt1 : reused.mwts()) {
                if (mwt1.getMs().equals(mwt2.getMs())) {
                    throw new ast.ErrorMessage.NotWellFormedMsk(s, s, "Method with type \"" + mwt1.getMs() + "\" already present in reused library " + s.getUrl());
                }
            }
            return null;
        });
    }
    return new ClassReuse(res, s.getUrl(), reused);
}
Also used : ExpCore(ast.ExpCore) ClassReuse(ast.Expression.ClassReuse) Member(ast.Expression.ClassB.Member) ClassB(ast.Expression.ClassB)

Example 3 with ClassReuse

use of ast.Expression.ClassReuse in project L42 by ElvisResearchGroup.

the class ReplState method start.

public static ReplState start(String code) {
    try {
        Expression.ClassReuse code1 = (ClassReuse) Parser.parse("Repl", code);
        auxiliaryGrammar.WellFormedness.checkAll(code1);
        Expression.ClassReuse code2 = (ClassReuse) Desugar.of(code1);
        assert auxiliaryGrammar.WellFormedness.checkAll(code2);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        assert coreVisitors.CheckNoVarDeclaredTwice.of(code3);
        // 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);
            }
        });
        ExpCore.ClassB result = ProgramReduction.allSteps(code3);
        return new ReplState(code, code2, result);
    } catch (org.antlr.v4.runtime.misc.ParseCancellationException parser) {
        System.out.println(parser.getMessage());
        return null;
    } catch (ErrorMessage msg) {
        ErrorFormatter.topFormatErrorMessage(msg);
        return null;
    }
}
Also used : ExpCore(ast.ExpCore) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) Position(ast.Ast.Position) CloneVisitor(coreVisitors.CloneVisitor) Expression(ast.Expression) ClassReuse(ast.Expression.ClassReuse) ErrorMessage(ast.ErrorMessage) ClassReuse(ast.Expression.ClassReuse) ClassB(ast.Expression.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Aggregations

ExpCore (ast.ExpCore)3 ClassB (ast.Expression.ClassB)3 ClassReuse (ast.Expression.ClassReuse)3 Position (ast.Ast.Position)2 ErrorMessage (ast.ErrorMessage)2 Expression (ast.Expression)2 CloneVisitor (coreVisitors.CloneVisitor)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 InjectionOnCore (sugarVisitors.InjectionOnCore)2 Member (ast.ExpCore.ClassB.Member)1 NestedClass (ast.ExpCore.ClassB.NestedClass)1 Member (ast.Expression.ClassB.Member)1 ArrayList (java.util.ArrayList)1