summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Language.java
diff options
context:
space:
mode:
authorAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-12-05 03:27:18 +0000
committerAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-12-05 03:27:18 +0000
commit9e6c89f1fa93287104381e02f0bbbdd6060a9382 (patch)
tree65a26fbbccb0172805b131b7100ffcabb7790da3 /src/Compiler/Language.java
parent43909b350b9084ed33f121a15c5770224cbdc79f (diff)
downloadesotericFORTRAN-9e6c89f1fa93287104381e02f0bbbdd6060a9382.tar.gz
esotericFORTRAN-9e6c89f1fa93287104381e02f0bbbdd6060a9382.zip
Added subroutines and comments for most files
Diffstat (limited to 'src/Compiler/Language.java')
-rw-r--r--src/Compiler/Language.java60
1 files changed, 43 insertions, 17 deletions
diff --git a/src/Compiler/Language.java b/src/Compiler/Language.java
index b433b36..5013cfd 100644
--- a/src/Compiler/Language.java
+++ b/src/Compiler/Language.java
@@ -8,7 +8,10 @@ import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
-//Base class for the interpreter
+/**
+ *Base class for running the Compiler
+ *Can run either from a file, or in an interactive mode
+ */
public class Language {
static boolean leaveCFile = false;
@@ -17,8 +20,10 @@ public class Language {
static boolean executeAfter = false;
static Path sourcefile;
+ //Main function for the compiler
public static void main(String[] args) {
+ //Extract required command line arguments
try {
sourcefile = Paths.get(args[0]);
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
@@ -38,6 +43,8 @@ public class Language {
Path initOutPath = Paths.get(args[0]);
String outname = initOutPath.getName(initOutPath.getNameCount() - 1).toString().split("\\.(?=[^\\.]+$)")[0];
+
+ //Extract optional command line arguments
ArrayList<String> arrayArgs = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
String arg = args[i];
@@ -66,21 +73,24 @@ public class Language {
return;
}
+ //Run the compiler on the source code
try {
- runInterpreter(Files.readString(sourcefile), outname);
+ runCompiler(Files.readString(sourcefile), outname);
} catch (IOException e) {
e.printStackTrace();
}
}
- //Function to take source code and output the result
- private static void runInterpreter(String sourceCode, String outName){
+ /**
+ * Function to take source code, run the compiler and write to C
+ *
+ * @param sourcecode the full source code as a string
+ * @param outName the name to write the compiled code to
+ */
+ private static void runCompiler(String sourceCode, String outName){
//Extract tokens from the source code
TokenScanner scanner = new TokenScanner();
List<Token> tokens = scanner.extractTokens(sourceCode);
- //for (Token token : tokens) {
- // System.out.println(token);
- //}
if (hadError) return;
//Parse into AST
@@ -98,25 +108,37 @@ public class Language {
cExecutor.compileAndExecuteC(code, outName, executeAfter, leaveCFile);
}
+ /**
+ * Method for running the compiler in an interactive mode
+ */
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();
+ String sourceCode = "1";
+ //Run compiler line by line
+ while (sourceCode!=""){
+ System.out.print("Code: ");
+ sourceCode = input.nextLine();
+ runCompiler(sourceCode, "out");
+ hadError=false;
+ }
+ input.close();
}
- //Basic error reporting
+ /**
+ * Method for displaying an error to the user
+ * @param line the line the error occured on
+ * @param message an error message to display to the user
+ */
static void displayError(int line,String message){
hadError=true;
System.out.println("An error was encountered on line: "+line);
System.out.println(message);
}
- //Basic error reporting
+ /**
+ * Method for displaying error based on a specific token
+ * @param token the token the parser detected the error on
+ * @param message an error message to display to the user
+ */
static void displayError(Token token,String message){
hadError=true;
System.out.println("An error was encountered on line: "+token.line);
@@ -124,6 +146,10 @@ public class Language {
System.out.println(message);
}
+ /**
+ * Method for getting the helpfile text
+ * @return the text for the helpfile
+ */
private static String getHelpText(){
String helpText = "";
try {