From a79a2332a1abf27264f051fa5b2824f824d87421 Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 4 Feb 2020 21:43:22 +0100 Subject: [PATCH] implement visitor to collect all struct names --- .../hsrm/compiler/Klang/GetStructNames.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/de/hsrm/compiler/Klang/GetStructNames.java diff --git a/src/main/java/de/hsrm/compiler/Klang/GetStructNames.java b/src/main/java/de/hsrm/compiler/Klang/GetStructNames.java new file mode 100644 index 0000000..0e5fcb3 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/GetStructNames.java @@ -0,0 +1,37 @@ +package de.hsrm.compiler.Klang; + +import java.util.Set; + +import de.hsrm.compiler.Klang.helper.Helper; + +public class GetStructNames extends KlangBaseVisitor { + private Set structNames; + + public GetStructNames(Set structNames) { + this.structNames = structNames; + } + + @Override + public Void visitProgram(KlangParser.ProgramContext ctx) { + for (int i = 0; i < ctx.structDef().size(); i++) { + this.visit(ctx.structDef(i)); + } + + return null; + } + + @Override + public Void visitStructDef(KlangParser.StructDefContext ctx) { + String name = ctx.structName.getText(); + int line = ctx.start.getLine(); + int col = ctx.start.getCharPositionInLine(); + + if (this.structNames.contains(name)) { + String error = "Struct " + name + " defined multiple times."; + throw new Error(Helper.getErrorPrefix(line, col) + error); + } + + this.structNames.add(name); + return null; + } +} \ No newline at end of file