Search in sources :

Example 1 with ClassB

use of ast.Expression.ClassB 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 ClassB

use of ast.Expression.ClassB 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 3 with ClassB

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

the class FlatFirstLevelLocalNestedClasses method of.

public static ClassB of(ClassB s, CloneVisitor step) {
    FlatFirstLevelLocalNestedClasses v = new FlatFirstLevelLocalNestedClasses();
    //to manage super
    ClassB result = v.start(s);
    List<Member> ms2 = new ArrayList<>();
    for (NestedClass nc : v.collected) {
        ms2.add(nc.withInner(nc.getInner().accept(step)));
    }
    ms2.addAll(result.getMs());
    return result.withMs(ms2);
}
Also used : ArrayList(java.util.ArrayList) ClassB(ast.Expression.ClassB)

Example 4 with ClassB

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

the class CloneVisitor method visit.

public Expression visit(ClassB s) {
    Header h = liftH(s.getH());
    List<FieldDec> fs = Map.of(this::liftF, s.getFields());
    List<Type> superT = Map.of(this::liftT, s.getSupertypes());
    List<Member> ms = Map.of(this::liftM, s.getMs());
    return new ClassB(liftDoc(s.getDoc1()), h, fs, superT, ms, s.getP());
}
Also used : FieldDec(ast.Ast.FieldDec) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Header(ast.Ast.Header) ConcreteHeader(ast.Ast.ConcreteHeader) Member(ast.Expression.ClassB.Member) ClassB(ast.Expression.ClassB)

Example 5 with ClassB

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

Aggregations

ClassB (ast.Expression.ClassB)10 Member (ast.Expression.ClassB.Member)5 ArrayList (java.util.ArrayList)5 ExpCore (ast.ExpCore)4 Ast (ast.Ast)3 FieldDec (ast.Ast.FieldDec)3 Expression (ast.Expression)3 NestedClass (ast.Expression.ClassB.NestedClass)3 ClassReuse (ast.Expression.ClassReuse)3 ConcreteHeader (ast.Ast.ConcreteHeader)2 Doc (ast.Ast.Doc)2 Header (ast.Ast.Header)2 MethodType (ast.Ast.MethodType)2 Position (ast.Ast.Position)2 Type (ast.Ast.Type)2 VarDecCE (ast.Ast.VarDecCE)2 ErrorMessage (ast.ErrorMessage)2 Mdf (ast.Ast.Mdf)1 MethodSelector (ast.Ast.MethodSelector)1 MethodSelectorX (ast.Ast.MethodSelectorX)1