Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
peq committed Jan 4, 2020
1 parent 9086113 commit e7ddf90
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 179 deletions.
11 changes: 1 addition & 10 deletions de.peeeq.wurstscript/parserspec/jass_im.parseq
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ ImProg(
ImFunctions functions,
ImMethods methods,
ImClasses classes,
ImTypeClassFuncs typeClassFunctions,
java.util.Map<ImVar, java.util.List<ImExpr>> globalInits)

ImVars * ImVar
ImFunctions * ImFunction
ImClasses * ImClass
ImTypeClassFuncs * ImTypeClassFunc

ImVar(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImType type, String name, boolean isBJ)

Expand Down Expand Up @@ -46,11 +44,6 @@ ImFunction(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace,
ImStmts body,
java.util.List<de.peeeq.wurstscript.translation.imtranslation.FunctionFlag> flags)

ImTypeClassFunc(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace,
String name,
ImTypeVars typeVariables,
ImVars parameters,
ref ImType returnType)


ImClass(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace,
Expand Down Expand Up @@ -100,8 +93,6 @@ ImExpr =
| ImGetStackTrace()
| ImCompiletimeExpr(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ImExpr expr, int executionOrderIndex)
| ImLExpr
| ImTypeVarDispatch(@ignoreForEquality de.peeeq.wurstscript.ast.Element trace, ref ImTypeClassFunc typeClassFunc, ImExprs arguments
, ref ImTypeVar typeVariable)
| ImCast(ImExpr expr, ref ImType toType)

// an expression which can be used on the left hand side of an assignment
Expand Down Expand Up @@ -148,7 +139,7 @@ ImConst =

ImTypeArguments * ImTypeArgument

ImTypeArgument(ref ImType type, java.util.Map<ImTypeClassFunc, io.vavr.control.Either<ImMethod, ImFunction>> typeClassBinding)
ImTypeArgument(ref ImType type)

// helper types:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,28 +398,6 @@ public ILconst get() {
}


public static ILconst eval(ImTypeVarDispatch e, ProgramState globalState, LocalState localState) {
Either<ImMethod, ImFunction> impl = localState.getImplementation(e.getTypeVariable(), e.getTypeClassFunc());
if (impl == null) {
throw new InterpreterException(globalState, "Could not find implementation for " + e.getTypeVariable() + "." + e.getTypeClassFunc().getName());
}
ILconst[] eArgs = e.getArguments().stream()
.map(arg -> arg.evaluate(globalState, localState))
.toArray(ILconst[]::new);

return impl.fold(
(ImMethod m) -> {
ILconst receiver1 = eArgs[0];
ILconstObject receiver = globalState.toObject(receiver1);
globalState.assertAllocated(receiver, e.attrTrace());
ImMethod mostPreciseMethod = findMostPreciseMethod(e.attrTrace(), globalState, receiver, m);
return evaluateFunc(globalState, mostPreciseMethod.getImplementation(), e, Collections.emptyList(), eArgs);
},
(ImFunction f) ->
evaluateFunc(globalState, f, e, Collections.emptyList(), eArgs) // TODO type var dispatch should also have type arguments?
);
}

public static ILconst eval(ImCast imCast, ProgramState globalState, LocalState localState) {
ILconst res = imCast.getExpr().evaluate(globalState, localState);
if (TypesHelper.isIntType(imCast.getToType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public static LocalState runFunc(ProgramState globalState, ImFunction f, @Nullab
}

LocalState localState = new LocalState();
localState.setTypeArguments(f.getTypeVariables(), typeArguments);

int i = 0;
for (ImVar p : f.getParameters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
public class LocalState extends State {

private @Nullable ILconst returnVal = null;
private Table<ImTypeVar, ImTypeClassFunc, Either<ImMethod, ImFunction>> typeClassImplementations = HashBasedTable.create();

public LocalState(ILconst returnVal) {
this.setReturnVal(returnVal);
Expand All @@ -32,18 +31,4 @@ public LocalState setReturnVal(@Nullable ILconst returnVal) {
return this;
}


public Either<ImMethod, ImFunction> getImplementation(ImTypeVar typeVariable, ImTypeClassFunc typeClassFunc) {
return typeClassImplementations.get(typeVariable, typeClassFunc);
}

public void setTypeArguments(ImTypeVars typeVariables, List<ImTypeArgument> typeArguments) {
for (int i = 0; i < typeVariables.size() && i < typeArguments.size(); i++) {
ImTypeVar typeVariable = typeVariables.get(i);
ImTypeArgument typeArgument = typeArguments.get(i);
for (Map.Entry<ImTypeClassFunc, Either<ImMethod, ImFunction>> e : typeArgument.getTypeClassBinding().entrySet()) {
typeClassImplementations.put(typeVariable, e.getKey(), e.getValue());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ public Boolean case_ImGetStackTrace(ImGetStackTrace imGetStackTrace) {
return true;
}

@Override
public Boolean case_ImTypeVarDispatch(ImTypeVarDispatch imTypeVarDispatch) {
return true;
}

@Override
public Boolean case_ImOperatorCall(ImOperatorCall e) {
return e.getArguments().stream().anyMatch(SideEffectAnalyzer::quickcheckHasSideeffects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ public static JassExpr translate(ImCompiletimeExpr e, ImToJassTranslator transla
"Enable '-runcompiletimefunctions' to evaluate compiletime expressions.");
}

public static JassExpr translate(ImTypeVarDispatch e, ImToJassTranslator translator) {
throw new CompileError(e, "Typevar dispatch not eliminated.");
}

public static JassExpr translate(ImCast imCast, ImToJassTranslator translator) {
return imCast.getExpr().translate(translator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ public static ImType getType(ImCompiletimeExpr e) {
return e.getExpr().attrTyp();
}

public static ImType getType(ImTypeVarDispatch e) {
return e.getTypeClassFunc().getReturnType();
}

public static ImType getType(ImCast imCast) {
return imCast.getToType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ public static boolean isEqualType(ImClassType c, ImType other) {
if (!x.getType().equalsType(y.getType())) {
return false;
}
if (!x.getTypeClassBinding().equals(y.getTypeClassBinding())) {
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ImType case_ImArrayType(ImArrayType t) {
public ImType case_ImClassType(ImClassType t) {
ImTypeArguments args = t.getTypeArguments()
.stream()
.map(ta -> JassIm.ImTypeArgument(ta.getType().match(this), ta.getTypeClassBinding()))
.map(ta -> JassIm.ImTypeArgument(ta.getType().match(this)))
.collect(Collectors.toCollection(JassIm::ImTypeArguments));
return JassIm.ImClassType(t.getClassDef(), args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,11 @@ public void check(Element e) {
ImFunction f = (ImFunction) e;
currentFunction = f;
checkType(e, (f).getReturnType());
} else if (e instanceof ImTypeClassFunc) {
checkType(e, ((ImTypeClassFunc) e).getReturnType());
} else if (e instanceof ImMethod) {
checkType(e, ((ImMethod) e).getMethodClass());
checkRooted(e, ((ImMethod) e).getImplementation());
} else if (e instanceof ImVarargLoop) {
checkRooted(e, ((ImVarargLoop) e).getLoopVar());
} else if (e instanceof ImTypeVarDispatch) {
checkRooted(e, ((ImTypeVarDispatch) e).getTypeClassFunc());
checkRooted(e, ((ImTypeVarDispatch) e).getTypeVariable());
} else if (e instanceof ImVarAccess) {
checkRooted(e, ((ImVarAccess) e).getVar());
} else if (e instanceof ImVarArrayAccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void createDestroyMethod(List<ClassDef> subClasses) {

private ImClassType imClassType() {
ImTypeArguments typeArgs = imClass.getTypeVariables().stream()
.map(tv -> JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv), Collections.emptyMap()))
.map(tv -> JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv)))
.collect(Collectors.toCollection(JassIm::ImTypeArguments));
return JassIm.ImClassType(imClass, typeArgs);
}
Expand Down Expand Up @@ -372,7 +372,7 @@ private void createNewFunc(ConstructorDef constr) {
}
ImTypeArguments typeArgs = ImTypeArguments();
for (ImTypeVar tv : imClass.getTypeVariables()) {
typeArgs.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv), Collections.emptyMap()));
typeArgs.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv)));
}
f.getBody().add(ImFunctionCall(trace, constrFunc, typeArgs, arguments, false, CallType.NORMAL));

Expand Down Expand Up @@ -412,7 +412,7 @@ private void createConstructFunc(ConstructorDef constr) {
// call classInitFunc:
ImTypeArguments typeArguments = JassIm.ImTypeArguments();
for (ImTypeVar tv : imClass.getTypeVariables()) {
typeArguments.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv), Collections.emptyMap()));
typeArguments.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(tv)));
}
f.getBody().add(ImFunctionCall(trace, classInitFunc, typeArguments, JassIm.ImExprs(JassIm.ImVarAccess(thisVar)), false, CallType.NORMAL));
// constructor user code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ImExpr translate() {
private ImTypeArguments getClassTypeArguments() {
ImTypeArguments res = JassIm.ImTypeArguments();
for (ImTypeVar typeVar : typeVars.keySet()) {
res.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(typeVar), Collections.emptyMap()));
res.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(typeVar)));
}
return res;
}
Expand Down Expand Up @@ -229,9 +229,7 @@ public ImType case_ImTypeVarRef(ImTypeVarRef t) {
result.put(oldTypevar, newTypevar);
c.getTypeVariables().add(newTypevar);
thisType.getTypeArguments().add(
JassIm.ImTypeArgument(
JassIm.ImTypeVarRef(newTypevar),
Collections.emptyMap()));
JassIm.ImTypeArgument(JassIm.ImTypeVarRef(newTypevar)));
}
return JassIm.ImTypeVarRef(newTypevar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void moveFunctionsOutOfClass(ImClass c) {
f.getTypeVariables().addAll(0, newTypeVars);
List<ImTypeArgument> typeArgs = newTypeVars
.stream()
.map(ta -> JassIm.ImTypeArgument(JassIm.ImTypeVarRef(ta), Collections.emptyMap()))
.map(ta -> JassIm.ImTypeArgument(JassIm.ImTypeVarRef(ta)))
.collect(Collectors.toList());
rewriteGenerics(f, new GenericTypes(typeArgs), c.getTypeVariables(), newTypeVars);
}
Expand Down Expand Up @@ -322,32 +322,6 @@ public void visit(ImDealloc e) {
super.visit(e);
}

@Override
public void visit(ImTypeVarDispatch e) {
super.visit(e);
ImTypeVar tv = e.getTypeVariable();
int index = newTypeVars.indexOf(tv);
if (index < 0) {
throw new CompileError(e.attrTrace(), "Could not find type variable " + tv + " in " + newTypeVars);
}
ImTypeArgument ta = generics.getTypeArguments().get(index);
Either<ImMethod, ImFunction> impl = ta.getTypeClassBinding().get(e.getTypeClassFunc());
if (impl == null) {
throw new CompileError(e.attrTrace(), "Could not find func " + e.getTypeClassFunc().getName() + " in " + ta.getTypeClassBinding().keySet());
}
ImExpr newExpr;
if (impl.isLeft()) {
ImMethod m = impl.getLeft();
ImExpr receiver = e.getArguments().remove(0);
e.getArguments().setParent(null);
newExpr = JassIm.ImMethodCall(e.getTrace(), m, JassIm.ImTypeArguments(), receiver, e.getArguments(), false);
} else {
ImFunction f = impl.get();
e.getArguments().setParent(null);
newExpr = JassIm.ImFunctionCall(e.getTrace(), f, JassIm.ImTypeArguments(), e.getArguments(), false, CallType.NORMAL);
}
e.replaceBy(newExpr);
}
});
}

Expand Down Expand Up @@ -697,7 +671,7 @@ public ImType case_ImClassType(ImClassType t) {
private List<ImTypeArgument> specializeTypeArgs(ImTypeArguments typeArgs) {
return typeArgs
.stream()
.map(ta -> JassIm.ImTypeArgument(specializeType(ta.getType()), ta.getTypeClassBinding()))
.map(ta -> JassIm.ImTypeArgument(specializeType(ta.getType())))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,11 @@ private static ImExpr translateFunctionCall(FunctionCall e, ImTranslator t, ImFu
dynamicDispatch = true;
}
// add implicit parameter to front
// TODO why would I add the implicit parameter here, if it is
// not a dynamic dispatch?
// note: might not be dynamic, in case of extension function)
leftExpr = (Expr) e.attrImplicitParameter();

if (leftExpr.attrTyp() instanceof WurstTypeTypeParam) {
WurstTypeTypeParam tp = (WurstTypeTypeParam) leftExpr.attrTyp();
typeParamDispatchOn = tp;
typeParamDispatchOn = (WurstTypeTypeParam) leftExpr.attrTyp();
}

}
Expand Down Expand Up @@ -517,11 +515,12 @@ private static ImExpr translateFunctionCall(FunctionCall e, ImTranslator t, ImFu

ImExpr call;
if (typeParamDispatchOn != null) {
ImTypeClassFunc typeClassFunc = t.getTypeClassFuncFor((FuncDef) calledFunc);
if (receiver != null) {
imArgs.add(0, receiver);
}
call = JassIm.ImTypeVarDispatch(e, typeClassFunc, imArgs, t.getTypeVar(typeParamDispatchOn.getDef()));

FuncDef calledFuncDef = (FuncDef) calledFunc;
ImMethod typeClassMethod = t.getTypeClassMethodFor(calledFuncDef);
ImTypeArguments typeArguments = getFunctionCallTypeArguments(t, e.attrFunctionSignature(), e, typeClassMethod.getImplementation().getTypeVariables());
ImVar typeClassDict = t.getTypeClassDict(typeParamDispatchOn, calledFuncDef);
call = JassIm.ImMethodCall(e, typeClassMethod, typeArguments, JassIm.ImVarAccess(typeClassDict), imArgs, false);
} else if (dynamicDispatch) {
ImMethod method = t.getMethodFor((FuncDef) calledFunc);
ImTypeArguments typeArguments = getFunctionCallTypeArguments(t, e.attrFunctionSignature(), e, method.getImplementation().getTypeVariables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
public class Flatten {


public static Result flatten(ImTypeVarDispatch e, ImTranslator t, ImFunction f) {
MultiResult r = flattenExprs(t, f, e.getArguments());
return new Result(r.stmts, ImTypeVarDispatch(e.getTrace(), e.getTypeClassFunc(), ImExprs(r.exprs), e.getTypeVariable()));
}

public static Result flatten(ImCast imCast, ImTranslator translator, ImFunction f) {
Result res = imCast.getExpr().flatten(translator, f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public boolean equals(Object o) {
if (!t1.getType().equalsType(t2.getType())) {
return false;
}
if (!t1.getTypeClassBinding().equals(t2.getTypeClassBinding())) {
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,6 @@ public static void print(ImVarargLoop e, Appendable sb, int indent) {
}


public static void print(ImTypeVarDispatch e, Appendable sb, int indent) {
append(sb, "<");
append(sb, e.getTypeVariable().getName());
append(sb, smallHash(e.getTypeVariable()));
append(sb, ">.");
append(sb, e.getTypeClassFunc().getName());
printArgumentList(sb, indent, e.getArguments());
}

public static void print(ImTypeVarRef e, Appendable sb, int indent) {
append(sb, e.getTypeVariable().getName());
append(sb, smallHash(e.getTypeVariable()));
Expand Down Expand Up @@ -552,9 +543,6 @@ public static String asString(List<?> s) {
.collect(Collectors.joining(", ")) + "]";
}

public static String asString(ImTypeClassFunc s) {
return s.getName() + smallHash(s);
}

public static String asString(ImClass s) {
return s.getName() + smallHash(s);
Expand All @@ -565,7 +553,7 @@ public static String asString(ImMethod s) {
}

public static String asString(ImTypeArgument s) {
return s.getType() + "" + s.getTypeClassBinding();
return s.getType().toString();
}

public static void print(ImCast e, Appendable sb, int indent) {
Expand Down
Loading

0 comments on commit e7ddf90

Please sign in to comment.