-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAnalyzer.java
More file actions
273 lines (246 loc) · 7.28 KB
/
SAnalyzer.java
File metadata and controls
273 lines (246 loc) · 7.28 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import java.util.ArrayList;
import java.util.Arrays;
public class SAnalyzer {
private static String lineNumber = "0";
private static ArrayList<Integer> scopeHistory = new ArrayList<Integer>();
private static int nextScope = 0;
private static ArrayList<Object> ast = new ArrayList<Object>();
private static String[] parsed;
private static int ts;
public static void createAST(String treeThis) {
parsed = treeThis.split("\\s");
for(ts = 0; ts < parsed.length; ts++) {
if(!isIgnored(parsed[ts]))
ast.add(createBranch(parsed[ts]));
}
System.out.println(Arrays.deepToString(ast.toArray()));
}
private static Object[] createBranch(String token) {
Object[] branch = null;
if(token.matches("B_OPEN")) {
branch = branchBlock();
}
else if(token.matches("C_[A-Z]+")) {
token = token.substring(2);
ts++;
Object condExpr = branchEXPR();
ts++;
branch = new Object[]{token, condExpr, branchBlock()};
}
else if(token.matches("D_[A-Z]+")) {
ts++;
branch = new Object[]{"DECLARE", token.substring(2), parsed[ts]};
}
else if(token.equals("PRINT")) {
ts++;
branch = new Object[]{token, branchEXPR()};
}
else if(token.matches("[a-z]")) {
ts += 2;
branch = new Object[]{"SET", token, branchEXPR()};
}
else if(token.startsWith("LN_")) {
branch = new Object[]{"LINE", token.substring(3)};
}
else {
MainDisplay.errorReport = "[Line: " + lineNumber + "] This message should never appear. (" + token + ")\n";
}
return branch;
}
private static boolean isIgnored(String token) {
if(token.matches("LN_\\d+")) {
lineNumber = token.substring(3);
}
else if(token.equals("EOF")) {
ts = parsed.length;
return true;
}
return false;
}
//Branch Methods
private static Object[] branchBlock() {
ArrayList<Object> block = new ArrayList<Object>();
block.add("BLOCK");
for(ts++; ts<parsed.length; ts++) {
if(parsed[ts].equals("B_CLOSE")) {
return block.toArray();
}
if(!isIgnored(parsed[ts]))
block.add(createBranch(parsed[ts]));
}
MainDisplay.errorReport = "[Line: " + lineNumber + "] This message should never appear. (BLOCK)\n";
return block.toArray();
}
private static Object branchEXPR() {
String root = "";
Object branchOne;
Object branchTwo;
if(parsed[ts].equals("E_OPEN")) {
ts++;
branchOne = branchEXPR();
}
else if(parsed[ts].equals("Q_OPEN")) {
branchOne = branchQuote();
}
else {
branchOne = parsed[ts];
}
if(!parsed[ts+1].matches("E_[A-Z]+")) {
return branchOne;
}
else {
ts++;
}
if(parsed[ts].equals("E_CLOSE")) {
return branchOne;
}
else {
root = parsed[ts].substring(2);
}
ts++;
branchTwo = branchEXPR();
if(root.isEmpty()) {
MainDisplay.errorReport = "[Line: " + lineNumber + "] This message should never appear. (EXPR)\n";
}
return new Object[]{root, branchOne, branchTwo};
}
private static Object[] branchQuote() {
ArrayList<String> quote = new ArrayList<String>();
quote.add("QUOTE");
for(ts++; ts < parsed.length && !parsed[ts].matches("Q_CLOSE"); ts++) {
if(parsed[ts].matches("Q_SPACE"))
quote.add("SPACE");
else
quote.add(parsed[ts]);
}
return quote.toArray();
}
//
public static ArrayList<Object> analyzeCode() {
lineNumber = "0";
for(Object branch : ast) {
if(branch instanceof Object[]) {
analyzeBranch((Object[]) branch);
}
}
System.out.println("\n" + Arrays.deepToString(ast.toArray()) + "\n");
for(ArrayList<String[]> scopeTable : MainDisplay.symbolTable) {
System.out.println(Arrays.deepToString(scopeTable.toArray()));
}
return ast;
}
private static void analyzeBlock(Object[] block) {
MainDisplay.symbolTable.add(new ArrayList<String[]>());
scopeHistory.add(nextScope);
nextScope++;
for(Object branch : block) {
if(branch instanceof Object[]) {
analyzeBranch((Object[]) branch);
}
}
scopeHistory.remove(scopeHistory.size()-1);
}
private static void analyzeBranch(Object[] branch) {
//System.out.println(branch[0]);
if(branch[0].equals("DECLARE")) {
addToSymbolTable((String)branch[1], (String)branch[2]);
}
else if(branch[0].equals("SET")) {
String idType = getVarTypeOf((String)branch[1]);
if(!idType.isEmpty()) {
String exprType = getExprTypeOf(branch[2]);
if(!idType.equals(exprType)) {
MainDisplay.errorReport += "[Line: " + lineNumber + "] " + branch[1] + " (" + idType + ") can not be set to a(n) " + exprType + ".\n";
}
}
}
else if(branch[0].equals("BLOCK")) {
analyzeBlock(branch);
}
else if(branch[0].equals("BRANCH")) {
if(getExprTypeOf(branch[1]).equals("BOOL")) {
analyzeBlock((Object[]) branch[2]);
}
}
else if(branch[0].equals("PRINT")) {
getExprTypeOf(branch[1]);
}
else if(branch[0].equals("LINE")) {
lineNumber = (String) branch[1];
}
}
private static void addToSymbolTable(String type, String id) {
int curScope = scopeHistory.get(scopeHistory.size()-1);
ArrayList<String[]> scopeTable = MainDisplay.symbolTable.get(curScope);
for(String[] symbolInfo : scopeTable) {
if(symbolInfo[0].equals(id)) {
MainDisplay.errorReport += "[Line: " + lineNumber + "] " + id + " has already been delared in this scope.\n";
return;
}
}
String[] declareInfo = new String[3];
declareInfo[0] = id; //Variable Name
declareInfo[1] = type; //Type
declareInfo[2] = lineNumber; //Line of Creation
scopeTable.add(declareInfo);
MainDisplay.symbolTable.set(curScope, scopeTable);
}
private static String getVarTypeOf(String id) {
ArrayList<String[]> scopeTable;
for(int h = scopeHistory.size(); h > 0; h--) {
scopeTable = MainDisplay.symbolTable.get(scopeHistory.get(h-1));
for(String[] idInfo : scopeTable) {
if(idInfo[0].equals(id)) {
return idInfo[1];
}
}
}
MainDisplay.errorReport += "[Line: " + lineNumber + "] " + id + " can not be found. Please declare it first.\n";
return "";
}
private static String getExprTypeOf(Object expr) {
if(expr instanceof String) {
String type = (String) expr;
if(type.matches("[a-z]")) {
return getVarTypeOf(type);
}
else if(type.matches("\\d")) {
return "INT";
}
else if(type.matches("TRUE|FALSE")) {
return "BOOL";
}
else {
MainDisplay.errorReport += "[Line: " + lineNumber + "] Something went wrong. You should not see this. (" + expr + ")\n";
}
}
else if (expr instanceof Object[]) {
Object[] branch = (Object[]) expr;
String type = (String) branch[0];
if(type.equals("QUOTE")) {
return "STR";
}
else if(type.equals("PLUS")) {
if(isTypeInt(branch)) {
return "INT";
}
MainDisplay.errorReport += "[Line: " + lineNumber + "] You can only add integers.\n";
}
else if(type.matches("EQTO|NOTEQ")) {
String checkOne = getExprTypeOf(branch[1]);
String checkTwo = getExprTypeOf(branch[2]);
if(checkOne.equals(checkTwo)) {
return "BOOL";
}
MainDisplay.errorReport += "[Line: " + lineNumber + "] You cannot compare a(n) " + checkOne + " with a(n) " + checkTwo + ".\n";
}
else {
MainDisplay.errorReport += "[Line: " + lineNumber + "] Something went wrong. You should not see this. " + Arrays.deepToString(branch) + "\n";
}
}
return "INVALID";
}
private static boolean isTypeInt(Object[] branch) {
return getExprTypeOf(branch[1]).equals("INT") && getExprTypeOf(branch[2]).equals("INT");
}
}