From 387eaf7dcb8c6f03be2208ccf0e985df0fbb43e3 Mon Sep 17 00:00:00 2001 From: Lorenz Holzbauer <73286868+lorenzholzbauer@users.noreply.github.com> Date: Wed, 28 Aug 2024 18:42:53 +0200 Subject: [PATCH] [#654] minor: formatted catch output --- kipper/target-js/src/code-generator.ts | 29 +++++++++++++++++--------- test/kipper-files/trycatch.kip | 12 ++++++++--- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/kipper/target-js/src/code-generator.ts b/kipper/target-js/src/code-generator.ts index b6e5f4ec1..8b23a5f99 100644 --- a/kipper/target-js/src/code-generator.ts +++ b/kipper/target-js/src/code-generator.ts @@ -13,6 +13,7 @@ import type { BitwiseXorExpression, BoolPrimaryExpression, CastOrConvertExpression, + CatchBlock, ClassConstructorDeclaration, ClassDeclaration, ClassMethodDeclaration, @@ -474,6 +475,16 @@ export class JavaScriptTargetCodeGenerator extends KipperTargetCodeGenerator { return [["return", ...(returnValue ? [" ", ...returnValue] : []), ";"]]; }; + generateCatchIfCondition = async (catchBlock: CatchBlock): Promise> => { + const parameterType = catchBlock.parameter.getTypeSemanticData().valueType.identifier; + const blockBody = await catchBlock.body.translateCtxAndChildren(); + const x = [ + ["if", " ", "(", "__e_1", " ", "instanceof", " ", parameterType, ")"], + ...blockBody + ]; + return x; + }; + /** * Translates a {@link TryCatchStatement} into the JavaScript language. * @since 0.12.0 @@ -481,22 +492,20 @@ export class JavaScriptTargetCodeGenerator extends KipperTargetCodeGenerator { tryCatchStatement = async (node: TryCatchStatement): Promise> => { const semanticData = node.getSemanticData(); const tryBlock = await semanticData.tryBlock.translateCtxAndChildren(); - const catchBlocks = await Promise.all( - semanticData.catchBlock.map(async (block) => { - const parameter = await block.parameter.translateCtxAndChildren(); - const body = await block.body.translateCtxAndChildren().then((lines) => indentLines(removeBrackets(lines))); - const parameterName = parameter.flat().join("").split(":")[0]; - // Tis is just a temporary solution until I implement the proper cases for the catch block - return ["catch", " ", "(", parameterName, ": unknown", ")", " ", "{", ...body, "}"]; + const catchBlocks = await Promise.all( + semanticData.catchBlock.map(async (block: CatchBlock) => { + return await this.generateCatchIfCondition(block); }), ); const finallyBlock = semanticData.finallyBlock ? await semanticData.finallyBlock.translateCtxAndChildren() : []; return [ ["try"], - ...indentLines(tryBlock), - catchBlocks.map((block) => block.flat()).flat(), - ...(finallyBlock.length > 0 ? [["finally"], ...indentLines(finallyBlock)] : []), + ...tryBlock, + ["catch", " ", "(", "__e_1", ": unknown", ")", " ", "{"], + ...indentLines(catchBlocks.flat()), + ["}"], + ...(finallyBlock.length > 0 ? [["finally"], ...finallyBlock] : []), ]; }; diff --git a/test/kipper-files/trycatch.kip b/test/kipper-files/trycatch.kip index 686eb997f..7fe1c154f 100644 --- a/test/kipper-files/trycatch.kip +++ b/test/kipper-files/trycatch.kip @@ -1,6 +1,9 @@ -interface Exception { - a: str; - b: num; +class Exception { + +} + +class Exception2 { + } var x: num = 1; @@ -10,6 +13,9 @@ try{ catch(e: Exception) { x = 3; } +catch(e: Exception2) { + x = 4; +} finally { x = 5; }