1
1
import {
2
- ParameterSubstitutions ,
3
2
ConstantBoolean ,
4
3
ConstantExpression ,
5
4
ConstantFloat ,
6
5
ConstantInt ,
6
+ ConstantList ,
7
+ ConstantMap ,
7
8
ConstantNull ,
8
9
ConstantString ,
9
- IntermediateBlockLambda ,
10
- IntermediateExpressionLambda ,
10
+ ParameterSubstitutions ,
11
11
SimplifiedExpression ,
12
+ UnknownValue ,
12
13
} from './model.js' ;
13
14
import { AstNode } from 'langium' ;
14
15
import {
@@ -22,6 +23,8 @@ import {
22
23
isSdsIndexedAccess ,
23
24
isSdsInfixOperation ,
24
25
isSdsInt ,
26
+ isSdsList ,
27
+ isSdsMap ,
25
28
isSdsMemberAccess ,
26
29
isSdsNull ,
27
30
isSdsParenthesizedExpression ,
@@ -45,32 +48,29 @@ import {
45
48
46
49
/* c8 ignore start */
47
50
/**
48
- * Tries to evaluate this expression. On success an SdsConstantExpression is returned, otherwise `undefined`.
51
+ * Tries to evaluate this expression.
49
52
*/
50
- export const toConstantExpressionOrUndefined = ( node : AstNode | undefined ) : ConstantExpression | undefined => {
53
+ export const toConstantExpression = ( node : AstNode | undefined ) : ConstantExpression => {
51
54
if ( ! node ) {
52
- return undefined ;
55
+ return UnknownValue ;
53
56
}
54
57
55
- return toConstantExpressionOrUndefinedImpl ( node , new Map ( ) ) ;
58
+ return toConstantExpressionImpl ( node , new Map ( ) ) ;
56
59
} ;
57
60
58
- const toConstantExpressionOrUndefinedImpl = (
59
- node : AstNode ,
60
- substitutions : ParameterSubstitutions ,
61
- ) : ConstantExpression | undefined => {
61
+ const toConstantExpressionImpl = ( node : AstNode , substitutions : ParameterSubstitutions ) : ConstantExpression => {
62
62
const simplifiedExpression = simplify ( node , substitutions ) ?. unwrap ( ) ;
63
63
if ( simplifiedExpression instanceof ConstantExpression ) {
64
64
return simplifiedExpression ;
65
65
} else {
66
- return undefined ;
66
+ return UnknownValue ;
67
67
}
68
68
} ;
69
69
70
- const simplify = ( node : AstNode , substitutions : ParameterSubstitutions ) : SimplifiedExpression | undefined => {
70
+ const simplify = ( node : AstNode , substitutions : ParameterSubstitutions ) : SimplifiedExpression => {
71
71
// Only expressions have a value
72
72
if ( ! isSdsExpression ( node ) ) {
73
- return undefined ;
73
+ return UnknownValue ;
74
74
}
75
75
76
76
// Base cases
@@ -101,6 +101,10 @@ const simplify = (node: AstNode, substitutions: ParameterSubstitutions): Simplif
101
101
return simplify ( node . value , substitutions ) ;
102
102
} else if ( isSdsInfixOperation ( node ) ) {
103
103
return simplifyInfixOperation ( node , substitutions ) ;
104
+ } else if ( isSdsList ( node ) ) {
105
+ return new ConstantList ( [ ] ) ;
106
+ } else if ( isSdsMap ( node ) ) {
107
+ return new ConstantMap ( [ ] ) ;
104
108
} else if ( isSdsParenthesizedExpression ( node ) ) {
105
109
return simplify ( node . expression , substitutions ) ;
106
110
} else if ( isSdsPrefixOperation ( node ) ) {
@@ -125,10 +129,7 @@ const simplify = (node: AstNode, substitutions: ParameterSubstitutions): Simplif
125
129
throw new Error ( `Missing case to handle ${ node . $type } .` ) ;
126
130
} ;
127
131
128
- const simplifyBlockLambda = (
129
- _node : SdsBlockLambda ,
130
- _substitutions : ParameterSubstitutions ,
131
- ) : IntermediateBlockLambda | undefined => {
132
+ const simplifyBlockLambda = ( _node : SdsBlockLambda , _substitutions : ParameterSubstitutions ) : SimplifiedExpression => {
132
133
// return when {
133
134
// callableHasNoSideEffects(resultIfUnknown = true) -> SdsIntermediateBlockLambda(
134
135
// parameters = parametersOrEmpty(),
@@ -137,13 +138,13 @@ const simplifyBlockLambda = (
137
138
// )
138
139
// else -> undefined
139
140
// }
140
- return undefined ;
141
+ return UnknownValue ;
141
142
} ;
142
143
143
144
const simplifyExpressionLambda = (
144
145
_node : SdsExpressionLambda ,
145
146
_substitutions : ParameterSubstitutions ,
146
- ) : IntermediateExpressionLambda | undefined => {
147
+ ) : SimplifiedExpression => {
147
148
// return when {
148
149
// callableHasNoSideEffects(resultIfUnknown = true) -> SdsIntermediateExpressionLambda(
149
150
// parameters = parametersOrEmpty(),
@@ -152,19 +153,23 @@ const simplifyExpressionLambda = (
152
153
// )
153
154
// else -> undefined
154
155
// }
155
- return undefined ;
156
+ return UnknownValue ;
156
157
} ;
157
158
158
159
const simplifyInfixOperation = (
159
160
node : SdsInfixOperation ,
160
161
substitutions : ParameterSubstitutions ,
161
- ) : ConstantExpression | undefined => {
162
+ ) : SimplifiedExpression => {
162
163
// By design none of the operators are short-circuited
163
- const constantLeft = toConstantExpressionOrUndefinedImpl ( node . leftOperand , substitutions ) ;
164
- if ( ! constantLeft ) return ;
164
+ const constantLeft = toConstantExpressionImpl ( node . leftOperand , substitutions ) ;
165
+ if ( constantLeft === UnknownValue ) {
166
+ return UnknownValue ;
167
+ }
165
168
166
- const constantRight = toConstantExpressionOrUndefinedImpl ( node . rightOperand , substitutions ) ;
167
- if ( ! constantRight ) return ;
169
+ const constantRight = toConstantExpressionImpl ( node . rightOperand , substitutions ) ;
170
+ if ( constantRight === UnknownValue ) {
171
+ return UnknownValue ;
172
+ }
168
173
169
174
// return when (operator()) {
170
175
// Or -> simplifyLogicalOp(constantLeft, Boolean::or, constantRight)
@@ -232,7 +237,7 @@ const simplifyInfixOperation = (
232
237
// else -> constantLeft
233
238
// }
234
239
// }
235
- return undefined ;
240
+ return UnknownValue ;
236
241
} ;
237
242
238
243
// private fun simplifyLogicalOp(
@@ -288,9 +293,11 @@ const simplifyInfixOperation = (
288
293
const simplifyPrefixOperation = (
289
294
node : SdsPrefixOperation ,
290
295
substitutions : ParameterSubstitutions ,
291
- ) : ConstantExpression | undefined => {
292
- const constantOperand = toConstantExpressionOrUndefinedImpl ( node . operand , substitutions ) ;
293
- if ( ! constantOperand ) return ;
296
+ ) : SimplifiedExpression => {
297
+ const constantOperand = toConstantExpressionImpl ( node . operand , substitutions ) ;
298
+ if ( constantOperand === UnknownValue ) {
299
+ return UnknownValue ;
300
+ }
294
301
295
302
if ( node . operator === 'not' ) {
296
303
if ( constantOperand instanceof ConstantBoolean ) {
@@ -304,22 +311,22 @@ const simplifyPrefixOperation = (
304
311
}
305
312
}
306
313
307
- return undefined ;
314
+ return UnknownValue ;
308
315
} ;
309
316
310
317
const simplifyTemplateString = (
311
318
node : SdsTemplateString ,
312
319
substitutions : ParameterSubstitutions ,
313
- ) : ConstantExpression | undefined => {
314
- const constantExpressions = node . expressions . map ( ( it ) => toConstantExpressionOrUndefinedImpl ( it , substitutions ) ) ;
315
- if ( constantExpressions . some ( ( it ) => it === undefined ) ) {
316
- return undefined ;
320
+ ) : SimplifiedExpression => {
321
+ const constantExpressions = node . expressions . map ( ( it ) => toConstantExpressionImpl ( it , substitutions ) ) ;
322
+ if ( constantExpressions . some ( ( it ) => it === UnknownValue ) ) {
323
+ return UnknownValue ;
317
324
}
318
325
319
326
return new ConstantString ( constantExpressions . map ( ( it ) => it ! . toInterpolationString ( ) ) . join ( '' ) ) ;
320
327
} ;
321
328
322
- const simplifyCall = ( _node : SdsCall , _substitutions : ParameterSubstitutions ) : SimplifiedExpression | undefined => {
329
+ const simplifyCall = ( _node : SdsCall , _substitutions : ParameterSubstitutions ) : SimplifiedExpression => {
323
330
// val simpleReceiver = simplifyReceiver(substitutions) ?: return undefined
324
331
// val newSubstitutions = buildNewSubstitutions(simpleReceiver, substitutions)
325
332
//
@@ -340,7 +347,7 @@ const simplifyCall = (_node: SdsCall, _substitutions: ParameterSubstitutions): S
340
347
// )
341
348
// }
342
349
// }
343
- return undefined ;
350
+ return UnknownValue ;
344
351
} ;
345
352
346
353
// private fun SdsCall.simplifyReceiver(substitutions: ParameterSubstitutions): SdsIntermediateCallable? {
@@ -387,19 +394,16 @@ const simplifyCall = (_node: SdsCall, _substitutions: ParameterSubstitutions): S
387
394
const simplifyIndexedAccess = (
388
395
_node : SdsIndexedAccess ,
389
396
_substitutions : ParameterSubstitutions ,
390
- ) : SimplifiedExpression | undefined => {
397
+ ) : SimplifiedExpression => {
391
398
// val simpleReceiver = receiver.simplify(substitutions) as? SdsIntermediateVariadicArguments ?: return undefined
392
399
// val simpleIndex = index.simplify(substitutions) as? SdsConstantInt ?: return undefined
393
400
//
394
401
// return simpleReceiver.getArgumentByIndexOrNull(simpleIndex.value)
395
402
// }
396
- return undefined ;
403
+ return UnknownValue ;
397
404
} ;
398
405
399
- const simplifyMemberAccess = (
400
- _node : SdsMemberAccess ,
401
- _substitutions : ParameterSubstitutions ,
402
- ) : SimplifiedExpression | undefined => {
406
+ const simplifyMemberAccess = ( _node : SdsMemberAccess , _substitutions : ParameterSubstitutions ) : SimplifiedExpression => {
403
407
// private fun SdsMemberAccess.simplifyMemberAccess(substitutions: ParameterSubstitutions): SdsSimplifiedExpression? {
404
408
// if (member.declaration is SdsEnumVariant) {
405
409
// return member.simplifyReference(substitutions)
@@ -413,13 +417,10 @@ const simplifyMemberAccess = (
413
417
// is SdsIntermediateRecord -> simpleReceiver.getSubstitutionByReferenceOrNull(member)
414
418
// else -> undefined
415
419
// }
416
- return undefined ;
420
+ return UnknownValue ;
417
421
} ;
418
422
419
- const simplifyReference = (
420
- _node : SdsReference ,
421
- _substitutions : ParameterSubstitutions ,
422
- ) : SimplifiedExpression | undefined => {
423
+ const simplifyReference = ( _node : SdsReference , _substitutions : ParameterSubstitutions ) : SimplifiedExpression => {
423
424
// return when (val declaration = this.declaration) {
424
425
// is SdsEnumVariant -> when {
425
426
// declaration.parametersOrEmpty().isEmpty() -> SdsConstantEnumVariant(declaration)
@@ -430,7 +431,7 @@ const simplifyReference = (
430
431
// is SdsStep -> declaration.simplifyStep()
431
432
// else -> undefined
432
433
// }
433
- return undefined ;
434
+ return UnknownValue ;
434
435
} ;
435
436
436
437
// private fun SdsAbstractAssignee.simplifyAssignee(substitutions: ParameterSubstitutions): SdsSimplifiedExpression? {
0 commit comments