-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathEvalVisitor.cs
More file actions
985 lines (838 loc) · 40 KB
/
EvalVisitor.cs
File metadata and controls
985 lines (838 loc) · 40 KB
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.Binding.BindInfo;
using Microsoft.PowerFx.Core.Entities;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.IR.Nodes;
using Microsoft.PowerFx.Core.IR.Symbols;
using Microsoft.PowerFx.Core.Texl;
using Microsoft.PowerFx.Core.Types;
using Microsoft.PowerFx.Functions;
using Microsoft.PowerFx.Interpreter;
using Microsoft.PowerFx.Interpreter.Exceptions;
using Microsoft.PowerFx.Interpreter.Localization;
using Microsoft.PowerFx.Types;
using static Microsoft.PowerFx.Functions.Library;
namespace Microsoft.PowerFx
{
// This used ValueTask for async, https://devblogs.microsoft.com/dotnet/understanding-the-whys-whats-and-whens-of-valuetask/
// Perf comparison of Task vs. ValueTask: https://ladeak.wordpress.com/2019/03/09/valuetask-vs-task
// Use Task for public methods, but ValueTask for internal methods that we expect to be mostly sync.
internal class EvalVisitor : IRNodeVisitor<ValueTask<FormulaValue>, EvalVisitorContext>
{
private readonly ReadOnlySymbolValues _symbolValues;
private readonly CancellationToken _cancellationToken;
public CancellationToken CancellationToken => _cancellationToken;
private readonly IServiceProvider _services;
public IServiceProvider FunctionServices => _services;
public CultureInfo CultureInfo { get; private set; }
public TimeZoneInfo TimeZoneInfo { get; private set; }
public DateTimeKind DateTimeKind => TimeZoneInfo.Equals(TimeZoneInfo.Utc) ? DateTimeKind.Utc : DateTimeKind.Unspecified;
public Governor Governor { get; private set; }
private readonly Stack<UDFStackFrame> _udfStack = new Stack<UDFStackFrame>();
public EvalVisitor(IRuntimeConfig config, CancellationToken cancellationToken)
{
_symbolValues = config.Values; // may be null
_cancellationToken = cancellationToken;
_services = config.ServiceProvider ?? new BasicServiceProvider();
TimeZoneInfo = GetService<TimeZoneInfo>() ?? TimeZoneInfo.Local;
Governor = GetService<Governor>() ?? new Governor();
CultureInfo = GetService<CultureInfo>() ?? throw new ArgumentNullException("Missing CultureInfo");
}
/// <summary>
/// Get a service from the <see cref="ReadOnlySymbolValues"/>. Returns null if not present.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetService<T>()
{
return (T)_services.GetService(typeof(T));
}
public bool TryGetService<T>(out T result)
{
result = GetService<T>();
return result != null;
}
// Check this cooperatively - especially in any loop.
public void CheckCancel()
{
// Throws OperationCanceledException exception
_cancellationToken.ThrowIfCancellationRequested();
Governor.Poll();
}
// Helper to eval an arg that might be a lambda.
internal async ValueTask<DValue<T>> EvalArgAsync<T>(FormulaValue arg, EvalVisitorContext context, IRContext irContext)
where T : ValidFormulaValue
{
if (arg is LambdaFormulaValue lambda)
{
arg = await lambda.EvalInRowScopeAsync(context).ConfigureAwait(false);
}
return arg switch
{
T t => DValue<T>.Of(t),
BlankValue b => DValue<T>.Of(b),
ErrorValue e => DValue<T>.Of(e),
_ => DValue<T>.Of(CommonErrors.RuntimeTypeMismatch(irContext))
};
}
public override async ValueTask<FormulaValue> Visit(TextLiteralNode node, EvalVisitorContext context)
{
return new StringValue(node.IRContext, node.LiteralValue);
}
public override async ValueTask<FormulaValue> Visit(NumberLiteralNode node, EvalVisitorContext context)
{
return new NumberValue(node.IRContext, node.LiteralValue);
}
public override async ValueTask<FormulaValue> Visit(DecimalLiteralNode node, EvalVisitorContext context)
{
return new DecimalValue(node.IRContext, node.LiteralValue);
}
public override async ValueTask<FormulaValue> Visit(BooleanLiteralNode node, EvalVisitorContext context)
{
return new BooleanValue(node.IRContext, node.LiteralValue);
}
public override async ValueTask<FormulaValue> Visit(ColorLiteralNode node, EvalVisitorContext context)
{
return new ColorValue(node.IRContext, node.LiteralValue);
}
public override async ValueTask<FormulaValue> Visit(RecordNode node, EvalVisitorContext context)
{
var fields = new List<NamedValue>();
foreach (var field in node.Fields)
{
CheckCancel();
var name = field.Key;
var value = field.Value;
var rhsValue = await value.Accept(this, context).ConfigureAwait(false);
fields.Add(new NamedValue(name.Value, rhsValue));
}
return new InMemoryRecordValue(node.IRContext, fields);
}
public override async ValueTask<FormulaValue> Visit(LazyEvalNode node, EvalVisitorContext context)
{
var val = await node.Child.Accept(this, context).ConfigureAwait(false);
return val;
}
// Handle the Set() function -
// Set is unique because it has an l-value for the first arg.
// Async params can't have out-params.
// Return null if not handled. Else non-null if handled.
private async Task<FormulaValue> TryHandleSet(CallNode node, EvalVisitorContext context)
{
// Special case Set() calls because they take an LValue.
if (node.Function.GetType() != typeof(RecalcEngineSetFunction))
{
return null;
}
var arg0 = node.Args[0];
var arg1 = node.Args[1];
var newValue = await arg1.Accept(this, context).ConfigureAwait(false);
if (arg0.IRContext.IsMutation)
{
if (arg0 is RecordFieldAccessNode rfan)
{
var arg0value = await rfan.From.Accept(this, context).ConfigureAwait(false);
if (arg0value is RecordValue rv)
{
rv.ShallowCopyFieldInPlace(rfan.Field);
rv.UpdateField(rfan.Field, newValue);
return node.IRContext.ResultType._type.Kind == DKind.Boolean ? FormulaValue.New(true) : FormulaValue.NewVoid();
}
else
{
return CommonErrors.UnreachableCodeError(node.IRContext);
}
}
else if (arg0 is BinaryOpNode bon && bon.Op == BinaryOpKind.DynamicGetField)
{
var arg0value = await bon.Left.Accept(this, context).ConfigureAwait(false);
var arg1value = await bon.Right.Accept(this, context).ConfigureAwait(false);
if (arg0value is UntypedObjectValue uov && uov.Impl is UntypedObjectBase impl)
{
return impl.SetUntypedObject(node.IRContext, (StringValue)arg1value, newValue);
}
else if (arg0value is ErrorValue || arg0value is BlankValue)
{
return arg0value;
}
}
else if (arg0 is CallNode callNode && callNode.Function == BuiltinFunctionsCore.Index_UO)
{
var child0Value = await callNode.Args[0].Accept(this, context).ConfigureAwait(false);
var child1Value = await callNode.Args[1].Accept(this, context).ConfigureAwait(false);
if (child0Value is UntypedObjectValue uov && uov.Impl is UntypedObjectBase impl)
{
return impl.SetUntypedObject(node.IRContext, (NumberValue)child1Value, newValue);
}
else if (child0Value is ErrorValue || child0Value is BlankValue)
{
return child0Value;
}
}
else
{
return CommonErrors.UnreachableCodeError(node.IRContext);
}
}
// Binder has already ensured this is a first name node as well as mutable symbol.
if (arg0 is ResolvedObjectNode obj)
{
if (obj.Value is ISymbolSlot sym)
{
if (_symbolValues != null)
{
_symbolValues.Set(sym, newValue);
return node.IRContext.ResultType._type.Kind == DKind.Boolean ? FormulaValue.New(true) : FormulaValue.NewVoid();
}
// This may happen if the runtime symbols are missing a value and we failed to update.
}
}
// Fail?
return CommonErrors.UnreachableCodeError(node.IRContext);
}
// Handle invoke SetProperty(source.Prop, newValue)
// Invoke as: SetProperty(source, "Prop", newValue)
private async Task<FormulaValue> TryHandleSetProperty(CallNode node, EvalVisitorContext context)
{
if (node.Function is not CustomSetPropertyFunction setPropFunc)
{
return null;
}
var arg0 = node.Args[0];
var arg1 = node.Args[1];
if (arg0 is not RecordFieldAccessNode r)
{
return null;
}
var source = await r.From.Accept(this, context).ConfigureAwait(false);
var fieldName = r.Field.Value;
var newValue = await arg1.Accept(this, context).ConfigureAwait(false);
var args = new FormulaValue[] { source, FormulaValue.New(fieldName), newValue };
var result = await setPropFunc.InvokeAsync(args, _cancellationToken).ConfigureAwait(false);
return result;
}
// Given a TexlFunction, get the implementation to invoke.
private IFunctionInvoker GetInvoker(TexlFunction func)
{
if (func is IFunctionInvoker invoker)
{
return invoker;
}
if (func is UserDefinedFunction userDefinedFunc)
{
return new UserDefinedFunctionAdapter(userDefinedFunc);
}
if (FunctionImplementations.TryGetValue(func, out AsyncFunctionPtr ptr))
{
return new AsyncFunctionPtrAdapter(ptr);
}
return null;
}
// Adapter for AsyncFunctionPtr to common invoker interface.
private class AsyncFunctionPtrAdapter : IFunctionInvoker
{
private readonly AsyncFunctionPtr _ptr;
public AsyncFunctionPtrAdapter(AsyncFunctionPtr ptr)
{
_ptr = ptr;
}
public async Task<FormulaValue> InvokeAsync(FunctionInvokeInfo invokeInfo, CancellationToken cancellationToken)
{
var args = invokeInfo.Args.ToArray();
var context = invokeInfo.Context;
var evalVisitor = invokeInfo.Runner;
var irContext = invokeInfo.IRContext;
var result = await _ptr(evalVisitor, context, irContext, args).ConfigureAwait(false);
return result;
}
}
// Adapter for UDF to common invoker.
// This still ensures that *invoking* a UDF has the same semantics as invoking other function calls.
private class UserDefinedFunctionAdapter : IFunctionInvoker
{
private readonly UserDefinedFunction _udf;
public UserDefinedFunctionAdapter(UserDefinedFunction udf)
{
_udf = udf;
}
public async Task<FormulaValue> InvokeAsync(FunctionInvokeInfo invokeInfo, CancellationToken cancellationToken)
{
var args = invokeInfo.Args.ToArray();
var context = invokeInfo.Context;
var evalVisitor = invokeInfo.Runner;
var udfStack = evalVisitor._udfStack;
UDFStackFrame frame = new UDFStackFrame(_udf, args);
UDFStackFrame framePop = null;
FormulaValue result = null;
try
{
// Push this so that we have access to args.
udfStack.Push(frame);
// https://github.com/microsoft/Power-Fx/issues/2822
// This repeats IRTranslator each time. Do once and save.
(var irnode, _) = _udf.GetIRTranslator();
evalVisitor.CheckCancel();
result = await irnode.Accept(evalVisitor, context).ConfigureAwait(false);
}
finally
{
framePop = udfStack.Pop();
}
if (frame != framePop)
{
throw new Exception("Something went wrong. UDF stack values didn't match.");
}
return result;
}
}
public override async ValueTask<FormulaValue> Visit(CallNode node, EvalVisitorContext context)
{
CheckCancel();
var setResult = await TryHandleSet(node, context.IncrementStackDepthCounter()).ConfigureAwait(false);
if (setResult != null)
{
return setResult;
}
var setPropResult = await TryHandleSetProperty(node, context.IncrementStackDepthCounter()).ConfigureAwait(false);
if (setPropResult != null)
{
return setPropResult;
}
var func = node.Function;
var carg = node.Args.Count;
var args = new FormulaValue[carg];
for (var i = 0; i < carg; i++)
{
CheckCancel();
var child = node.Args[i];
var isLambda = node.IsLambdaArg(i);
if (!isLambda)
{
args[i] = await child.Accept(this, context.IncrementStackDepthCounter()).ConfigureAwait(false);
}
else
{
// This is where Lambdas are created. They close over key values to invoke.
args[i] = new LambdaFormulaValue(node.IRContext, child, this, context);
}
}
var childContext = context.SymbolContext.WithScope(node.Scope);
FormulaValue result;
try
{
IFunctionInvoker invoker = GetInvoker(func);
// Standard invoke path. Make everything go through here.
// Eventually collapse all cases to this.
if (invoker != null)
{
var invokeInfo = new FunctionInvokeInfo
{
Args = args,
FunctionServices = _services,
Runner = this,
Context = context.IncrementStackDepthCounter(childContext),
IRContext = node.IRContext,
};
result = await invoker.InvokeAsync(invokeInfo, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction asyncFunc)
{
result = await asyncFunc.InvokeAsync(args, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction4 asyncFunc4)
{
// https://github.com/microsoft/Power-Fx/issues/2818
// This is used for Json() functions. IsType, AsType
result = await asyncFunc4.InvokeAsync(TimeZoneInfo, node.IRContext.ResultType, args, _cancellationToken).ConfigureAwait(false);
}
else if (func is IAsyncTexlFunction5 asyncFunc5)
{
// https://github.com/microsoft/Power-Fx/issues/2818
// This is used for Json() functions.
BasicServiceProvider services2 = new BasicServiceProvider(_services);
// Invocation should not get its own provider.
if (services2.GetService(typeof(TimeZoneInfo)) == null)
{
services2.AddService(TimeZoneInfo);
}
if (services2.GetService(typeof(Canceller)) == null)
{
services2.AddService(new Canceller(CheckCancel));
}
result = await asyncFunc5.InvokeAsync(services2, node.IRContext.ResultType, args, _cancellationToken).ConfigureAwait(false);
}
else
{
result = CommonErrors.NotYetImplementedFunctionError(node.IRContext, func.Name);
}
// https://github.com/microsoft/Power-Fx/issues/2820
// We should remove this check that limits to just Adapter1, so we apply this check to all impls.
if (invoker is AsyncFunctionPtrAdapter)
{
if (!(result.IRContext.ResultType._type == node.IRContext.ResultType._type || result is ErrorValue || result.IRContext.ResultType is BlankType))
{
throw CommonExceptions.RuntimeMisMatch;
}
}
}
catch (CustomFunctionErrorException ex)
{
var irContext = node.IRContext;
result = new ErrorValue(irContext, new ExpressionError() { Message = ex.Message, Span = irContext.SourceContext, Kind = ex.ErrorKind });
}
CheckCancel();
return result;
}
public override async ValueTask<FormulaValue> Visit(BinaryOpNode node, EvalVisitorContext context)
{
var arg1 = await node.Left.Accept(this, context).ConfigureAwait(false);
var arg2 = await node.Right.Accept(this, context).ConfigureAwait(false);
var args = new FormulaValue[] { arg1, arg2 };
return await VisitBinaryOpNode(node, context, args).ConfigureAwait(false);
}
private ValueTask<FormulaValue> VisitBinaryOpNode(BinaryOpNode node, EvalVisitorContext context, FormulaValue[] args)
{
switch (node.Op)
{
case BinaryOpKind.AddNumbers:
return OperatorBinaryAdd(this, context, node.IRContext, args);
case BinaryOpKind.AddDecimals:
return OperatorDecimalBinaryAdd(this, context, node.IRContext, args);
case BinaryOpKind.MulNumbers:
return OperatorBinaryMul(this, context, node.IRContext, args);
case BinaryOpKind.MulDecimals:
return OperatorDecimalBinaryMul(this, context, node.IRContext, args);
case BinaryOpKind.DivNumbers:
return OperatorBinaryDiv(this, context, node.IRContext, args);
case BinaryOpKind.DivDecimals:
return OperatorDecimalBinaryDiv(this, context, node.IRContext, args);
case BinaryOpKind.EqBlob:
case BinaryOpKind.EqBoolean:
case BinaryOpKind.EqColor:
case BinaryOpKind.EqCurrency:
case BinaryOpKind.EqDate:
case BinaryOpKind.EqDateTime:
case BinaryOpKind.EqGuid:
case BinaryOpKind.EqHyperlink:
case BinaryOpKind.EqImage:
case BinaryOpKind.EqMedia:
case BinaryOpKind.EqNumbers:
case BinaryOpKind.EqOptionSetValue:
case BinaryOpKind.EqText:
case BinaryOpKind.EqTime:
case BinaryOpKind.EqNull:
case BinaryOpKind.EqDecimals:
return OperatorBinaryEq(this, context, node.IRContext, args);
case BinaryOpKind.EqNullUntyped:
return OperatorBinaryEqNullUntyped(this, context, node.IRContext, args);
case BinaryOpKind.EqPolymorphic:
return OperatorBinaryEqPolymorphic(this, context, node.IRContext, args);
case BinaryOpKind.NeqBlob:
case BinaryOpKind.NeqBoolean:
case BinaryOpKind.NeqColor:
case BinaryOpKind.NeqCurrency:
case BinaryOpKind.NeqDate:
case BinaryOpKind.NeqDateTime:
case BinaryOpKind.NeqGuid:
case BinaryOpKind.NeqHyperlink:
case BinaryOpKind.NeqImage:
case BinaryOpKind.NeqMedia:
case BinaryOpKind.NeqNumbers:
case BinaryOpKind.NeqOptionSetValue:
case BinaryOpKind.NeqText:
case BinaryOpKind.NeqTime:
case BinaryOpKind.NeqNull:
case BinaryOpKind.NeqDecimals:
return OperatorBinaryNeq(this, context, node.IRContext, args);
case BinaryOpKind.NeqNullUntyped:
return OperatorBinaryNeqNullUntyped(this, context, node.IRContext, args);
case BinaryOpKind.NeqPolymorphic:
return OperatorBinaryNeqPolymorphic(this, context, node.IRContext, args);
case BinaryOpKind.GtNumbers:
case BinaryOpKind.GtNull:
return OperatorBinaryGt(this, context, node.IRContext, args);
case BinaryOpKind.GeqNumbers:
case BinaryOpKind.GeqNull:
return OperatorBinaryGeq(this, context, node.IRContext, args);
case BinaryOpKind.LtNumbers:
case BinaryOpKind.LtNull:
return OperatorBinaryLt(this, context, node.IRContext, args);
case BinaryOpKind.LeqNumbers:
case BinaryOpKind.LeqNull:
return OperatorBinaryLeq(this, context, node.IRContext, args);
case BinaryOpKind.GtDecimals:
return OperatorDecimalBinaryGt(this, context, node.IRContext, args);
case BinaryOpKind.GeqDecimals:
return OperatorDecimalBinaryGeq(this, context, node.IRContext, args);
case BinaryOpKind.LtDecimals:
return OperatorDecimalBinaryLt(this, context, node.IRContext, args);
case BinaryOpKind.LeqDecimals:
return OperatorDecimalBinaryLeq(this, context, node.IRContext, args);
case BinaryOpKind.InText:
return OperatorTextIn(this, context, node.IRContext, args);
case BinaryOpKind.ExactInText:
return OperatorTextInExact(this, context, node.IRContext, args);
case BinaryOpKind.InScalarTable:
return OperatorScalarTableIn(this, context, node.IRContext, args);
case BinaryOpKind.ExactInScalarTable:
return OperatorScalarTableInExact(this, context, node.IRContext, args);
case BinaryOpKind.AddDateAndTime:
return OperatorAddDateAndTime(this, context, node.IRContext, args);
case BinaryOpKind.AddDateAndDay:
return OperatorAddDateAndDay(this, context, node.IRContext, args);
case BinaryOpKind.AddDateTimeAndDay:
return OperatorAddDateTimeAndDay(this, context, node.IRContext, args);
case BinaryOpKind.AddTimeAndNumber:
return OperatorAddTimeAndNumber(this, context, node.IRContext, args);
case BinaryOpKind.AddNumberAndTime:
return OperatorAddTimeAndNumber(this, context, node.IRContext, new[] { args[1], args[0] });
case BinaryOpKind.AddTimeAndTime:
return OperatorAddTimeAndTime(this, context, node.IRContext, args);
case BinaryOpKind.DateDifference:
return OperatorDateDifference(this, context, node.IRContext, args);
case BinaryOpKind.TimeDifference:
return OperatorTimeDifference(this, context, node.IRContext, args);
case BinaryOpKind.SubtractDateAndTime:
return OperatorSubtractDateAndTime(this, context, node.IRContext, args);
case BinaryOpKind.SubtractNumberAndDate:
return OperatorSubtractNumberAndDate(this, context, node.IRContext, args);
case BinaryOpKind.SubtractNumberAndTime:
return OperatorSubtractNumberAndTime(this, context, node.IRContext, args);
case BinaryOpKind.LtDateTime:
return OperatorLtDateTime(this, context, node.IRContext, args);
case BinaryOpKind.LeqDateTime:
return OperatorLeqDateTime(this, context, node.IRContext, args);
case BinaryOpKind.GtDateTime:
return OperatorGtDateTime(this, context, node.IRContext, args);
case BinaryOpKind.GeqDateTime:
return OperatorGeqDateTime(this, context, node.IRContext, args);
case BinaryOpKind.LtDate:
return OperatorLtDate(this, context, node.IRContext, args);
case BinaryOpKind.LeqDate:
return OperatorLeqDate(this, context, node.IRContext, args);
case BinaryOpKind.GtDate:
return OperatorGtDate(this, context, node.IRContext, args);
case BinaryOpKind.GeqDate:
return OperatorGeqDate(this, context, node.IRContext, args);
case BinaryOpKind.LtTime:
return OperatorLtTime(this, context, node.IRContext, args);
case BinaryOpKind.LeqTime:
return OperatorLeqTime(this, context, node.IRContext, args);
case BinaryOpKind.GtTime:
return OperatorGtTime(this, context, node.IRContext, args);
case BinaryOpKind.GeqTime:
return OperatorGeqTime(this, context, node.IRContext, args);
case BinaryOpKind.DynamicGetField:
return new ValueTask<FormulaValue>(OperatorDynamicGetField(node, args));
default:
return new ValueTask<FormulaValue>(CommonErrors.UnreachableCodeError(node.IRContext));
}
}
private static FormulaValue OperatorDynamicGetField(BinaryOpNode node, FormulaValue[] args)
{
var arg1 = args[0];
var arg2 = args[1];
if (arg1 is UntypedObjectValue cov && arg2 is StringValue sv)
{
if (cov.Impl.Type is ExternalType et && (et.Kind == ExternalTypeKind.Object || et.Kind == ExternalTypeKind.ArrayAndObject))
{
if (cov.Impl is UntypedObjectBase untypedObjectBase)
{
return untypedObjectBase.GetProperty(sv.Value, node.IRContext.ResultType);
}
if (cov.Impl.TryGetProperty(sv.Value, out var res))
{
if (res.Type == FormulaType.Blank)
{
return new BlankValue(node.IRContext);
}
return new UntypedObjectValue(node.IRContext, res);
}
else
{
return new BlankValue(node.IRContext);
}
}
else if (cov.Impl.Type == FormulaType.Blank)
{
return new BlankValue(node.IRContext);
}
else
{
return new ErrorValue(node.IRContext, new ExpressionError()
{
ResourceKey = RuntimeStringResources.ErrAccessingFieldNotValidValue,
Span = node.IRContext.SourceContext,
Kind = ErrorKind.InvalidArgument
});
}
}
else if (arg1 is BlankValue)
{
return new BlankValue(node.IRContext);
}
else if (arg1 is ErrorValue)
{
return arg1;
}
else
{
return CommonErrors.UnreachableCodeError(node.IRContext);
}
}
public override async ValueTask<FormulaValue> Visit(UnaryOpNode node, EvalVisitorContext context)
{
var arg1 = await node.Child.Accept(this, context).ConfigureAwait(false);
var args = new FormulaValue[] { arg1 };
if (UnaryOps.TryGetValue(node.Op, out var unaryOp))
{
return await unaryOp(this, context, node.IRContext, args).ConfigureAwait(false);
}
return CommonErrors.NotYetImplementedUnaryOperatorError(node.IRContext, node.Op.ToString());
}
public override async ValueTask<FormulaValue> Visit(AggregateCoercionNode node, EvalVisitorContext context)
{
var arg1 = await node.Child.Accept(this, context).ConfigureAwait(false);
if (arg1 is BlankValue || arg1 is ErrorValue)
{
return arg1;
}
if (node.Op == UnaryOpKind.TableToTable)
{
var table = (TableValue)arg1;
var tableType = (TableType)node.IRContext.ResultType;
var resultRows = new List<DValue<RecordValue>>();
foreach (var row in table.Rows)
{
CheckCancel();
if (row.IsValue)
{
var fields = new List<NamedValue>();
var scopeContext = context.SymbolContext.WithScope(node.Scope);
foreach (var field in row.Value.Fields)
{
CheckCancel();
if (node.FieldCoercions.TryGetValue(new Core.Utils.DName(field.Name), out var coercion))
{
var record = row.Value;
var newScope = scopeContext.WithScopeValues(record);
var newValue = await coercion.Accept(this, context.NewScope(newScope)).ConfigureAwait(false);
fields.Add(new NamedValue(field.Name, newValue));
}
else
{
fields.Add(field);
}
}
resultRows.Add(DValue<RecordValue>.Of(new InMemoryRecordValue(IRContext.NotInSource(tableType.ToRecord()), fields)));
}
else if (row.IsBlank)
{
resultRows.Add(DValue<RecordValue>.Of(row.Blank));
}
else
{
resultRows.Add(DValue<RecordValue>.Of(row.Error));
}
}
return new InMemoryTableValue(node.IRContext, resultRows);
}
else if (node.Op == UnaryOpKind.RecordToRecord)
{
var fields = new List<NamedValue>();
var scopeContext = context.SymbolContext.WithScope(node.Scope);
var newScope = scopeContext.WithScopeValues((RecordValue)arg1);
var recordSrc = (RecordValue)arg1;
foreach (var f2 in recordSrc.Fields)
{
CheckCancel();
if (node.FieldCoercions.TryGetValue(new Core.Utils.DName(f2.Name), out var coercion))
{
var newValue = await coercion.Accept(this, context.NewScope(newScope)).ConfigureAwait(false);
fields.Add(new NamedValue(f2.Name, newValue));
}
else
{
// Existing field, no coercion needed.
fields.Add(f2);
}
}
return FormulaValue.NewRecordFromFields(fields);
}
return CommonErrors.UnreachableCodeError(node.IRContext);
}
public override async ValueTask<FormulaValue> Visit(ScopeAccessNode node, EvalVisitorContext context)
{
if (node.Value is ScopeAccessSymbol s1)
{
var scope = s1.Parent;
var val = context.SymbolContext.GetScopeVar(scope, s1.Name);
return val;
}
// Binds to whole scope
if (node.Value is ScopeSymbol s2)
{
var r = context.SymbolContext.ScopeValues[s2.Id];
return r.Resolve(string.Empty);
}
return CommonErrors.UnreachableCodeError(node.IRContext);
}
public override async ValueTask<FormulaValue> Visit(RecordFieldAccessNode node, EvalVisitorContext context)
{
var left = await node.From.Accept(this, context).ConfigureAwait(false);
if (left is BlankValue)
{
return new BlankValue(node.IRContext);
}
if (left is ErrorValue)
{
return left;
}
var record = (RecordValue)left;
if (node.IRContext.IsMutation)
{
record.ShallowCopyFieldInPlace(node.Field.Value);
}
var val = await record.GetFieldAsync(node.IRContext.ResultType, node.Field.Value, _cancellationToken).ConfigureAwait(false);
return val;
}
public override async ValueTask<FormulaValue> Visit(SingleColumnTableAccessNode node, EvalVisitorContext context)
{
return new ErrorValue(node.IRContext, new ExpressionError()
{
ResourceKey = RuntimeStringResources.ErrSingleColumnTableAccessNodeNotYetImplemented,
Span = node.IRContext.SourceContext,
Kind = ErrorKind.NotSupported
});
}
public override async ValueTask<FormulaValue> Visit(ErrorNode node, EvalVisitorContext context)
{
return new ErrorValue(node.IRContext, new ExpressionError()
{
Message = node.ErrorHint,
Span = node.IRContext.SourceContext,
Kind = ErrorKind.AnalysisError
});
}
public override async ValueTask<FormulaValue> Visit(ChainingNode node, EvalVisitorContext context)
{
CheckCancel();
if (!node.Nodes.Any())
{
return CommonErrors.InvalidChain(node.IRContext, node.ToString());
}
FormulaValue fv = null;
var errors = new List<ExpressionError>();
foreach (var iNode in node.Nodes)
{
CheckCancel();
fv = await iNode.Accept(this, context).ConfigureAwait(false);
if (fv is ErrorValue ev)
{
errors.AddRange(ev.Errors);
}
}
return errors.Any() ? new ErrorValue(node.IRContext, errors) : fv;
}
public override async ValueTask<FormulaValue> Visit(ResolvedObjectNode node, EvalVisitorContext context)
{
switch (node.Value)
{
case NameSymbol name:
return GetVariableOrFail(node, name, node.IRContext.IsMutation);
case FormulaValue fi:
return fi;
case IExternalOptionSet optionSet:
return ResolvedObjectHelpers.OptionSet(optionSet, node.IRContext);
case Func<IServiceProvider, Task<FormulaValue>> getHostObject:
FormulaValue hostObj;
try
{
hostObj = await getHostObject(_services).ConfigureAwait(false);
if (!hostObj.Type._type.Accepts(node.IRContext.ResultType._type, exact: true, useLegacyDateTimeAccepts: false, usePowerFxV1CompatibilityRules: true))
{
hostObj = CommonErrors.RuntimeTypeMismatch(node.IRContext);
}
}
catch (CustomFunctionErrorException ex)
{
hostObj = CommonErrors.RuntimeExceptionError(node.IRContext, ex.Message);
}
return hostObj;
case UDFParameterInfo uDFParameterInfo:
var frame = _udfStack.Peek();
return frame.GetArg(uDFParameterInfo);
default:
return ResolvedObjectHelpers.ResolvedObjectError(node);
}
}
private FormulaValue GetVariableOrFail(ResolvedObjectNode node, ISymbolSlot slot, bool isMutation = false)
{
if (_symbolValues != null)
{
var value = _symbolValues.Get(slot);
if (value != null)
{
if (isMutation)
{
value = value.MaybeShallowCopy();
_symbolValues.Set(slot, value);
}
return value;
}
}
return ResolvedObjectHelpers.ResolvedObjectError(node);
}
public DateTime GetNormalizedDateTime(FormulaValue arg)
{
return GetNormalizedDateTimeLibrary(arg, TimeZoneInfo);
}
public DateTime GetNormalizedDateTimeAllowTimeValue(FormulaValue arg)
{
switch (arg)
{
case DateTimeValue dtv:
return dtv.GetConvertedValue(TimeZoneInfo);
case DateValue dv:
return dv.GetConvertedValue(TimeZoneInfo);
case TimeValue tv:
return _epoch.Add(tv.Value);
default:
throw CommonExceptions.RuntimeMisMatch;
}
}
public TimeSpan GetNormalizedTimeSpan(FormulaValue arg)
{
switch (arg)
{
case DateTimeValue dtv:
return dtv.GetConvertedValue(TimeZoneInfo).TimeOfDay;
case TimeValue dv:
return dv.Value;
default:
throw CommonExceptions.RuntimeMisMatch;
}
}
public TimeSpan GetNormalizedTimeSpanWithoutDay(FormulaValue arg)
{
switch (arg)
{
case DateTimeValue dtv:
var dtvValue = dtv.GetConvertedValue(TimeZoneInfo);
return new TimeSpan(0, dtvValue.Hour, dtvValue.Minute, dtvValue.Second, dtvValue.Millisecond);
case TimeValue tv:
return tv.Value;
default:
throw CommonExceptions.RuntimeMisMatch;
}
}
}
}