implement symboltable

This commit is contained in:
ChUrl
2020-12-13 14:48:51 +01:00
parent d949a7fa8b
commit 36241a2699
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package typechecker;
public class SymbolAlreadyDefinedException extends RuntimeException {
public SymbolAlreadyDefinedException(String message) {
super(message);
}
}

View File

@ -0,0 +1,81 @@
package typechecker;
import parser.ast.AST;
import parser.ast.Node;
import java.util.HashMap;
import java.util.Map;
import static util.Logger.log;
public class SymbolTable {
private final Map<String, String> symbolTable;
private final Map<String, String> methodTable;
public SymbolTable(Map<String, String> symbolTable) {
this.symbolTable = symbolTable;
final Map<String, String> methodTable = new HashMap<>();
methodTable.put("ADD", "INTEGER_TYPE");
methodTable.put("SUB", "INTEGER_TYPE");
methodTable.put("MUL", "INTEGER_TYPE");
methodTable.put("DIV", "INTEGER_TYPE");
methodTable.put("MOD", "INTEGER_TYPE");
methodTable.put("NOT", "BOOLEAN_TYPE");
methodTable.put("AND", "BOOLEAN_TYPE");
methodTable.put("OR", "BOOLEAN_TYPE");
methodTable.put("LESS", "BOOLEAN_TYPE");
methodTable.put("LESS_EQUAL", "BOOLEAN_TYPE");
methodTable.put("GREATER", "BOOLEAN_TYPE");
methodTable.put("GREATER_EQUAL", "BOOLEAN_TYPE");
methodTable.put("EQUAL", "BOOLEAN_TYPE");
methodTable.put("NOT_EQUAL", "BOOLEAN_TYPE");
this.methodTable = methodTable;
}
public static SymbolTable fromAST(AST tree) {
final Map<String, String> tableOut = new HashMap<>();
log("Creating SymbolTable");
scanTree(tree.getRoot(), tableOut);
log("-".repeat(100));
return new SymbolTable(tableOut);
}
private static void scanTree(Node root, Map<String, String> table) {
for (Node child : root.getChildren()) {
scanTree(child, table);
}
if ("DECLARATION".equals(root.getName())) {
Node left = root.getChildren().get(0);
Node right = root.getChildren().get(1);
log("Adding Entry " + right.getValue() + " -> " + left.getName());
String oldEntry = table.put(right.getValue(), left.getName());
if (oldEntry != null) {
System.out.println("Typfehler - Symbol bereits definiert: " + right.getValue());
throw new SymbolAlreadyDefinedException("Das Symbol " + right.getValue() + " wurde bereits deklariert.");
}
}
}
public String getSymbolType(String sym) {
return this.symbolTable.get(sym);
}
public String getMethodType(String meth) {
return this.methodTable.get(meth);
}
public int getSymbolCount() {
return this.symbolTable.size();
}
}