summaryrefslogtreecommitdiff
path: root/st/SymTableVis.java
diff options
context:
space:
mode:
Diffstat (limited to 'st/SymTableVis.java')
-rw-r--r--st/SymTableVis.java72
1 files changed, 55 insertions, 17 deletions
diff --git a/st/SymTableVis.java b/st/SymTableVis.java
index 280a89c..fb62979 100644
--- a/st/SymTableVis.java
+++ b/st/SymTableVis.java
@@ -9,10 +9,11 @@ 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 class SymTableVis extends GJDepthFirst<String,ArrayList<TypeInstance>> {
public HashMap<String,AbstractInstance> symt = new HashMap<>();
+
/**
* f0 -> "class"
* f1 -> Identifier()
@@ -33,7 +34,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
* f16 -> "}"
* f17 -> "}"
*/
- public R visit(MainClass n, A argu) {
+ public String visit(MainClass n, ArrayList<TypeInstance> argu) {
PrintFilter.print("Processing main", true);
@@ -73,7 +74,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
* f4 -> ( MethodDeclaration() )*
* f5 -> "}"
*/
- public R visit(ClassDeclaration n, A argu) {
+ public String visit(ClassDeclaration n, ArrayList<TypeInstance> argu) {
PrintFilter.print("Processing class", true);
@@ -105,7 +106,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
* f6 -> ( MethodDeclaration() )*
* f7 -> "}"
*/
- public R visit(ClassExtendsDeclaration n, A argu) {
+ public String visit(ClassExtendsDeclaration n, ArrayList<TypeInstance> argu) {
PrintFilter.print("Processing class", true);
String id = n.f1.f0.tokenImage;
@@ -134,7 +135,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
* f1 -> Identifier()
* f2 -> ";"
*/
- public R visit(VarDeclaration n, A argu) {
+ public String visit(VarDeclaration n, ArrayList<TypeInstance> argu) {
PrintFilter.print("Processing declaration", true);
@@ -156,6 +157,7 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
TypeInstance type = new TypeInstance(id, rtrn);
PrintFilter.print("Inserting " + type, true);
symt.put(id, type);
+ argu.add(type);
n.f0.accept(this, argu);
n.f1.accept(this, argu);
@@ -178,7 +180,25 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
* f11 -> ";"
* f12 -> "}"
*/
- public R visit(MethodDeclaration n, A argu) {
+ public String visit(MethodDeclaration n, ArrayList<TypeInstance> argu) {
+
+ ArrayList<TypeInstance> argu_list = new ArrayList<TypeInstance>();
+ ArrayList<TypeInstance> var_list = new ArrayList<TypeInstance>();
+
+ 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_list);
+ n.f5.accept(this, argu);
+ n.f6.accept(this, argu);
+ n.f7.accept(this, var_list);
+ 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);
+
PrintFilter.print("Processing method", true);
@@ -197,22 +217,40 @@ public class SymTableVis<R,A> extends GJDepthFirst<R,A> {
}
MethodInstance type = new MethodInstance(id, rtrn);
+ type.set_args(argu_list);
+ type.set_locals(var_list);
PrintFilter.print("Inserting " + type, true);
symt.put(id, type);
+ return null;
+ }
+
+ /**
+ * f0 -> Type()
+ * f1 -> Identifier()
+ */
+ public String visit(FormalParameter n, ArrayList<TypeInstance> argu) {
+ PrintFilter.print("Processing parameter", 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;
+ default:
+ PrintFilter.print("Unsupported case", true);
+ }
+
+ TypeInstance type = new TypeInstance(id, rtrn);
+ PrintFilter.print("Adding Argument " + type, true);
+ argu.add(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;
}