Repository navigation
Expand file tree
/
Copy pathLJUI.java
More file actions
273 lines (243 loc) · 10.1 KB
/
Copy pathLJUI.java
File metadata and controls
273 lines (243 loc) · 10.1 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
package com.github.lessjava;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import com.github.lessjava.generated.LJLexer;
import com.github.lessjava.generated.LJParser;
import com.github.lessjava.types.ast.ASTProgram;
import com.github.lessjava.types.ast.ASTClass;
import com.github.lessjava.types.ast.ASTClassBlock;
import com.github.lessjava.visitor.impl.BuildParentLinks;
import com.github.lessjava.visitor.impl.BuildSymbolTables;
import com.github.lessjava.visitor.impl.PrintDebugTree;
import com.github.lessjava.visitor.impl.LJASTBuildClassLinks;
import com.github.lessjava.visitor.impl.LJASTCheckTypesHaveChanged;
import com.github.lessjava.visitor.impl.LJASTConverter;
import com.github.lessjava.visitor.impl.LJASTInferConstructors;
import com.github.lessjava.visitor.impl.LJASTInferTypes;
import com.github.lessjava.visitor.impl.LJAssignTestVariables;
import com.github.lessjava.visitor.impl.LJGenerateJava;
import com.github.lessjava.visitor.impl.LJInstantiateFunctions;
import com.github.lessjava.visitor.impl.LJStaticAnalysis;
import com.github.lessjava.visitor.impl.StaticAnalysis;
public class LJUI extends JFrame
{
public static void main(String args[])
{
(new LJUI()).setVisible(true);
}
public LJUI()
{
// main text fields in a split pane
JTextArea editField = new JTextArea();
editField.setText("main() {\n println(\"Hello, world!\")\n}\n" +
"test 1+2 == 3\n");
editField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13));
JTextArea outputField = new JTextArea();
editField.setFont(new Font(Font.MONOSPACED, Font.BOLD, 13));
JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
new JScrollPane(editField), new JScrollPane(outputField));
splitPanel.setResizeWeight(0.33);
// toolbar
JButton testButton = new JButton();
testButton.setLabel("Test");
testButton.setToolTipText("Run tests");
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ outputField.setText(compile(editField.getText()) + test()); }
});
JButton runButton = new JButton();
runButton.setLabel("Run");
runButton.setToolTipText("Run program");
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ outputField.setText(compile(editField.getText()) + run()); }
});
JToolBar toolbar = new JToolBar();
toolbar.add(testButton);
toolbar.add(runButton);
// top-level panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(toolbar, BorderLayout.NORTH);
mainPanel.add(splitPanel, BorderLayout.CENTER);
add(mainPanel);
pack();
// main window
setTitle("Less-Java");
setSize(500,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void deleteFile(String path)
{
File f = new File(path);
if (f.isDirectory()) {
String[] children = f.list();
if (children == null) {
// empty folder; just delete it
f.delete();
} else {
// non-empty folder; delete all children first
for (String c : children) {
deleteFile(f.getAbsolutePath() + File.separator + c);
}
}
}
f.delete();
}
public String compile(String code)
{
// remove any old generated files
deleteFile("generated");
new File("generated").mkdir();
// add newline to avoid EOL/EOF issues w/ ANTLR:
// https://www.antlr3.org/pipermail/antlr-interest/2011-January/040642.html
code += "\n";
// lexing and parsing (TODO: report errors in UI)
LJLexer lexer = new LJLexer(new ANTLRInputStream(code));
LJParser parser = new LJParser(new CommonTokenStream(lexer));
ParseTree parseTree = parser.program();
if (parser.getNumberOfSyntaxErrors() != 0) {
return "Syntax error";
}
// conversion to AST
ParseTreeWalker walker = new ParseTreeWalker();
LJASTConverter converter = new LJASTConverter();
walker.walk(converter, parseTree);
ASTProgram program = converter.getAST();
// visitors
BuildParentLinks buildParentLinks = new BuildParentLinks();
BuildSymbolTables buildSymbolTables = new BuildSymbolTables();
LJInstantiateFunctions instantiateFunctions = new LJInstantiateFunctions();
LJASTInferTypes inferTypes = new LJASTInferTypes();
LJASTInferConstructors inferConstructors = new LJASTInferConstructors();
LJASTCheckTypesHaveChanged checkTypesHaveChanged = new LJASTCheckTypesHaveChanged();
// reset static analysis (TODO: use fewer static data structures to
// avoid having to do this)
StaticAnalysis.resetErrors();
BuildSymbolTables.nodeSymbolTableMap.clear();
LJASTBuildClassLinks.nameClassMap.clear();
LJASTBuildClassLinks.nameClassMap.put("Object",null);
LJASTBuildClassLinks.nameClassMap.put("String", null);
LJASTBuildClassLinks.nameClassMap.put("Integer", null);
LJASTBuildClassLinks.nameClassMap.put("Double", null);
LJASTBuildClassLinks.nameClassMap.put("Boolean", null);
LJASTBuildClassLinks.nameClassMap.put("Char", null);
LJASTBuildClassLinks.nameClassMap.put("LJList", null);
LJASTBuildClassLinks.nameClassMap.put("LJSet", null);
LJASTBuildClassLinks.nameClassMap.put("LJMap", null);
ASTClass.nameClassMap.clear();
ASTClassBlock.nameAttributeMap.clear();
StaticAnalysis.collectErrors = true;
// initial processing
program.traverse(buildParentLinks);
program.traverse(new LJASTBuildClassLinks());
if(!StaticAnalysis.getErrors().isEmpty()) {
return StaticAnalysis.getErrorString();
}
program.traverse(new LJASTInferConstructors());
program.traverse(buildSymbolTables);
// iterative type inference
do {
program.traverse(buildParentLinks);
StaticAnalysis.resetErrors();
program.traverse(buildSymbolTables);
program.traverse(instantiateFunctions);
program.traverse(inferTypes);
program.traverse(checkTypesHaveChanged);
} while (LJASTCheckTypesHaveChanged.typesChanged);
// type checking
program.traverse(new LJAssignTestVariables());
program.traverse(new LJStaticAnalysis());
if (!StaticAnalysis.getErrors().isEmpty()) {
return StaticAnalysis.getErrorString();
}
// code generation
program.traverse(new LJGenerateJava());
// run Java compiler (TODO: report error messages in UI)
LJCompiler.JCompiler.compile();
return "";
}
private String test()
{
// TODO: fix and re-enable this (and remove all use of test.sh)
//return runShellCommand("java -jar junit-platform-console-standalone-1.4.2.jar" +
//" --class-path generated --include-classname='.*'" +
//" --disable-banner -c Main");
try {
PrintWriter script = new PrintWriter("test.sh");
script.println("java -jar junit-platform-console-standalone-1.4.2.jar" +
" --class-path generated --include-classname='.*'" +
" --disable-banner -c Main");
script.close();
} catch (FileNotFoundException ex) {
return ex.getMessage();
}
String[] results = runShellCommand("sh test.sh").split("\n");
StringBuilder str = new StringBuilder();
for (String line : results) {
if (line.contains("tests found") ||
line.contains("tests successful") ||
line.contains("tests failed") ||
line.contains("tests aborted") ||
line.contains("tests successful") ||
line.contains("AssertionFailedError")) {
str.append(line + "\n");
}
}
return str.toString();
}
private String run()
{
return runShellCommand("java -cp generated:lj-ui.jar Main");
}
/**
* Runs a command as if it was executed from a command-line shell.
*
* @param command Text of command to execute
* @return All output as a String
*/
private static String runShellCommand(String command)
{
StringBuilder str = new StringBuilder();
try {
Process cmdProc = Runtime.getRuntime().exec(command);
String line;
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(cmdProc.getInputStream()));
while ((line = stdoutReader.readLine()) != null) {
str.append(line + "\n");
}
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(cmdProc.getErrorStream()));
while ((line = stderrReader.readLine()) != null) {
str.append(line + "\n");
}
cmdProc.waitFor();
} catch (IOException ex) {
str.append("ERROR: " + ex.getMessage());
} catch (InterruptedException ex) {
str.append("ERROR: " + ex.getMessage());
}
return str.toString();
}
}