summaryrefslogtreecommitdiff
path: root/submit/SymbolTable.java
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
commita1c15f046183373baf5deb66e77188e656806fb7 (patch)
treefc1a7f584e67dc583d335f09d5271a45ff7d9df4 /submit/SymbolTable.java
parent5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (diff)
downloadcminus-main.tar.gz
cminus-main.zip
squash all the thingsHEADmain
Diffstat (limited to 'submit/SymbolTable.java')
-rw-r--r--submit/SymbolTable.java67
1 files changed, 61 insertions, 6 deletions
diff --git a/submit/SymbolTable.java b/submit/SymbolTable.java
index 7ca6b27..09251bc 100644
--- a/submit/SymbolTable.java
+++ b/submit/SymbolTable.java
@@ -3,6 +3,7 @@ package submit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import submit.ast.VarType;
/*
* Code formatter project
@@ -17,14 +18,71 @@ public class SymbolTable {
private SymbolTable parent;
private final List<SymbolTable> children;
+ private int offset;
+
+ public static int LABEL_IDENTIFIER = 0;
+
+ public static String nextId() {
+ return Integer.toString(SymbolTable.LABEL_IDENTIFIER++);
+ }
+
public SymbolTable() {
+ offset = 0;
table = new HashMap<>();
parent = null;
children = new ArrayList<>();
+
+ this.addGlobalSymbols();
+ }
+
+ public List<String> symbolNames() { return new ArrayList<>(table.keySet()); }
+
+ public void addGlobalSymbols() {
+ SymbolInfo println = new SymbolInfo("println", null, true);
+ this.addSymbol("println", println);
+ }
+
+ public void addSymbol(String id, SymbolInfo symbol) { table.put(id, symbol); }
+
+ public int addOffset(int n) {
+ offset -= 4 * n;
+ return offset;
+ }
+
+ public int getOffset() { return this.offset; }
+
+ // Add symbols in before and reorder offsets such that symbols in before have
+ // a "higher" offset
+ public void addOtherTableBefore(SymbolTable before) {
+ offset = 0;
+ List<String> thisSymbols = symbolNames();
+
+ for (String id : before.symbolNames()) {
+ SymbolInfo symbol = before.find(id);
+ if (!symbol.isFunction()) {
+ addOffset(1);
+ symbol.setOffset(offset);
+ }
+ addSymbol(id, symbol);
+ }
+
+ for (String id : thisSymbols) {
+ SymbolInfo symbol = table.get(id);
+ if (!symbol.isFunction()) {
+ addOffset(1);
+ table.get(id).setOffset(offset);
+ }
+ }
}
- public void addSymbol(String id, SymbolInfo symbol) {
- table.put(id, symbol);
+ public int offsetOf(String id) {
+ if (table.containsKey(id)) {
+ return table.get(id).getOffset();
+ }
+ if (parent != null) {
+ return -parent.getOffset() + parent.offsetOf(id);
+ }
+ return 0; // This shouldn't happen :D
}
/**
@@ -56,8 +114,5 @@ public class SymbolTable {
return child;
}
- public SymbolTable getParent() {
- return parent;
- }
-
+ public SymbolTable getParent() { return parent; }
}