Skip to content

Commit

Permalink
feat: partially evaluate lambdas and segments
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Nov 6, 2023
1 parent 10bed13 commit b44ab6b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import {
isSdsParenthesizedExpression,
isSdsPrefixOperation,
isSdsReference,
isSdsSegment,
isSdsString,
isSdsTemplateString,
isSdsTemplateStringEnd,
isSdsTemplateStringInner,
isSdsTemplateStringStart,
SdsBlockLambda,
SdsCall,
SdsExpression,
SdsExpressionLambda,
SdsIndexedAccess,
SdsInfixOperation,
SdsList,
Expand All @@ -41,18 +40,21 @@ import { getArguments, getParameters } from '../helpers/nodeProperties.js';
import { SafeDsNodeMapper } from '../helpers/safe-ds-node-mapper.js';
import { SafeDsServices } from '../safe-ds-module.js';
import {
BlockLambdaClosure,
BooleanConstant,
EvaluatedEnumVariant,
EvaluatedList,
EvaluatedMap,
EvaluatedMapEntry,
EvaluatedNode,
ExpressionLambdaClosure,
FloatConstant,
IntConstant,
isConstant,
NullConstant,
NumberConstant,
ParameterSubstitutions,
SegmentClosure,
StringConstant,
UnknownEvaluatedNode,
} from './model.js';
Expand Down Expand Up @@ -100,9 +102,11 @@ export class SafeDsPartialEvaluator {
): EvaluatedNode {
if (isSdsExpression(node)) {
return this.evaluateExpression(node, substitutions);
} else {
} else if (isSdsSegment(node)) {
return new SegmentClosure(node);
} /* c8 ignore start */ else {
return UnknownEvaluatedNode;
}
} /* c8 ignore stop */
}

private evaluateExpression(node: SdsExpression, substitutions: ParameterSubstitutions): EvaluatedNode {
Expand All @@ -124,9 +128,9 @@ export class SafeDsPartialEvaluator {
} else if (isSdsTemplateStringEnd(node)) {
return new StringConstant(node.value);
} else if (isSdsBlockLambda(node)) {
return this.evaluateBlockLambda(node, substitutions);
return new BlockLambdaClosure(node, substitutions);
} else if (isSdsExpressionLambda(node)) {
return this.evaluateExpressionLambda(node, substitutions);
return new ExpressionLambdaClosure(node, substitutions);
}

// Recursive cases
Expand Down Expand Up @@ -157,33 +161,6 @@ export class SafeDsPartialEvaluator {
} /* c8 ignore stop */
}

private evaluateBlockLambda(_node: SdsBlockLambda, _substitutions: ParameterSubstitutions): EvaluatedNode {
// return when {
// callableHasNoSideEffects(resultIfUnknown = true) -> SdsIntermediateBlockLambda(
// parameters = parametersOrEmpty(),
// results = blockLambdaResultsOrEmpty(),
// substitutionsOnCreation = substitutions
// )
// else -> undefined
// }
return UnknownEvaluatedNode;
}

private evaluateExpressionLambda(
_node: SdsExpressionLambda,
_substitutions: ParameterSubstitutions,
): EvaluatedNode {
// return when {
// callableHasNoSideEffects(resultIfUnknown = true) -> SdsIntermediateExpressionLambda(
// parameters = parametersOrEmpty(),
// result = result,
// substitutionsOnCreation = substitutions
// )
// else -> undefined
// }
return UnknownEvaluatedNode;
}

private evaluateInfixOperation(node: SdsInfixOperation, substitutions: ParameterSubstitutions): EvaluatedNode {
// By design none of the operators are short-circuited
const evaluatedLeft = this.evaluateWithSubstitutions(node.leftOperand, substitutions);
Expand Down Expand Up @@ -388,6 +365,7 @@ export class SafeDsPartialEvaluator {
);

// Override default values with the actual arguments
// TODO: If any argument has side effects, return UnknownEvaluatedNode
getArguments(node).forEach((it) => {
const parameter = this.nodeMapper.argumentToParameter(it);
if (parameter && args.has(parameter)) {
Expand Down Expand Up @@ -488,11 +466,11 @@ export class SafeDsPartialEvaluator {
}

private evaluateReference(_node: SdsReference, _substitutions: ParameterSubstitutions): EvaluatedNode {
// TODO: always call evaluateWithSubstitutions so caching works
// const target = node.target.ref;

// is SdsPlaceholder -> declaration.evaluateAssignee(substitutions)
// is SdsParameter -> declaration.evaluateParameter(substitutions)
// is SdsStep -> declaration.evaluateStep()
// else -> undefined
// }
return UnknownEvaluatedNode;
Expand Down Expand Up @@ -520,16 +498,6 @@ export class SafeDsPartialEvaluator {
// else -> undefined
// }
// }
//
// private fun SdsStep.evaluateStep(): SdsIntermediateStep? {
// return when {
// callableHasNoSideEffects(resultIfUnknown = true) -> SdsIntermediateStep(
// parameters = parametersOrEmpty(),
// results = resultsOrEmpty()
// )
// else -> undefined
// }
// }
}

const NO_SUBSTITUTIONS: ParameterSubstitutions = new Map();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tests.partialValidation.baseCases.expressionLambdas

pipeline test {
// $TEST$ serialization $ExpressionLambdaClosure
»() -> 1«;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tests.partialValidation.baseCases.blockLambdas

pipeline test {
// $TEST$ serialization $BlockLambdaClosure
»() {}«;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tests.partialValidation.baseCases.segments

// $TEST$ serialization $SegmentClosure
»segment mySegment() {}«

0 comments on commit b44ab6b

Please sign in to comment.