diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-22 18:35:10 +0000 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-22 18:35:10 +0000 |
commit | 6557b7b080abab676cc15774bb4b4428e776cd03 (patch) | |
tree | 93c5f32c5804cf9081e31bacabb39c5756489b44 /src | |
parent | f4c76ea5acaf2213289cd12003d9ec6a7eeae9ca (diff) | |
download | esotericFORTRAN-6557b7b080abab676cc15774bb4b4428e776cd03.tar.gz esotericFORTRAN-6557b7b080abab676cc15774bb4b4428e776cd03.zip |
added argument parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/Compiler/ExecuteC.java | 71 | ||||
-rw-r--r-- | src/Compiler/Language.java | 112 | ||||
-rw-r--r-- | src/Compiler/Translator.java | 10 | ||||
-rw-r--r-- | src/Compiler/helpfile.txt | 19 |
4 files changed, 158 insertions, 54 deletions
diff --git a/src/Compiler/ExecuteC.java b/src/Compiler/ExecuteC.java index 035813a..dd26e3c 100644 --- a/src/Compiler/ExecuteC.java +++ b/src/Compiler/ExecuteC.java @@ -8,34 +8,54 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.nio.file.Paths; +import java.nio.file.Path; import java.nio.file.Files; public class ExecuteC { - public void compileAndExecuteC(List<String> code, String filename) { - writeProgram(code, filename); - if (!compileC(filename)){ - String output = runProgram(filename); - System.out.println(output); + + Path buildToDir; + String filename; + + public void compileAndExecuteC(List<String> code, String outPathStr, boolean executeAfter, boolean leaveCFile) { + + Path outPath = Path.of(outPathStr); + int numElements = outPath.getNameCount(); + filename = outPath.getName(numElements - 1).toString(); + if (numElements == 1) { + buildToDir = Paths.get("build"); + } else { + buildToDir = outPath.subpath(0, numElements - 1); + } + + try { + Files.createDirectories(buildToDir); + } catch (IOException e) { + e.printStackTrace(); } - else{ + + File cProgram = writeProgram(code); + if (!compileC()) { + if (executeAfter) { + String output = runProgram(); + System.out.println(output); + } + } else { Language.displayError("Runtime Error"); } + if (!leaveCFile) { + cProgram.delete(); + } } - public void compileAndExecuteC(List<String> code){ - compileAndExecuteC(code, "main"); + public void compileAndExecuteC(List<String> code) { + compileAndExecuteC(code, "main", true, true); } - public void writeProgram(List<String> codeLines, String filename){ - try { - Files.createDirectories(Paths.get("build")); - } catch (IOException e) { - e.printStackTrace(); - } - + public File writeProgram(List<String> codeLines){ BufferedWriter output = null; + File file = null; try { - File file = Paths.get("build", String.format("%s.c", filename)).toFile(); + file = Paths.get(buildToDir.toString(), String.format("%s.c", filename)).toFile(); output = new BufferedWriter(new FileWriter(file)); for(String line:codeLines){ output.write(line+"\n"); @@ -44,20 +64,23 @@ public class ExecuteC { } catch ( IOException e ) { e.printStackTrace(); } - + return file; } - public Boolean compileC(String filename){ + public Boolean compileC(){ try{ String s= null; Process p; if (System.getProperty("os.name").equals("Linux")) { - p = Runtime.getRuntime().exec(String.format("gcc build/%s.c -o build/%s", filename, filename)); + p = Runtime.getRuntime().exec(String.format( + "gcc %s/%s.c -o %s/%s", + buildToDir.toString(), filename, buildToDir.toString(), filename + )); } else { p = Runtime.getRuntime().exec(String.format( "cmd /C gcc %s -o %s", - Paths.get("build", String.format("%s.c", filename)).toString(), - Paths.get("build", String.format("%s.exe", filename)).toString() + Paths.get(buildToDir.toString(), String.format("%s.c", filename)).toString(), + Paths.get(buildToDir.toString(), String.format("%s.exe", filename)).toString() )); } BufferedReader stdError = new BufferedReader(new @@ -73,15 +96,15 @@ public class ExecuteC { return false; } - public String runProgram(String filename){ + public String runProgram(){ try{ ProcessBuilder probuilder; if (System.getProperty("os.name").equals("Linux")) { - String[] command = {"sh", "-c", String.format("./build/%s", filename)}; + String[] command = {"sh", "-c", Paths.get(buildToDir.toString(), filename).toString()}; probuilder = new ProcessBuilder(command); } else { - String[] command = {"cmd", "/C", Paths.get("build", String.format("%s.exe", filename)).toString()}; + String[] command = {"cmd", "/C", Paths.get(buildToDir.toString(), String.format("%s.exe", filename)).toString()}; probuilder = new ProcessBuilder(command); } Process p = probuilder.start(); diff --git a/src/Compiler/Language.java b/src/Compiler/Language.java index bb7e235..7326d10 100644 --- a/src/Compiler/Language.java +++ b/src/Compiler/Language.java @@ -3,38 +3,75 @@ package Compiler; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.nio.file.Path; import java.util.List; +import java.util.Base64; import java.util.Scanner; +import java.util.ArrayList; +import java.io.File; +import java.io.FileNotFoundException; //Base class for the interpreter public class Language { + + static boolean leaveCFile = false; static boolean hadError = false; - public static void main(String[] args){ - //Allow users to input a single line of code - //Still needs some work to re-ask for input after each line - if (args.length < 1){ - Scanner input = new Scanner(System.in); - String sourceCode = "1"; - while (sourceCode!=""){ - System.out.print("Code: "); - sourceCode = input.nextLine(); - runInterpreter(sourceCode, "out"); - hadError=false; - } - input.close(); + static boolean printC = false; + static boolean executeAfter = false; + static Path sourcefile; - //Allow users to provide a path to a file as an argument - } else if (args.length==1){ - try { - String sourcecode = Files.readString(Paths.get(args[0])); //Maybe should set charset here - runInterpreter(sourcecode, args[0].split("\\.(?=[^\\.]+$)")[0]); - } catch (IOException exception){ - System.out.println("File not found"); + public static void main(String[] args) { + + try { + sourcefile = Paths.get(args[0]); + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + interactiveMode(); + return; + } + + if (args[0].equals("-h") || args[0].equals("--help")) { + System.out.println(getHelpText()); + return; + } + + if (!(Files.exists(sourcefile))) { + System.err.println("Could not find source code path."); + return; + } + + String outname = args[0].split("\\.(?=[^\\.]+$)")[0]; + ArrayList<String> arrayArgs = new ArrayList<>(); + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + arrayArgs.add(arg); + if (arg.equals("-o") || arg.equals("--out")) { + try { + outname = args[i + 1]; + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + System.err.println("Invalid output name provided"); + return; + } + } + if (arg.equals("-c") || arg.equals("--keep-c-file")) { + leaveCFile = true; + } + if (arg.equals("-pc") || arg.equals("--print-c")) { + printC = true; + } + if (arg.equals("-e") || arg.equals("--execute")) { + executeAfter = true; } + } + + if (outname.startsWith("-")) { + System.err.println("Invalid output name provided"); + return; + } - } else { - System.out.println("Error, argument should be file path"); - System.exit(64); + try { + runInterpreter(Files.readString(sourcefile), outname); + } catch (IOException e) { + e.printStackTrace(); } } @@ -55,19 +92,42 @@ public class Language { //Translate AST into equivalent C code Translator translator = new Translator(); - List<String> code = translator.compileToC(ast); + List<String> code = translator.compileToC(ast, printC); if (hadError) return; //Execute created C code ExecuteC cExecutor = new ExecuteC(); - cExecutor.compileAndExecuteC(code, outName); + cExecutor.compileAndExecuteC(code, outName, executeAfter, leaveCFile); } - + private static void interactiveMode() { + Scanner input = new Scanner(System.in); + String sourceCode = "1"; + while (sourceCode!=""){ + System.out.print("Code: "); + sourceCode = input.nextLine(); + runInterpreter(sourceCode, "out"); + hadError=false; + } + input.close(); + } + //Basic error reporting static void displayError(String message){ hadError=true; System.out.println("An error was encountered"); System.out.println(message); } + + static String getHelpText() { + try { + byte[] bytes = Base64.getDecoder().decode(helpb64); + return new String(bytes); + } catch (Exception e) { + e.printStackTrace(); + return "fjakdfjsd"; + } + } + + static String helpb64 = "dXNhZ2U6IExhbmd1YWdlIFstaF0gW3NvdXJjZWZpbGVdIFstbyBvdXRwdXQgcGF0aF0gWy1jXSBbLXBjXSBbLWVdCgpwb3NpdGlvbmFsIGFyZ3VtZW50czoKICAgIGZpbGUgICAgICAgICAgICAgICAgICAgICAgICBwYXRoIHRvIGEgRk9SVFJBTi1saWtlIGZpbGUgdG8gY29tcGlsZS4KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgb21taXR0ZWQsIGdvIHRvIGludGVyYWN0aXZlIG1vZGUuCgpuYW1lZCBhcmd1bWVudHM6CiAgICAtbyAtLW91dCBQQVRIICAgICAgICAgICAgICAgcGF0aCBhbmQgbmFtZSB0byBwcm9kdWNlIHRoZSBvdXRwdXQuIGRvCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5vdCBpbmNsdWRlIGEgZmlsZSBleHRlbnNpb24uIGlmIHByZWNlZWRpbmcKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGlyZWN0b3JpZXMgZG8gbm90IGV4aXN0LCB0aGV5IHdpbGwgYmUgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNyZWF0ZWQuCgpmbGFnczoKICAgIC1oIC0taGVscCAgICAgICAgICAgICAgICAgICBwcmludCB0aGlzIG1lc3NhZ2UgYW5kIGV4aXQuCiAgICAtYyAtLWtlZXAtYy1maWxlICAgICAgICAgICAgdGhlIGMgcHJvZ3JhbSBwcm9kdWNlZCBpbiB0aGUgY29tcGlsYXRpb24KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJvY2VzcyB3aWxsIG5vdCBiZSBkZWxldGVkLgogICAgLXBjIC0tcHJpbnQtYyAgICAgICAgICAgICAgIHRoZSBjIHByb2dyYW0gd2lsbCBiZSBwcmludGVkIHRvIHN0ZG91dAogICAgLWUgLS1leGVjdXRlICAgICAgICAgICAgICAgIHRoZSBwcm9kdWNlZCBiaW5hcnkgd2lsbCBiZSBleGVjdXRlZCBhcyBzb29uCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFzIGNvbXBpbGF0aW9uIGlzIGZpbmlzaGVkLgo="; } diff --git a/src/Compiler/Translator.java b/src/Compiler/Translator.java index 50430f2..ee43905 100644 --- a/src/Compiler/Translator.java +++ b/src/Compiler/Translator.java @@ -13,7 +13,7 @@ public class Translator{ private Environment environment = new Environment(); - public List<String> compileToC(List<Statement> statements){ + public List<String> compileToC(List<Statement> statements, boolean printC){ CCode.add("#include <stdio.h>"); CCode.add("#include <string.h>"); CCode.add("int main(){"); @@ -26,10 +26,12 @@ public class Translator{ } CCode.add("}"); - for(String t:CCode){ - System.out.println(t); + if (printC) { + for(String t:CCode){ + System.out.println(t); + } + System.out.println(""); } - System.out.println(""); return CCode; } diff --git a/src/Compiler/helpfile.txt b/src/Compiler/helpfile.txt new file mode 100644 index 0000000..1d1c22b --- /dev/null +++ b/src/Compiler/helpfile.txt @@ -0,0 +1,19 @@ +usage: Language [-h] [sourcefile] [-o output path] [-c] [-pc] [-e]
+
+positional arguments:
+ file path to a FORTRAN-like file to compile.
+ if ommitted, go to interactive mode.
+
+named arguments:
+ -o --out PATH path and name to produce the output. do
+ not include a file extension. if preceeding
+ directories do not exist, they will be
+ created.
+
+flags:
+ -h --help print this message and exit.
+ -c --keep-c-file the c program produced in the compilation
+ process will not be deleted.
+ -pc --print-c the c program will be printed to stdout
+ -e --execute the produced binary will be executed as soon
+ as compilation is finished.
|