-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions.ts
385 lines (343 loc) · 9.96 KB
/
expressions.ts
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
import { isRecord, mapRecordValues } from '../utils.js'
export type VariableName = string
export type BinaryOperator =
| '+'
| '-'
| '*'
| '/'
| '//'
| '%'
| '=='
| '!='
| '>'
| '>='
| '<'
| '<='
| 'in'
| 'and'
| 'or'
export type UnaryOperator = '-' | '+' | 'not'
// Operator precendence for unary and binary operators in the Workflows language.
// Note that these differ somewhat from Javascript.
// https://cloud.google.com/workflows/docs/reference/syntax/datatypes
const operatorPrecedenceValue: Map<string, number> = new Map<string, number>([
['not', 7],
// ['-', 6], // unary minus, commented out to avoid confusion with binary minus
['*', 5],
['/', 5],
['//', 5],
['%', 5],
['+', 4],
['-', 4],
['<=', 3],
['>=', 3],
['<', 3],
['>', 3],
['==', 3],
['===', 3],
['!=', 3],
['!==', 3],
['in', 3],
['and', 2],
['or', 1],
])
export type Primitive =
| null
| string
| number
| boolean
| (Primitive | Expression)[]
| { [key: string]: Primitive | Expression }
export type LiteralValueOrLiteralExpression =
| null
| string
| number
| boolean
| LiteralValueOrLiteralExpression[]
| { [key: string]: LiteralValueOrLiteralExpression }
export type Expression =
| PrimitiveExpression
| BinaryExpression
| VariableReferenceExpression
| FunctionInvocationExpression
| MemberExpression
| UnaryExpression
// A primitive (string, number, list, etc) value
// TODO: Is this needed? Use unboxed Primitive instead?
export class PrimitiveExpression {
readonly expressionType = 'primitive'
readonly value: Primitive
constructor(value: Primitive) {
this.value = value
}
// Return the string representation of this expression.
// Not enclosed in ${}.
toString(): string {
const val = this.value
if (Array.isArray(val)) {
const elements = val.map((v) => {
return isExpression(v) ? v.toString() : primitiveToString(v)
})
return `[${elements.join(', ')}]`
} else if (isRecord(val)) {
const elements = Object.entries(val).map(([k, v]) => {
return `"${k}": ${isExpression(v) ? v.toString() : primitiveToString(v)}`
})
return `{${elements.join(', ')}}`
} else {
return `${JSON.stringify(val)}`
}
}
}
// expr OPERATOR expr
export class BinaryExpression {
readonly expressionType = 'binary'
readonly binaryOperator: BinaryOperator
readonly left: Expression
readonly right: Expression
constructor(
left: Expression,
binaryOperator: BinaryOperator,
right: Expression,
) {
this.binaryOperator = binaryOperator
this.left = left
this.right = right
}
toString(): string {
let leftString: string = this.left.toString()
let rightString: string = this.right.toString()
if (this.left.expressionType === 'binary') {
const leftOpValue =
operatorPrecedenceValue.get(this.left.binaryOperator) ?? 0
const thisOpValue = operatorPrecedenceValue.get(this.binaryOperator) ?? 0
if (leftOpValue < thisOpValue) {
leftString = `(${leftString})`
}
}
if (this.right.expressionType === 'binary') {
const rightOpValue =
operatorPrecedenceValue.get(this.right.binaryOperator) ?? 0
const thisOpValue = operatorPrecedenceValue.get(this.binaryOperator) ?? 0
if (rightOpValue < thisOpValue) {
rightString = `(${rightString})`
}
}
return `${leftString} ${this.binaryOperator} ${rightString}`
}
}
// Variable name: a plain identifier (y, year) or a list
// element accessor (names[3])
export class VariableReferenceExpression {
readonly expressionType = 'variableReference'
readonly variableName: VariableName
constructor(variableName: VariableName) {
this.variableName = variableName
}
toString(): string {
return this.variableName
}
}
// Function invocation with anonymous parameters: sys.get_env("GOOGLE_CLOUD_PROJECT_ID")
export class FunctionInvocationExpression {
readonly expressionType = 'functionInvocation'
readonly functionName: string
readonly arguments: Expression[]
constructor(functionName: string, argumentExpressions: Expression[]) {
this.functionName = functionName
this.arguments = argumentExpressions
}
toString(): string {
const argumentStrings = this.arguments.map((x) => x.toString())
return `${this.functionName}(${argumentStrings.join(', ')})`
}
}
// object.property or object[property]
export class MemberExpression {
readonly expressionType = 'member'
readonly object: Expression
readonly property: Expression
readonly computed: boolean
constructor(object: Expression, property: Expression, computed: boolean) {
this.object = object
this.property = property
this.computed = computed
}
toString(): string {
if (this.computed) {
return `${this.object.toString()}[${this.property.toString()}]`
} else {
return `${this.object.toString()}.${this.property.toString()}`
}
}
}
export class UnaryExpression {
readonly expressionType = 'unary'
readonly operator: UnaryOperator
readonly value: Expression
constructor(operator: UnaryOperator, value: Expression) {
this.operator = operator
this.value = value
}
toString(): string {
const separator = this.operator === 'not' ? ' ' : ''
let valueString = this.value.toString()
if (this.value.expressionType === 'binary') {
valueString = `(${valueString})`
}
return `${this.operator}${separator}${valueString}`
}
}
// Convert a Primitive to a string representation.
// Does not add ${}.
function primitiveToString(val: Primitive): string {
if (Array.isArray(val)) {
const elements = val.map((x) => {
return isExpression(x) ? x.toString() : primitiveToString(x)
})
return `[${elements.join(', ')}]`
} else if (val !== null && typeof val === 'object') {
const items = Object.entries(val).map(([k, v]) => {
return [k, isExpression(v) ? v.toString() : primitiveToString(v)]
})
const elements = items.map(([k, v]) => `"${k}":${v}`)
return `{${elements.join(',')}}`
} else {
return JSON.stringify(val)
}
}
export function isExpression(val: Primitive | Expression): val is Expression {
return val instanceof Object && 'expressionType' in val && !isRecord(val)
}
// Returns a literal for simple terms and a literal expression enclosed in ${} for complex terms.
export function expressionToLiteralValueOrLiteralExpression(
ex: Expression,
): LiteralValueOrLiteralExpression {
switch (ex.expressionType) {
case 'primitive':
return primitiveExpressionToLiteralValueOrLiteralExpression(ex)
case 'binary':
case 'variableReference':
case 'functionInvocation':
case 'member':
return `\${${ex.toString()}}`
case 'unary':
if (
ex.value.expressionType === 'primitive' &&
typeof ex.value.value === 'number'
) {
if (ex.operator === '+') {
return ex.value.value
} else if (ex.operator === '-') {
return -ex.value.value
} else {
return `\${${ex.toString()}}`
}
} else {
return `\${${ex.toString()}}`
}
}
}
function primitiveExpressionToLiteralValueOrLiteralExpression(
ex: PrimitiveExpression,
): LiteralValueOrLiteralExpression {
if (
typeof ex.value === 'number' ||
typeof ex.value === 'string' ||
typeof ex.value === 'boolean' ||
ex.value === null
) {
return ex.value
} else if (Array.isArray(ex.value)) {
return ex.value.map((x) => {
if (isExpression(x)) {
return expressionToLiteralValueOrLiteralExpression(x)
} else if (Array.isArray(x) || isRecord(x)) {
return primitiveExpressionToLiteralValueOrLiteralExpression(
new PrimitiveExpression(x),
)
} else if (
x === null ||
typeof x === 'string' ||
typeof x === 'number' ||
typeof x === 'boolean'
) {
return x
} else {
return `\${${primitiveToString(x)}}`
}
})
} else if (isRecord(ex.value)) {
return mapRecordValues(ex.value, (v) => {
if (isExpression(v)) {
return expressionToLiteralValueOrLiteralExpression(v)
} else if (Array.isArray(v) || isRecord(v)) {
return primitiveExpressionToLiteralValueOrLiteralExpression(
new PrimitiveExpression(v),
)
} else if (
v === null ||
typeof v === 'string' ||
typeof v === 'number' ||
typeof v === 'boolean'
) {
return v
} else {
return `\${${primitiveToString(v)}}`
}
})
} else {
return `\${${ex.toString()}}`
}
}
// Returns true if expression is a literal value.
// Examples of literals: number, string, array of numbers or strings, etc.
// Examples of non-literals: array that contains complex expressions.
export function isLiteral(ex: Expression): boolean {
switch (ex.expressionType) {
case 'primitive':
return primitiveIsLiteral(ex.value)
case 'unary':
return isLiteral(ex.value)
case 'binary':
case 'variableReference':
case 'functionInvocation':
case 'member':
return false
}
}
function primitiveIsLiteral(value: Primitive): boolean {
if (Array.isArray(value)) {
return value.every((x) => {
return isExpression(x) ? isLiteral(x) : primitiveIsLiteral(x)
})
} else if (isRecord(value)) {
return Object.values(value).every((x) => {
return isExpression(x) ? isLiteral(x) : primitiveIsLiteral(x)
})
} else {
return (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean' ||
value === null
)
}
}
export function isFullyQualifiedName(ex: Expression): boolean {
switch (ex.expressionType) {
case 'primitive':
case 'binary':
case 'functionInvocation':
return false
case 'variableReference':
return true
case 'member':
return (
isFullyQualifiedName(ex.object) &&
(ex.computed || isFullyQualifiedName(ex.property))
)
case 'unary':
return isFullyQualifiedName(ex.value)
}
}