Skip to content

Commit

Permalink
refactor: generalize SegmentClosure to NamedCallable
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Nov 6, 2023
1 parent 217011b commit de19da4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
46 changes: 20 additions & 26 deletions packages/safe-ds-lang/src/language/partialEvaluation/model.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { stream } from 'langium';
import { type NamedAstNode, stream } from 'langium';
import { isEmpty } from '../../helpers/collectionUtils.js';
import {
isSdsAbstractResult,
SdsAbstractResult,
type SdsAbstractResult,
type SdsBlockLambda,
SdsBlockLambdaResult,
type SdsBlockLambdaResult,
type SdsCallable,
type SdsDeclaration,
SdsEnumVariant,
SdsExpression,
type SdsEnumVariant,
type SdsExpression,
type SdsExpressionLambda,
SdsParameter,
SdsReference,
SdsResult,
type SdsSegment,
type SdsLambda,
type SdsParameter,
type SdsReference,
} from '../generated/ast.js';
import { getParameters, getResults, streamBlockLambdaResults } from '../helpers/nodeProperties.js';
import { getParameters, streamBlockLambdaResults } from '../helpers/nodeProperties.js';

export type ParameterSubstitutions = Map<SdsParameter, EvaluatedNode>;
export type ResultSubstitutions = Map<SdsAbstractResult, EvaluatedNode>;
Expand Down Expand Up @@ -143,12 +142,15 @@ export const isConstant = (node: EvaluatedNode): node is Constant => {
};

// -------------------------------------------------------------------------------------------------
// Closures
// Callables
// -------------------------------------------------------------------------------------------------

export abstract class Closure<T extends SdsCallable> extends EvaluatedNode {
override readonly isFullyEvaluated: boolean = false;
export abstract class EvaluatedCallable<T extends SdsCallable> extends EvaluatedNode {
abstract readonly callable: T;
override readonly isFullyEvaluated: boolean = false;
}

export abstract class Closure<T extends SdsLambda> extends EvaluatedCallable<T> {
abstract readonly substitutionsOnCreation: ParameterSubstitutions;
}

Expand Down Expand Up @@ -210,27 +212,19 @@ export class ExpressionLambdaClosure extends Closure<SdsExpressionLambda> {
}
}

export class SegmentClosure extends Closure<SdsSegment> {
override readonly substitutionsOnCreation = new Map<SdsParameter, EvaluatedNode>();
readonly results: SdsResult[];
export class NamedCallable<T extends SdsCallable & NamedAstNode> extends EvaluatedCallable<T> {
override readonly isFullyEvaluated: boolean = false;

constructor(override readonly callable: SdsSegment) {
constructor(override readonly callable: T) {
super();
this.results = getResults(callable.resultList);
}

override equals(other: unknown): boolean {
if (other === this) {
return true;
} else if (!(other instanceof SegmentClosure)) {
return false;
}

return this.callable === other.callable;
return other instanceof NamedCallable && this.callable === other.callable;
}

override toString(): string {
return `$SegmentClosure`;
return this.callable.name;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ import {
FloatConstant,
IntConstant,
isConstant,
NamedCallable,
NullConstant,
NumberConstant,
ParameterSubstitutions,
SegmentClosure,
StringConstant,
UnknownEvaluatedNode,
} from './model.js';
Expand Down Expand Up @@ -103,7 +103,7 @@ export class SafeDsPartialEvaluator {
if (isSdsExpression(node)) {
return this.evaluateExpression(node, substitutions);
} else if (isSdsSegment(node)) {
return new SegmentClosure(node);
return new NamedCallable(node);
} else {
return UnknownEvaluatedNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
ExpressionLambdaClosure,
FloatConstant,
IntConstant,
NamedCallable,
NullConstant,
SegmentClosure,
StringConstant,
UnknownEvaluatedNode,
} from '../../../src/language/partialEvaluation/model.js';
Expand Down Expand Up @@ -103,8 +103,8 @@ describe('partial evaluation model', async () => {
nodeOfOtherType: () => NullConstant,
},
{
node: () => new SegmentClosure(segment1),
unequalNodeOfSameType: () => new SegmentClosure(segment2),
node: () => new NamedCallable(segment1),
unequalNodeOfSameType: () => new NamedCallable(segment2),
nodeOfOtherType: () => NullConstant,
},
{
Expand Down Expand Up @@ -190,8 +190,8 @@ describe('partial evaluation model', async () => {
expectedString: '$ExpressionLambdaClosure',
},
{
node: new SegmentClosure(segment1),
expectedString: '$SegmentClosure',
node: new NamedCallable(segment1),
expectedString: 'mySegment',
},
{
node: new EvaluatedEnumVariant(enumVariantWithoutParameters, undefined),
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('partial evaluation model', async () => {
expectedValue: false,
},
{
node: new SegmentClosure(segment1),
node: new NamedCallable(segment1),
expectedValue: false,
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests.partialValidation.baseCases.segments

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

0 comments on commit de19da4

Please sign in to comment.