From 19daaa63af084b1f969b501e4a1380f128fab982 Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 4 Feb 2020 21:44:04 +0100 Subject: [PATCH] use GetStructNames instead of collecting the names in GetStructs --- src/main/java/de/hsrm/compiler/Klang/GetStructs.java | 12 ++---------- src/main/java/de/hsrm/compiler/Klang/Klang.java | 7 ++++++- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/GetStructs.java b/src/main/java/de/hsrm/compiler/Klang/GetStructs.java index 878c8a3..ca1dc2f 100644 --- a/src/main/java/de/hsrm/compiler/Klang/GetStructs.java +++ b/src/main/java/de/hsrm/compiler/Klang/GetStructs.java @@ -2,7 +2,6 @@ package de.hsrm.compiler.Klang; import java.util.Map; import java.util.Set; -import java.util.HashSet; import de.hsrm.compiler.Klang.helper.Helper; import de.hsrm.compiler.Klang.nodes.Node; @@ -16,9 +15,9 @@ public class GetStructs extends KlangBaseVisitor { private Set structNames; private Map structs; - public GetStructs(Map structs) { + public GetStructs(Set structNames, Map structs) { this.structs = structs; - this.structNames = new HashSet<>(); + this.structNames = structNames; } @Override @@ -36,13 +35,6 @@ public class GetStructs extends KlangBaseVisitor { int line = ctx.start.getLine(); int col = ctx.start.getCharPositionInLine(); StructField[] fields = new StructField[ctx.structField().size()]; - - if (this.structNames.contains(name)) { - String error = "Struct " + name + " defined multiple times."; - throw new Error(Helper.getErrorPrefix(line, col) + error); - } - - this.structNames.add(name); for (int i = 0; i < ctx.structField().size(); i++) { StructField field = (StructField) this.visit(ctx.structField(i)); diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index a321c29..8b63cd1 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -8,6 +8,7 @@ import java.io.*; import java.util.Arrays; import java.util.List; import java.util.HashMap; +import java.util.HashSet; import de.hsrm.compiler.Klang.nodes.Node; import de.hsrm.compiler.Klang.nodes.StructDefinition; @@ -91,9 +92,13 @@ public class Klang { var functionDefinitions = new HashMap(); new GetFunctions(functionDefinitions).visit(tree); + // Extract names of all structs + var structNames = new HashSet(); + new GetStructNames(structNames).visit(tree); + // Extract information about all structs var structs = new HashMap(); - new GetStructs(structs).visit(tree); + new GetStructs(structNames, structs).visit(tree); // Create the DAST ContextAnalysis ctxAnal = new ContextAnalysis(functionDefinitions, structs);