summaryrefslogtreecommitdiff
path: root/jlox/app/src/main/java/org/example/lox.java
blob: e15f8adcd93458e17bc9f07bf3e51779c3255526 (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
package org.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

public class lox {
    public static void main(String[] args) throws IOException {
        switch (args.length) {
            case 0:
                RunPropmt();
                return;
            case 1:
                InerpritFile(args[0]);
                return;
            default:
                System.out.println("Invalid arguments");
        }
    }

    private static void RunPropmt() throws IOException {
        InputStreamReader inp = new InputStreamReader(System.in);
        BufferedReader reader= new BufferedReader(inp);

        while (true) {
            System.out.print("$ ");
            String prompt = reader.readLine();
            if (prompt == null) break;
            run(prompt);
        }
    }

    private static void InerpritFile(String fileName) throws IOException {
        byte[] bytes = Files.readAllBytes(Paths.get(fileName));
        run(new String(bytes, Charset.defaultCharset()));
    }

    private static void run(String program) {
        System.out.println(program);
    }
}