summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/dicelang/sclv2/SCLEngine.java
blob: d557a9ba731fd59a66b7b4de0b98b72b4c378ed3 (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
87
88
89
package bjc.dicelang.sclv2;

import java.util.*;

import bjc.esodata.*;

/**
 * Engine for running SCL.
 * 
 * Not threadsafe.
 * 
 * @author Ben Culkin
 */
public class SCLEngine
{
	private TapeLibrary<Deque<String>> streams;
	private Deque<String>              currentStream;

	private Map<String, Deque<String>> streamLibrary;

	private boolean inQuoteMode;

	/**
	 * Create a new SCL engine.
	 */
	public SCLEngine()
	{
		reinit();
	}

	/**
	 * Reinitialize this engine, as if it was newly constructed.
	 */
	public void reinit()
	{
		streams = new TapeLibrary<>();
		streams.setAllowAutoCreation(true);
		streams.mountTape("default");

		streamLibrary = new HashMap<>();

		currentStream = new ArrayDeque<>();
		streams.item(currentStream);

		inQuoteMode = false;
	}

	/**
	 * Run the engine on a given set of input.
	 * 
	 * @param input
	 *              The source of input to use.
	 * 
	 * @return The stream that was active at the end of the input.
	 */
	public Deque<String> run(Iterator<String> input)
	{
		while (input.hasNext())
		{
			String token = input.next();

			if (inQuoteMode)
			{

				if      (token.equalsIgnoreCase("{@SQ}")) inQuoteMode = true;
				else if (token.equalsIgnoreCase("{@SU}")) inQuoteMode = false;
				else                                      currentStream.add(token);
			} else
			{
				if (token.startsWith("{@S") && token.endsWith("}"))
				{
					String[] commands = token.substring(3, token.length()).split(";");
					for (String command : commands) {
						switch (command)
						{
						default:
							// Unknown command; need to handle it
						}
					}
				} else
				{					
					currentStream.add(token);
				}
			}
		}

		return currentStream;
	}
}