+/EE379K

programming paradigms

git clone https://git.julia.blue/+/EE379K

Joseph RyanFinish visitor pass for ident41480ce

master
7.2 KiB197 linesraw
1import java.util.*;
2
3public class Visitor implements MyParserVisitor {
4    private HashSet<String> types;
5    private HashSet<String> globals;
6    private HashSet<String> locals;
7    private boolean inClass = false;
8    private boolean inMethod = false;
9    private boolean inMain = false;
10    private boolean foundMain = false;
11
12    public Visitor() {
13        globals = new HashSet();
14        locals = new HashSet();
15        String arr[] = {"int", "bool", "char"};
16        types = new HashSet(Arrays.asList(arr));
17    }
18
19    public void defaultVisit(SimpleNode node, Object data)
20        throws InvalidProgramException {
21        node.childrenAccept(this, data);
22        return;
23    }
24    public void visit(SimpleNode node, Object data)
25        throws InvalidProgramException {
26        defaultVisit(node, data);
27    }
28    public void visit(Program node, Object data)
29        throws InvalidProgramException {
30        defaultVisit(node, data);
31        if (!foundMain) {
32            throw new InvalidProgramException("No main function");
33        }
34    }
35    public void visit(ConstDecl node, Object data)
36        throws InvalidProgramException {
37        String type = node.jjtGetChild(0).jjtGetChild(0).toString();
38        System.out.print("CONSTs of type: " + type);
39        System.out.print("\t\twith names: ");
40        for (int i = 1; i < node.jjtGetNumChildren(); i++) {
41            String current = node.jjtGetChild(i).toString();
42            if (globals.contains(current)) {
43                throw new InvalidProgramException(
44                    "Can't define 2 variables with the same name: " + current);
45            }
46            globals.add(current);
47            System.out.print(current + " ");
48        }
49        System.out.println();
50    }
51    public void visit(EnumDecl node, Object data)
52        throws InvalidProgramException {
53        System.out.print("Enum of type: " + ((Ident)node.jjtGetChild(0)).name);
54        System.out.print("\t\twith variants: ");
55        for (int i = 1; i < node.jjtGetNumChildren(); i++) {
56            System.out.print(((Ident)node.jjtGetChild(i)).name + " ");
57        }
58        System.out.println();
59    }
60    public void visit(VarDecl node, Object data)
61        throws InvalidProgramException {
62        String type = node.jjtGetChild(0).jjtGetChild(0).toString();
63        System.out.print("Vars of type: " + type);
64        System.out.print("\t\twith names: ");
65        for (int i = 1; i < node.jjtGetNumChildren(); i++) {
66            String current = node.jjtGetChild(i).toString();
67            if (inMethod) {
68                locals.add(current);
69            } else {
70                globals.add(current);
71            }
72            System.out.print(current + " ");
73        }
74        System.out.println();
75    }
76    public void visit(ClassDecl node, Object data)
77        throws InvalidProgramException {
78        String className = ((Ident)node.jjtGetChild(0)).name;
79        types.add(className);
80        System.out.println("Entering " + className);
81        inClass = true;
82        defaultVisit(node, data);
83        inClass = false;
84        System.out.println("Leaving " + className);
85    }
86    public void visit(InterfaceDecl node, Object data)
87        throws InvalidProgramException {
88        String interfaceName = ((Ident)node.jjtGetChild(0)).name;
89        if (types.contains(interfaceName)) {
90            throw new InvalidProgramException(
91                "Can't have 2 interfaces named: " + interfaceName);
92        }
93        types.add(interfaceName);
94        System.out.println("Entering " + interfaceName);
95        inClass = true;
96        defaultVisit(node, data);
97        inClass = false;
98        System.out.println("Leaving " + interfaceName);
99    }
100    public void visit(InterfaceMethodDecl node, Object data)
101        throws InvalidProgramException {
102        defaultVisit(node, data);
103        locals.clear();
104    }
105    public void visit(MethodDecl node, Object data)
106        throws InvalidProgramException {
107        int namePos = 0;
108        if (node.jjtGetChild(0) instanceof Type) {
109            namePos = 1;
110        }
111        String methodName = node.jjtGetChild(namePos).toString();
112        globals.add(methodName);
113        if (methodName.equals("main") && !inClass) {
114            foundMain = true;
115            inMain = true;
116            if (namePos == 1) {
117                throw new InvalidProgramException("Main function must be void");
118            }
119        }
120        inMethod = true;
121        System.out.println("Entering " + methodName);
122        defaultVisit(node, data);
123        inMethod = false;
124        locals.clear();
125        inMain = false;
126        System.out.println("Leaving " + methodName);
127    }
128    public void visit(ParamDecl node, Object data)
129        throws InvalidProgramException {
130        System.out.print("Params:\t\t\t\t\t");
131        if (inMain && node.jjtGetNumChildren() > 0) {
132            throw new InvalidProgramException(
133                "Main function shouldn't have any arguments");
134        }
135        for (int i = 1; i < node.jjtGetNumChildren(); i += 2) {
136            String current = node.jjtGetChild(i).toString();
137            if (locals.contains(current)) {
138                throw new InvalidProgramException(
139                    "Can't define 2 variables with the same name: " + current);
140            }
141            locals.add(current);
142            System.out.print(current + " ");
143        }
144        System.out.println();
145        defaultVisit(node, data);
146    }
147    public void visit(Statement node, Object data)
148        throws InvalidProgramException {
149        defaultVisit(node, data);
150    }
151    public void visit(DesignatorStatement node, Object data)
152        throws InvalidProgramException {
153        defaultVisit(node, data);
154    }
155    public void visit(Condition node, Object data)
156        throws InvalidProgramException {
157        defaultVisit(node, data);
158    }
159    public void visit(CompareAnd node, Object data)
160        throws InvalidProgramException {
161        defaultVisit(node, data);
162    }
163    public void visit(Comparison node, Object data)
164        throws InvalidProgramException {
165        defaultVisit(node, data);
166    }
167    public void visit(Params node, Object data) throws InvalidProgramException {
168        defaultVisit(node, data);
169    }
170    public void visit(Expr node, Object data) throws InvalidProgramException {
171        defaultVisit(node, data);
172    }
173    public void visit(Term node, Object data) throws InvalidProgramException {
174        defaultVisit(node, data);
175    }
176    public void visit(Factor node, Object data) throws InvalidProgramException {
177        defaultVisit(node, data);
178    }
179    public void visit(Type node, Object data) throws InvalidProgramException {
180        String name = node.jjtGetChild(0).toString();
181        if (!types.contains(name)) {
182            throw new InvalidProgramException("Type not declared before use: " +
183                                              name);
184        }
185    }
186    public void visit(Designator node, Object data)
187        throws InvalidProgramException {
188        defaultVisit(node, data);
189    }
190    public void visit(Ident node, Object data) throws InvalidProgramException {
191        String name = node.name;
192        if (!globals.contains(name) && !locals.contains(name)) {
193            throw new InvalidProgramException(
194                "Variable not declared before use: " + name);
195        }
196    }
197}