From d5ce459413ca67c11bfbee5cce497898002181fb Mon Sep 17 00:00:00 2001 From: yihong Date: Wed, 17 Apr 2024 13:33:30 +0800 Subject: [PATCH] fix: for statement init declarations (#37) --- src/go-slang/goroutine.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/go-slang/goroutine.ts b/src/go-slang/goroutine.ts index 376bfa7dc..99aed52e9 100644 --- a/src/go-slang/goroutine.ts +++ b/src/go-slang/goroutine.ts @@ -209,13 +209,21 @@ const Interpreter: { ) } else if (form.type === ForFormType.ForClause) { const { init, cond, post } = form + const initDeclIds = init && init.type === NodeType.VariableDeclaration ? init.left : [] const forCond = { type: NodeType.ForStatement, form: { type: ForFormType.ForCondition, expression: cond ?? True }, block: { type: NodeType.Block, statements: [ - { ...forBlock, statements: forBlock.statements.concat(ForPostMarker()) }, + { + type: NodeType.Block, + statements: [ + // eqv to: id = id (copy of init declarations with the current values) + { type: NodeType.VariableDeclaration, left: initDeclIds, right: initDeclIds }, + ...forBlock.statements.concat(ForPostMarker()) + ] + }, post ?? EmptyStmt ] }