Skip to content

Commit

Permalink
orExpressionts to Expressions in loops and conditional statement
Browse files Browse the repository at this point in the history
  • Loading branch information
robmeth committed Apr 22, 2024
1 parent ec99195 commit d9a8520
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions packages/ttsl-lang/src/language/grammar/safe-ds.langium
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ interface TslConditionalStatement extends TslStatement{
}

TslConditionalStatement returns TslConditionalStatement:
'if(' expression=TslOrExpression ')' ifBlock=TslBlock
'if(' expression=TslExpression ')' ifBlock=TslBlock
('else' elseBlock=TslBlock)?
;

Expand All @@ -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{
Expand All @@ -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
;

Expand Down

0 comments on commit d9a8520

Please sign in to comment.