Use LinkedHashMaps and LinkedHashSets to preserve the order of parameters and struct fields.

This commit is contained in:
2023-03-15 23:08:38 +01:00
parent 6fd3f5a2e6
commit 22634c9652
2 changed files with 5 additions and 10 deletions

View File

@@ -5,10 +5,7 @@ import de.hsrm.compiler.Klang.nodes.*;
import de.hsrm.compiler.Klang.types.NamedType; import de.hsrm.compiler.Klang.types.NamedType;
import de.hsrm.compiler.Klang.types.Type; import de.hsrm.compiler.Klang.types.Type;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class GetDefinitions extends KlangBaseVisitor<Node> { public class GetDefinitions extends KlangBaseVisitor<Node> {
private final Map<String, FunctionDefinition> functionDefs; private final Map<String, FunctionDefinition> functionDefs;
@@ -110,7 +107,7 @@ public class GetDefinitions extends KlangBaseVisitor<Node> {
} }
// IDENT() includes the enumName as the first entry, which we skip // IDENT() includes the enumName as the first entry, which we skip
var enumFields = new HashSet<String>(); var enumFields = new LinkedHashSet<String>();
for (int i = 1; i < ctx.IDENT().size(); i++) { for (int i = 1; i < ctx.IDENT().size(); i++) {
var currentEnumField = ctx.IDENT(i); var currentEnumField = ctx.IDENT(i);
var currentEnumFieldName = currentEnumField.getText(); var currentEnumFieldName = currentEnumField.getText();
@@ -136,7 +133,7 @@ public class GetDefinitions extends KlangBaseVisitor<Node> {
public Node visitStructDef(KlangParser.StructDefContext ctx) { public Node visitStructDef(KlangParser.StructDefContext ctx) {
var structName = ctx.structName.getText(); var structName = ctx.structName.getText();
var structFieldCount = ctx.structField().size(); var structFieldCount = ctx.structField().size();
var structFields = new HashMap<String, StructField>(); var structFields = new LinkedHashMap<String, StructField>();
var line = ctx.start.getLine(); var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine(); var col = ctx.start.getCharPositionInLine();
@@ -178,7 +175,7 @@ public class GetDefinitions extends KlangBaseVisitor<Node> {
public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) { public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) {
var funcName = ctx.funcName.getText(); var funcName = ctx.funcName.getText();
var paramCount = ctx.params.parameter().size(); var paramCount = ctx.params.parameter().size();
var parameters = new HashMap<String, Parameter>(); var parameters = new LinkedHashMap<String, Parameter>();
var line = ctx.start.getLine(); var line = ctx.start.getLine();
var col = ctx.start.getCharPositionInLine(); var col = ctx.start.getCharPositionInLine();

View File

@@ -1,7 +1,5 @@
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.jupiter.api.Test;
import de.hsrm.compiler.Klang.ContextAnalysis; import de.hsrm.compiler.Klang.ContextAnalysis;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;