summaryrefslogtreecommitdiffstats
path: root/code/Interpreter/Language.java
diff options
context:
space:
mode:
authorAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-10-25 16:55:22 +0100
committerAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-10-25 16:55:22 +0100
commit74c5732bded6695eed3aabf125a888fbdf206a40 (patch)
treea429332a21ad595c190cae80264fbaf3bf19ed13 /code/Interpreter/Language.java
parentcb29252f1e0d29d555fb232f39d343930fc76105 (diff)
downloadesotericFORTRAN-74c5732bded6695eed3aabf125a888fbdf206a40.tar.gz
esotericFORTRAN-74c5732bded6695eed3aabf125a888fbdf206a40.zip
Interpreter capable of handing variables
Diffstat (limited to 'code/Interpreter/Language.java')
-rw-r--r--code/Interpreter/Language.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/code/Interpreter/Language.java b/code/Interpreter/Language.java
new file mode 100644
index 0000000..80aa1e3
--- /dev/null
+++ b/code/Interpreter/Language.java
@@ -0,0 +1,63 @@
+package Interpreter;
+
+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;
+ Interpreter interpreter = new Interpreter();
+ interpreter.interpret(ast);
+ }
+
+ static void displayError(String message){
+ hadError=true;
+ System.out.println("An error was encountered");
+ System.out.println(message);
+ }
+}