Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Strata/Languages/TypeScript/TS_to_Strata.lean
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,15 @@ partial def translate_statement_core
dbg_trace s!"[DEBUG] Function parameters: {funcDecl.params.toList.map (·.name)}"
dbg_trace s!"[DEBUG] Function body has statements"

let funcBody := match funcDecl.body with
| .TS_BlockStatement blockStmt =>
(blockStmt.body.toList.map (fun stmt => translate_statement_core stmt ctx ct |>.snd)).flatten
| _ => panic! s!"Expected block statement as function body, got: {repr funcDecl.body}"
let (bodyCtx, funcBody) := match funcDecl.body with
| .TS_BlockStatement blockStmt =>
-- Thread context through function body to handle nested functions
blockStmt.body.toList.foldl
(fun (accCtx, accStmts) stmt =>
let (newCtx, stmts) := translate_statement_core stmt accCtx ct
(newCtx, accStmts ++ stmts))
(ctx, [])
| _ => panic! s!"Expected block statement as function body, got: {repr funcDecl.body}"

dbg_trace s!"[DEBUG] Translated function body to {funcBody.length} Strata statements"

Expand All @@ -242,7 +247,8 @@ partial def translate_statement_core
body := funcBody,
returnType := none -- We'll infer this later if needed
}
let newCtx := ctx.addFunction strataFunc
-- Use bodyCtx which includes any nested function declarations
let newCtx := bodyCtx.addFunction strataFunc
dbg_trace s!"[DEBUG] Added TypeScript function '{funcDecl.id.name}' to context"
-- Function definitions don't generate statements themselves, just update context
(newCtx, [])
Expand Down
9 changes: 9 additions & 0 deletions StrataTest/Languages/TypeScript/test_nested_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function op1(x: number, y: number): number {
function op2(a: number, b: number): number {
return a + b;
}
let returnVal: number = op2(x, y);
return returnVal
}

let result: number = op1(4, 7);