summaryrefslogtreecommitdiff
path: root/src/main/java/no/eliashaugsbakk/kompilator/Main.java
diff options
context:
space:
mode:
authorElias Haugsbakk <[email protected]>2026-09-18 00:13:13 +0200
committerElias Haugsbakk <[email protected]>2026-09-18 00:13:13 +0200
commitc7363c58612b7d3183c0ee91d5eee166fd515758 (patch)
tree175dfd722168a51c446da7f3a971f4464a3be30b /src/main/java/no/eliashaugsbakk/kompilator/Main.java
parent30b415615740939a7522a20f222699a30f89a22e (diff)
implement print("String") for IR to assembly translation
Diffstat (limited to 'src/main/java/no/eliashaugsbakk/kompilator/Main.java')
-rw-r--r--src/main/java/no/eliashaugsbakk/kompilator/Main.java62
1 files changed, 31 insertions, 31 deletions
diff --git a/src/main/java/no/eliashaugsbakk/kompilator/Main.java b/src/main/java/no/eliashaugsbakk/kompilator/Main.java
index f57b13a..be5746c 100644
--- a/src/main/java/no/eliashaugsbakk/kompilator/Main.java
+++ b/src/main/java/no/eliashaugsbakk/kompilator/Main.java
@@ -8,39 +8,41 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.List;
public class Main {
public static final String programFileExtension = "spå";
- private record InputProgram(String programName, String programContent) {}
+
+ private record InputProgram(String programName, String programContent) {
+ }
static void main(String[] args) {
if (args.length != 1) {
- IO.println("Only accepts one argument: The filename of the input program");
+ IO.println("err: Only accepts one argument: The filename of the input program");
System.exit(1);
}
InputProgram inputProgram = readInputProgram(args[0]);
- /*
- String IR = createIR(inputFile);
- String assembly = createAssembly(IR);
- */
+ List<String> IR = List.of();
- assemblingAndLInking(inputProgram);
+ String assembly = new AssemblyBuilder().createAssembly(IR);
+
+ assemblingAndLInking(inputProgram.programName(), assembly);
System.exit(0);
}
- private static void assemblingAndLInking(InputProgram inputProgram) {
- String assemblyFileName = inputProgram.programName + ".s";
- String assembledFileName = inputProgram.programContent + ".o";
+ private static void assemblingAndLInking(String programName, String assembly) {
+ String assemblyFileName = programName + ".s";
+ String assembledFileName = programName + ".o";
try {
FileWriter fw = new FileWriter(assemblyFileName);
- fw.write(inputProgram.programContent);
+ fw.write(assembly);
fw.close();
} catch (IOException e) {
- IO.println("Could not write file: " + e.getMessage());
+ IO.println("err: Could not write file: " + e.getMessage());
}
try {
@@ -48,39 +50,41 @@ public class Main {
ProcessBuilder asPB = new ProcessBuilder("as", "-o", assembledFileName, assemblyFileName);
runProcess(asPB);
} catch (IOException e) {
- IO.println("Failed to invoke GCC\n\n" + e);
+ IO.println("err: Failed to invoke GCC\n\n" + e);
} catch (InterruptedException e) {
- IO.println("Failed to wait for gcc output\n\n" + e);
+ IO.println("err: Failed to wait for gcc output\n\n" + e);
}
try {
// Linking using GNU linker: ld -o program program.o
- ProcessBuilder ldPB = new ProcessBuilder("ld", "-o", inputProgram.programName, assembledFileName);
+ ProcessBuilder ldPB = new ProcessBuilder("ld", "-o", programName, assembledFileName);
runProcess(ldPB);
} catch (IOException e) {
- IO.println("Failed to invoke GCC\n\n" + e);
+ IO.println("err: Failed to invoke GCC\n\n" + e);
} catch (InterruptedException e) {
- IO.println("Failed to wait for gcc output\n\n" + e);
+ IO.println("err: Failed to wait for gcc output\n\n" + e);
}
// Clean up
try {
Files.delete(Path.of(assemblyFileName));
} catch (IOException e) {
- IO.println("Could not delete assembly file: " + e.getMessage());
+ IO.println("err: Could not delete assembly file: " + e.getMessage());
}
try {
Files.delete(Path.of(assembledFileName));
} catch (IOException e) {
- IO.println("Could not delete assembled file: " + e.getMessage());
+ IO.println("err: Could not delete assembled file: " + e.getMessage());
}
}
- private static void runProcess(ProcessBuilder processBuilder) throws IOException, InterruptedException {
+ private static void runProcess(ProcessBuilder processBuilder)
+ throws IOException, InterruptedException {
Process process = processBuilder.start();
process.waitFor();
if (process.exitValue() != 0) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+ BufferedReader bufferedReader =
+ new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
@@ -91,21 +95,17 @@ public class Main {
}
private static InputProgram readInputProgram(String inputFilePath) {
- IO.println("Input file path: " + inputFilePath);
-
if (!inputFilePath.endsWith("." + programFileExtension)) {
- IO.println("Input file must use the file extension: " + programFileExtension);
+ IO.println("err: Input file must use the file extension: " + programFileExtension);
System.exit(1);
}
- String programName = inputFilePath.substring(
- inputFilePath.lastIndexOf("/") + 1,
- inputFilePath.lastIndexOf("."));
- IO.println("Program name: " + programName);
+ String programName =
+ inputFilePath.substring(inputFilePath.lastIndexOf("/") + 1, inputFilePath.lastIndexOf("."));
String programContent = null;
- try(BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
+ try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
@@ -116,13 +116,13 @@ public class Main {
}
programContent = sb.toString();
} catch (FileNotFoundException e) {
- IO.println("File not found");
+ IO.println("err: File not found");
} catch (IOException e) {
IO.println(e.getMessage());
}
if (programContent == null) {
- IO.println("Could not read input file");
+ IO.println("err: Could not read input file");
System.exit(1);
}