aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/com/jantuomi/tunkki/Tunkki.java
blob: 30a728bb110c452fcf2c2b8433605e66285fccf5 (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
package com.jantuomi.tunkki;

import com.jantuomi.tunkki.core.CommandLineArgumentContainer;
import com.jantuomi.tunkki.core.parser.ASTGenerator;
import com.jantuomi.tunkki.core.parser.Parser;
import com.jantuomi.tunkki.core.parser.ast.ASTNode;
import com.jantuomi.tunkki.core.runtime.Interpreter;
import com.jantuomi.tunkki.core.tokenizer.Tokenizer;
import com.jantuomi.tunkki.core.tokenizer.token.Token;
import com.jantuomi.tunkki.exception.TunkkiError;
import com.jantuomi.tunkki.repl.Repl;
import com.jantuomi.tunkki.utils.Utilities;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class Tunkki {

    private static Tunkki instance;

    public Tunkki() {
        instance = this;
    }

    public static Tunkki getInstance() {
        return instance;
    }

    public void repl() throws InterruptedException, IOException {
        Repl repl = new Repl(this);
        repl.run();
    }

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

        Collections.reverse(sequence);

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

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

        String output = Interpreter.execute(nodes);

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

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


        try {
            if (args.length == 0) {
                throw new CmdLineException("Please provide an argument.");
            }
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            parser.printUsage(System.err);
            return false;
        }

        return true;
    }

    public String versionString() {
        return Utilities.getVersionFromFile();
    }
}