summaryrefslogtreecommitdiffstats
path: root/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
diff options
context:
space:
mode:
Diffstat (limited to 'code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java')
-rw-r--r--code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
new file mode 100644
index 0000000..992e873
--- /dev/null
+++ b/code/simpleSableCCCalulator/sableCCCalculator/SymbolTable.java
@@ -0,0 +1,63 @@
+package sableCCCalculator;
+import java.util.HashMap;
+import sableCCCalculator.types.*;
+
+public class SymbolTable {
+
+ public interface SymbolTableIndex {}
+
+ public abstract class Name implements SymbolTableIndex {
+ // can be used for functions too hopefully one day...
+ protected String name;
+
+ String getName() {
+ return this.name;
+ }
+ }
+
+ public class Constant implements SymbolTableIndex {
+ int index;
+
+ public Constant(int index) {
+ this.index = index;
+ }
+ }
+
+ public class Variable extends Name {
+ public Variable(String name) {
+ this.name = name;
+ }
+ }
+
+ private HashMap<SymbolTableIndex, Type> theSymbolTable = new HashMap<>();
+
+ public SymbolTableIndex addConstant(Type item) {
+ SymbolTableIndex index = new Constant(item.hashCode());
+ theSymbolTable.put(index, item);
+ return index;
+ }
+
+ public SymbolTableIndex addVariable(Type item, String name) {
+ SymbolTableIndex index = new Variable(name);
+ theSymbolTable.put(index, item);
+ return index;
+ }
+
+ public void updateVariable(Type newItem, SymbolTableIndex index) {
+ theSymbolTable.replace(index, newItem);
+ }
+
+ public Type get(SymbolTableIndex index) {
+ return theSymbolTable.get(index);
+ }
+
+ public static void main(String[] args) {
+ SymbolTable symbolTable = new SymbolTable();
+ symbolTable.addConstant(new Int(3));
+ SymbolTableIndex i_var = symbolTable.addVariable(new Int(0), "i");
+ System.out.println(symbolTable.get(i_var));
+ symbolTable.updateVariable(symbolTable.get(i_var).add(new Int(1)), i_var);
+ System.out.println(symbolTable.get(i_var));
+ }
+}
+ \ No newline at end of file