summaryrefslogtreecommitdiff
path: root/st
diff options
context:
space:
mode:
Diffstat (limited to 'st')
-rw-r--r--st/AbstractInstance.java34
-rw-r--r--st/SymTableBottomUp.java6
-rw-r--r--st/SymbolTable.java7
3 files changed, 29 insertions, 18 deletions
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java
index a6c2ca1..bddaccf 100644
--- a/st/AbstractInstance.java
+++ b/st/AbstractInstance.java
@@ -1,14 +1,17 @@
package st;
+import java.util.ArrayList;
+
public abstract class AbstractInstance {
- protected String name; // the literal name of the declaration
- protected TypeEnum type; // the type of the declaration
- protected int size; // the size in memory
- protected AbstractInstance scope; // the surrounding method or class
+ protected String name; // the literal name of the declaration
+ protected TypeEnum type; // the type of the declaration
+ protected int size; // the size in memory
+ protected ArrayList<AbstractInstance> scope; // the scope where the instance is valid
public AbstractInstance(String name, TypeEnum type) {
this.type = type;
this.name = name;
+ this.scope = new ArrayList<>();
}
public String toString() {
@@ -24,11 +27,18 @@ public abstract class AbstractInstance {
return this.name.hashCode();
}
- public void setScope(AbstractInstance cls) {
- if (this.scope != null)
- throw new RuntimeException("setScope: Attempted to set scope twice!");
-
- this.scope = cls;
+ public void setScope(AbstractInstance ins) {
+ /**
+ * If the scope is a MethodInstance, add the single method.
+ * If the scope is a ClassInstance, add the classes' methods,
+ * and the class itself.
+ */
+ if (ins instanceof MethodInstance)
+ this.scope.add(ins);
+ else if (ins instanceof ClassInstance) {
+ for (MethodInstance mtd : ((ClassInstance) ins).getMethods())
+ this.scope.add(mtd);
+ }
}
public String getName() {
@@ -43,11 +53,7 @@ public abstract class AbstractInstance {
return this.size;
}
- public AbstractInstance getScope() {
- /**
- * Returns the scope of the method, or
- * `null' if unset.
- */
+ public ArrayList<AbstractInstance> getScope() {
return this.scope;
}
diff --git a/st/SymTableBottomUp.java b/st/SymTableBottomUp.java
index f4abb2a..9f60b91 100644
--- a/st/SymTableBottomUp.java
+++ b/st/SymTableBottomUp.java
@@ -141,6 +141,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
rtrn = TypeEnum.bool; break;
case 2:
rtrn = TypeEnum.integer; break;
+ case 3:
+ rtrn = TypeEnum.classname; break;
default:
PrintFilter.print("Unsupported case", true);
}
@@ -193,6 +195,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
rtrn = TypeEnum.bool; break;
case 2:
rtrn = TypeEnum.integer; break;
+ case 3:
+ rtrn = TypeEnum.classname; break;
default:
PrintFilter.print("Unsupported case", true);
}
@@ -223,6 +227,8 @@ public class SymTableBottomUp<R> extends GJDepthFirst<R,SymbolTable> {
rtrn = TypeEnum.bool; break;
case 2:
rtrn = TypeEnum.integer; break;
+ case 3:
+ rtrn = TypeEnum.classname; break;
default:
PrintFilter.print("Unsupported case", true);
}
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
index 9d7ffb4..7475e5e 100644
--- a/st/SymbolTable.java
+++ b/st/SymbolTable.java
@@ -96,22 +96,21 @@ public class SymbolTable {
public TypeInstance getType(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
- null && symbol.getType() != TypeEnum.classname &&
- symbol.getType() != TypeEnum.method) ?
+ null && symbol instanceof TypeInstance) ?
(TypeInstance) symbol : null;
}
public MethodInstance getMethod(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
- null && symbol.getType() == TypeEnum.method) ?
+ null && symbol instanceof MethodInstance) ?
(MethodInstance) symbol : null;
}
public ClassInstance getClass(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
- null && symbol.getType() == TypeEnum.classname) ?
+ null && symbol instanceof ClassInstance) ?
(ClassInstance) symbol : null;
}