-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentScriptLoader.java
188 lines (164 loc) · 7.35 KB
/
ArgumentScriptLoader.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
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
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ArgumentScriptLoader extends ArgumentScriptBaseListener{
PrintWriter fileWriter = new PrintWriter("Out.java");
// Stores formatted terms after they've been parsed
ArrayList<String> terms = new ArrayList<>();
ArrayList<String> arguments = new ArrayList<>();
public ArgumentScriptLoader() throws FileNotFoundException {
}
// Removes quotes and spaces from term
public String condenseTerm(String term) {
ArrayList<String> split = new ArrayList(Arrays.asList(term.split(" ")));
term = "";
for(int i=0;i<split.size();i++) {
split.set(i, (Character.toUpperCase(split.get(i).charAt(0)) +
split.get(i).substring(1,split.get(i).length())));
term += split.get(i);
}
term = term.replaceAll("\\s", "");
term = term.replaceAll("\"","");
char first = term.charAt(0);
return(Character.toLowerCase(first) + term.substring(1,term.length()));
}
public String parseAnd(ArgumentScriptParser.PropositionContext proposition) {
return(proposition.getText());
}
@Override
public void enterFile(ArgumentScriptParser.FileContext ctx) {
fileWriter.println("public class Out {\n" +
" public static void main(String[] args) {\n");
}
@Override
public void exitFile(ArgumentScriptParser.FileContext ctx) {
for(String a:arguments) {
fileWriter.println(a);
}
for(String t:terms) {
fileWriter.println(" System.out.println(\"" + t + "\" + \" = \" + " + t + ");");
}
fileWriter.println("}}");
fileWriter.close();
}
@Override
public void enterDefinition(ArgumentScriptParser.DefinitionContext ctx) {
// Create ArrayList of each line in the definition
ArrayList<String> defs = new ArrayList<String>(Arrays.asList(ctx.getText().split("\n")));
// Remove definition header and footer
defs.remove(defs.size() - 1);
defs.remove(0);
// Output a condensed version of each string to the file as a boolean
for(String s:defs) {
terms.add(this.condenseTerm(s));
fileWriter.println(" String " + this.condenseTerm(s) + " = \"unknown\";");
}
}
@Override
public void exitDefinition(ArgumentScriptParser.DefinitionContext ctx) {
fileWriter.println("\n");
}
public void parseImp(TerminalNodeImpl term) {
}
public ParseTree parseProp(ParseTree node) {
/*try {
if(node.getChild(0).getText().equals("~")) {
if(node.getChild(1).getChild(1).getText().equals("->")) {
System.out.println("Found an IF");
fileWriter.println(" if(!" +
this.condenseTerm(node.getChild(1).getChild(0).getText()) +
") {");
fileWriter.println(" }");
}
} else {
if(node.getChild(1).getText().equals("->")) {
System.out.println("Found an IF");
fileWriter.println(" if(" +
this.condenseTerm(node.getChild(0).getText()) +
") {");
fileWriter.println(" }");
}
}
} catch(NullPointerException e) {
System.out.println("Found a TERM");
}*/
//System.out.println(node.getChildCount());
if(node.getChildCount() == 2) {
parseProp(node.getChild(1));
} else if(node.getChild(1).getText().equals("|")) {
arguments.add(" if(" +
this.condenseTerm(node.getChild(0).getText()) +
".equals(\"false\")) {" + this.condenseTerm(node.getChild(2).getText()) +
" = \"true\"; }\n");
arguments.add(" if(" +
this.condenseTerm(node.getChild(2).getText()) +
".equals(\"false\")) {" + this.condenseTerm(node.getChild(0).getText()) +
" = \"true\"; }\n");
} else if(node.getChild(1).getText().equals("&")) {
arguments.add(" " + this.condenseTerm(node.getChild(0).getText()) +
" = \"true\";\n");
arguments.add(" " + this.condenseTerm(node.getChild(2).getText()) +
" = \"true\";\n");
} else if(node.getChild(1).getText().equals("->")) {
arguments.add(" if(" +
this.condenseTerm(node.getChild(0).getText()) +
".equals(\"true\")) {" +
this.condenseTerm(node.getChild(2).getText()) +
" = \"true\";}");
//this.parseProp(node.getChild(2)) +
} else if(node.getChildCount() == 0) {
arguments.add(this.condenseTerm(node.getText() + " = \"true\""));
}
return(node);
}
@Override
public void enterArgument(ArgumentScriptParser.ArgumentContext ctx) {
for(ArgumentScriptParser.PropositionContext p:ctx.proposition()) {
//System.out.println(this.parseProp(p.getPayload()).getText());
this.parseProp(p.getPayload()).getText();
//System.out.println("-------------");
//fileWriter.println(parseProp(p));
}
}
@Override
public void exitArgument(ArgumentScriptParser.ArgumentContext ctx) {
super.exitArgument(ctx);
}
public void parseAsrt(ParseTree node) {
}
@Override
public void enterAssertion(ArgumentScriptParser.AssertionContext ctx) {
for(int i=0;i<ctx.getPayload().getChildCount();i++) {
//System.out.println(ctx.getPayload().getChild(i).getText());
if(ctx.getPayload().getChild(i).getText().equals("~")) {
fileWriter.println(" " +
this.condenseTerm(ctx.getPayload().getChild(i+1).getText()) +
" = \"false\";");
i++;
} else if(ctx.getPayload().getChild(i).getText().charAt(0) == '"'){
fileWriter.println(this.condenseTerm(ctx.getPayload().getChild(i).getText()) +
" = \"true\";");
}
//System.out.println(ctx.getChild(i).getText());
}
}
@Override
public void exitAssertion(ArgumentScriptParser.AssertionContext ctx) {
super.exitAssertion(ctx);
}
@Override
public void enterProposition(ArgumentScriptParser.PropositionContext ctx) {
}
@Override
public void exitProposition(ArgumentScriptParser.PropositionContext ctx) {
super.exitProposition(ctx);
}
}