-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathMap.cs
More file actions
195 lines (164 loc) · 7.03 KB
/
Copy pathMap.cs
File metadata and controls
195 lines (164 loc) · 7.03 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System.Collections.Generic;
using Microsoft.PowerFx.Core.App.ErrorContainers;
using Microsoft.PowerFx.Core.Binding;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.Localization;
using Microsoft.PowerFx.Core.Types;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Syntax;
#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable SA1649 // File name should match first type name
namespace Microsoft.PowerFx.Core.Texl.Builtins
{
// Map(source:*, formula)
// Map is a functional programming style function that is equivalent to ForAll
// but does not allow imperative logic (side effects) within the formula.
internal sealed class MapFunction : FunctionWithTableInput
{
public const string MapInvariantFunctionName = "Map";
public override bool SkipScopeForInlineRecords => true;
public override bool IsSelfContained => true;
public override bool SupportsParamCoercion => false;
public MapFunction()
: base(MapInvariantFunctionName, TexlStrings.AboutMap, FunctionCategories.Table, DType.Unknown, 0x2, 2, 2, DType.EmptyTable)
{
ScopeInfo = new FunctionScopeInfo(this);
}
public override IEnumerable<TexlStrings.StringGetter[]> GetSignatures()
{
yield return new[] { TexlStrings.MapArg1, TexlStrings.MapArg2 };
}
public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DType[] argTypes, IErrorContainer errors, out DType returnType, out Dictionary<TexlNode, DType> nodeToCoercedTypeMap)
{
Contracts.AssertValue(args);
Contracts.AssertAllValues(args);
Contracts.AssertValue(argTypes);
Contracts.AssertAllValues(argTypes);
Contracts.Assert(args.Length == argTypes.Length);
Contracts.AssertValue(errors);
nodeToCoercedTypeMap = null;
var fArgsValid = CheckType(context, args[0], argTypes[0], ParamTypes[0], errors, ref nodeToCoercedTypeMap);
if (argTypes[1].IsRecord)
{
returnType = argTypes[1].ToTable();
}
else if (argTypes[1].IsPrimitive || argTypes[1].IsTable || argTypes[1].IsUntypedObject)
{
returnType = DType.CreateTable(new TypedName(argTypes[1], ColumnName_Value));
}
else if (argTypes[1].IsVoid)
{
// Map does not support Void return type since it doesn't allow side effects
returnType = DType.Error;
fArgsValid = false;
}
else
{
returnType = DType.Error;
fArgsValid = false;
}
return fArgsValid;
}
public override bool HasSuggestionsForParam(int index)
{
Contracts.Assert(index >= 0);
return index == 0;
}
/// <summary>
/// Map requires a pure (non-side-effectful) lambda expression.
/// This validation checks if the lambda argument contains any behavior functions.
/// </summary>
public override bool PostVisitValidation(TexlBinding binding, CallNode callNode)
{
Contracts.AssertValue(binding);
Contracts.AssertValue(callNode);
// Get the lambda argument (second argument, index 1)
var args = callNode.Args.Children;
if (args.Count >= 2)
{
var lambdaArg = args[1];
if (binding.HasSideEffects(lambdaArg))
{
binding.ErrorContainer.EnsureError(lambdaArg, TexlStrings.ErrMapFunctionRequiresPureLambda, Name);
return true;
}
}
return false;
}
}
internal sealed class MapFunction_UO : BuiltinFunction
{
public override bool IsSelfContained => true;
public override bool SupportsParamCoercion => false;
public MapFunction_UO()
: base(MapFunction.MapInvariantFunctionName, TexlStrings.AboutMap, FunctionCategories.Table, DType.Unknown, 0x2, 2, 2, DType.UntypedObject)
{
ScopeInfo = new FunctionScopeInfo(this);
}
public override IEnumerable<TexlStrings.StringGetter[]> GetSignatures()
{
yield return new[] { TexlStrings.MapArg1, TexlStrings.MapArg2 };
}
public override bool CheckTypes(CheckTypesContext context, TexlNode[] args, DType[] argTypes, IErrorContainer errors, out DType returnType, out Dictionary<TexlNode, DType> nodeToCoercedTypeMap)
{
Contracts.AssertValue(args);
Contracts.AssertAllValues(args);
Contracts.AssertValue(argTypes);
Contracts.AssertAllValues(argTypes);
Contracts.Assert(args.Length == argTypes.Length);
Contracts.AssertValue(errors);
nodeToCoercedTypeMap = null;
var fArgsValid = CheckType(context, args[0], argTypes[0], ParamTypes[0], errors, ref nodeToCoercedTypeMap);
if (argTypes[1].IsRecord)
{
returnType = argTypes[1].ToTable();
}
else if (argTypes[1].IsPrimitive || argTypes[1].IsTable || argTypes[1].IsUntypedObject)
{
returnType = DType.CreateTable(new TypedName(argTypes[1], ColumnName_Value));
}
else if (argTypes[1].IsVoid)
{
// Map does not support Void return type since it doesn't allow side effects
returnType = DType.Error;
fArgsValid = false;
}
else
{
returnType = DType.Error;
fArgsValid = false;
}
return fArgsValid;
}
public override bool HasSuggestionsForParam(int index)
{
Contracts.Assert(index >= 0);
return index == 0;
}
/// <summary>
/// Map requires a pure (non-side-effectful) lambda expression.
/// This validation checks if the lambda argument contains any behavior functions.
/// </summary>
public override bool PostVisitValidation(TexlBinding binding, CallNode callNode)
{
Contracts.AssertValue(binding);
Contracts.AssertValue(callNode);
// Get the lambda argument (second argument, index 1)
var args = callNode.Args.Children;
if (args.Count >= 2)
{
var lambdaArg = args[1];
if (binding.HasSideEffects(lambdaArg))
{
binding.ErrorContainer.EnsureError(lambdaArg, TexlStrings.ErrMapFunctionRequiresPureLambda, Name);
return true;
}
}
return false;
}
}
}
#pragma warning restore SA1402 // File may only contain a single type
#pragma warning restore SA1649 // File name should match first type name