Search in sources :

Example 51 with ExpCore

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

Example 52 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class _Sum method normalizedSum.

/*
  private static void interfaceClash(Program p, ClassB candidate) {
   try{
     Configuration.typeSystem.computeStage(p,candidate);
   }catch(ErrorMessage.IncoherentMwts i){
     List<Path>ps=new ArrayList<>();
     List<Member>mems=new ArrayList<>();
     Path ph=Path.outer(0,i.getExploredPath());
     for(PathMwt e:i.getIncoherent()){
       ps.add(From.fromP(e.getOriginal(),ph));
     }
     assert !ps.isEmpty();
     Member  notInterf=null;
     Program p1=p.addAtTop(candidate);
     for(Path pi:ps){
       ClassB cb=p1.extractCb(pi);
       System.out.println(pi);
       System.out.println(i);
       System.out.println(cb.getStage());
       System.out.println(candidate.getStage());
       Optional<Member> currentOpt=Program.getIfInDom(cb.getMs(),i.getGuilty());
       Member current=currentOpt.get();

       if(cb.isInterface()){
         mems.add(current);
       }
       else notInterf=current;
     }
     if(notInterf==null){
       throw Errors42.errorClassClash(i.getExploredPath(), ps);
     }
     Member mb=mems.get(0);
     throw Errors42.errorMethodClash(i.getExploredPath(), notInterf,mb, true,Collections.emptyList(),true,true,true);

   }
  }
*/
static ClassB normalizedSum(Program p, ClassB topA, ClassB topB, ClassB a, ClassB b, List<Ast.C> current) {
    List<Member> ms = doubleSimetricalMatch(p, topA, topB, a, b, current);
    List<ast.Ast.Type> superT = new ArrayList<>(a.getSupertypes());
    superT.addAll(b.getSupertypes());
    superT = Collections.unmodifiableList(superT);
    Doc doc1 = a.getDoc1().sum(b.getDoc1());
    //Sum.checkClassClash(p, current, topA, topB, a, b);
    boolean isInterface = a.isInterface() || b.isInterface();
    Phase accPhase = a.getPhase().acc(b.getPhase());
    ExpCore.ClassB res = new ClassB(doc1, isInterface, superT, ms, CollapsePositions.accumulatePos(a.getP(), b.getP()), accPhase, accPhase == Phase.None ? 0 : 1);
    res = (ClassB) res.accept(new coreVisitors.CloneVisitor() {

        public ExpCore visit(ClassB s) {
            if (s.getPhase() == Phase.None) {
                return super.visit(s);
            }
            return super.visit(s.withPhase(accPhase));
        }
    });
    //TODO: remove after new reduction introduced
    return res;
}
Also used : ExpCore(ast.ExpCore) Phase(ast.ExpCore.ClassB.Phase) ArrayList(java.util.ArrayList) MethodType(ast.Ast.MethodType) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Doc(ast.Ast.Doc) ClassB(ast.ExpCore.ClassB) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB)

Example 53 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class PlgWrapperGenerator method updateTemplateUsing.

private static MethodWithType updateTemplateUsing(UsingInfo ui, MethodWithType mwt) {
    MCall e = templateUsingExc;
    MethodType mt = mwt.getMt();
    ExpCore.Block b = (Block) e.getEs().get(0);
    if (mwt.getMt().getExceptions().isEmpty()) {
        b = b.withOns(Collections.emptyList());
    }
    ExpCore.Using u = (Using) b.getDecs().get(0).getInner();
    //parameter expressions
    ExpCore.MCall p0 = (MCall) u.getEs().get(0);
    //e#mcall.inner<-mwt.retType.path
    e = e.withInner(ExpCore.EPath.wrap(mt.getReturnType().getPath()));
    //u=u.withS(ui.usingMs);
    List<ExpCore> ues = new ArrayList<>();
    if (mt.getMdf() != Mdf.Class) {
        ues.add(p0.withInner(new ExpCore.X(mwt.getP(), "this")));
    }
    {
        int i = -1;
        for (String x : mwt.getMs().getNames()) {
            i++;
            ExpCore pi = new ExpCore.X(mwt.getP(), x);
            boolean needAddBinaryRepr = true;
            Type ti = mwt.getMt().getTs().get(i);
            if (ti.equals(Type.immLibrary)) {
                needAddBinaryRepr = false;
            }
            if (ti.getMdf() == Mdf.Class) {
                needAddBinaryRepr = false;
            }
            if (needAddBinaryRepr) {
                pi = p0.withInner(pi);
            }
            ues.add(pi);
        }
    }
    u = new Using(u.getPath(), ui.usingMs, u.getDoc(), ues, u.getInner());
    String errorS = "plugin string: " + ui.plgInfo.plgString + "\n" + "plugin part: " + ui.plgInfo.plgName + "\n" + "method name: " + mwt.getMs() + "\n" + "java method: " + ui.plgInfo.plgClass.getName() + "." + ui.usingMs + "\n";
    //u.inner#mcall.es(0)<-EncodingHelper.wrapStringU()
    List<ExpCore> errorEs = Collections.singletonList(EncodingHelper.wrapStringU(errorS));
    Signal tmpS = (Signal) u.getInner();
    MCall tmpMc = ((MCall) tmpS.getInner()).withEs(errorEs);
    u = u.withInner(tmpS.withInner(tmpMc));
    b = b.withDecs(Collections.singletonList(b.getDecs().get(0).withInner(u)));
    //--
    if (!mwt.getMt().getExceptions().isEmpty()) {
        On on = b.getOns().get(0);
        Dec k0 = ((Block) on.getInner()).getDecs().get(0);
        List<Dec> ks = new ArrayList<>();
        {
            int i = -1;
            for (Type ti : mt.getExceptions()) {
                i++;
                MCall mci = ((MCall) k0.getInner()).withInner(ExpCore.EPath.wrap(ti.getPath()));
                ks.add(k0.withInner(mci).withX(k0.getX() + i));
            }
        }
        on = on.withInner(((Block) on.getInner()).withDecs(ks));
        b = b.withOns(Collections.singletonList(on));
    //k0=b.k(0).inner#block.decs(0)
    //k0 add more on need
    //ki.inner#mcall.inner<-Pi
    }
    if (ui.isVoid) {
        b = b.withDecs(Collections.singletonList(b.getDecs().get(0).withT(Optional.of(Type.immVoid))));
    }
    if (!ui.isVoid && !mwt.getMt().getReturnType().equals(Type.immLibrary)) {
        e = e.withEs(Collections.singletonList(b));
        mwt = mwt.withInner(e);
    } else {
        mwt = mwt.withInner(b);
    }
    return mwt;
}
Also used : MethodType(ast.Ast.MethodType) ExpCore(ast.ExpCore) Block(ast.ExpCore.Block) Dec(ast.ExpCore.Block.Dec) Using(ast.ExpCore.Using) ArrayList(java.util.ArrayList) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Signal(ast.ExpCore.Signal) Using(ast.ExpCore.Using) MCall(ast.ExpCore.MCall) Block(ast.ExpCore.Block) MCall(ast.ExpCore.MCall) On(ast.ExpCore.Block.On)

Example 54 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class BigStep method executeAtomicStep.

protected ExpCore executeAtomicStep(PData p1, ExpCore _e1) {
    if (!IsCompiled.of(_e1)) {
        return step(p1, _e1);
    }
    return Resources.withPDo(p1, () -> {
        ExpCore e1 = _e1;
        boolean runned = false;
        e1 = NormalizeBlocks.of(e1);
        try {
            while (!(e1 instanceof ClassB)) {
                log(ToFormattedText.ofCompact(e1.accept(new InjectionOnSugar()), false));
                e1 = step(p1, e1);
                e1 = NormalizeBlocks.of(e1);
                runned = true;
            }
        } catch (Throwable t) {
            if (!runned) {
                throw t;
            }
        }
        log(ToFormattedText.ofCompact(e1.accept(new InjectionOnSugar()), false));
        return e1;
    });
}
Also used : ExpCore(ast.ExpCore) InjectionOnSugar(coreVisitors.InjectionOnSugar) ClassB(ast.ExpCore.ClassB)

Example 55 with ExpCore

use of ast.ExpCore in project L42 by ElvisResearchGroup.

the class CompiledStep method executeAtomicStep.

@Override
protected ExpCore executeAtomicStep(PData p1, ExpCore _e1, Ast.C nestedName) {
    if (!IsCompiled.of(_e1)) {
        return step(p1, _e1);
    }
    return Resources.withPDo(p1, () -> {
        ExpCore e1 = NormalizeBlocks.of(_e1);
        if (e1 instanceof ExpCore.Signal) {
            throw new ErrorMessage.CtxExtractImpossible(e1, null);
        }
        Translator code = Timer.record("Translator.translateProgram", () -> Translator.translateProgram(p1.p, e1));
        try {
            L42.compilationRounds++;
            System.out.println("Compilation Iteration-- " + nestedName + ":" + L42.compilationRounds + "");
            Timer.activate("code.runMap");
            Object o = code.runMap();
            Timer.deactivate("code.runMap");
            System.out.println("Compilation Iteration complete-- " + nestedName + ":" + L42.compilationRounds + "");
            assert o instanceof ClassB;
            return (ClassB) o;
        } catch (Resources.Error err) {
            Resources.cacheMessage(err);
            return EncodingHelper.wrapResource(err);
        } catch (Resources.Exception err) {
            Resources.cacheMessage(err);
            return EncodingHelper.wrapResource(err);
        } catch (Resources.Return err) {
            //it can happen if other stuff is wrong, in this way we can see the error.
            Resources.cacheMessage(err);
            return EncodingHelper.wrapResource(err);
        }
    });
}
Also used : ExpCore(ast.ExpCore) Translator(platformSpecific.javaTranslation.Translator) Resources(platformSpecific.javaTranslation.Resources) ClassB(ast.ExpCore.ClassB)

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