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);
}
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;
}
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;
}
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);
});
}
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);
}
Aggregations