summaryrefslogtreecommitdiff
path: root/st
diff options
context:
space:
mode:
Diffstat (limited to 'st')
-rw-r--r--st/AbstractInstance.java35
-rw-r--r--st/ClassInstance.java37
-rw-r--r--st/MethodInstance.java24
-rw-r--r--st/SymTableVis.java219
-rw-r--r--st/TypeEnum.java5
-rw-r--r--st/TypeInstance.java32
6 files changed, 352 insertions, 0 deletions
diff --git a/st/AbstractInstance.java b/st/AbstractInstance.java
new file mode 100644
index 0000000..a7dc8eb
--- /dev/null
+++ b/st/AbstractInstance.java
@@ -0,0 +1,35 @@
+package st;
+
+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
+
+ public AbstractInstance(String name, TypeEnum type) {
+ this.type = type;
+ this.name = name;
+ }
+
+ public abstract String toString();
+
+ public boolean equals(AbstractInstance other) {
+ return this.name == other.get_name();
+ }
+
+ public int hashCode() {
+ return this.name.hashCode();
+ }
+
+ public String get_name() {
+ return this.name;
+ }
+
+ public TypeEnum get_type() {
+ return this.type;
+ }
+
+ public int get_size() {
+ return this.size;
+ }
+
+}
diff --git a/st/ClassInstance.java b/st/ClassInstance.java
new file mode 100644
index 0000000..c1b07af
--- /dev/null
+++ b/st/ClassInstance.java
@@ -0,0 +1,37 @@
+package st;
+
+import java.util.ArrayList;
+
+public class ClassInstance extends AbstractInstance {
+ private ArrayList<TypeInstance> attrs; // the list of class-fields
+ private ArrayList<MethodInstance> mtds; // the list of methods
+ private String ext; // the name of the extended class (null if none)
+
+ public ClassInstance(String name) {
+ super(name, TypeEnum.classname);
+ this.ext = null;
+ }
+
+ public ClassInstance(String name, String ext) {
+ super(name, TypeEnum.classname);
+ this.ext = ext;
+ }
+
+ public String toString() {
+ return this.name + ":" + this.type + "(" +
+ this.ext + ")";
+ }
+
+ public void add_attribute(TypeInstance attr) {
+ this.attrs.add(attr);
+ }
+
+ public void add_attribute(MethodInstance mtd) {
+ this.mtds.add(mtd);
+ }
+
+ public String get_extend() {
+ return this.ext;
+ }
+
+}
diff --git a/st/MethodInstance.java b/st/MethodInstance.java
new file mode 100644
index 0000000..c0082af
--- /dev/null
+++ b/st/MethodInstance.java
@@ -0,0 +1,24 @@
+package st;
+
+import java.util.ArrayList;
+
+public class MethodInstance extends AbstractInstance {
+ private ArrayList<TypeEnum> args; // the list of arguments
+ private TypeEnum rtrn; // the returned type
+
+ public MethodInstance(String name, TypeEnum rtrn) {
+ super(name, TypeEnum.method);
+ this.args = new ArrayList<>();
+ this.rtrn = rtrn;
+ }
+
+ public String toString() {
+ return name + ":" + type + "[" +
+ this.rtrn + "]";
+ }
+
+ public void add_argument(TypeEnum arg) {
+ this.args.add(arg);
+ }
+
+}
diff --git a/st/SymTableVis.java b/st/SymTableVis.java
new file mode 100644
index 0000000..280a89c
--- /dev/null
+++ b/st/SymTableVis.java
@@ -0,0 +1,219 @@
+package st;
+
+import syntaxtree.*;
+import visitor.*;
+import java.util.*;
+import misc.*;
+
+/**
+ * Provides default methods which visit each node in the tree in depth-first
+ * order. Your visitors may extend this class.
+ */
+public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
+
+ public HashMap<String,AbstractInstance> symt = new HashMap<>();
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> "public"
+ * f4 -> "static"
+ * f5 -> "void"
+ * f6 -> "main"
+ * f7 -> "("
+ * f8 -> "String"
+ * f9 -> "["
+ * f10 -> "]"
+ * f11 -> Identifier()
+ * f12 -> ")"
+ * f13 -> "{"
+ * f14 -> ( VarDeclaration() )*
+ * f15 -> ( Statement() )*
+ * f16 -> "}"
+ * f17 -> "}"
+ */
+ public R visit(MainClass n, A argu) {
+
+ PrintFilter.print("Processing main", true);
+
+ String id = n.f1.f0.tokenImage;
+
+ ClassInstance type = new ClassInstance(id);
+ PrintFilter.print("Inserting " + id + " => " + type, true);
+ symt.put(id, type);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ n.f5.accept(this, argu);
+ n.f6.accept(this, argu);
+ n.f7.accept(this, argu);
+ n.f8.accept(this, argu);
+ n.f9.accept(this, argu);
+ n.f10.accept(this, argu);
+ n.f11.accept(this, argu);
+ n.f12.accept(this, argu);
+ n.f13.accept(this, argu);
+ n.f14.accept(this, argu);
+ n.f15.accept(this, argu);
+ n.f16.accept(this, argu);
+ n.f17.accept(this, argu);
+ return null;
+
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "{"
+ * f3 -> ( VarDeclaration() )*
+ * f4 -> ( MethodDeclaration() )*
+ * f5 -> "}"
+ */
+ public R visit(ClassDeclaration n, A argu) {
+
+ PrintFilter.print("Processing class", true);
+
+ String id = n.f1.f0.tokenImage;
+
+ ClassInstance type = new ClassInstance(id);
+ PrintFilter.print("Inserting " + id + " => " + type, true);
+ // Safe?
+
+ symt.put(id, type);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ n.f5.accept(this, argu);
+ return null;
+
+ }
+
+ /**
+ * f0 -> "class"
+ * f1 -> Identifier()
+ * f2 -> "extends"
+ * f3 -> Identifier()
+ * f4 -> "{"
+ * f5 -> ( VarDeclaration() )*
+ * f6 -> ( MethodDeclaration() )*
+ * f7 -> "}"
+ */
+ public R visit(ClassExtendsDeclaration n, A argu) {
+ PrintFilter.print("Processing class", true);
+
+ String id = n.f1.f0.tokenImage;
+ String ext = n.f3.f0.tokenImage;
+
+ ClassInstance type = new ClassInstance(id, ext);
+ PrintFilter.print("Inserting " + id + " => " + type +
+ "(" + ext + ")", true);
+
+ symt.put(id, type);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ n.f5.accept(this, argu);
+ n.f6.accept(this, argu);
+ n.f7.accept(this, argu);
+ return null;
+ }
+
+
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ * f2 -> ";"
+ */
+ public R visit(VarDeclaration n, A argu) {
+
+ PrintFilter.print("Processing declaration", true);
+
+ String id = n.f1.f0.tokenImage;
+ TypeEnum rtrn = TypeEnum.ERROR;
+ switch (n.f0.f0.which) {
+ case 0:
+ rtrn = TypeEnum.intarray; break;
+ case 1:
+ rtrn = TypeEnum.bool; break;
+ case 2:
+ rtrn = TypeEnum.integer; break;
+ case 3:
+ rtrn = TypeEnum.classname; break;
+ default:
+ PrintFilter.print("Unsupported case", true);
+ }
+
+ TypeInstance type = new TypeInstance(id, rtrn);
+ PrintFilter.print("Inserting " + type, true);
+ symt.put(id, type);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ return null;
+ }
+
+ /**
+ * f0 -> "public"
+ * f1 -> Type()
+ * f2 -> Identifier()
+ * f3 -> "("
+ * f4 -> ( FormalParameterList() )?
+ * f5 -> ")"
+ * f6 -> "{"
+ * f7 -> ( VarDeclaration() )*
+ * f8 -> ( Statement() )*
+ * f9 -> "return"
+ * f10 -> Expression()
+ * f11 -> ";"
+ * f12 -> "}"
+ */
+ public R visit(MethodDeclaration n, A argu) {
+
+ PrintFilter.print("Processing method", true);
+
+ String id = n.f2.f0.tokenImage;
+
+ TypeEnum rtrn = TypeEnum.ERROR;
+ switch (n.f1.f0.which) {
+ case 0:
+ rtrn = TypeEnum.intarray; break;
+ case 1:
+ rtrn = TypeEnum.bool; break;
+ case 2:
+ rtrn = TypeEnum.integer; break;
+ default:
+ PrintFilter.print("Unsupported case", true);
+ }
+
+ MethodInstance type = new MethodInstance(id, rtrn);
+ PrintFilter.print("Inserting " + type, true);
+ symt.put(id, type);
+
+ n.f0.accept(this, argu);
+ n.f1.accept(this, argu);
+ n.f2.accept(this, argu);
+ n.f3.accept(this, argu);
+ n.f4.accept(this, argu);
+ n.f5.accept(this, argu);
+ n.f6.accept(this, argu);
+ n.f7.accept(this, argu);
+ n.f8.accept(this, argu);
+ n.f9.accept(this, argu);
+ n.f10.accept(this, argu);
+ n.f11.accept(this, argu);
+ n.f12.accept(this, argu);
+ return null;
+ }
+
+}
diff --git a/st/TypeEnum.java b/st/TypeEnum.java
new file mode 100644
index 0000000..f6fc6ae
--- /dev/null
+++ b/st/TypeEnum.java
@@ -0,0 +1,5 @@
+package st;
+
+public enum TypeEnum {
+ classname, method, intarray, bool, integer, CHECK, ERROR
+}
diff --git a/st/TypeInstance.java b/st/TypeInstance.java
new file mode 100644
index 0000000..13947ae
--- /dev/null
+++ b/st/TypeInstance.java
@@ -0,0 +1,32 @@
+package st;
+
+public class TypeInstance extends AbstractInstance {
+
+ public TypeInstance(String name, TypeEnum type) {
+ super(name, type);
+ }
+
+ public String toString() {
+ return this.name + ":" + this.type;
+ }
+
+ public boolean same_type(TypeInstance other) {
+ /**
+ * Given a TypeInstance object other,
+ * returns true if other object
+ * is the same type as this one.
+ *
+ * We can say two types are equal, as
+ * long as they are not equal on a
+ * type error!
+ */
+
+ return this.type != TypeEnum.ERROR &&
+ this.type == other.get_type();
+ }
+
+ public boolean has_checked() {
+ return type != TypeEnum.ERROR;
+ }
+
+}