summaryrefslogtreecommitdiff
path: root/minijava/SymTableVis.java
blob: 1a48fd2b0d3dd70a9b72dd2b5b5256e7cda518b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package minijava;

import syntaxtree.*;
import visitor.*;
import java.util.*;

/**
 * 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,TypeInstance> 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) {

        n.f1.accept(this, argu);
        n.f11.accept(this, argu);
        n.f14.accept(this, argu);
        n.f15.accept(this, argu);

        Utilities.print_filter("Processing main", true);

        String id = n.f1.f0.tokenImage;

        TypeInstance type = new TypeInstance(id, TypeEnum.classname);
        Utilities.print_filter("Inserting " + id + " => " + type, true);
        symt.put(id, type);

        return null;

    }

    /**
     * f0 -> Type()
     * f1 -> Identifier()
     * f2 -> ";"
     */
    public R visit(VarDeclaration n, A argu) {

        Utilities.print_filter("Processing declaration", true);

        String id = n.f1.f0.tokenImage;
        TypeInstance type = new TypeInstance("ERROR", TypeEnum.ERROR);
        switch (n.f0.f0.which) {
        case 0:
            type = new TypeInstance("int_array", TypeEnum.int_array); break;
        case 1:
            type = new TypeInstance("bool", TypeEnum.bool); break;
        case 2:
            type = new TypeInstance("int", TypeEnum.integer); break;
        case 3:
            type = new TypeInstance(id, TypeEnum.classname); break;
        default:
            Utilities.print_filter("Unsupported case", true);
        }

        Utilities.print_filter("Inserting " + id + " => " + type, true);
        // Safe?
        symt.put(id, type);

        return null;
    }

    public R visit(ClassDeclaration n, A argu) {

        Utilities.print_filter("Processing class", true);

        String id = n.f1.f0.tokenImage;

        TypeInstance type = new TypeInstance(id, TypeEnum.classname);
        Utilities.print_filter("Inserting " + id + " => " + type, true);
        // Safe?
        symt.put(id, type);

        return null;

    }


}