Skip to content

Commit

Permalink
[#654] minor: formatted catch output
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzholzbauer committed Aug 28, 2024
1 parent 4d0d7f8 commit 387eaf7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
29 changes: 19 additions & 10 deletions kipper/target-js/src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
BitwiseXorExpression,
BoolPrimaryExpression,
CastOrConvertExpression,
CatchBlock,
ClassConstructorDeclaration,
ClassDeclaration,
ClassMethodDeclaration,
Expand Down Expand Up @@ -474,29 +475,37 @@ export class JavaScriptTargetCodeGenerator extends KipperTargetCodeGenerator {
return [["return", ...(returnValue ? [" ", ...returnValue] : []), ";"]];
};

generateCatchIfCondition = async (catchBlock: CatchBlock): Promise<Array<TranslatedCodeLine>> => {
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
*/
tryCatchStatement = async (node: TryCatchStatement): Promise<Array<TranslatedCodeLine>> => {
const semanticData = node.getSemanticData();
const tryBlock = await semanticData.tryBlock.translateCtxAndChildren();
const catchBlocks = <TranslatedCodeLine[][]>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] : []),
];
};

Expand Down
12 changes: 9 additions & 3 deletions test/kipper-files/trycatch.kip
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
interface Exception {
a: str;
b: num;
class Exception {

}

class Exception2 {

}

var x: num = 1;
Expand All @@ -10,6 +13,9 @@ try{
catch(e: Exception) {
x = 3;
}
catch(e: Exception2) {
x = 4;
}
finally {
x = 5;
}
Expand Down

0 comments on commit 387eaf7

Please sign in to comment.