summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/Evaluator.java
blob: 969b2d50f353f86013cf4c3eee2ae39aaa3f7b5a (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
package bjc.dicelang.v2;

import bjc.utils.data.ITree;
import bjc.utils.data.Tree;
import bjc.utils.data.TransformedIterator;
import bjc.utils.data.TopDownTransformIterator;
import bjc.utils.data.TopDownTransformResult;
import java.util.Iterator;
import java.util.function.Consumer;

import static bjc.dicelang.v2.Errors.ErrorKey.*;
import static bjc.dicelang.v2.Evaluator.Result.Type.*;

public class Evaluator {
	public static class Result {
		public static enum Type {
			FAILURE,
			INT, FLOAT, DICE, STRING
		}

		public final Type type;

		// These may or may not have values based
		// off of the result type
		public long                  intVal;
		public double                floatVal;
		public DiceBox.DieExpression diceVal;
		public String                stringVal;

		// Original node data
		public ITree<Node>           origVal;

		public Result(Type typ) {
			type = typ;
		}

		public Result(Type typ, ITree<Node> orig) {
			this(typ);

			origVal = orig;
		}

		public Result(Type typ, Node orig) {
			this(typ, new Tree<>(orig));
		}

		public Result(Type typ, Result orig) {
			this(typ, new Node(Node.Type.RESULT, orig));
		}

		public Result(Type typ, long iVal) {
			this(typ);

			intVal = iVal;
		}

		public Result(Type typ, double dVal) {
			this(typ);

			floatVal = dVal;
		}

		public Result(Type typ, DiceBox.DieExpression dVal) {
			this(typ);

			diceVal = dVal;
		}

		public Result(Type typ, DiceBox.Die dVal) {
			this(typ);

			diceVal = new DiceBox.DieExpression(dVal);
		}

		public Result(Type typ, DiceBox.DieList dVal) {
			this(typ);

			diceVal = new DiceBox.DieExpression(dVal);
		}

		public Result(Type typ, String strang) {
			this(typ);

			stringVal = strang;
		}

		public String toString() {
			switch(type) {
				case INT:
					return type.toString() + "(" + intVal + ")";
				case FLOAT:
					return type.toString() + "(" + floatVal + ")";
				case DICE:
					return type.toString() + "(" + diceVal + ")";
				case STRING:
					return type.toString() + "(" + stringVal + ")";
				case FAILURE:
					return type.toString();
				default:
					return "Unknown result type " + type.toString();
			}
		}
	}

	private static class Context {
		public Consumer<Iterator<ITree<Node>>> thunk;
		
		public boolean isDebug;
	}

	private static Node FAIL() {
		return new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE));
	}

	private static Node FAIL(ITree<Node> orig) {
		return new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE, orig));
	}

	private static Node FAIL(Node orig) {
		return new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE, orig));
	}

	private static Node FAIL(Result res) {
		return new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE, new Node(Node.Type.RESULT, res)));
	}

	private DiceLangEngine eng;

	public Evaluator(DiceLangEngine en) {
		eng = en;
	}

	public Result evaluate(ITree<Node> comm) {
		Context ctx = new Context();

		ctx.isDebug = false;
		ctx.thunk = (itr) -> {
			// Deliberately finish the iterator, but ignore results. It's only for stepwise evaluation
			// but we don't know if stepping the iterator causes something to happen
			while(itr.hasNext()) itr.next();
		};

		return comm.topDownTransform(this::pickEvaluationType,
				(node) -> this.evaluateNode(node, ctx)).getHead().resultVal;
	}

	public Iterator<ITree<Node>> stepDebug(ITree<Node> comm) {
		Context ctx = new Context();

		ctx.isDebug = true;

		return new TopDownTransformIterator<>(this::pickEvaluationType, (node, thnk) -> {
			ctx.thunk = thnk;

			return this.evaluateNode(node, ctx);	
		}, comm);
	}

	private TopDownTransformResult pickEvaluationType(Node nd) {
		switch(nd.type) {
			case UNARYOP:
				switch(nd.operatorType) {
					case COERCE:
						return TopDownTransformResult.PULLUP;
					default:
						return TopDownTransformResult.PUSHDOWN;
				}
			default:
				return TopDownTransformResult.PUSHDOWN;
		}
	}

	private ITree<Node> evaluateNode(ITree<Node> ast, Context ctx) {
		switch(ast.getHead().type) {
			case UNARYOP:
				return evaluateUnaryOp(ast, ctx);
			case BINOP:
				return evaluateBinaryOp(ast, ctx);
			case TOKREF:
				return evaluateTokenRef(ast.getHead().tokenVal, ctx);
			case ROOT:
				return ast.getChild(ast.getChildrenCount() - 1);
			default:
				Errors.inst.printError(EK_EVAL_INVNODE, ast.getHead().type.toString());
				return new Tree<>(FAIL(ast));
		}
	}

	private ITree<Node> evaluateUnaryOp(ITree<Node> ast, Context ctx) {
		switch(ast.getHead().operatorType) {
			case COERCE:
				if(ast.getChildrenCount() != 1) {
					Errors.inst.printError(EK_EVAL_NOTUNARY, ast.getChildrenCount());
					return new Tree<>(FAIL(AST));
				}
				break;
			default:
				Errors.inst.printError(EK_EVAL_INVUNARY, ast.getHead().operatorType.toString());
				return new Tree<>(FAIL(ast));
		}
	}

	private ITree<Node> evaluateBinaryOp(ITree<Node> ast, Context ctx) {
		Token.Type binOp = ast.getHead().operatorType;

		if(ast.getChildrenCount() != 2) {
			Errors.inst.printError(EK_EVAL_INVBIN, Integer.toString(ast.getChildrenCount()));

			return new Tree<>(FAIL(ast));
		}

		ITree<Node> left  = ast.getChild(0);
		ITree<Node> right = ast.getChild(1);

		switch(binOp) {
			case ADD:
			case SUBTRACT:
			case MULTIPLY:
			case DIVIDE:
			case IDIVIDE:
				return evaluateMathBinary(binOp,
						left.getHead().resultVal, right.getHead().resultVal,
						ctx);
			case DICEGROUP:
			case DICECONCAT:
			case DICELIST:
				return evaluateDiceBinary(binOp,
						left.getHead().resultVal, right.getHead().resultVal,
						ctx);
			default:
				Errors.inst.printError(EK_EVAL_UNBIN, binOp.toString());
				return new Tree<>(FAIL(ast));
		}
	}

	private ITree<Node> evaluateDiceBinary(Token.Type op,
			Result left, Result right, Context ctx) {
		Result res = null;

		switch(op) {
			case DICEGROUP:
				if(left.type == DICE && !left.diceVal.isList) {
					if(right.type == DICE && !right.diceVal.isList) {
						res = new Result(DICE,
								new DiceBox.SimpleDie(left.diceVal.scalar, right.diceVal.scalar));
					} else if (right.type == INT) {
						res = new Result(DICE, new DiceBox.SimpleDie(left.diceVal.scalar, right.intVal));
					} else {
						Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
						return new Tree<>(FAIL(right));
					}
				} else if(left.type == INT) {
					if(right.type == DICE && !right.diceVal.isList) {
						res = new Result(DICE, new DiceBox.SimpleDie(left.intVal, right.diceVal.scalar));
					} else if (right.type == INT) {
						res = new Result(DICE, new DiceBox.SimpleDie(left.intVal, right.intVal));
					} else {
						Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
						return new Tree<>(FAIL(right));
					}
				} else {
					Errors.inst.printError(EK_EVAL_INVDGROUP, left.type.toString());
					return new Tree<>(FAIL(left));
				}
			case DICECONCAT:
				if(left.type != DICE || left.diceVal.isList) {
					Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
					return new Tree<>(FAIL(left));
				} else if(right.type != DICE || right.diceVal.isList) {
					Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
					return new Tree<>(FAIL(right));
				} else {
					res = new Result(DICE, 
							new DiceBox.CompoundDie(left.diceVal.scalar, right.diceVal.scalar));
				}
				break;
			case DICELIST:
				if(left.type != DICE || left.diceVal.isList) {
					Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
					return new Tree<>(FAIL(left));
				} else if(right.type != DICE || right.diceVal.isList) {
					Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
					return new Tree<>(FAIL(right));
				} else {
					res = new Result(DICE,
							new DiceBox.SimpleDieList(left.diceVal.scalar, right.diceVal.scalar));
				}
				break;
			default:
				Errors.inst.printError(EK_EVAL_UNDICE, op.toString());
				return new Tree<>(FAIL());
		}

		return new Tree<>(new Node(Node.Type.RESULT, res));
	}

	private ITree<Node> evaluateMathBinary(Token.Type op,
			Result left, Result right, Context ctx) {
		if(left.type == Result.Type.DICE || right.type == Result.Type.DICE) {
			System.out.println("\tEVALUATOR ERROR: Math on dice isn't supported yet");
			return new Tree<>(FAIL());
		} else if(left.type == Result.Type.STRING || right.type == Result.Type.STRING) {
			Errors.inst.printError(EK_EVAL_STRINGMATH);
			return new Tree<>(FAIL());
		} else if(left.type == Result.Type.FAILURE || right.type == Result.Type.FAILURE) {
			return new Tree<>(FAIL());
		} else if(left.type == Result.Type.INT && right.type != Result.Type.INT) {
			Errors.inst.printError(EK_EVAL_MISMATH);
			return new Tree<>(FAIL(right));
		} else if(left.type == Result.Type.FLOAT && right.type != Result.Type.FLOAT) {
			Errors.inst.printError(EK_EVAL_MISMATH);
			return new Tree<>(FAIL(right));
		} else if(right.type == Result.Type.INT && left.type != Result.Type.INT) {
			Errors.inst.printError(EK_EVAL_MISMATH);
			return new Tree<>(FAIL(left));
		} else if(right.type == Result.Type.FLOAT && left.type != Result.Type.FLOAT) {
			Errors.inst.printError(EK_EVAL_MISMATH);
			return new Tree<>(FAIL(left));
		}

		Result res = null;

		switch(op) {
			case ADD:
				if(left.type == Result.Type.INT) {
					res = new Result(Result.Type.INT, left.intVal + right.intVal);
				} else {
					res = new Result(Result.Type.FLOAT, left.floatVal + right.floatVal);
				}
				break;
			case SUBTRACT:
				if(left.type == Result.Type.INT) {
					res = new Result(Result.Type.INT, left.intVal - right.intVal);
				} else {
					res = new Result(Result.Type.FLOAT, left.floatVal - right.floatVal);
				}
				break;
			case MULTIPLY:
				if(left.type == Result.Type.INT) {
					res = new Result(Result.Type.INT, left.intVal * right.intVal);
				} else {
					res = new Result(Result.Type.FLOAT, left.floatVal * right.floatVal);
				}
				break;
			case DIVIDE:
				if(left.type == Result.Type.INT) {
					if(right.intVal == 0) {
						Errors.inst.printError(EK_EVAL_DIVZERO);
						res = new Result(Result.Type.FAILURE, right);
					} else {
						res = new Result(Result.Type.FLOAT, left.intVal / right.intVal);
					}
				} else {
					if(right.floatVal == 0) {
						Errors.inst.printError(EK_EVAL_DIVZERO);
						res = new Result(Result.Type.FAILURE, right);
					} else {
						res = new Result(Result.Type.FLOAT, left.floatVal / right.floatVal);
					}
				}
				break;
			case IDIVIDE:
				if(left.type == Result.Type.INT) {
					if(right.intVal == 0) {
						Errors.inst.printError(EK_EVAL_DIVZERO);
						res = new Result(Result.Type.FAILURE, right);
					} else {
						res = new Result(Result.Type.INT, (int) (left.intVal / right.intVal));
					}
				} else {
					if(right.floatVal == 0) {
						Errors.inst.printError(EK_EVAL_DIVZERO);
						res = new Result(Result.Type.FAILURE, right);
					} else {
						res = new Result(Result.Type.INT, (int) (left.floatVal / right.floatVal));
					}
				}
				break;
			default:
				Errors.inst.printError(EK_EVAL_UNMATH, op.toString());
				return new Tree<>(FAIL());
		}

		return new Tree<>(new Node(Node.Type.RESULT, res));
	}

	private ITree<Node> evaluateTokenRef(Token tk, Context ctx) {
		Result res = null;

		switch(tk.type) {
			case INT_LIT:
				res = new Result(Result.Type.INT, tk.intValue);
				break;
			case FLOAT_LIT:
				res = new Result(Result.Type.FLOAT, tk.floatValue);
				break;
			case DICE_LIT:
				res = new Result(Result.Type.DICE, tk.diceValue);
				break;
			case STRING_LIT:
				res = new Result(Result.Type.STRING, eng.stringLits.get((int)(tk.intValue)));
				break;
			default:
				Errors.inst.printError(EK_EVAL_UNTOK, tk.type.toString());
				res = new Result(Result.Type.FAILURE);
		}

		return new Tree<>(new Node(Node.Type.RESULT, res));
	}
}