-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectiveListener.java
150 lines (116 loc) · 4.04 KB
/
DirectiveListener.java
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
package optimizer;
import java.util.LinkedList;
public class DirectiveListener extends GloryBaseListener {
private int loopCount = 0;
private LinkedList<Integer> loopTypeList;
public DirectiveListener () {
loopTypeList = new LinkedList<>();
}
@Override
public void enterStatement(GloryParser.StatementContext ctx) {
//System.out.println("Statement starts:\n");
}
@Override
public void enterL(GloryParser.LContext ctx) {
super.enterL(ctx);
if (ctx.loopType == null) {
return;
}
loopTypeList.add(ctx.loopType.getType());
loopCount++;
int intend = loopCount;
while (intend > 0) {
intend--;
for (int i = 0; i < intend; i++) {
System.out.printf(" ");
}
}
if (ctx.loopType.getType() == GloryParser.OTHER) {
System.out.print("while ");
} else {
System.out.print("for ");
}
}
@Override
public void enterForParam(GloryParser.ForParamContext ctx) {
super.enterForParam(ctx);
String firstBlk = "(" + ctx.id().getText() + "=" + ctx.lBound().getText() + ";";
System.out.print(firstBlk);
String secondBlk = ctx.id().getText() + "<" + ctx.uBound().getText() + ";";
System.out.printf(secondBlk);
String thirdBlk = ctx.id().getText() + "++" + ") {";
System.out.println(thirdBlk);
}
@Override
public void enterConditionExpression(GloryParser.ConditionExpressionContext ctx) {
super.enterConditionExpression(ctx);
System.out.println("(" + idFilter(ctx.getText()) + ") {");
}
@Override
public void exitStatement(GloryParser.StatementContext ctx) {
super.exitStatement(ctx);
int intend = loopCount;
while (intend>0) {
for (int i = 0; i < intend; i++) {
System.out.printf(" ");
}
intend--;
}
int loopType = loopTypeList.getLast();
String r = idFilter(ctx.r().getText());
String e = idFilter(ctx.e().getText());
e = checkSin(e);
if ((loopType == GloryParser.NORMAL) || (loopType == GloryParser.OTHER)) {
System.out.println(r + " = " + e);
}
if (loopType == GloryParser.SUMMATION) {
System.out.println(r + " = " + r + " + " + e);
}
if (loopType == GloryParser.PRODUCT) {
System.out.println(r + " = " + r + " * " + e);
}
while (loopCount > 0) {
loopCount--;
for (int i = 0; i < loopCount; i++) {
System.out.printf(" ");
}
System.out.println("}");
}
}
private static String checkSin(String string) {
boolean isFound = string.contains("synthetic");
if (isFound) {
String refinedString = "sin(";
int indexCount = 0;
String[] arr = string.split("_");
for (char ch : arr[1].toCharArray()) {
if (ch == '[' ) {
break;
}
indexCount++;
}
String[] idArray = arr[1].substring(0, indexCount).split("");
String[] indexArray = arr[1].substring(indexCount+1, arr[1].length()-1).split(",");
for (int i = 0; i < indexArray.length; i++) {
refinedString = refinedString + idArray[i] + "[" + indexArray[i] + "]+";
}
refinedString = refinedString.substring(0, refinedString.length()-1);
string = refinedString + ")";
}
return string;
}
private String idFilter(String string) {
StringBuilder sb = new StringBuilder();
boolean andSwitch = true; //
for (char ch : string.toCharArray()) {
if (ch == '$') { // when encounter a '&', flip
andSwitch = !andSwitch;
continue;
}
if (andSwitch) {
sb.append(ch);
}
}
return sb.toString();
}
}