Skip to content

Commit

Permalink
feat: basic implementation of partial evaluator service (#649)
Browse files Browse the repository at this point in the history
Closes partially #603

### Summary of Changes

* Add partial evaluator service. This replaces the old
`toConstantExpressionOrUndefined` function.
* Improve the model classes that represent evaluation results.
* Evaluate more types of expressions.
* Add more tests.

Still TODO in a future PR: Add tests for the model classes, particularly
for their `toString` and `equals` methods, and include them in the
coverage computation.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored Oct 19, 2023
1 parent d76e597 commit 10ed8bf
Show file tree
Hide file tree
Showing 67 changed files with 2,179 additions and 790 deletions.
2 changes: 1 addition & 1 deletion docs/development/partial-evaluation-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ partial evaluation test.
test marker, the second test comment corresponds to the second test marker, etc.
* `// $TEST$ constant equivalence_class <id>`: Assert that all nodes with the same `<id>` get partially evaluated
successfully to the same constant expression.
* `// $TEST$ constant serialization <value>`: Assert that the node gets partially evaluated to a constant expression
* `// $TEST$ serialization <value>`: Assert that the node gets partially evaluated to a constant expression
that serializes to `<value>`.
* `// $TEST$ not constant`: Assert that the node cannot be evaluated to a constant expression.
6. Run the tests. The test runner will automatically pick up the new test.
32 changes: 32 additions & 0 deletions img/safe-ds_file_icon_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions img/safe-ds_file_icon_light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img/safe-ds_logo_rounded.png
Binary file not shown.
Binary file added img/safe-ds_logo_rounded_128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"galleryBanner": {
"color": "#e3e9e9"
},
"icon": "img/safe-ds_logo_rounded.png",
"icon": "img/safe-ds_logo_rounded_128x128.png",
"homepage": "https://dsl.safeds.com",
"qna": "https://github.com/orgs/Safe-DS/discussions",
"bugs": {
Expand Down Expand Up @@ -60,7 +60,11 @@
".sdsstub",
".sdstest"
],
"configuration": "./language-configuration.json"
"configuration": "./language-configuration.json",
"icon": {
"light": "img/safe-ds_file_icon_light.svg",
"dark": "img/safe-ds_file_icon_dark.svg"
}
}
],
"grammars": [
Expand Down
16 changes: 9 additions & 7 deletions src/language/builtins/safe-ds-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { resourceNameToUri } from '../../helpers/resources.js';
import { URI } from 'langium';
import { SafeDsServices } from '../safe-ds-module.js';
import { SafeDsNodeMapper } from '../helpers/safe-ds-node-mapper.js';
import { toConstantExpression } from '../partialEvaluation/toConstantExpression.js';
import { ConstantExpression, ConstantString } from '../partialEvaluation/model.js';
import { EvaluatedNode, StringConstant } from '../partialEvaluation/model.js';
import { SafeDsPartialEvaluator } from '../partialEvaluation/safe-ds-partial-evaluator.js';

const ANNOTATION_USAGE_URI = resourceNameToUri('builtins/safeds/lang/annotationUsage.sdsstub');
const CODE_GENERATION_URI = resourceNameToUri('builtins/safeds/lang/codeGeneration.sdsstub');
Expand All @@ -15,11 +15,13 @@ const MATURITY_URI = resourceNameToUri('builtins/safeds/lang/maturity.sdsstub');

export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {
private readonly nodeMapper: SafeDsNodeMapper;
private readonly partialEvaluator: SafeDsPartialEvaluator;

constructor(services: SafeDsServices) {
super(services);

this.nodeMapper = services.helpers.NodeMapper;
this.partialEvaluator = services.evaluation.PartialEvaluator;
}

isDeprecated(node: SdsAnnotatedObject | undefined): boolean {
Expand Down Expand Up @@ -48,7 +50,7 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {

getPythonModule(node: SdsModule | undefined): string | undefined {
const value = this.getArgumentValue(node, this.PythonModule, 'qualifiedName');
if (value instanceof ConstantString) {
if (value instanceof StringConstant) {
return value.value;
} else {
return undefined;
Expand All @@ -61,7 +63,7 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {

getPythonName(node: SdsAnnotatedObject | undefined): string | undefined {
const value = this.getArgumentValue(node, this.PythonName, 'name');
if (value instanceof ConstantString) {
if (value instanceof StringConstant) {
return value.value;
} else {
return undefined;
Expand Down Expand Up @@ -92,11 +94,11 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {
node: SdsAnnotatedObject | undefined,
annotation: SdsAnnotation | undefined,
parameterName: string,
): ConstantExpression | undefined {
): EvaluatedNode {
const annotationCall = findFirstAnnotationCallOf(node, annotation);
const expression = argumentsOrEmpty(annotationCall).find(
const argumentValue = argumentsOrEmpty(annotationCall).find(
(it) => this.nodeMapper.argumentToParameterOrUndefined(it)?.name === parameterName,
)?.value;
return toConstantExpression(expression);
return this.partialEvaluator.evaluate(argumentValue);
}
}
Loading

0 comments on commit 10ed8bf

Please sign in to comment.