Remove FunctionInformation and replace it with FunctionDefinition.

This commit is contained in:
2023-03-15 16:19:42 +01:00
parent 7af815042b
commit 6e4431652c
5 changed files with 39 additions and 59 deletions

View File

@@ -1,9 +1,5 @@
package de.hsrm.compiler.Klang;
import java.util.Map;
import java.util.HashMap;
import de.hsrm.compiler.Klang.helper.FunctionInformation;
import de.hsrm.compiler.Klang.helper.Helper;
import de.hsrm.compiler.Klang.nodes.*;
import de.hsrm.compiler.Klang.nodes.expressions.*;
@@ -13,9 +9,12 @@ import de.hsrm.compiler.Klang.nodes.loops.WhileLoop;
import de.hsrm.compiler.Klang.nodes.statements.*;
import de.hsrm.compiler.Klang.types.Type;
import java.util.HashMap;
import java.util.Map;
public class ContextAnalysis extends KlangBaseVisitor<Node> {
Map<String, VariableDeclaration> vars = new HashMap<>();
Map<String, FunctionInformation> functionDefs;
Map<String, FunctionDefinition> functionDefs;
Map<String, StructDefinition> structDefs;
Map<String, EnumDefinition> enumDefs;
Type currentDeclaredReturnType;
@@ -29,7 +28,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
}
public ContextAnalysis(
Map<String, FunctionInformation> functionDefs,
Map<String, FunctionDefinition> functionDefs,
Map<String, StructDefinition> structDefs,
Map<String, EnumDefinition> enumDefs
) {
@@ -801,15 +800,15 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
int line = ctx.start.getLine();
int col = ctx.start.getCharPositionInLine();
FunctionInformation func = this.functionDefs.get(name);
if (func == null) {
var functionDef = this.functionDefs.get(name);
if (functionDef == null) {
String error = "Function with name \"" + name + "\" not defined.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
// Make sure the number of arguments matches the number of parameters
int argCount = ctx.functionCall().arguments().expression().size();
int paramCount = func.parameters.size();
int paramCount = functionDef.parameters.length;
if (argCount != paramCount) {
String error = "Function \"" + name + "\" expects " + paramCount + " parameters, but got " + argCount + ".";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
@@ -819,14 +818,14 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
Expression[] args = new Expression[argCount];
for (int i = 0; i < argCount; i++) {
Expression expression = (Expression) this.visit(ctx.functionCall().arguments().expression(i));
if (!expression.type.equals(func.signature[i])) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + "argument " + i + " Expected " + func.signature[i].getName() + " but got: " + expression.type.getName());
if (!expression.type.equals(functionDef.parameters[i].type)) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + "argument " + i + " Expected " + functionDef.parameters[i].type.getName() + " but got: " + expression.type.getName());
}
args[i] = expression;
}
FunctionCall result = new FunctionCall(name, args);
result.type = func.returnType;
result.type = functionDef.type;
result.line = line;
result.col = col;
return result;