summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/DiceLangEngine.java
blob: e0cddb12e7493d815d4048655fa7a2136424bc5d (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
package bjc.dicelang;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bjc.dicelang.eval.DiceEvaluatorResult;
import bjc.dicelang.eval.Evaluator;
import bjc.dicelang.eval.EvaluatorResult;
import bjc.dicelang.eval.FailureEvaluatorResult;
import bjc.dicelang.scl.StreamEngine;
import bjc.dicelang.tokens.Token;
import bjc.data.Tree;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.FunctionalMap;
import bjc.funcdata.FunctionalStringTokenizer;
import bjc.funcdata.ListEx;
import bjc.funcdata.MapEx;
import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.TokenUtils;
import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter;

import static bjc.dicelang.Errors.ErrorKey.*;
import static bjc.dicelang.tokens.Token.Type.*;

/**
 * Implements the orchestration necessary for processing DiceLang commands.
 *
 * @author Ben Culkin
 */
public class DiceLangEngine {
	/* Logger. */
	private static final Logger LOG = Logger.getLogger(DiceLangEngine.class.getName());

	/*
	 * The random fields that are package private instead of private-private are for
	 * the benefit of the tweaker, so that it can mess around with them.
	 */

	/* Split tokens around operators with regex */
	ConfigurableTokenSplitter opExpander;

	/* ID for generation. */
	int nextLiteral;

	/**
	 * Debug indicator.
	 */
	public boolean debugMode;
	/* Should we do shunting? */
	private boolean postfixMode;
	/* Should we reverse the token stream? */
	private boolean prefixMode;
	/* Should we do step-by-step evaluation? */
	private boolean stepEval;

	/* Shunter for token shunting. */
	Shunter shunt;
	/* Tokenizer for tokenizing. */
	Tokenizer tokenzer;
	/* Parser for tree construction. */
	Parser parsr;
	/* Evaluator for evaluating. */
	Evaluator eval;

	/* Tables for various things. */
	/**
	 * The symbol table.
	 */
	public final MapEx<Integer, String> symTable;

	/* String literal tables */
	private final MapEx<Integer, String> stringLits;
	private final MapEx<String, String> stringLiterals;

	/* Lists of defns. */
	private final ListEx<Define> lineDefns;
	private final ListEx<Define> tokenDefns;

	/* Are defns currently sorted by priority? */
	private boolean defnsSorted;

	/* Stream engine for processing streams. */
	StreamEngine streamEng;

	/**
	 * Create a new DiceLang engine.
	 */
	public DiceLangEngine() {
		/* Initialize defns. */
		lineDefns = new FunctionalList<>();
		tokenDefns = new FunctionalList<>();
		defnsSorted = true;

		/* Initialize tables. */
		symTable = new FunctionalMap<>();
		stringLits = new FunctionalMap<>();
		stringLiterals = new FunctionalMap<>();

		/* Initialize operator expander. */
		opExpander = new ConfigurableTokenSplitter(true);
		/* Add grouping operators */
		opExpander.addMultiDelimiters("(", ")");
		opExpander.addMultiDelimiters("[", "]");
		opExpander.addMultiDelimiters("{", "}");

		/* Add simple operators */
		opExpander.addSimpleDelimiters(":=");
		opExpander.addSimpleDelimiters("=>");
		opExpander.addSimpleDelimiters("//");
		opExpander.addSimpleDelimiters(".+.");
		opExpander.addSimpleDelimiters(".*.");
		opExpander.addSimpleDelimiters("+");
		opExpander.addSimpleDelimiters("-");
		opExpander.addSimpleDelimiters("*");
		opExpander.addSimpleDelimiters("/");

		opExpander.compile();

		/* Initialize literal IDs */
		nextLiteral = 1;

		/* Initial mode settings. */
		debugMode   = true;
		postfixMode = false;
		prefixMode  = false;
		stepEval    = false;

		/* Create components. */
		shunt = new Shunter();
		parsr = new Parser();

		streamEng = new StreamEngine();
		tokenzer  = new Tokenizer(this);
		eval      = new Evaluator(this);
	}

	/** Sort defns by priority. */
	public void sortDefns() {
		lineDefns.sort(null);
		tokenDefns.sort(null);

		defnsSorted = true;
	}

	/**
	 * Add a defn that's applied to lines.
	 *
	 * @param dfn
	 *            The defn to add.
	 */
	public void addLineDefine(final Define dfn) {
		lineDefns.add(dfn);

		defnsSorted = false;
	}

	/**
	 * Add a defn that's applied to tokens.
	 *
	 * @param dfn
	 *            The defn to add.
	 */
	public void addTokenDefine(final Define dfn) {
		tokenDefns.add(dfn);

		defnsSorted = false;
	}

	/**
	 * Toggle debug mode.
	 *
	 * @return The current state of debug mode.
	 */
	public boolean toggleDebug() {
		debugMode = !debugMode;

		return debugMode;
	}

	/**
	 * Toggle postfix mode.
	 *
	 * @return The current state of postfix mode.
	 */
	public boolean togglePostfix() {
		postfixMode = !postfixMode;

		return postfixMode;
	}

	/**
	 * Toggle prefix mode.
	 *
	 * @return The current state of prefix mode
	 */
	public boolean togglePrefix() {
		prefixMode = !prefixMode;

		return prefixMode;
	}

	/**
	 * Toggle step-eval mode
	 *
	 * @return The current state of step-eval mode
	 */
	public boolean toggleStepEval() {
		stepEval = !stepEval;

		return stepEval;
	}

	/*
	 * Matches double-angle bracketed strings.
	 *
	 * These are used for tokens that aren't expanded.
	 */
	private final Pattern nonExpandPattern = Pattern.compile("<<([^\\>]*(?:\\>(?:[^\\>])*)*)>>");

	/**
	 * Run a command to completion.
	 *
	 * @param command
	 *            The command to run
	 *
	 * @return Whether or not the command ran successfully
	 */
	public boolean runCommand(final String command) {
		/* Preprocess the command into tokens */
		/*
		 * @NOTE
		 * 
		 * Instead of strings, this should maybe use a RawToken class or something.
		 */
		final ListEx<String> preprocessedTokens = preprocessCommand(command);

		if (preprocessedTokens == null) {
			return false;
		}

		/* Lex the string tokens into token-tokens */
		final ListEx<Token> lexedTokens = lexTokens(preprocessedTokens);

		if (lexedTokens == null) {
			return false;
		}

		/* Parse the tokens into an AST forest */
		final ListEx<Tree<Node>> astForest = new FunctionalList<>();
		final boolean succ = Parser.parseTokens(lexedTokens, astForest);

		if (!succ) {
			return false;
		}

		/* Evaluate the AST forest */
		evaluateForest(astForest);
		return true;
	}

	/* Lex string tokens into token-tokens */
	private ListEx<Token> lexTokens(final ListEx<String> preprocessedTokens) {
		final ListEx<Token> lexedTokens = new FunctionalList<>();

		for (final String token : preprocessedTokens) {
			String newTok = token;

			/* Apply token defns */
			for (final Define dfn : tokenDefns.toIterable()) {
				/*
				 * @NOTE 
				 *
				 * What happens with a define that produces multiple tokens from one
				 * token?
				 *
				 * 	At the moment, nothing.
				 */
				newTok = dfn.apply(newTok);
			}

			/* Lex the token */
			final Token tk = tokenzer.lexToken(token, stringLiterals);

			if (debugMode) {
				LOG.finer(String.format("lexed token: %s\n", tk));
			}

			if (tk == null) {
				/* Ignore blank tokens */
				continue;
			} else if (tk == Token.NIL_TOKEN) {
				/* Fail on bad tokens */
				return null;
			} else {
				lexedTokens.add(tk);
			}
		}

		if (debugMode) {
			String msg = String.format("\tCommand after tokenization: %s\n", lexedTokens.toString());
			LOG.fine(msg);
			System.out.print(msg);
		}

		/* Preshunt preshunt-marked groups of tokens */
		ListEx<Token> shuntedTokens = lexedTokens;
		final ListEx<Token> preparedTokens = new FunctionalList<>();

		boolean succ = removePreshuntTokens(lexedTokens, preparedTokens);

		if (!succ) {
			return null;
		}

		if (debugMode && !postfixMode) {
			String msg = String.format("\tCommand after pre-shunter removal: %s\n", preparedTokens.toString());
			LOG.fine(msg);
			System.out.print(msg);
		}

		/* Only shunt if we're not in a special mode. */
		if (!postfixMode && !prefixMode) {
			/* Shunt the tokens */
			shuntedTokens = new FunctionalList<>();
			succ = shunt.shuntTokens(preparedTokens, shuntedTokens);

			if (!succ) {
				return null;
			}
		} else if (prefixMode) {
			/* Reverse directional tokens */
			/*
			 * @NOTE Merge these two operations into one iteration over the list?
			 */
			preparedTokens.reverse();
			shuntedTokens = preparedTokens.map(this::reverseToken);
		}

		if (debugMode && !postfixMode) {
			String msg = String.format("\tCommand after shunting: %s\n", shuntedTokens.toString());
			LOG.fine(msg);
			System.out.print(msg);
		}

		/* Expand token groups */
		final ListEx<Token> readyTokens = shuntedTokens.flatMap(tk -> {
			if (tk.type == Token.Type.TOKGROUP || tk.type == Token.Type.TAGOP || tk.type == Token.Type.TAGOPR) {
				String msg = String.format("Expanding token group to: %s\n", tk.tokenValues.toString());
				LOG.finer(msg);

				if(debugMode)
					System.out.print(msg);

				return tk.tokenValues;
			} else {
				return new FunctionalList<>(tk);
			}
		});

		if (debugMode && !postfixMode) {
			String msg = String.format("\tCommand after re-preshunting: %s\n", readyTokens.toString());
			LOG.fine(msg);
			System.out.print(msg);
		}

		return readyTokens;
	}

	/*
	 * Reverse orientation-sensitive tokens.
	 *
	 * These are things like (, {, and [
	 */
	private Token reverseToken(final Token tk) {
		switch (tk.type) {
		case OBRACE:
			return new Token(CBRACE, tk.intValue);
		case OPAREN:
			return new Token(CPAREN, tk.intValue);
		case OBRACKET:
			return new Token(CBRACKET, tk.intValue);
		case CBRACE:
			return new Token(OBRACE, tk.intValue);
		case CPAREN:
			return new Token(OPAREN, tk.intValue);
		case CBRACKET:
			return new Token(OBRACKET, tk.intValue);
		default:
			return tk;
		}
	}

	/* Preprocess a command into a list of string tokens. */
	private ListEx<String> preprocessCommand(final String command) {
		/* Sort the defines if they aren't sorted */
		if (!defnsSorted) {
			sortDefns();
		}

		/* Run the tokens through the stream engine */
		final ListEx<String> streamToks = new FunctionalList<>();
		final boolean succ             = streamEng.doStreams(command.split(" "), streamToks);

		if (!succ) {
			return null;
		}

		String newComm = ListUtils.collapseTokens(streamToks, " ");

		if (debugMode) {
			String msg = String.format("\tCommand after stream commands: %s\n", newComm);

			LOG.fine(msg);

			System.out.print(msg);
		}

		/* Apply line defns */
		for (final Define dfn : lineDefns.toIterable()) {
			newComm = dfn.apply(newComm);
		}

		if (debugMode) {
			String msg = String.format("\tCommand after line defines: %s\n", newComm);

			LOG.fine(msg);

			System.out.print(msg);
		}

		/* Remove string literals. */
		final List<String> destringedParts = TokenUtils.removeDQuotedStrings(newComm);
		final StringBuffer destringedCommand = new StringBuffer();

		for (final String part : destringedParts) {
			/* Handle string literals */
			if (part.startsWith("\"") && part.endsWith("\"")) {
				/* Get the actual string. */
				final String litName = "stringLiteral" + nextLiteral;
				final String litVal = part.substring(1, part.length() - 1);

				/*
				 * Insert the string with its escape sequences interpreted.
				 */
				final String descVal = TokenUtils.descapeString(litVal);
				stringLiterals.put(litName, descVal);

				if (debugMode) {
					String msg = String.format("Replaced string literal '%s' with literal no. %d", descVal, nextLiteral);

					System.out.printf("\t\tDEBUG(1): %s\n", msg);

					LOG.finer(msg);
				}

				nextLiteral += 1;

				/* Place a ref. to the string in the command */
				destringedCommand.append(" " + litName + " ");
			} else {
				destringedCommand.append(part);
			}
		}

		if (debugMode) {
			String msg = String.format("\tCommand after destringing: %s\n", destringedCommand);

			LOG.fine(msg);

			System.out.print(msg);

			/* Print the string table if it exists. */
			if (stringLiterals.size() > 0) {
				System.out.println("\tString literals in table");

				stringLiterals.forEach((key, val) -> {
					System.out.printf("\t\tName: (%s)\tValue: (%s)\n", key, val);
				});
			}
		}

		/* Split the command into tokens */
		final String strang  = destringedCommand.toString();
		ListEx<String> tokens = FunctionalStringTokenizer.fromString(strang).toList();

		/* Temporarily remove non-expanding tokens */
		final MapEx<String, String> nonExpandedTokens = new FunctionalMap<>();
		tokens = tokens.map(tk -> {
			final Matcher nonExpandMatcher = nonExpandPattern.matcher(tk);

			if (nonExpandMatcher.matches()) {
				final String tkName = "nonExpandToken" + nextLiteral++;
				nonExpandedTokens.put(tkName, nonExpandMatcher.group(1));
				String msg = String.format("Pulled non-expander '%s' to '%s'", nonExpandMatcher.group(1), tkName);

				if(debugMode)
					System.out.printf("\t\tDEBUG(1): %s\n", msg);

				LOG.finer(msg);

				return tkName;
			}

			return tk;
		});

		if (debugMode) {
			String msg = String.format("\tCommand after removal of non-expanders: %s\n", tokens.toString());

			LOG.fine(msg);

			System.out.print(msg);
		}

		/* Expand tokens */
		ListEx<String> fullyExpandedTokens = tokens.flatMap(opExpander::split);

		if (debugMode) {
			String msg = String.format("\tCommand after token expansion: %s\n", fullyExpandedTokens.toString());

			LOG.fine(msg);

			System.out.print(msg);
		}

		/* Reinsert non-expanded tokens */
		fullyExpandedTokens = fullyExpandedTokens.map(tk -> {
			if (tk.startsWith("nonExpandToken"))
				return nonExpandedTokens.get(tk).get();

			return tk;
		});

		if (debugMode) {
			String msg = String.format("\tCommand after non-expander reinsertion: %s\n",
					fullyExpandedTokens.toString());

			LOG.fine(msg);

			System.out.print(msg);
		}

		return fullyExpandedTokens;
	}

	/* Evaluate a forest of AST nodes. */
	private void evaluateForest(final ListEx<Tree<Node>> astForest) {
		int treeNo = 1;

		for (final Tree<Node> ast : astForest) {
			if (debugMode) {
				System.out.printf("\t\tTree %d in forest:\n%s\n", treeNo, ast.toString());
			}

			if (debugMode && stepEval) {
				/*
				 * @NOTE This is broken until stepwise top-down tree transforms are fixed.
				 */
				int step = 1;

				/* Evaluate it step by step */
				for (final Iterator<Tree<Node>> itr = eval.stepDebug(ast); itr.hasNext();) {
					final Tree<Node> nodeStep = itr.next();

					System.out.printf("\t\tStep %d: Node is %s", step, nodeStep);

					/* Don't evaluate null steps */
					if (nodeStep == null) {
						System.out.println();

						step += 1;
						continue;
					}

					/* Print out details for results */
					if (nodeStep.getHead().type == Node.Type.RESULT) {
						final EvaluatorResult res = nodeStep.getHead().resultVal;

						System.out.printf(" (result is %s", res);

						if (res.type == EvaluatorResult.Type.DICE) {
							String value = ((DiceEvaluatorResult) res).diceVal.value();

							System.out.printf(" (sample roll %s)", value);
						}

						if (res.type == EvaluatorResult.Type.FAILURE) {
							Tree<Node> otree = ((FailureEvaluatorResult) res).origVal;

							System.out.printf(" (original tree is %s)", otree);
						}

						System.out.printf(")");
					}

					/* Advance a step */
					System.out.println();
					step += 1;
				}
			} else {
				/* Evaluate it normally */
				final EvaluatorResult res = eval.evaluate(ast);

				if (debugMode) {
					System.out.printf("\t\tEvaluates to %s", res);

					if(res == null) {
						// Don't need to do anything in this case
					} else if (res.type == EvaluatorResult.Type.DICE) {
						String value = ((DiceEvaluatorResult) res).diceVal.value();

						System.out.println("\t\t (sample roll " + value + ")");
					}
				}
			}

			System.out.println();

			treeNo += 1;
		}
	}

	/* Preshunt preshunt-marked groups of tokens. */
	private boolean removePreshuntTokens(final ListEx<Token> lexedTokens, final ListEx<Token> preparedTokens) {
		/* Current nesting level of tokens. */
		int curBraceCount = 0;

		/* Data storage. */
		final Deque<ListEx<Token>> bracedTokens = new LinkedList<>();
		ListEx<Token> curBracedTokens = new FunctionalList<>();

		for (final Token tk : lexedTokens) {
			if (tk.type == Token.Type.OBRACE && tk.intValue == 2) {
				/* Open a preshunt group. */
				curBraceCount += 1;

				if (curBraceCount != 1) {
					/*
					 * Push the old group onto the group stack.
					 */
					bracedTokens.push(curBracedTokens);
				}

				curBracedTokens = new FunctionalList<>();
			} else if (tk.type == Token.Type.CBRACE && tk.intValue == 2) {
				/* Close a preshunt group. */
				if (curBraceCount == 0) {
					/*
					 * Error if there couldn't have been an opening.
					 */
					Errors.inst.printError(EK_ENG_NOOPENING);
					return false;
				}

				curBraceCount -= 1;

				final ListEx<Token> preshuntTokens = new FunctionalList<>();

				/* Shunt preshunt group. */
				final boolean success = shunt.shuntTokens(curBracedTokens, preshuntTokens);

				if (debugMode) {
					System.out.println("\t\tPreshunted " + curBracedTokens + " into " + preshuntTokens);
				}

				if (!success) {
					return false;
				}

				if (curBraceCount >= 1) {
					/*
					 * Add the preshunt group to the previous group.
					 */
					curBracedTokens = bracedTokens.pop();

					curBracedTokens.add(new Token(Token.Type.TOKGROUP, preshuntTokens));
				} else {
					/*
					 * Add the preshunt group to the token stream.
					 */
					preparedTokens.add(new Token(Token.Type.TOKGROUP, preshuntTokens));
				}
			} else {
				/*
				 * Add the token to the active preshunt group, if there is one..
				 */
				if (curBraceCount >= 1) {
					curBracedTokens.add(tk);
				} else {
					preparedTokens.add(tk);
				}
			}
		}

		if (curBraceCount > 0) {
			/* There was an unclosed group. */
			Errors.inst.printError(EK_ENG_NOCLOSING);
			return false;
		}

		return true;
	}

	/**
	 * Get a string literal from the string literal table.
	 * 
	 * @param key
	 *            The key for the literal.
	 * @return The literal value.
	 * 
	 */
	public String getStringLiteral(final int key) {
		return stringLits.get(key).get();
	}

	/* Add a string literal to the string literal table. */
	/*
	 * @NOTE
	 * 
	 * The string literal table should be abstracted into some kind of auto-numbered
	 * map thing.
	 */
	void addStringLiteral(final int key, final String val) {
		stringLits.put(key, val);
	}
}