-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathBooleanPlugin.ts
More file actions
297 lines (274 loc) · 9.19 KB
/
Copy pathBooleanPlugin.ts
File metadata and controls
297 lines (274 loc) · 9.19 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
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {CellError, ErrorType} from '../../Cell'
import {ErrorMessage} from '../../error-message'
import {ProcedureAst} from '../../parser'
import {SimpleRangeValue} from '../../SimpleRangeValue'
import {InterpreterState} from '../InterpreterState'
import {InternalNoErrorScalarValue, InternalScalarValue, InterpreterValue} from '../InterpreterValue'
import {FunctionArgumentType, FunctionPlugin, FunctionPluginTypecheck, ImplementedFunctions} from './FunctionPlugin'
/**
* Interpreter plugin containing boolean functions
*/
export class BooleanPlugin extends FunctionPlugin implements FunctionPluginTypecheck<BooleanPlugin> {
public static implementedFunctions: ImplementedFunctions = {
'TRUE': {
method: 'literalTrue',
parameters: [],
},
'FALSE': {
method: 'literalFalse',
parameters: [],
},
'IF': {
method: 'conditionalIf',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
{argumentType: FunctionArgumentType.SCALAR, defaultValue: false, passSubtype: true},
],
},
'IFS': {
method: 'ifs',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
],
repeatLastArgs: 2,
},
'AND': {
method: 'and',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
],
repeatLastArgs: 1,
expandRanges: true,
},
'OR': {
method: 'or',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
],
repeatLastArgs: 1,
expandRanges: true,
},
'XOR': {
method: 'xor',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
],
repeatLastArgs: 1,
expandRanges: true,
},
'NOT': {
method: 'not',
parameters: [
{argumentType: FunctionArgumentType.BOOLEAN},
]
},
'SWITCH': {
method: 'switch',
parameters: [
{argumentType: FunctionArgumentType.NOERROR},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
],
repeatLastArgs: 1,
},
'IFERROR': {
method: 'iferror',
parameters: [
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
]
},
'IFNA': {
method: 'ifna',
parameters: [
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
]
},
'CHOOSE': {
method: 'choose',
parameters: [
{argumentType: FunctionArgumentType.INTEGER, minValue: 1},
{argumentType: FunctionArgumentType.SCALAR, passSubtype: true},
],
repeatLastArgs: 1,
},
}
/**
* Corresponds to TRUE()
*
* Returns the logical true
*
* @param ast
* @param state
*/
public literalTrue(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('TRUE'), () => true)
}
/**
* Corresponds to FALSE()
*
* Returns the logical false
*
* @param ast
* @param state
*/
public literalFalse(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('FALSE'), () => false)
}
/**
* Corresponds to IF(expression, value_if_true, value_if_false)
*
* Returns value specified as second argument if expression is true and third argument if expression is false
*
* @param ast
* @param state
*/
public conditionalIf(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('IF'), (condition, arg2, arg3) => {
return condition ? arg2 : arg3
})
}
/**
* Implementation for the IFS function. Returns the value that corresponds to the first true condition.
*
* @param ast
* @param state
*/
public ifs(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
const metadata = this.metadata('IFS')
const argumentsMetadata = this.buildMetadataForEachArgumentValue(ast.args.length, metadata)
if (!this.isNumberOfArgumentValuesValid(argumentsMetadata, ast.args.length)) {
return new CellError(ErrorType.NA, ErrorMessage.WrongArgNumber)
}
for (let idx = 0; idx < ast.args.length; idx += 2) {
const condition = this.evaluateAst(ast.args[idx], state)
if (condition instanceof SimpleRangeValue && state.arraysFlag) {
return this.runFunction(ast.args, state, metadata, (...args: InterpreterValue[]) => this.evaluateIfsArguments(args))
}
const coercedCondition = this.coerceToType(condition, argumentsMetadata[idx], state)
if (coercedCondition === undefined) {
return new CellError(ErrorType.VALUE, ErrorMessage.WrongType)
}
if (coercedCondition instanceof CellError) {
return coercedCondition
}
if (coercedCondition) {
const value = this.evaluateAst(ast.args[idx + 1], state)
if (value instanceof SimpleRangeValue && state.arraysFlag) {
return this.runFunction(ast.args, state, metadata, (...args: InterpreterValue[]) => this.evaluateIfsArguments(args))
}
const coercedValue = this.coerceToType(value, argumentsMetadata[idx + 1], state)
if (coercedValue === undefined) {
return new CellError(ErrorType.VALUE, ErrorMessage.WrongType)
}
return coercedValue as InterpreterValue
}
}
return new CellError(ErrorType.NA, ErrorMessage.NoConditionMet)
}
/**
* Preserves the previous vectorized IFS implementation for array arithmetic.
*
* @param {InterpreterValue[]} args evaluated IFS arguments
*/
private evaluateIfsArguments(args: InterpreterValue[]): InterpreterValue {
for (let idx = 0; idx < args.length; idx += 2) {
if (args[idx]) {
return args[idx + 1]
}
}
return new CellError(ErrorType.NA, ErrorMessage.NoConditionMet)
}
/**
* Corresponds to AND(expression1, [expression2, ...])
*
* Returns true if all of the provided arguments are logically true, and false if any of it is logically false
*
* @param ast
* @param state
*/
public and(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('AND'),
(...args: (boolean | undefined)[]) => args.filter(arg => arg !== undefined).every(arg => !!arg)
)
}
/**
* Corresponds to OR(expression1, [expression2, ...])
*
* Returns true if any of the provided arguments are logically true, and false otherwise
*
* @param ast
* @param state
*/
public or(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('OR'),
(...args: (boolean | undefined)[]) => args.filter(arg => arg !== undefined).some(arg => arg)
)
}
public not(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('NOT'), (arg) => !arg)
}
public xor(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('XOR'), (...args: (boolean | undefined)[]) => {
let cnt = 0
args.filter(arg => arg !== undefined).forEach(arg => {
if (arg) {
cnt++
}
})
return (cnt % 2) === 1
})
}
public switch(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('SWITCH'), (selector, ...args) => {
const n = args.length
let i = 0
for (; i + 1 < n; i += 2) {
if (args[i] instanceof CellError) {
continue
}
if (this.arithmeticHelper.eq(selector, args[i] as InternalNoErrorScalarValue)) {
return args[i + 1]
}
}
if (i < n) {
return args[i]
} else {
return new CellError(ErrorType.NA, ErrorMessage.NoDefault)
}
})
}
public iferror(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('IFERROR'), (arg1: InternalScalarValue, arg2: InternalScalarValue) => {
if (arg1 instanceof CellError) {
return arg2
} else {
return arg1
}
})
}
public ifna(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('IFNA'), (arg1: InternalScalarValue, arg2: InternalScalarValue) => {
if (arg1 instanceof CellError && arg1.type === ErrorType.NA) {
return arg2
} else {
return arg1
}
})
}
public choose(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('CHOOSE'), (selector, ...args) => {
if (selector > args.length) {
return new CellError(ErrorType.NUM, ErrorMessage.Selector)
}
return args[selector - 1]
})
}
}