implement context analysis for struct field access expressions

This commit is contained in:
2020-02-04 21:30:17 +01:00
parent 6a7eb8fde2
commit 1ca3f5ca8b
2 changed files with 78 additions and 2 deletions

View File

@@ -1,7 +1,44 @@
package de.hsrm.compiler.Klang.helper;
import java.util.Map;
import de.hsrm.compiler.Klang.nodes.StructDefinition;
import de.hsrm.compiler.Klang.types.Type;
public class Helper {
public static String getErrorPrefix(int line, int col) {
return "Error in line " + line + ":" + col + " ";
public static String getErrorPrefix(int line, int col) {
return "Error in line " + line + ":" + col + " ";
}
public static Type drillType(Map<String, StructDefinition> structs, String name, String[] path, int pathIndex) {
// Search for the referenced field
var structDef = structs.get(name);
for (var field : structDef.fields) {
if (field.name.equals(path[pathIndex])) {
if (!field.type.isPrimitiveType()) {
// this references a struct!
// if we exhausted the path, this field type is our type
if (pathIndex == path.length - 1) {
return field.type;
}
// we did not exhaust the path, go on
return drillType(structs, field.type.getName(), path, pathIndex + 1);
} else {
// this references a primitive, we hit bedrock!
// make sure we exhausted the complete path
if (pathIndex < path.length - 1) {
throw new RuntimeException(field.name + " must be a struct but is of type " + field.type.getName() + ":");
}
// hooray, we exhausted the path, this field type is our type
return field.type;
}
}
}
throw new RuntimeException("Struct " + structDef.name + " does not contain field " + path[pathIndex]);
}
}