summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Language.java
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2021-11-22 18:35:10 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2021-11-22 18:35:10 +0000
commit6557b7b080abab676cc15774bb4b4428e776cd03 (patch)
tree93c5f32c5804cf9081e31bacabb39c5756489b44 /src/Compiler/Language.java
parentf4c76ea5acaf2213289cd12003d9ec6a7eeae9ca (diff)
downloadesotericFORTRAN-6557b7b080abab676cc15774bb4b4428e776cd03.tar.gz
esotericFORTRAN-6557b7b080abab676cc15774bb4b4428e776cd03.zip
added argument parsing
Diffstat (limited to 'src/Compiler/Language.java')
-rw-r--r--src/Compiler/Language.java112
1 files changed, 86 insertions, 26 deletions
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=";
}