Skip to content

Commit

Permalink
fix: yielded placeholders in block lambdas are now assigned a prefix
Browse files Browse the repository at this point in the history
fix: return correct code for enum variant expression

fix: handle BlockLambdaResult in SdsMemberAccess

test: updated "expressions/block lambda" and "assignment" tests
  • Loading branch information
WinPlay02 committed Oct 19, 2023
1 parent 564896a commit bcf9fb5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
26 changes: 17 additions & 9 deletions src/cli/generator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import fs from 'fs';
import { expandToString, expandToStringWithNL, findRootNode, getDocument, streamAllContents } from 'langium';
import {
expandToString,
expandToStringWithNL,
findRootNode,
getContainerOfType,
getDocument,
streamAllContents,
} from 'langium';
import path from 'path';
import {
isSdsAbstractResult,
isSdsAssignment,
isSdsBlockLambda,
isSdsBlockLambdaResult,
isSdsCall,
isSdsCallable,
isSdsEnum,
isSdsEnumVariant,
isSdsExpressionLambda,
isSdsExpressionStatement,
Expand All @@ -21,7 +31,6 @@ import {
isSdsPrefixOperation,
isSdsQualifiedImport,
isSdsReference,
isSdsResult,
isSdsSegment,
isSdsTemplateString,
isSdsTemplateStringEnd,
Expand Down Expand Up @@ -203,7 +212,6 @@ const generateParameter = function (
frame: GenerationInfoFrame,
defaultValue: boolean = true,
): string {
// TODO isConstant?
return expandToString`${getPythonNameOrDefault(frame.getServices(), parameter)}${
defaultValue && parameter.defaultValue !== undefined
? '=' + generateExpression(parameter.defaultValue, frame)
Expand Down Expand Up @@ -310,7 +318,7 @@ const generateAssignment = function (assignment: SdsAssignment, frame: Generatio

const generateAssignee = function (assignee: SdsAssignee): string {
if (isSdsBlockLambdaResult(assignee)) {
return assignee.name;
return `__block_lambda_${assignee.name}`;
} else if (isSdsPlaceholder(assignee)) {
return assignee.name;
} else if (isSdsWildcard(assignee)) {
Expand All @@ -325,7 +333,7 @@ const generateBlockLambda = function (blockLambda: SdsBlockLambda, frame: Genera
const lambdaResult = blockLambdaResultsOrEmpty(blockLambda);
let lambdaBlock = generateBlock(blockLambda.body, frame);
if (lambdaResult.length !== 0 && lambdaBlock !== 'pass') {
lambdaBlock += `\nreturn ${lambdaResult.map((result) => result.name).join(', ')}`;
lambdaBlock += `\nreturn ${lambdaResult.map((result) => `__block_lambda_${result.name}`).join(', ')}`;
}
return expandToString`def ${frame.getUniqueLambdaBlockName(blockLambda)}(${generateParameters(
blockLambda.parameterList,
Expand Down Expand Up @@ -375,7 +383,8 @@ const generateExpression = function (expression: SdsExpression, frame: Generatio
} else if (potentialConstantExpression instanceof ConstantString) {
return `'${formatStringSingleLine(potentialConstantExpression.value)}'`;
} else if (potentialConstantExpression instanceof ConstantEnumVariant) {
return potentialConstantExpression.value.name; // TODO correct interpretation of SdsEnumVariant / SdsConstantEnumVariant?
const enumType = getContainerOfType(potentialConstantExpression.value, isSdsEnum);
return `${enumType!.name}.${potentialConstantExpression.value.name}`;
}

Check warning on line 388 in src/cli/generator.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/generator.ts#L386-L388

Added lines #L386 - L388 were not covered by tests
}

Expand Down Expand Up @@ -424,7 +433,6 @@ const generateExpression = function (expression: SdsExpression, frame: Generatio
if (isSdsMemberAccess(expression)) {
const member = expression.member?.target.ref!;
const receiver = generateExpression(expression.receiver, frame);
// TODO SdsBlockLambdaResult should not be possible? Grammar defines SdsBlockLambdaResult as only part of an assignment. There is no way to parse it anywhere else
if (isSdsEnumVariant(member)) {
const enumMember = generateExpression(expression.member!, frame);
const suffix = isSdsCall(expression.$container) ? '' : '()';
Expand All @@ -434,8 +442,8 @@ const generateExpression = function (expression: SdsExpression, frame: Generatio
} else {
return `${receiver}.${enumMember}${suffix}`;
}
} else if (isSdsResult(member)) {
const resultList = member.$container.results;
} else if (isSdsAbstractResult(member)) {
const resultList = abstractResultsOrEmpty(getContainerOfType(member, isSdsCallable));
if (resultList.length === 1) {
return receiver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

def test():
def __block_lambda_0(a, b=2):
d = g()
return d
__block_lambda_d = g()
return __block_lambda_d
f1(__block_lambda_0)
def __block_lambda_1(a, c):
d = g()
return d
__block_lambda_d = g()
return __block_lambda_d
f2(__block_lambda_1)
def __block_lambda_2():
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def testPipeline():
f1(x)
def __block_lambda_0():
g()
a, _, c = g()
a, _, __block_lambda_c = g()
x, _, _ = g()
f1(a)
f1(x)
return c
return __block_lambda_c
f2(__block_lambda_0)

0 comments on commit bcf9fb5

Please sign in to comment.