summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/dicelang/scl/StreamControlConsole.java
blob: ae4dcca4c1ae53cfa94c4e503e778a9a7c91d5a3 (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
package bjc.dicelang.scl;

import java.util.Scanner;

import bjc.funcdata.FunctionalList;
import bjc.funcdata.IList;

/**
 * Implement a SCL REPL
 *
 * @author Ben Culkin
 */
public class StreamControlConsole {
	/*
	 * @TODO 10/08/17 :SCLArgs
	 * 
	 * Do something useful with the CLI args.
	 *
	 */
	/**
	 * Main method
	 *
	 * @param args
	 *        Unused CLI args.
	 */
	public static void main(String[] args) {
		/*
		 * Initialize vars.
		 * 
		 */
		StreamEngine sengine = new StreamEngine();
		StreamControlEngine sclengine = new StreamControlEngine(sengine);
		Scanner scn = new Scanner(System.in);

		/* Get input from the user. */
		System.out.print("Enter a SCL command string (blank to exit): ");

		/* Process it. */
		while(scn.hasNextLine()) {
			String ln = scn.nextLine().trim();

			if(ln.equals("")) {
				/* Ignore empty lines. */
				break;
			}

			/* Break the token into strings. */
			IList<String> res = new FunctionalList<>();
			String[] tokens = ln.split(" ");

			/* Run the stream engine on the tokens. */
			boolean succ = sengine.doStreams(tokens, res);
			if(!succ) {
				System.out.printf("\tERROR: Stream engine failed for line '%s'\n", ln);
				continue;
			}

			if(sengine.debug)
				System.out.printf("\tDEBUG: Streamed tokens: { %s }\n", res);

			/* Run the command through SCL. */
			succ = sclengine.runProgram(res.iterator());
			if(!succ) {
				System.out.printf("\tERROR: SCL engine failed for line '%s'\n", ln);
				continue;
			}

			/* Prompt again. */
			System.out.print("Command string executed succesfully.\n\n");
			System.out.print("Enter a SCL command string (blank to exit): ");
		}

		scn.close();
	}
}