aboutsummaryrefslogtreecommitdiffstats
path: root/main/Main.java
blob: 77c2f51c5b8315880de8bdba2db0c6924d5d74d0 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.jantuomi.interpreter.main;

import com.jantuomi.interpreter.main.core.CommandLineArgumentContainer;
import com.jantuomi.interpreter.main.core.parser.ASTGenerator;
import com.jantuomi.interpreter.main.core.parser.Parser;
import com.jantuomi.interpreter.main.core.parser.ast.ASTNode;
import com.jantuomi.interpreter.main.core.runtime.Interpreter;
import com.jantuomi.interpreter.main.core.tokenizer.Tokenizer;
import com.jantuomi.interpreter.main.core.tokenizer.token.Token;
import com.jantuomi.interpreter.main.exception.InterpreterException;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        boolean parseSuccess = parseArguments(args);
        if (!parseSuccess) {
            throw new Exception("Argument files could not be parsed successfully.");
        }

        if (CommandLineArgumentContainer.getInstance().isInteractive()) {
            repl();
        } else {
            String sourceFileContents = CommandLineArgumentContainer.getInstance().getSourceFileContents();
            run(sourceFileContents);
        }
    }

    public static void repl() {
        Scanner scanner = new Scanner(System.in);
        String input;
        while (true) {
            System.out.print(">> ");
            input = scanner.nextLine();

            try {
                run(input);
            } catch (InterpreterException e) {
                e.printStackTrace();
            }
        }
    }

    public static void run(String input) throws InterpreterException {
        Tokenizer tokenizer = Tokenizer.getInstance();
        List<Token> sequence = tokenizer.tokenize(input);

        Collections.reverse(sequence);

        List<Token> trees = Parser.getInstance().parse(sequence);
        if (CommandLineArgumentContainer.getInstance().isDebugModeActive()) {
            Parser.getInstance().printAllTrees(trees);
        }

        List<ASTNode> nodes = ASTGenerator.getInstance().generate(trees);
        if (CommandLineArgumentContainer.getInstance().isDebugModeActive()) {
            ASTGenerator.getInstance().printAllTrees(nodes);
        }

        String output = Interpreter.execute(nodes);

        if (output.trim().length() > 0) {
            System.out.println(output);
        }
    }

    public static boolean parseArguments(String[] args) {
        CommandLineArgumentContainer container = CommandLineArgumentContainer.getInstance();
        CmdLineParser parser = new CmdLineParser(container);

        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            parser.printUsage(System.err);
            return false;
        }

        return true;
    }
}