diff options
author | AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com> | 2021-11-06 01:44:14 +0000 |
---|---|---|
committer | AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com> | 2021-11-06 01:44:14 +0000 |
commit | 0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed (patch) | |
tree | d64267b7da1691bad8797f81188798fb9628a212 /src/Compiler/Language.java | |
parent | d3c80f8bd236b1b4ed571ed6b347095efdaa99ed (diff) | |
download | esotericFORTRAN-0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed.tar.gz esotericFORTRAN-0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed.zip |
Re-arranged files and added C compilation
Diffstat (limited to 'src/Compiler/Language.java')
-rw-r--r-- | src/Compiler/Language.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Compiler/Language.java b/src/Compiler/Language.java new file mode 100644 index 0000000..9151bd5 --- /dev/null +++ b/src/Compiler/Language.java @@ -0,0 +1,66 @@ +package Compiler; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Scanner; + +//Base class for the interpreter +public class Language { + 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); + hadError=false; + } + input.close(); + + //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); + } catch (IOException exception){ + System.out.println("File not found"); + } + + } else { + System.out.println("Error, argument should be file path"); + System.exit(64); + } + } + + //Extract and print each token + private static void runInterpreter(String sourceCode){ + TokenScanner scanner = new TokenScanner(); + List<Token> tokens = scanner.extractTokens(sourceCode); + //for (Token token : tokens) { + // System.out.println(token); + //} + if (hadError) return; + //Parse into AST + Parser parser = new Parser(tokens); + List<Statement> ast = parser.parse(); + if (hadError) return; + Translator translator = new Translator(); + List<String> code = translator.compileToC(ast); + if (hadError) return; + ExecuteC cExecutor = new ExecuteC(); + cExecutor.compileAndExecuteC(code); + } + + static void displayError(String message){ + hadError=true; + System.out.println("An error was encountered"); + System.out.println(message); + } +} |