diff --git a/packages/ttsl-lang/src/language/generation/safe-ds-python-generator.ts b/packages/ttsl-lang/src/language/generation/safe-ds-python-generator.ts index 8e1feae4..a7d9e423 100644 --- a/packages/ttsl-lang/src/language/generation/safe-ds-python-generator.ts +++ b/packages/ttsl-lang/src/language/generation/safe-ds-python-generator.ts @@ -66,6 +66,8 @@ import { TslPlaceholder, TslSegment, TslStatement, + isTslConditionalStatement, + isTslLoop, } from '../generated/ast.js'; import { isInStubFile, isStubFile } from '../helpers/fileExtensions.js'; import { IdManager } from '../helpers/idManager.js'; @@ -606,7 +608,22 @@ export class SafeDsPythonGenerator { return joinTracedToNode(statement)(blockLambdaCode, (stmt) => stmt, { separator: NL, })!; - } + } else if (isTslConditionalStatement(statement)) { + for (const lambda of AstUtils.streamAllContents(statement.expression).filter(isTslBlockLambda)) { + blockLambdaCode.push(this.generateBlockLambda(lambda, frame)); + } + blockLambdaCode.push(this.generateExpression(statement.expression, frame)); + return joinTracedToNode(statement)(blockLambdaCode, (stmt) => stmt, { + separator: NL, + })!; + } else if (isTslLoop(statement)) { + for (const lambda of AstUtils.streamAllContents(statement.expression).filter(isTslBlockLambda)) { + blockLambdaCode.push(this.generateBlockLambda(lambda, frame)); + } + blockLambdaCode.push(this.generateExpression(statement.expression, frame)); + return joinTracedToNode(statement)(blockLambdaCode, (stmt) => stmt, { + separator: NL, + })!; /* c8 ignore next 2 */ throw new Error(`Unknown TslStatement: ${statement}`); } @@ -733,7 +750,7 @@ export class SafeDsPythonGenerator { } } - // Handled after constant expressions: EnumVariant, List, Map + // Handled after constant expressions: EnumVariant, List, Dictionary if (isTslTemplateString(expression)) { return expandTracedToNode(expression)`f'${joinTracedToNode(expression, 'expressions')( expression.expressions, diff --git a/packages/ttsl-lang/src/language/grammar/safe-ds.langium b/packages/ttsl-lang/src/language/grammar/safe-ds.langium index 8048fbe8..8b0c74c6 100644 --- a/packages/ttsl-lang/src/language/grammar/safe-ds.langium +++ b/packages/ttsl-lang/src/language/grammar/safe-ds.langium @@ -587,7 +587,7 @@ interface TslConditionalStatement extends TslStatement{ } TslConditionalStatement returns TslConditionalStatement: - 'if(' expression=TslOrExpression ')' ifBlock=TslBlock + 'if(' expression=TslExpression ')' ifBlock=TslBlock ('else' elseBlock=TslBlock)? ; @@ -606,7 +606,7 @@ interface TslWhileLoop extends TslLoop{ } TslWhileLoop returns TslWhileLoop: - 'while(' condition=TslOrExpression ')' block=TslBlock + 'while(' condition=TslExpression ')' block=TslBlock ; interface TslForLoop extends TslLoop{ @@ -617,18 +617,18 @@ interface TslForLoop extends TslLoop{ TslForLoop returns TslForLoop: 'for(' (definitionStatement=TslStatement)? - ';' condition=TslOrExpression + ';' condition=TslExpression ';' (iteration=TslStatement)? ')' block=TslBlock ; interface TslForeachLoop extends TslLoop{ element: string - list: @TslList + list: TslExpression } TslForeachLoop returns TslForeachLoop: - 'foreach(' element=STRING 'in' list=[TslList:ID] ')' + 'foreach(' element=STRING 'in' list=TslExpression ')' block=TslBlock ;