-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
112 lines (111 loc) · 3.9 KB
/
Copy pathMain.java
File metadata and controls
112 lines (111 loc) · 3.9 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
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class Main extends CompilerMain {
public static void main(String[] args) {
ArgParser argParser = new ArgParser(args);
String fileName;
boolean hotReload = false;
try {
fileName = argParser.getArgs().get(0);
} catch (IndexOutOfBoundsException e) {
fileName = "";
}
if (argParser.getItems().contains("hotreload") || argParser.getItems().contains("r")) {
if (!fileName.equals("")) {
hotReload = true;
if (argParser.getItems().contains("shell")) {
System.err.println("ERROR: shell and hot reload Interference");
hotReload = false;
}
} else {
System.err.println("when hot reload enabled, the file name must be entered [hot reload disabled]");
}
}
if (fileName.endsWith(".ser")) {
SyntaxTreeSerializer serializer = new SyntaxTreeSerializer();
serializer.deserialize(fileName).eval();
System.exit(0);
}
if (fileName.endsWith(".xml")) {
XMLToSyntaxTree xmlToSyntaxTree = new XMLToSyntaxTree();
File file = new File(fileName);
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.err.println("Can't open the file");
return;
}
scanner.useDelimiter("\\Z");
try {
xmlToSyntaxTree.xmlToProgram(scanner.next()).eval();
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
String outputFileName = argParser.getValues().get("output");
String classFileName = argParser.getValues().get("class-output");
String xmlFileName = argParser.getValues().get("xml-output");
String serializeFileName = argParser.getValues().get("serialize");
if (serializeFileName != null && !serializeFileName.endsWith(".ser")) {
serializeFileName += ".ser";
}
if (serializeFileName != null && !serializeFileName.endsWith(".xml")) {
serializeFileName += ".xml";
}
if (fileName.equals(outputFileName) && !argParser.getItems().contains("y")) {
System.err.print("input and output files are the same file. continue? [y/n] ");
Scanner scanner = new Scanner(System.in);
if (!scanner.next().equals("y")) {
System.out.println();
System.exit(0);
}
System.out.println();
}
if (serializeFileName != null && serializeFileName.equals(outputFileName) && !argParser.getItems().contains("y")) {
System.err.print("serialize and output files are the same file. continue? [y/n] ");
Scanner scanner = new Scanner(System.in);
if (!scanner.next().equals("y")) {
System.out.println();
System.exit(0);
}
System.out.println();
}
if (xmlFileName != null && xmlFileName.equals(outputFileName) && !argParser.getItems().contains("y")) {
System.err.print("xml output and output files are the same file. continue? [y/n] ");
Scanner scanner = new Scanner(System.in);
if (!scanner.next().equals("y")) {
System.out.println();
System.exit(0);
}
System.out.println();
}
Compiler compiler = new Compiler(fileName, argParser.getItems().contains("shell"), outputFileName, (classFileName != null? classFileName.split("\\.")[0]:null), serializeFileName, xmlFileName);
do {
if (argParser.getItems().contains("lex")) {
System.out.println(lex(compiler));
} else {
if (argParser.getItems().contains("shell") || hotReload) {
SyntaxTree.getFunctions().clear();
SyntaxTree.getVariables().clear();
SyntaxTree.getClassesParameters().clear();
SyntaxTree.CreateLambda.setCounter(0);
SyntaxTree.resetNameSpaces();
}
compile(compiler);
}
if (hotReload) {
long lastModified, previousLastModified;
File file = new File(fileName);
lastModified = file.lastModified();
previousLastModified = lastModified;
while (lastModified == previousLastModified) {
lastModified = file.lastModified();
}
System.out.println("\n" + fileName + " rewritten...\n");
}
} while (argParser.getItems().contains("shell") || hotReload);
}
}