WIP: Add enum support

This commit is contained in:
2023-03-15 15:55:41 +01:00
parent 7c40a50196
commit 7af815042b
28 changed files with 436 additions and 255 deletions

View File

@@ -15,8 +15,9 @@ import de.hsrm.compiler.Klang.types.Type;
public class ContextAnalysis extends KlangBaseVisitor<Node> {
Map<String, VariableDeclaration> vars = new HashMap<>();
Map<String, FunctionInformation> funcs;
Map<String, StructDefinition> structs;
Map<String, FunctionInformation> functionDefs;
Map<String, StructDefinition> structDefs;
Map<String, EnumDefinition> enumDefs;
Type currentDeclaredReturnType;
String currentFunctionDefinitionName;
@@ -27,9 +28,14 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
}
public ContextAnalysis(Map<String, FunctionInformation> funcs, Map<String, StructDefinition> structs) {
this.funcs = funcs;
this.structs = structs;
public ContextAnalysis(
Map<String, FunctionInformation> functionDefs,
Map<String, StructDefinition> structDefs,
Map<String, EnumDefinition> enumDefs
) {
this.functionDefs = functionDefs;
this.structDefs = structDefs;
this.enumDefs = enumDefs;
}
@Override
@@ -41,7 +47,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
Expression expression = (Expression) this.visit(ctx.expression());
Program result = new Program(funcs, this.structs, expression);
Program result = new Program(funcs, structDefs, enumDefs, expression);
result.type = expression.type;
result.line = ctx.start.getLine();
result.col = ctx.start.getCharPositionInLine();
@@ -67,7 +73,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
statements[i] = (Statement) currentStatement;
actualStatementCount += 1;
// We use the existance of a type to indicate that this statement returns
// We use the existence of a type to indicate that this statement returns
// something for which the VariableDeclaration is an exception
if (currentStatement.type != null && !(currentStatement instanceof VariableDeclaration)) {
// check whether the type matches
@@ -89,9 +95,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
// create a shorter statements array and copy the statements to there
if (actualStatementCount < declaredStatementCount) {
Statement[] newStatements = new Statement[actualStatementCount];
for (int i = 0; i < actualStatementCount; i++) {
newStatements[i] = statements[i];
}
System.arraycopy(statements, 0, newStatements, 0, actualStatementCount);
statements = newStatements;
}
@@ -177,7 +181,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int col = ctx.start.getCharPositionInLine();
Type declaredType = Type.getByName(ctx.type_annotation().type().getText());
if (!declaredType.isPrimitiveType() && this.structs.get(declaredType.getName()) == null) {
if (!declaredType.isPrimitiveType() && this.structDefs.get(declaredType.getName()) == null) {
String error = "Type " + declaredType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
@@ -291,7 +295,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
String structName = variableDef.type.getName();
Type fieldType;
try {
fieldType = Helper.drillType(this.structs, structName, path, 0);
fieldType = Helper.drillType(this.structDefs, structName, path, 0);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
@@ -338,7 +342,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
String structName = variableDef.type.getName();
Type resultType;
try {
resultType = Helper.drillType(this.structs, structName, path, 0);
resultType = Helper.drillType(this.structDefs, structName, path, 0);
} catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
}
@@ -708,7 +712,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
@Override
public Node visitBoolAtom(KlangParser.BoolAtomContext ctx) {
Node n = new BooleanExpression(ctx.getText().equals("true") ? true : false);
Node n = new BooleanExpression(ctx.getText().equals("true"));
n.type = Type.getBooleanType();
n.line = ctx.start.getLine();
n.col = ctx.start.getCharPositionInLine();
@@ -733,7 +737,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
this.currentDeclaredReturnType = returnType;
this.currentFunctionDefinitionName = name;
if (!returnType.isPrimitiveType() && this.structs.get(returnType.getName()) == null) {
if (!returnType.isPrimitiveType() && this.structDefs.get(returnType.getName()) == null) {
String error = "Type " + returnType.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
@@ -779,7 +783,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int col = ctx.start.getCharPositionInLine();
Type type = Type.getByName(ctx.type_annotation().type().getText());
if (!type.isPrimitiveType() && this.structs.get(type.getName()) == null) {
if (!type.isPrimitiveType() && this.structDefs.get(type.getName()) == null) {
String error = "Type " + type.getName() + " not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
@@ -797,7 +801,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
FunctionInformation func = this.funcs.get(name);
FunctionInformation func = this.functionDefs.get(name);
if (func == null) {
String error = "Function with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
@@ -835,7 +839,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int col = ctx.start.getCharPositionInLine();
// Get the corresponding struct definition
var struct = this.structs.get(name);
var struct = this.structDefs.get(name);
if (struct == null) {
String error = "Struct with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);