summaryrefslogtreecommitdiffstats
path: root/code/Interpreter/Interpreter.java
blob: 17f2ccfc6138bdc1f7ad333a9393f4b9c1bd23db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 Interpreter {
    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);
            System.out.print("Code: ");
            String sourceCode = input.nextLine();
            runInterpreter(sourceCode);
            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);
        }
    }

    private static void runInterpreter(String sourceCode){
        TokenScanner scanner = new TokenScanner();
        List<Token> tokens = scanner.extractTokens(sourceCode);
        for (Token token : tokens) {
            System.out.println(token);
        }
    }
}