Skip to content

Commit 139b076

Browse files
committed
Add file writing to write the final result
1 parent 39f9afc commit 139b076

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/pgdp/minijava/Compiler.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
package pgdp.minijava;
22

3+
import pgdp.minijava.ast.SyntaxTreeNode;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.StandardOpenOption;
9+
310
public class Compiler {
11+
private Compiler() {
12+
13+
}
14+
415
public static void main(String[] args) {
516
compileFromFile("resources/input.java");
617
}
718

819
public static void compileFromFile(String filePath) {
920
SyntaxTreeNode node = Parser.parseFromFile(filePath);
10-
String out = Emitter.emit(node);
21+
String out = """
22+
//Generated using my MiniJavaCompiler at https://github.com/SilicDev/MiniJavaCompiler
23+
""" + Emitter.emit(node);
24+
25+
if(filePath.startsWith("resources/")) {
26+
filePath = "resources/bin/" + filePath.substring(10);
27+
}
28+
if(filePath.endsWith(".java")) {
29+
filePath = filePath.substring(0, filePath.length() - 5) + ".jvm";
30+
}
31+
Path path = Path.of(filePath);
32+
try {
33+
Files.writeString(path, out, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
1137
}
1238
}

0 commit comments

Comments
 (0)