summaryrefslogtreecommitdiff
path: root/src/semanticanalysis
diff options
context:
space:
mode:
Diffstat (limited to 'src/semanticanalysis')
-rw-r--r--src/semanticanalysis/STentry.java13
-rw-r--r--src/semanticanalysis/SemanticError.java4
-rw-r--r--src/semanticanalysis/Share.java58
-rw-r--r--src/semanticanalysis/SymbolTable.java44
4 files changed, 98 insertions, 21 deletions
diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java
index 0e4b00e..51fb109 100644
--- a/src/semanticanalysis/STentry.java
+++ b/src/semanticanalysis/STentry.java
@@ -6,9 +6,10 @@ import ast.types.Type;
* Entry class for the symbol table.
*/
public class STentry {
- private Type type;
- private int offset;
- private int nesting;
+
+ private final Type type;
+ private final int offset;
+ private final int nesting;
private String label;
public STentry(Type type, int offset, int nesting) {
@@ -52,4 +53,10 @@ public class STentry {
return label;
}
+ @Override
+ public String toString() {
+ // Print all the fields of the STentry
+ return "Type: " + type + ", Offset: " + offset + ", Nesting: " + nesting + ", Label: " + label;
+ }
+
}
diff --git a/src/semanticanalysis/SemanticError.java b/src/semanticanalysis/SemanticError.java
index 745e3fb..ac2dc4c 100644
--- a/src/semanticanalysis/SemanticError.java
+++ b/src/semanticanalysis/SemanticError.java
@@ -4,12 +4,14 @@ package semanticanalysis;
* Class respresents a semantic error.
*/
public class SemanticError {
- private String msg;
+
+ private final String msg;
public SemanticError(String msg) {
this.msg = msg;
}
+ @Override
public String toString() {
return msg;
}
diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java
new file mode 100644
index 0000000..7542cf7
--- /dev/null
+++ b/src/semanticanalysis/Share.java
@@ -0,0 +1,58 @@
+package semanticanalysis;
+
+import java.io.*;
+import java.util.*;
+
+public class Share {
+
+ /**
+ * Removes the duplicate elements in a list of Semantic Errors. It's not
+ * generic because it's used a custom contains function.
+ */
+ public static ArrayList<SemanticError> removeDuplicates(ArrayList<SemanticError> list) {
+ ArrayList<SemanticError> newList = new ArrayList();
+
+ for (SemanticError element : list) {
+ if (!customContains(newList, element)) {
+ newList.add(element);
+ }
+ }
+ return newList;
+ }
+
+ /**
+ * Normal contains did not work, so we made a custom contains function.
+ * Returns `true` if the String rappresentation of an object in the list is
+ * equal to the element given in input.
+ */
+ private static boolean customContains(ArrayList<SemanticError> list, SemanticError e) {
+ String e1 = e.toString();
+ for (SemanticError 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);
+ }
+ }
+
+ public static String readFile(String filePath) throws IOException {
+ StringBuilder content = new StringBuilder();
+ try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ content.append(line).append("\n");
+ }
+ }
+ return content.toString();
+ }
+}
diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java
index f2876db..b620af7 100644
--- a/src/semanticanalysis/SymbolTable.java
+++ b/src/semanticanalysis/SymbolTable.java
@@ -1,8 +1,8 @@
package semanticanalysis;
+import ast.types.*;
import java.util.ArrayList;
import java.util.HashMap;
-import ast.types.*;
/**
* Class representing a symbol table. It's a list of hash table symbol table. We
@@ -11,12 +11,12 @@ import ast.types.*;
*/
public class SymbolTable {
- private ArrayList<HashMap<String, STentry>> symbolTable;
- private ArrayList<Integer> offset;
+ private final ArrayList<HashMap<String, STentry>> symbolTable;
+ private final ArrayList<Integer> offset;
public SymbolTable() {
- this.symbolTable = new ArrayList<HashMap<String, STentry>>();
- this.offset = new ArrayList<Integer>();
+ this.symbolTable = new ArrayList();
+ this.offset = new ArrayList();
}
/**
@@ -40,8 +40,8 @@ public class SymbolTable {
HashMap<String, STentry> H = this.symbolTable.get(n);
T = H.get(id);
if (T != null) {
- found = true;
- }else {
+ found = true;
+ } else {
n = n - 1;
}
}
@@ -60,8 +60,8 @@ public class SymbolTable {
while ((n >= 0) && !found) {
HashMap<String, STentry> H = this.symbolTable.get(n);
if (H.get(id) != null) {
- found = true;
- }else {
+ found = true;
+ } else {
n = n - 1;
}
}
@@ -96,7 +96,7 @@ public class SymbolTable {
*/
public boolean top_lookup(String id) {
int n = symbolTable.size() - 1;
- STentry T = null;
+ STentry T;
HashMap<String, STentry> H = symbolTable.get(n);
T = H.get(id);
return (T != null);
@@ -125,15 +125,10 @@ public class SymbolTable {
// We always increment the offset by 1 otherwise we need ad-hoc bytecode
// operations
- if (type.getClass().equals((new BoolType()).getClass())) {
- offs = offs + 1;
- } else if (type.getClass().equals((new IntType()).getClass())) {
- offs = offs + 1;
- } else {
- offs = offs + 1;
- }
+ offs = offs + 1;
this.offset.add(offs);
+
}
/**
@@ -149,4 +144,19 @@ public class SymbolTable {
this.offset.add(offs);
}
+ @Override
+ public String toString() {
+ // Print the symbol table
+ String str = "";
+ for (int i = 0; i < this.symbolTable.size(); i++) {
+ str += "Level " + i + "\n";
+ HashMap<String, STentry> H = this.symbolTable.get(i);
+ for (String key : H.keySet()) {
+ STentry T = H.get(key);
+ str += key + " -> " + T.toString() + "\n";
+ }
+ }
+ return str;
+ }
+
}