()
| 32 | } |
| 33 | |
| 34 | override execute(): null { |
| 35 | const switchExpression = tcbExpression(this.block.expression, this.tcb, this.scope); |
| 36 | const clauses = this.block.groups.flatMap<TcbExpr>((current) => { |
| 37 | const checkBody = this.tcb.env.config.checkControlFlowBodies; |
| 38 | const clauseScope = this.scope.createChildScope( |
| 39 | this.scope, |
| 40 | null, |
| 41 | checkBody ? current.children : [], |
| 42 | checkBody ? this.generateGuard(current, switchExpression) : null, |
| 43 | ); |
| 44 | |
| 45 | const statements = [...clauseScope.render(), new TcbExpr('break')]; |
| 46 | |
| 47 | return current.cases.map((switchCase, index) => { |
| 48 | const statementsStr = getStatementsBlock( |
| 49 | index === current.cases.length - 1 ? statements : [], |
| 50 | true /* singleLine */, |
| 51 | ); |
| 52 | |
| 53 | const source = |
| 54 | switchCase.expression === null |
| 55 | ? `default: ${statementsStr}` |
| 56 | : `case ${tcbExpression(switchCase.expression, this.tcb, this.scope).print()}: ${statementsStr}`; |
| 57 | |
| 58 | return new TcbExpr(source); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | if (this.block.exhaustiveCheck) { |
| 63 | let translateExpression = this.block.expression; |
| 64 | if (this.block.exhaustiveCheck.expression) { |
| 65 | translateExpression = this.block.exhaustiveCheck.expression; |
| 66 | } |
| 67 | |
| 68 | const switchValue = tcbExpression(translateExpression, this.tcb, this.scope); |
| 69 | const exhaustiveId = this.tcb.allocateId(); |
| 70 | |
| 71 | clauses.push( |
| 72 | new TcbExpr(`default: const tcbExhaustive${exhaustiveId}: never = ${switchValue.print()};`), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | this.scope.addStatement( |
| 77 | new TcbExpr( |
| 78 | `switch (${switchExpression.print()}) { ${clauses.map((c) => c.print()).join('\n')} }`, |
| 79 | ), |
| 80 | ); |
| 81 | |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | private generateGuard(group: SwitchBlockCaseGroup, switchValue: TcbExpr): TcbExpr | null { |
| 86 | // For non-default cases, the guard needs to compare against the case value, e.g. |
nothing calls this directly
no test coverage detected