summaryrefslogtreecommitdiff
path: root/st
diff options
context:
space:
mode:
Diffstat (limited to 'st')
-rw-r--r--st/AbstractInstance.java12
-rw-r--r--st/ClassInstance.java34
-rw-r--r--st/MethodInstance.java24
-rw-r--r--st/SymTableVis.java18
-rw-r--r--st/SymbolTable.java52
-rw-r--r--st/TypeInstance.java21
6 files changed, 118 insertions, 43 deletions
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java
index a7dc8eb..65b0db7 100644
--- a/st/AbstractInstance.java
+++ b/st/AbstractInstance.java
@@ -10,25 +10,27 @@ public abstract class AbstractInstance {
this.name = name;
}
- public abstract String toString();
+ public String toString() {
+ return this.name;
+ }
public boolean equals(AbstractInstance other) {
- return this.name == other.get_name();
+ return this.name == other.getName();
}
public int hashCode() {
return this.name.hashCode();
}
- public String get_name() {
+ public String getName() {
return this.name;
}
- public TypeEnum get_type() {
+ public TypeEnum getType() {
return this.type;
}
- public int get_size() {
+ public int getSize() {
return this.size;
}
diff --git a/st/ClassInstance.java b/st/ClassInstance.java
index a444f26..a5f1493 100644
--- a/st/ClassInstance.java
+++ b/st/ClassInstance.java
@@ -3,35 +3,43 @@ package st;
import java.util.ArrayList;
public class ClassInstance extends AbstractInstance {
- private ArrayList<String> attrs; // the list of class-fields
- private ArrayList<String> mtds; // the list of methods
- private String ext; // the name of the extended class (null if none)
+ private ClassInstance ext; // the name of the extended class (null if none)
+ private ArrayList<TypeInstance> attrs; // the list of class-fields
+ private ArrayList<MethodInstance> mtds; // the list of methods
public ClassInstance(String name) {
super(name, TypeEnum.classname);
this.attrs = new ArrayList<>();
this.mtds = new ArrayList<>();
- this.ext = "";
}
- public ClassInstance(String name, String ext) {
+ public ClassInstance(String name, ClassInstance ext) {
super(name, TypeEnum.classname);
this.ext = ext;
}
- public String toString() {
- return name + ":T[" + type + "]E[" +
- this.ext + "]A[" + this.attrs.toString() +
- "]M[" + this.mtds.toString() + "]";
+ public void addAttribute(TypeInstance attr) {
+ this.attrs.add(attr);
+ }
+
+ public void addMethod(MethodInstance mtd) {
+ this.mtds.add(mtd);
+ }
+ public AbstractInstance getExtend() {
+ /**
+ * Returns the parent class, or
+ * `null' if unset.
+ */
+ return this.ext;
}
- public void set_attrs(ArrayList<String> attrs) {
- this.attrs = attrs;
+ public ArrayList<TypeInstance> getAttributes() {
+ return this.attrs;
}
- public void set_mtds(ArrayList<String> mtds) {
- this.mtds = mtds;
+ public ArrayList<MethodInstance> getMethod() {
+ return this.mtds;
}
}
diff --git a/st/MethodInstance.java b/st/MethodInstance.java
index 8b4ed57..d6c22b6 100644
--- a/st/MethodInstance.java
+++ b/st/MethodInstance.java
@@ -3,9 +3,10 @@ package st;
import java.util.ArrayList;
public class MethodInstance extends AbstractInstance {
- private ArrayList<String> args; // the list of arguments
- private ArrayList<String> lvars; // the list of local variables
+ private ArrayList<TypeInstance> args; // the list of arguments
+ private ArrayList<TypeInstance> lvars; // the list of local variables
private TypeEnum rtrn; // the returned type
+ private ClassInstance scope; // the surrounding class
public MethodInstance(String name, TypeEnum rtrn) {
super(name, TypeEnum.method);
@@ -14,18 +15,21 @@ public class MethodInstance extends AbstractInstance {
this.rtrn = rtrn;
}
- public String toString() {
- return name + ":T[" + type + "]R[" +
- this.rtrn + "]P[" + this.args.toString() +
- "]V[" + this.lvars.toString() + "]";
+ public void addArgument(TypeInstance arg) {
+ this.args.add(arg);
+ this.lvars.add(arg);
}
- public void set_args(ArrayList<String> args) {
- this.args = args;
+ public void addLocal(TypeInstance lvar) {
+ this.lvars.add(lvar);
}
- public void set_locals(ArrayList<String> lvars) {
- this.lvars = lvars;
+ public AbstractInstance getScope() {
+ /**
+ * Returns the scope of the method, or
+ * `null' if unset.
+ */
+ return this.scope;
}
}
diff --git a/st/SymTableVis.java b/st/SymTableVis.java
index 399ea09..36233b3 100644
--- a/st/SymTableVis.java
+++ b/st/SymTableVis.java
@@ -37,7 +37,7 @@ public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> {
public R visit(MainClass n, ArrayList<String> argu) {
ArrayList<String> attr_list = new ArrayList<String>();
- ArrayList<String> mtd_list = new ArrayList<String>();
+ ArrayList<String> mtd_list = new ArrayList<String>();
n.f0.accept(this, argu);
n.f1.accept(this, argu);
@@ -64,7 +64,7 @@ public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> {
String id = n.f1.f0.tokenImage;
ClassInstance type = new ClassInstance(id);
- type.set_attrs(attr_list);
+ type.set_attrs(attr_list);
type.set_mtds(mtd_list);
PrintFilter.print("Inserting " + id + " => " + type, true);
symt.put(id, type);
@@ -141,8 +141,10 @@ public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> {
String ext = n.f3.f0.tokenImage;
ClassInstance type = new ClassInstance(id, ext);
- type.set_attrs(attr_list);
- type.set_mtds(mtd_list);
+ ClassInstance parent = (ClassInstance) symt.get(ext);
+ System.out.println("Parent addrs: " + parent.get_attrs());
+ // type.set_attrs(attr_list.addAll(parent.get_attrs()));
+ // type.set_mtds(mtd_list.addAll(parent.get_mtds()));
PrintFilter.print("Inserting " + id + " => " + type, true);
symt.put(id, type);
@@ -238,14 +240,14 @@ public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> {
}
MethodInstance type = new MethodInstance(id, rtrn);
- // add children to current class
+ // add children to current class
type.set_args(argu_list);
type.set_locals(var_list);
PrintFilter.print("Inserting " + type, true);
symt.put(id, type);
- // add method to parent class
- argu.add(id);
+ // add method to parent class
+ argu.add(id);
return null;
}
@@ -273,7 +275,7 @@ public class SymTableVis<R> extends GJDepthFirst<R,ArrayList<String>> {
TypeInstance type = new TypeInstance(id, rtrn);
- // add type to parent class
+ // add type to parent class
argu.add(id);
n.f0.accept(this, argu);
diff --git a/st/SymbolTable.java b/st/SymbolTable.java
new file mode 100644
index 0000000..0757541
--- /dev/null
+++ b/st/SymbolTable.java
@@ -0,0 +1,52 @@
+package st;
+
+import java.util.*;
+
+public class SymbolTable {
+
+ private HashMap<String,AbstractInstance> symt;
+
+ public SymbolTable() {
+ this.symt = new HashMap<>();
+ }
+
+ public String toString() {
+ StringBuilder mapAsString = new StringBuilder("{");
+ for (String key : this.symt.keySet()) {
+ mapAsString.append(key + ":" + this.symt.get(key) + ", ");
+ }
+ mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
+ return mapAsString.toString();
+ }
+
+ public void addMethod() {
+ this.
+ }
+
+ public void put(String id, AbstractInstance symbol) {
+ this.symt.put(id, symbol);
+ }
+
+ public TypeInstance getType(String index) {
+ AbstractInstance symbol;
+ return ((symbol = this.symt.get(index)) !=
+ null && symbol.getType() != TypeEnum.classname &&
+ symbol.getType() != TypeEnum.method) ?
+ (TypeInstance) symbol : null;
+ }
+
+ public MethodInstance getMethod(String index) {
+ AbstractInstance symbol;
+ return ((symbol = this.symt.get(index)) !=
+ null && symbol.getType() == TypeEnum.method) ?
+ (MethodInstance) symbol : null;
+ }
+
+ public ClassInstance getClass(String index) {
+ AbstractInstance symbol;
+ return ((symbol = this.symt.get(index)) !=
+ null && symbol.getType() == TypeEnum.classname) ?
+ (ClassInstance) symbol : null;
+ }
+
+}
diff --git a/st/TypeInstance.java b/st/TypeInstance.java
index 13947ae..e00c1f9 100644
--- a/st/TypeInstance.java
+++ b/st/TypeInstance.java
@@ -2,15 +2,14 @@ package st;
public class TypeInstance extends AbstractInstance {
+ private AbstractInstance scope;
+
public TypeInstance(String name, TypeEnum type) {
super(name, type);
+ this.scope = null;
}
- public String toString() {
- return this.name + ":" + this.type;
- }
-
- public boolean same_type(TypeInstance other) {
+ public boolean sameType(TypeInstance other) {
/**
* Given a TypeInstance object other,
* returns true if other object
@@ -22,11 +21,19 @@ public class TypeInstance extends AbstractInstance {
*/
return this.type != TypeEnum.ERROR &&
- this.type == other.get_type();
+ this.type == other.getType();
}
- public boolean has_checked() {
+ public boolean hasChecked() {
return type != TypeEnum.ERROR;
}
+ public AbstractInstance getScope() {
+ /**
+ * Returns the scope of the variable, or
+ * `null' if unset.
+ */
+ return this.scope;
+ }
+
}