-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd07d62
commit 2f7747a
Showing
9 changed files
with
324 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import ISequence from '../dataTypes/ISequence'; | ||
import sequenceFactory from '../dataTypes/sequenceFactory'; | ||
import { SequenceType } from '../dataTypes/Value'; | ||
import DynamicContext from '../DynamicContext'; | ||
import ExecutionParameters from '../ExecutionParameters'; | ||
import Expression, { RESULT_ORDERINGS } from '../Expression'; | ||
import sequenceDeepEqual, { | ||
itemDeepEqual, | ||
} from '../functions/builtInFunctions_sequences_deepEqual'; | ||
import PossiblyUpdatingExpression, { SequenceCallbacks } from '../PossiblyUpdatingExpression'; | ||
import Specificity from '../Specificity'; | ||
import StaticContext from '../StaticContext'; | ||
import atomizeSequence from '../util/atomizeSequence'; | ||
import { IterationHint } from '../util/iterators'; | ||
import { errXUST0001 } from '../xquery-update/XQueryUpdateFacilityErrors'; | ||
import { errXPTY0004 } from './XQueryErrors'; | ||
|
||
export type SwitchExpressionClause = { | ||
caseClauseExpression: PossiblyUpdatingExpression; | ||
tests: Expression[]; | ||
}; | ||
|
||
class SwitchExpression extends PossiblyUpdatingExpression { | ||
private _amountOfCases: number; | ||
private _argExpression: Expression; | ||
private _testsByCase: Expression[][]; | ||
|
||
constructor( | ||
argExpression: Expression, | ||
caseClauses: SwitchExpressionClause[], | ||
defaultExpression: PossiblyUpdatingExpression, | ||
type: SequenceType, | ||
) { | ||
const specificity = new Specificity({}); | ||
super( | ||
specificity, | ||
[ | ||
argExpression, | ||
...caseClauses.map((clause) => clause.caseClauseExpression), | ||
defaultExpression, | ||
].concat(...caseClauses.map((clause) => clause.tests.map((test) => test))), | ||
{ | ||
canBeStaticallyEvaluated: false, | ||
peer: false, | ||
resultOrder: RESULT_ORDERINGS.UNSORTED, | ||
subtree: false, | ||
}, | ||
type, | ||
); | ||
this._argExpression = argExpression; | ||
this._amountOfCases = caseClauses.length; | ||
this._testsByCase = caseClauses.map((clause) => clause.tests); | ||
} | ||
|
||
public performFunctionalEvaluation( | ||
dynamicContext: DynamicContext, | ||
executionParameters: ExecutionParameters, | ||
sequenceCallbacks: ((dynamicContext: DynamicContext) => ISequence)[], | ||
) { | ||
// Pick the argumentExpression. | ||
const evaluatedExpression = atomizeSequence( | ||
sequenceCallbacks[0](dynamicContext), | ||
executionParameters, | ||
); | ||
|
||
// Map over all values the type test, and return the result. | ||
return evaluatedExpression.switchCases({ | ||
multiple: () => { | ||
throw errXPTY0004( | ||
'The operand for a switch expression should result in zero or one item', | ||
); | ||
}, | ||
default: () => { | ||
const singleValue = evaluatedExpression.first(); | ||
const isEmpty = !singleValue; | ||
|
||
for (let i = 0; i < this._amountOfCases; i++) { | ||
const results = this._testsByCase[i].map((x) => | ||
x.evaluateMaybeStatically(dynamicContext, executionParameters), | ||
); | ||
|
||
for (const result of results) { | ||
const atomizedResult = atomizeSequence(result, executionParameters); | ||
if (atomizedResult.isEmpty()) { | ||
if (isEmpty) { | ||
return sequenceCallbacks[i + 1](dynamicContext); | ||
} | ||
continue; | ||
} | ||
|
||
if (!atomizedResult.isSingleton()) { | ||
throw errXPTY0004( | ||
'The operand for a switch case should result in zero or one item', | ||
); | ||
} | ||
|
||
if (isEmpty) { | ||
continue; | ||
} | ||
|
||
const first = atomizedResult.first(); | ||
|
||
if ( | ||
itemDeepEqual( | ||
dynamicContext, | ||
executionParameters, | ||
null, | ||
singleValue, | ||
first, | ||
).next(IterationHint.NONE).value | ||
) { | ||
return sequenceCallbacks[i + 1](dynamicContext); | ||
} | ||
} | ||
} | ||
|
||
return sequenceCallbacks[this._amountOfCases + 1](dynamicContext); | ||
}, | ||
}); | ||
} | ||
|
||
public performStaticEvaluation(staticContext: StaticContext) { | ||
super.performStaticEvaluation(staticContext); | ||
|
||
if (this._argExpression.isUpdating) { | ||
throw errXUST0001(); | ||
} | ||
} | ||
} | ||
|
||
export default SwitchExpression; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.