Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: info if elvis operator is unnecessary #645

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/language/builtins/safe-ds-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { URI } from 'langium';
import { SafeDsServices } from '../safe-ds-module.js';
import { SafeDsNodeMapper } from '../helpers/safe-ds-node-mapper.js';
import { toConstantExpressionOrUndefined } from '../partialEvaluation/toConstantExpressionOrUndefined.js';
import { SdsConstantExpression, SdsConstantString } from '../partialEvaluation/model.js';
import { ConstantExpression, ConstantString } from '../partialEvaluation/model.js';

const ANNOTATION_USAGE_URI = resourceNameToUri('builtins/safeds/lang/annotationUsage.sdsstub');
const CODE_GENERATION_URI = resourceNameToUri('builtins/safeds/lang/codeGeneration.sdsstub');
Expand Down Expand Up @@ -48,7 +48,7 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {

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

getPythonName(node: SdsAnnotatedObject | undefined): string | undefined {
const value = this.getArgumentValue(node, this.PythonName, 'name');
if (value instanceof SdsConstantString) {
if (value instanceof ConstantString) {
return value.value;
} else {
return undefined;
Expand Down Expand Up @@ -92,7 +92,7 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {
node: SdsAnnotatedObject | undefined,
annotation: SdsAnnotation | undefined,
parameterName: string,
): SdsConstantExpression | undefined {
): ConstantExpression | undefined {
const annotationCall = findFirstAnnotationCallOf(node, annotation);
const expression = argumentsOrEmpty(annotationCall).find(
(it) => this.nodeMapper.argumentToParameterOrUndefined(it)?.name === parameterName,
Expand Down
76 changes: 39 additions & 37 deletions src/language/partialEvaluation/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import {
} from '../generated/ast.js';

/* c8 ignore start */
export type ParameterSubstitutions = Map<SdsParameter, SdsSimplifiedExpression | undefined>;
export type ResultSubstitutions = Map<SdsAbstractResult, SdsSimplifiedExpression | undefined>;
export type ParameterSubstitutions = Map<SdsParameter, SimplifiedExpression | undefined>;
export type ResultSubstitutions = Map<SdsAbstractResult, SimplifiedExpression | undefined>;

export abstract class SdsSimplifiedExpression {
export abstract class SimplifiedExpression {
/**
* Removes any unnecessary containers from the expression.
*/
unwrap(): SdsSimplifiedExpression {
unwrap(): SimplifiedExpression {
return this;
}
}

export abstract class SdsIntermediateExpression extends SdsSimplifiedExpression {}
export abstract class IntermediateExpression extends SimplifiedExpression {}

export abstract class SdsIntermediateCallable extends SdsIntermediateExpression {}
export abstract class IntermediateCallable extends IntermediateExpression {}

export class SdsIntermediateBlockLambda extends SdsIntermediateCallable {
export class IntermediateBlockLambda extends IntermediateCallable {
constructor(
readonly parameters: SdsParameter[],
readonly results: SdsBlockLambdaResult[],
Expand All @@ -36,7 +36,7 @@ export class SdsIntermediateBlockLambda extends SdsIntermediateCallable {
}
}

export class SdsIntermediateExpressionLambda extends SdsIntermediateCallable {
export class IntermediateExpressionLambda extends IntermediateCallable {
constructor(
readonly parameters: SdsParameter[],
readonly result: SdsExpression,
Expand All @@ -46,7 +46,7 @@ export class SdsIntermediateExpressionLambda extends SdsIntermediateCallable {
}
}

export class SdsIntermediateStep extends SdsIntermediateCallable {
export class IntermediateStep extends IntermediateCallable {
constructor(
readonly parameters: SdsParameter[],
readonly results: SdsResult[],
Expand All @@ -55,12 +55,12 @@ export class SdsIntermediateStep extends SdsIntermediateCallable {
}
}

export class SdsIntermediateRecord extends SdsIntermediateExpression {
export class IntermediateRecord extends IntermediateExpression {
constructor(readonly resultSubstitutions: ResultSubstitutions) {
super();
}

getSubstitutionByReferenceOrNull(reference: SdsReference): SdsSimplifiedExpression | null {
getSubstitutionByReferenceOrNull(reference: SdsReference): SimplifiedExpression | null {
const referencedDeclaration = reference.target;
if (!isSdsAbstractResult(referencedDeclaration)) {
return null;
Expand All @@ -69,7 +69,7 @@ export class SdsIntermediateRecord extends SdsIntermediateExpression {
return this.resultSubstitutions.get(referencedDeclaration) ?? null;
}

getSubstitutionByIndexOrNull(index: number | null): SdsSimplifiedExpression | null {
getSubstitutionByIndexOrNull(index: number | null): SimplifiedExpression | null {
if (index === null) {
return null;
}
Expand All @@ -79,7 +79,7 @@ export class SdsIntermediateRecord extends SdsIntermediateExpression {
/**
* If the record contains exactly one substitution its value is returned. Otherwise, it returns `this`.
*/
override unwrap(): SdsSimplifiedExpression {
override unwrap(): SimplifiedExpression {
if (this.resultSubstitutions.size === 1) {
return this.resultSubstitutions.values().next().value;
} else {
Expand All @@ -95,21 +95,21 @@ export class SdsIntermediateRecord extends SdsIntermediateExpression {
}
}

export class SdsIntermediateVariadicArguments extends SdsIntermediateExpression {
constructor(readonly arguments_: (SdsSimplifiedExpression | null)[]) {
export class IntermediateVariadicArguments extends IntermediateExpression {
constructor(readonly arguments_: (SimplifiedExpression | null)[]) {
super();
}

getArgumentByIndexOrNull(index: number | null): SdsSimplifiedExpression | null {
getArgumentByIndexOrNull(index: number | null): SimplifiedExpression | null {
if (index === null) {
return null;
}
return this.arguments_[index] ?? null;
}
}

export abstract class SdsConstantExpression extends SdsSimplifiedExpression {
abstract equals(other: SdsConstantExpression): boolean;
export abstract class ConstantExpression extends SimplifiedExpression {
abstract equals(other: ConstantExpression): boolean;

abstract override toString(): string;

Expand All @@ -121,81 +121,83 @@ export abstract class SdsConstantExpression extends SdsSimplifiedExpression {
}
}

export class SdsConstantBoolean extends SdsConstantExpression {
export class ConstantBoolean extends ConstantExpression {
constructor(readonly value: boolean) {
super();
}

equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantBoolean && this.value === other.value;
equals(other: ConstantExpression): boolean {
return other instanceof ConstantBoolean && this.value === other.value;
}

toString(): string {
return this.value.toString();
}
}

export class SdsConstantEnumVariant extends SdsConstantExpression {
export class ConstantEnumVariant extends ConstantExpression {
constructor(readonly value: SdsEnumVariant) {
super();
}

equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantEnumVariant && this.value === other.value;
equals(other: ConstantExpression): boolean {
return other instanceof ConstantEnumVariant && this.value === other.value;
}

toString(): string {
return this.value.name;
}
}

export abstract class SdsConstantNumber extends SdsConstantExpression {}
export abstract class ConstantNumber extends ConstantExpression {}

export class SdsConstantFloat extends SdsConstantNumber {
export class ConstantFloat extends ConstantNumber {
constructor(readonly value: number) {
super();
}

equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantFloat && this.value === other.value;
equals(other: ConstantExpression): boolean {
return other instanceof ConstantFloat && this.value === other.value;
}

toString(): string {
return this.value.toString();
}
}

export class SdsConstantInt extends SdsConstantNumber {
export class ConstantInt extends ConstantNumber {
constructor(readonly value: bigint) {
super();
}

equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantInt && this.value === other.value;
equals(other: ConstantExpression): boolean {
return other instanceof ConstantInt && this.value === other.value;
}

toString(): string {
return this.value.toString();
}
}

export class SdsConstantNull extends SdsConstantExpression {
equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantNull;
class ConstantNullClass extends ConstantExpression {
equals(other: ConstantExpression): boolean {
return other instanceof ConstantNullClass;
}

toString(): string {
return 'null';
}
}

export class SdsConstantString extends SdsConstantExpression {
export const ConstantNull = new ConstantNullClass();

export class ConstantString extends ConstantExpression {
constructor(readonly value: string) {
super();
}

equals(other: SdsConstantExpression): boolean {
return other instanceof SdsConstantString && this.value === other.value;
equals(other: ConstantExpression): boolean {
return other instanceof ConstantString && this.value === other.value;
}

toString(): string {
Expand Down
Loading