From 673117132daa9c4fdd103189b5cd9a32a3731f5a Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 13:08:54 +0200 Subject: remove duplicates --- src/Main.java | 5 ++++- src/ParseAll.java | 6 +++++- src/ast/nodes/AtomNode.java | 2 +- src/semanticanalysis/Share.java | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/semanticanalysis/Share.java diff --git a/src/Main.java b/src/Main.java index 24f4c4e..04f7183 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -11,6 +12,7 @@ import ast.*; import ast.nodes.*; import parser.*; import semanticanalysis.*; +import semanticanalysis.Share; public class Main { @@ -53,7 +55,8 @@ public class Main { Python3VisitorImpl visitor = new Python3VisitorImpl(); SymbolTable ST = new SymbolTable(); Node ast = visitor.visit(tree); - ArrayList errors = ast.checkSemantics(ST, 0); + ArrayList errorsWithDup = ast.checkSemantics(ST, 0); + ArrayList errors = Share.removeDuplicates(errorsWithDup); if (errors.size() > 0) { System.out.println("You had " + errors.size() + " errors:"); for (SemanticError e : errors) { diff --git a/src/ParseAll.java b/src/ParseAll.java index 87b4ca3..2662a85 100644 --- a/src/ParseAll.java +++ b/src/ParseAll.java @@ -1,3 +1,4 @@ + import java.io.File; import java.util.ArrayList; import java.util.Objects; @@ -9,8 +10,10 @@ import parser.Python3Lexer; import parser.Python3Parser; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; +import semanticanalysis.Share; public class ParseAll { + @SuppressWarnings("unused") public static void main(String[] args) { new File("./trees/").mkdirs(); @@ -36,7 +39,8 @@ public class ParseAll { Python3VisitorImpl visitor = new Python3VisitorImpl(); SymbolTable ST = new SymbolTable(); Node ast = visitor.visit(tree); - ArrayList errors = ast.checkSemantics(ST, 0); + ArrayList errorsWithDup = ast.checkSemantics(ST, 0); + ArrayList errors = Share.removeDuplicates(errorsWithDup); if (errors.size() > 0) { System.out.println(); System.out.println(fileStr); diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index ceafc07..58cd7f9 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -27,7 +27,7 @@ public class AtomNode implements Node { var errors = new ArrayList(); if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { - errors.add(new SemanticError("'" + this.getId() + "' is not defined.")); + errors.add(new SemanticError("name '" + this.getId() + "' is not defined.")); } return errors; diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java new file mode 100644 index 0000000..c1f03c2 --- /dev/null +++ b/src/semanticanalysis/Share.java @@ -0,0 +1,37 @@ +package semanticanalysis; + +import java.util.ArrayList; + +public class Share { + + public static ArrayList removeDuplicates(ArrayList list) { + ArrayList newList = new ArrayList(); + + for (T element : list) { + if (!customContains(newList, element)) { + newList.add(element); + } + } + return newList; + } + + public static boolean customContains(ArrayList list, T e) { + String e1 = e.toString(); + for (T element : list) { + String e2 = element.toString(); + if (e2.equals(e1)) { + return true; + } + } + return false; + } + + public static String getExtension(String fileName) { + int extensionIndex = fileName.lastIndexOf('.'); + if (extensionIndex == -1) { + return fileName; + } else { + return fileName.substring(extensionIndex + 1); + } + } +} -- cgit v1.2.3-18-g5258