Search in sources :

Example 1 with Type

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

the class Errors42 method checkCompatibleMs.

public static void checkCompatibleMs(List<Ast.C> pathForError, MethodWithType mem, MethodSelector dest) {
    int sizeA = mem.getMs().getNames().size();
    int sizeB = dest.getNames().size();
    if (sizeA == sizeB) {
        return;
    }
    List<Integer> parsWrong = new ArrayList<>();
    int min = Math.min(sizeA, sizeB);
    int max = Math.max(sizeA, sizeB);
    for (int i = min; i < max; i++) {
        parsWrong.add(i);
    }
    List<Type> ts = new ArrayList<>(mem.getMt().getTs());
    for (int i = sizeA; i < sizeB; i++) {
        ts.add(ast.Ast.Type.immVoid);
    }
    if (sizeA > sizeB) {
        ts = ts.subList(0, sizeB);
    }
    MethodWithType memb = mem.withMs(dest).withMt(mem.getMt().withTs(ts));
    throw errorMethodClash(pathForError, mem, memb, true, parsWrong, true, true, false);
}
Also used : Type(ast.Ast.Type) MethodWithType(ast.ExpCore.ClassB.MethodWithType) ArrayList(java.util.ArrayList) MethodWithType(ast.ExpCore.ClassB.MethodWithType)

Example 2 with Type

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

the class ExtractInfo method isModule.

public static boolean isModule(ClassB cb) {
    for (Member m : cb.getMs()) {
        if (!(m instanceof MethodWithType)) {
            continue;
        }
        MethodWithType mwt = (MethodWithType) m;
        if (mwt.getMt().getMdf() != Mdf.Class) {
            return false;
        }
        Type rt = mwt.getMt().getReturnType();
        if (!(rt instanceof Type)) {
            continue;
        }
        Type nt = (Type) rt;
        if (nt.getPath().equals(Path.outer(0))) {
            return false;
        }
    }
    return true;
}
Also used : Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) MethodWithType(ast.ExpCore.ClassB.MethodWithType) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Member(ast.ExpCore.ClassB.Member)

Example 3 with Type

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

the class MakeKs method candidate.

private ast.Ast.Type candidate(List<Member> ms, String fName) {
    Optional<Member> a = Functions.getIfInDom(ms, MethodSelector.of(fName, Collections.singletonList("that")));
    Optional<Member> b = Functions.getIfInDom(ms, MethodSelector.of("#" + fName, Collections.singletonList("that")));
    Optional<Member> c = Functions.getIfInDom(ms, MethodSelector.of(fName, Collections.emptyList()));
    Optional<Member> d = Functions.getIfInDom(ms, MethodSelector.of("#" + fName, Collections.emptyList()));
    Type ta = getType(a);
    Type tb = getType(b);
    Type tc = getType(c);
    Type td = getType(d);
    ast.Ast.Type res = null;
    if (a.isPresent() && b.isPresent()) {
        if (!ta.equals(tb)) {
            throw new Error();
        }
    }
    if (a.isPresent()) {
        res = ta;
    } else if (b.isPresent()) {
        res = tb;
    }
    if (res != null) {
        if (c.isPresent() && !more(res, tc)) {
            throw new Error();
        }
        if (d.isPresent() && !more(res, td)) {
            throw new Error();
        }
    }
    if (c.isPresent() && d.isPresent() && !compatible(tc, td)) {
        throw new Error();
    }
    if (res == null) {
        res = moreSpecific(tc, td);
    }
    if (res == null) {
        throw new Error();
    }
    if ((a.isPresent() || b.isPresent()) && (res.getMdf().equals(Mdf.Lent) || res.getMdf().equals(Mdf.Readable))) {
        this.hasReadLentSetter = true;
    }
    if (!a.isPresent() && !b.isPresent() && res.getMdf().equals(Mdf.Lent)) {
        return res.withMdf(Mdf.Capsule);
    }
    return res;
}
Also used : Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Ast(ast.Ast) Type(ast.Ast.Type)

Example 4 with Type

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

the class MakeMethod method addMethod.

public static ClassB addMethod(ClassB _lib, List<Ast.C> path, MethodSelector ms, String mdfs, int excNumber) {
    Errors42.checkExistsPathMethod(_lib, path, Optional.empty());
    ClassB innerLib = _lib.getClassB(path);
    String[] _mdfs = mdfs.split(" ");
    assert _mdfs.length == ms.getNames().size() + 2;
    List<String> nc = new ArrayList<>();
    Type retT = new Type(Mdf.valueOf(_mdfs[1]), Path.outer(0, Arrays.asList(C.of("$0"))), Doc.empty());
    nc.add("$0");
    List<Type> ts = new ArrayList<>();
    List<Doc> docs = new ArrayList<>();
    int count = 1;
    for (String n : ms.getNames()) {
        String cn = "$" + count;
        ts.add(new Type(Mdf.valueOf(_mdfs[count + 1]), Path.outer(0, Arrays.asList(C.of(cn))), Doc.empty()));
        nc.add(cn);
        count++;
    }
    List<Path> exceptions = new ArrayList<>();
    for (int i = 0; i < excNumber; i++) {
        String cn = "$" + count++;
        exceptions.add(Path.outer(0, Arrays.asList(C.of(cn))));
        nc.add(cn);
    }
    MethodType mt = new MethodType(false, Mdf.valueOf(_mdfs[0]), ts, retT, Map.of(pi -> pi.toImmNT(), exceptions));
    MethodWithType mwt = new MethodWithType(Doc.empty(), ms, mt, Optional.empty(), innerLib.getP());
    Optional<Member> optM = Functions.getIfInDom(innerLib.getMs(), ms);
    if (optM.isPresent()) {
        throw Errors42.errorMethodClash(path, mwt, optM.get(), false, Collections.emptyList(), false, false, false);
    }
    ClassB emptyCb = ClassB.membersClass(Collections.emptyList(), innerLib.getP(), innerLib.getPhase());
    return _lib.onClassNavigateToPathAndDo(path, cbi -> {
        List<Member> mem = new ArrayList<>(cbi.getMs());
        mem.add(mwt);
        for (String s : nc) {
            mem.add(new NestedClass(Doc.empty(), C.of(s), emptyCb, cbi.getP()));
        }
        return cbi.withMs(mem);
    });
}
Also used : Path(ast.Ast.Path) Stage(ast.Ast.Stage) Arrays(java.util.Arrays) Program(programReduction.Program) Ast(ast.Ast) Path(ast.Ast.Path) Map(tools.Map) Doc(ast.Ast.Doc) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) MethodSelector(ast.Ast.MethodSelector) Functions(auxiliaryGrammar.Functions) ArrayList(java.util.ArrayList) List(java.util.List) ClassB(ast.ExpCore.ClassB) Optional(java.util.Optional) C(ast.Ast.C) Collections(java.util.Collections) Mdf(ast.Ast.Mdf) MethodType(ast.Ast.MethodType) ArrayList(java.util.ArrayList) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Doc(ast.Ast.Doc) ClassB(ast.ExpCore.ClassB)

Example 5 with Type

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

the class MethodPathCloneVisitor method visit.

public ExpCore visit(MCall s) {
    Program ep = p;
    for (ClassB cbi : this.getLocator().getCbs()) {
        if (cbi != null) {
            ep = ep.evilPush(cbi);
        }
    }
    MethodSelector ms = s.getS();
    Path guessed = null;
    try {
        TIn in = TIn.top(Phase.Typed, ep, s.getInner());
        in = in.withG(varEnv.entrySet().stream().collect(Collectors.toMap(me -> me.getKey(), me -> new java.util.AbstractMap.SimpleEntry<>(false, me.getValue()))));
        Type tGuessed = newTypeSystem.GuessTypeCore._of(in.p, in, s.getInner());
        if (tGuessed == null) {
            return super.visit(s);
        }
        guessed = tGuessed.getPath();
        assert guessed != null;
    } catch (NormImpossible ignored) {
        return super.visit(s);
    }
    MethodSelector ms2 = visitMS(ms, guessed);
    if (ms2.equals(ms)) {
        return super.visit(s);
    }
    s = new MCall(s.getInner(), ms2, s.getDoc(), s.getEs(), s.getP());
    return super.visit(s);
}
Also used : Path(ast.Ast.Path) From(coreVisitors.From) GuessTypeCore(newTypeSystem.GuessTypeCore) Program(programReduction.Program) Ast(ast.Ast) MCall(ast.ExpCore.MCall) Map(tools.Map) Type(ast.Ast.Type) Configuration(facade.Configuration) HashMap(java.util.HashMap) Assertions(tools.Assertions) MethodWithType(ast.ExpCore.ClassB.MethodWithType) MethodSelector(ast.Ast.MethodSelector) ArrayList(java.util.ArrayList) ClassB(ast.ExpCore.ClassB) NormImpossible(ast.ErrorMessage.NormImpossible) Norm(programReduction.Norm) On(ast.ExpCore.Block.On) Path(ast.Ast.Path) Doc(ast.Ast.Doc) Phase(ast.ExpCore.ClassB.Phase) MethodSelectorX(ast.Ast.MethodSelectorX) ExpCore(ast.ExpCore) Member(ast.ExpCore.ClassB.Member) Collectors(java.util.stream.Collectors) Block(ast.ExpCore.Block) Functions(auxiliaryGrammar.Functions) List(java.util.List) TIn(newTypeSystem.TIn) Optional(java.util.Optional) Dec(ast.ExpCore.Block.Dec) Collections(java.util.Collections) MethodImplemented(ast.ExpCore.ClassB.MethodImplemented) MethodSelector(ast.Ast.MethodSelector) Type(ast.Ast.Type) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Program(programReduction.Program) MCall(ast.ExpCore.MCall) TIn(newTypeSystem.TIn) NormImpossible(ast.ErrorMessage.NormImpossible) ClassB(ast.ExpCore.ClassB)

Aggregations

Type (ast.Ast.Type)81 MethodType (ast.Ast.MethodType)50 MethodWithType (ast.ExpCore.ClassB.MethodWithType)28 MethodWithType (ast.Expression.ClassB.MethodWithType)24 ArrayList (java.util.ArrayList)24 Mdf (ast.Ast.Mdf)16 Expression (ast.Expression)15 Path (ast.Ast.Path)13 Doc (ast.Ast.Doc)11 Ast (ast.Ast)10 MethodSelector (ast.Ast.MethodSelector)10 HashMap (java.util.HashMap)10 MethodSelectorX (ast.Ast.MethodSelectorX)8 VarDecXE (ast.Ast.VarDecXE)8 ExpCore (ast.ExpCore)8 Catch (ast.Expression.Catch)8 X (ast.Expression.X)8 VarDec (ast.Ast.VarDec)7 Block (ast.ExpCore.Block)7 List (java.util.List)7