-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathExpressionEvaluator.cs
559 lines (501 loc) · 20.4 KB
/
ExpressionEvaluator.cs
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Bindings;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine
{
// Evaluates simple expressions.
// - supports data types: float, int, double, long.
// - operations: + - * / % ^ ( )
// - functions: sqrt, sin, cos, tan, floor, ceil, round, L, R
// L(a,b) results in a linear ramp between a and b, based on index & count.
// R(a,b) results in a random value between a and b.
// Both L and R function presence is treated as if the variable was present and an
// Instance needs to be evaluated.
// - variables: 'x', 'v' or 'f' can be treated as input for later evaluation via Instance
// - expressions starting with +=, -=, *=, /= are treated the same as if they were "x+(...)" etc.
[MovedFrom(true, "UnityEditor", "UnityEditor")]
public class ExpressionEvaluator
{
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal class Expression
{
internal Expression(string expression)
{
expression = PreFormatExpression(expression);
var infixTokens = ExpressionToTokens(expression, out hasVariables);
infixTokens = FixUnaryOperators(infixTokens);
rpnTokens = InfixToRPN(infixTokens);
}
public bool Evaluate<T>(ref T value, int index = 0, int count = 1)
{
return EvaluateTokens(rpnTokens, ref value, index, count);
}
public override bool Equals(object obj)
{
if (obj is Expression other)
return rpnTokens.SequenceEqual(other.rpnTokens);
return false;
}
public override int GetHashCode()
{
return rpnTokens.GetHashCode();
}
public override string ToString()
{
return string.Join(" ", rpnTokens);
}
internal readonly string[] rpnTokens;
internal readonly bool hasVariables;
}
public static bool Evaluate<T>(string expression, out T value)
{
return Evaluate(expression, out value, out _);
}
internal static bool Evaluate<T>(string expression, out T value, out Expression delayed)
{
value = default;
delayed = null;
if (TryParse(expression, out value))
return true;
var expr = new Expression(expression);
if (expr.hasVariables)
{
value = default;
delayed = expr;
return false;
}
return EvaluateTokens(expr.rpnTokens, ref value, 0, 1);
}
// Simple PCG (https://www.pcg-random.org/) based random number generator
struct PcgRandom
{
readonly ulong increment;
ulong state;
public PcgRandom(ulong state = 0, ulong sequence = 0)
{
this.increment = (sequence << 1) | 1u;
this.state = 0;
Step();
this.state += state;
Step();
}
public uint GetUInt()
{
var prevState = state;
Step();
return XshRr(prevState);
}
static uint RotateRight(uint v, int rot) => (v >> rot) | (v << (-rot & 31));
static uint XshRr(ulong s) => RotateRight((uint)(((s >> 18) ^ s) >> 27), (int)(s >> 59));
const ulong Multiplier64 = 6364136223846793005ul;
void Step() { state = unchecked(state * Multiplier64 + increment); }
}
static PcgRandom s_Random = new PcgRandom(0);
static Dictionary<string, Operator> s_Operators = new Dictionary<string, Operator>
{
{"-", new Operator(Op.Sub, 2, 2, Associativity.Left)},
{"+", new Operator(Op.Add, 2, 2, Associativity.Left)},
{"/", new Operator(Op.Div, 3, 2, Associativity.Left)},
{"*", new Operator(Op.Mul, 3, 2, Associativity.Left)},
{"%", new Operator(Op.Mod, 3, 2, Associativity.Left)},
{"^", new Operator(Op.Pow, 5, 2, Associativity.Right)},
// unary minus trick. For example we convert 2/-7+(-9*8)*2^-9-5 to 2/_7+(_9*8)*2^_9-5 before evaluation
{"_", new Operator(Op.Neg, 5, 1, Associativity.Left)},
{"sqrt", new Operator(Op.Sqrt, 4, 1, Associativity.Left)},
{"cos", new Operator(Op.Cos, 4, 1, Associativity.Left)},
{"sin", new Operator(Op.Sin, 4, 1, Associativity.Left)},
{"tan", new Operator(Op.Tan, 4, 1, Associativity.Left)},
{"floor", new Operator(Op.Floor, 4, 1, Associativity.Left)},
{"ceil", new Operator(Op.Ceil, 4, 1, Associativity.Left)},
{"round", new Operator(Op.Round, 4, 1, Associativity.Left)},
{"R", new Operator(Op.Rand, 4, 2, Associativity.Left)},
{"L", new Operator(Op.Linear, 4, 2, Associativity.Left)},
};
enum Op
{
Add, Sub, Mul, Div, Mod,
Neg,
Pow, Sqrt,
Sin, Cos, Tan,
Floor, Ceil, Round,
Rand, Linear
}
enum Associativity { Left, Right }
class Operator
{
public readonly Op op;
public readonly int precedence;
public readonly Associativity associativity;
public readonly int inputs;
public Operator(Op op, int precedence, int inputs, Associativity associativity)
{
this.op = op;
this.precedence = precedence;
this.inputs = inputs;
this.associativity = associativity;
}
}
internal static void SetRandomState(uint state)
{
s_Random = new PcgRandom(state);
}
// Evaluate RPN tokens (https://en.wikipedia.org/wiki/Reverse_Polish_notation)
static bool EvaluateTokens<T>(string[] tokens, ref T value, int index, int count)
{
var res = false;
if (typeof(T) == typeof(float))
{
var v = (double) UnsafeUtility.As<T, float>(ref value);
res = EvaluateDouble(tokens, ref v, index, count);
var outValue = (float)v;
value = UnsafeUtility.As<float, T>(ref outValue);
}
else if (typeof(T) == typeof(int))
{
var v = (double) UnsafeUtility.As<T, int>(ref value);
res = EvaluateDouble(tokens, ref v, index, count);
var outValue = (int)v;
value = UnsafeUtility.As<int, T>(ref outValue);
}
else if (typeof(T) == typeof(long))
{
var v = (double) UnsafeUtility.As<T, long>(ref value);
res = EvaluateDouble(tokens, ref v, index, count);
var outValue = (long)v;
value = UnsafeUtility.As<long, T>(ref outValue);
}
else if (typeof(T) == typeof(ulong))
{
var v = (double) UnsafeUtility.As<T, ulong>(ref value);
res = EvaluateDouble(tokens, ref v, index, count);
if (v < 0d)
{
v = 0d;
}
var outValue = (ulong)v;
value = UnsafeUtility.As<ulong, T>(ref outValue);
}
else if (typeof(T) == typeof(double))
{
var v = UnsafeUtility.As<T, double>(ref value);
res = EvaluateDouble(tokens, ref v, index, count);
value = UnsafeUtility.As<double, T>(ref v);
}
return res;
}
static bool EvaluateDouble(string[] tokens, ref double value, int index, int count)
{
var stack = new Stack<string>();
foreach (var token in tokens)
{
if (IsOperator(token))
{
Operator oper = TokenToOperator(token);
var values = new List<double>();
var parsed = true;
while (stack.Count > 0 && !IsCommand(stack.Peek()) && values.Count < oper.inputs)
{
parsed &= TryParse<double>(stack.Pop(), out var newValue);
values.Add(newValue);
}
values.Reverse();
if (parsed && values.Count == oper.inputs)
stack.Push(EvaluateOp(values.ToArray(), oper.op, index, count).ToString(CultureInfo.InvariantCulture));
else // Can't parse values or too few values for the operator -> exit
{
return false;
}
}
else if (IsVariable(token))
{
stack.Push(token == "#" ? index.ToString() : value.ToString(CultureInfo.InvariantCulture));
}
else
{
stack.Push(token);
}
}
if (stack.Count == 1)
{
if (TryParse(stack.Pop(), out value))
return true;
}
return false;
}
// Translate tokens from infix into RPN (https://en.wikipedia.org/wiki/Shunting-yard_algorithm)
static string[] InfixToRPN(string[] tokens)
{
var operatorStack = new Stack<string>();
var outputQueue = new Queue<string>();
foreach (string token in tokens)
{
if (IsCommand(token))
{
char command = token[0];
if (command == '(') // Bracket open
{
operatorStack.Push(token);
}
else if (command == ')') // Bracket close
{
while (operatorStack.Count > 0 && operatorStack.Peek() != "(")
outputQueue.Enqueue(operatorStack.Pop());
if (operatorStack.Count > 0)
operatorStack.Pop();
if (operatorStack.Count > 0 && IsDelayedFunction(operatorStack.Peek()))
outputQueue.Enqueue(operatorStack.Pop());
}
else if (command == ',') // Function argument separator
{
while (operatorStack.Count > 0 && operatorStack.Peek() != "(")
outputQueue.Enqueue(operatorStack.Pop());
}
else // All the other operators
{
Operator o = TokenToOperator(token);
while (NeedToPop(operatorStack, o))
outputQueue.Enqueue(operatorStack.Pop());
operatorStack.Push(token);
}
}
else if (IsDelayedFunction(token))
{
operatorStack.Push(token);
}
else // Not a command, just a regular number
{
outputQueue.Enqueue(token);
}
}
while (operatorStack.Count > 0)
outputQueue.Enqueue(operatorStack.Pop());
return outputQueue.ToArray();
}
// While there is an operator (topOfStack) at the top of the operators stack and
// either (newOperator) is left-associative and its precedence is less or equal to that of (topOfStack), or
// (newOperator) is right-associative and its precedence is less than (topOfStack)
static bool NeedToPop(Stack<string> operatorStack, Operator newOperator)
{
if (operatorStack.Count > 0 && newOperator != null)
{
Operator topOfStack = TokenToOperator(operatorStack.Peek());
if (topOfStack != null)
{
if (newOperator.associativity == Associativity.Left && newOperator.precedence <= topOfStack.precedence ||
newOperator.associativity == Associativity.Right && newOperator.precedence < topOfStack.precedence)
{
return true;
}
}
}
return false;
}
// Splits expression to meaningful tokens
static string[] ExpressionToTokens(string expression, out bool hasVariables)
{
hasVariables = false;
var result = new List<string>();
var currentString = "";
foreach (var currentChar in expression)
{
if (IsCommand(currentChar.ToString()))
{
if (currentString.Length > 0)
result.Add(currentString);
result.Add(currentChar.ToString());
currentString = "";
}
else
{
if (currentChar != ' ')
currentString += currentChar;
else
{
if (currentString.Length > 0)
result.Add(currentString);
currentString = "";
}
}
}
if (currentString.Length > 0)
result.Add(currentString);
hasVariables = result.Any(f => IsVariable(f) || IsDelayedFunction(f));
return result.ToArray();
}
static bool IsCommand(string token)
{
if (token.Length == 1)
{
char c = token[0];
if (c == '(' || c == ')' || c == ',')
return true;
}
return IsOperator(token);
}
static bool IsVariable(string token)
{
if (token.Length == 1)
{
char c = token[0];
return c == 'x' || c == 'v' || c == 'f' || c == '#';
}
return false;
}
static bool IsDelayedFunction(string token)
{
var op = TokenToOperator(token);
if (op != null)
{
if (op.op == Op.Rand || op.op == Op.Linear)
return true;
}
return false;
}
static bool IsOperator(string token)
{
return s_Operators.ContainsKey(token);
}
static Operator TokenToOperator(string token)
{
return s_Operators.TryGetValue(token, out var op) ? op : null;
}
// Clean up the expression before any parsing
static string PreFormatExpression(string expression)
{
var result = expression;
result = result.Trim();
if (result.Length == 0)
return result;
var lastChar = result[result.Length - 1];
// remove trailing operator for niceness (user is middle of typing, and we don't want to evaluate to zero)
if (IsOperator(lastChar.ToString()))
result = result.TrimEnd(lastChar);
// turn +=, -=, *=, /= into variable forms
if (result.Length >= 2 && result[1] == '=')
{
char op = result[0];
string expr = result.Substring(2);
if (op == '+') result = $"x+({expr})";
if (op == '-') result = $"x-({expr})";
if (op == '*') result = $"x*({expr})";
if (op == '/') result = $"x/({expr})";
}
return result;
}
// Turn unary minus into an operator. For example: - ( 1 - 2 ) * - 3 becomes: _ ( 1 - 2 ) * _ 3
static string[] FixUnaryOperators(string[] tokens)
{
if (tokens.Length == 0)
return tokens;
if (tokens[0] == "-")
tokens[0] = "_";
for (int i = 1; i < tokens.Length - 1; i++)
{
string token = tokens[i];
string previousToken = tokens[i - 1];
if (token == "-" && IsCommand(previousToken) && previousToken != ")")
tokens[i] = "_";
}
return tokens;
}
static double EvaluateOp(double[] values, Op op, int index, int count)
{
var a = values.Length >= 1 ? values[0] : 0;
var b = values.Length >= 2 ? values[1] : 0;
switch (op)
{
case Op.Neg: return -a;
case Op.Add: return a + b;
case Op.Sub: return a - b;
case Op.Mul: return a * b;
case Op.Div: return a / b;
case Op.Mod: return a % b;
case Op.Pow: return Math.Pow(a, b);
case Op.Sqrt: return a <= 0 ? 0 : Math.Sqrt(a);
case Op.Floor: return Math.Floor(a);
case Op.Ceil: return Math.Ceiling(a);
case Op.Round: return Math.Round(a);
case Op.Cos: return Math.Cos(a);
case Op.Sin: return Math.Sin(a);
case Op.Tan: return Math.Tan(a);
case Op.Rand:
{
var r = s_Random.GetUInt() & 0xFFFFFF;
var f = r / (double)0xFFFFFF;
return a + f * (b - a);
}
case Op.Linear:
{
if (count < 1)
count = 1;
var f = count < 2 ? 0.5 : index / (double)(count - 1);
return a + f * (b - a);
}
}
return 0;
}
static bool TryParse<T>(string expression, out T result)
{
expression = expression.Replace(',', '.'); // any actual reason for that? CultureInfo.InvariantCulture.NumberFormat used below
var expressionLowerCase = expression.ToLowerInvariant();
if (expressionLowerCase.Length > 1 && Char.IsDigit(expressionLowerCase[expressionLowerCase.Length-2]))
{
char[] numberDesignator = {'f','d','l'};
expressionLowerCase = expressionLowerCase.TrimEnd(numberDesignator);
}
bool success = false;
result = default;
if (expressionLowerCase.Length == 0)
return true;
if (typeof(T) == typeof(float))
{
if (expressionLowerCase == "pi")
{
success = true;
result = (T)(object)(float)Math.PI;
}
else
{
success = float.TryParse(expressionLowerCase, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out var temp);
result = (T)(object)temp;
}
}
else if (typeof(T) == typeof(int))
{
success = int.TryParse(expressionLowerCase, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out var temp);
result = (T)(object)temp;
}
else if (typeof(T) == typeof(double))
{
if (expressionLowerCase == "pi")
{
success = true;
result = (T)(object)Math.PI;
}
else
{
success = double.TryParse(expressionLowerCase, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out var temp);
result = (T)(object)temp;
}
}
else if (typeof(T) == typeof(long))
{
success = long.TryParse(expressionLowerCase, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out var temp);
result = (T)(object)temp;
}
else if (typeof(T) == typeof(ulong))
{
success = ulong.TryParse(expressionLowerCase, NumberStyles.Integer, CultureInfo.InvariantCulture.NumberFormat, out var temp);
result = (T)(object)temp;
}
return success;
}
}
}