Skip to content
Snippets Groups Projects
Commit 5df8f15b authored by Jan Gruteser's avatar Jan Gruteser
Browse files

replace unifications and setType in visitOpApplNode

parent c96c767f
No related branches found
No related tags found
No related merge requests found
...@@ -232,72 +232,52 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo ...@@ -232,72 +232,52 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo
} }
private TLAType visitOpApplNode(OpApplNode n, TLAType expected) throws TLA2BException { private TLAType visitOpApplNode(OpApplNode n, TLAType expected) throws TLA2BException {
switch (n.getOperator().getKind()) { switch (n.getOperator().getKind()) {
case ConstantDeclKind: { case ConstantDeclKind: {
OpDeclNode con = (OpDeclNode) n.getOperator(); OpDeclNode con = (OpDeclNode) n.getOperator();
TLAType c = getType(con);
TLAType c = (TLAType) con.getToolObject(TYPE_ID);
if (c == null) { if (c == null) {
throw new TypeErrorException(con.getName() + " has no type yet!"); throw new TypeErrorException(con.getName() + " has no type yet!");
} }
try { return unifyAndSetType(c, expected, con.getName().toString(), con);
TLAType result = expected.unify(c);
con.setToolObject(TYPE_ID, result);
return result;
} catch (UnificationException e) {
throw new TypeErrorException(String.format("Expected %s, found %s at constant '%s',%n%s", expected, c,
con.getName(), n.getLocation())
);
}
} }
case VariableDeclKind: { case VariableDeclKind: {
SymbolNode symbolNode = n.getOperator(); SymbolNode symbolNode = n.getOperator();
String vName = symbolNode.getName().toString(); String vName = symbolNode.getName().toString();
TLAType v = (TLAType) symbolNode.getToolObject(TYPE_ID); TLAType v = getType(symbolNode);
if (v == null) { if (v == null) {
SymbolNode var = this.specAnalyser.getSymbolNodeByName(vName); SymbolNode var = this.specAnalyser.getSymbolNodeByName(vName);
if (var != null) { if (var != null) {
// symbolNode is variable of an expression, e.g. v + 1 // symbolNode is variable of an expression, e.g. v + 1
v = (TLAType) var.getToolObject(TYPE_ID); v = getType(var);
} else { } else {
throw new TypeErrorException(vName + " has no type yet!"); throw new TypeErrorException(vName + " has no type yet!");
} }
} }
try { return unifyAndSetType(v, expected, vName, n);
TLAType result = expected.unify(v);
symbolNode.setToolObject(TYPE_ID, result);
return result;
} catch (UnificationException e) {
throw new TypeErrorException(String.format("Expected %s, found %s at variable '%s',%n%s", expected, v,
vName, n.getLocation()));
}
} }
case BuiltInKind: { case BuiltInKind:
return evalBuiltInKind(n, expected); return evalBuiltInKind(n, expected);
}
case FormalParamKind: { case FormalParamKind: {
SymbolNode symbolNode = n.getOperator(); SymbolNode symbolNode = n.getOperator();
String pName = symbolNode.getName().toString(); TLAType t = getType(symbolNode, paramId);
TLAType t = (TLAType) symbolNode.getToolObject(paramId); if (t == null) { // no temp type
if (t == null) { t = getType(symbolNode);
t = (TLAType) symbolNode.getToolObject(TYPE_ID); if (t == null) { // no type at all
}
if (t == null) {
t = new UntypedType(); // TODO is this correct? t = new UntypedType(); // TODO is this correct?
// throw new RuntimeException(); // throw new RuntimeException();
} }
}
try { try {
TLAType result = expected.unify(t); TLAType result = expected.unify(t);
symbolNode.setToolObject(paramId, result); setType(symbolNode, result, paramId);
return result; return result;
} catch (UnificationException e) { } catch (UnificationException e) {
throw new TypeErrorException(String.format("Expected %s, found %s at parameter '%s',%n%s", expected, t, throw new TypeErrorException(String.format("Expected %s, found %s at parameter '%s',%n%s", expected, t,
pName, n.getLocation())); symbolNode.getName(), n.getLocation()));
} }
} }
...@@ -310,44 +290,35 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo ...@@ -310,44 +290,35 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo
return evalBBuiltIns(n, expected); return evalBBuiltIns(n, expected);
} }
TLAType found = ((TLAType) def.getToolObject(TYPE_ID)); TLAType found = getType(def);
if (found == null) { if (found == null) {
found = new UntypedType(); found = new UntypedType();
// throw new RuntimeException(def.getName() + // throw new RuntimeException(def.getName() + " has no type yet!");
// " has no type yet!");
} }
if (n.getArgs().length != 0) { if (n.getArgs().length != 0) {
found = found.cloneTLAType(); found = found.cloneTLAType();
} }
found = unify(found, expected, def.getName().toString(), def);
try {
found = found.unify(expected);
} catch (UnificationException e) {
throw new TypeErrorException(String.format("Expected %s, found %s at definition '%s',%n%s", expected,
found, def.getName(), n.getLocation()));
}
boolean untyped = false; boolean untyped = false;
FormalParamNode[] params = def.getParams(); FormalParamNode[] params = def.getParams();
for (int i = 0; i < n.getArgs().length; i++) { for (int i = 0; i < n.getArgs().length; i++) {
// clone the parameter type, because the parameter type is not // clone the parameter type, because the parameter type is not
// set/changed at a definition call // set/changed at a definition call
FormalParamNode p = params[i]; FormalParamNode p = params[i];
TLAType pType = ((TLAType) p.getToolObject(TYPE_ID)); TLAType pType = getType(p);
if (pType == null) { if (pType == null) {
pType = new UntypedType(); pType = new UntypedType();
// throw new RuntimeException("Parameter " + p.getName() // throw new RuntimeException("Parameter " + p.getName() + " has no type yet!%n" + p.getLocation());
// + " has no type yet!%n" + p.getLocation());
} }
pType = pType.cloneTLAType(); pType = pType.cloneTLAType();
if (pType.isUntyped()) if (pType.isUntyped())
untyped = true; untyped = true;
pType = visitExprOrOpArgNode(n.getArgs()[i], pType); // unify pType = visitExprOrOpArgNode(n.getArgs()[i], pType);
// both // unify both types,
// types // set types of the arguments of the definition call to the parameters for reevaluation the def body
// set types of the arguments of the definition call to the setType(p, pType, TEMP_TYPE_ID);
// parameters for reevaluation the def body
p.setToolObject(TEMP_TYPE_ID, pType);
} }
if (found.isUntyped() || untyped || !def.getInRecursive()) { if (found.isUntyped() || untyped || !def.getInRecursive()) {
...@@ -357,19 +328,15 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo ...@@ -357,19 +328,15 @@ public class TypeChecker extends BuiltInOPs implements BBuildIns, TranslationGlo
paramId = TYPE_ID; paramId = TYPE_ID;
} }
n.setToolObject(TYPE_ID, found); setType(n, found);
return found; return found;
} }
default: { default:
throw new NotImplementedException(n.getOperator().getName().toString()); throw new NotImplementedException(n.getOperator().getName().toString());
} }
} }
}
private TLAType evalBuiltInKind(OpApplNode n, TLAType expected) throws TLA2BException { private TLAType evalBuiltInKind(OpApplNode n, TLAType expected) throws TLA2BException {
switch (getOpCode(n.getOperator().getName())) { switch (getOpCode(n.getOperator().getName())) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment