summaryrefslogtreecommitdiff
path: root/minijava/SymTableVis.java
blob: 65e511fe9113633b1fad03248a427c2d7686a5a0 (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
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,TypeEnum> symt = new HashMap<>();

    private void print_filter(String message) {
        boolean debug = true;
        if (debug)
            System.out.println(message);
    }

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

        this.print_filter("Processing declaration");

        TypeEnum type = TypeEnum.ERROR;
        switch (n.f0.f0.which) {
        case 0:
            type = TypeEnum.int_array; break;
        case 2:
            type = TypeEnum.integer; break;
        default:
            this.print_filter("Unsupported case");
        }

        String id = n.f1.f0.tokenImage;

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

        return null;
    }

}