diff --git a/kipper/core/src/compiler/ast/nodes/statements/try-catch-statement/try-catch-statement.ts b/kipper/core/src/compiler/ast/nodes/statements/try-catch-statement/try-catch-statement.ts index b47fdca0d..482e182fe 100644 --- a/kipper/core/src/compiler/ast/nodes/statements/try-catch-statement/try-catch-statement.ts +++ b/kipper/core/src/compiler/ast/nodes/statements/try-catch-statement/try-catch-statement.ts @@ -10,6 +10,7 @@ import type { Expression } from "../../expressions"; import type { CompilableNodeParent } from "../../../compilable-ast-node"; import type { TryCatchStatementTypeSemantics } from "./try-catch-statement-type-semantics"; import type { CatchBlock, TryCatchStatementSemantics } from "./try-catch-statement-semantics"; +import type { ParameterDeclaration } from "../../declarations"; /** * TryCatchStatement class, which represents try-catch statements in the Kipper language and is compilable using @@ -94,17 +95,24 @@ export class TryCatchStatement extends Statement { const tryBlock: Statement = this._children[0]; - const catchClausesWithFinally: Array = >( + const catchClausesWithFinally: Array = >( this._children.slice(1) ); let catchClauses: CatchBlock[] = []; let finallyBlock: Statement | undefined = undefined; - if (catchClausesWithFinally[catchClausesWithFinally.length - 1] instanceof Statement) { + // In the catchClausesWithFinally array, there are always a pair of parameterdeclarations and compoundstatements because of the grammar. + // When the length is odd, it means that there is a finally block. + if (catchClausesWithFinally.length % 2 == 1) { finallyBlock = catchClausesWithFinally.pop(); - catchClauses = catchClausesWithFinally; - } else { - catchClauses = catchClausesWithFinally; + } + + for (let i = 0; i < catchClausesWithFinally.length; i += 2) { + const catchClause: CatchBlock = { + parameter: catchClausesWithFinally[i], + body: catchClausesWithFinally[i + 1], + }; + catchClauses.push(catchClause); } this.semanticData = { diff --git a/test/kipper-files/trycatch.kip b/test/kipper-files/trycatch.kip index db4c73e24..686eb997f 100644 --- a/test/kipper-files/trycatch.kip +++ b/test/kipper-files/trycatch.kip @@ -2,6 +2,7 @@ interface Exception { a: str; b: num; } + var x: num = 1; try{ x = 2; @@ -10,7 +11,7 @@ catch(e: Exception) { x = 3; } finally { - x = 4; + x = 5; } print(x);