closures with captures: env struct + global instance + body rewriting

- AST: captureCount + captureName0..7 + captureType0..7 in Expr
- Sema: Scope_LookupUpTo + closureScope for capture analysis
- HIR Lower: env struct generation, capture assignments in skLet,
  body rewriting (ekIdent -> hFieldAccess on env instance)
- C Backend: env struct def + global instance emission for closures
- Bootstrap: full capture support in sema, hir_lower, lir_lower, lir_c_backend
- Selfhost loop remains deterministic
This commit is contained in:
2026-06-09 21:23:05 +03:00
parent ba54aa240e
commit 9b473c667c
17 changed files with 5267 additions and 53 deletions
+12
View File
@@ -23,6 +23,7 @@ type
hLoad
hStore
hFieldPtr
hFieldAccess
hArrowField
hIndexPtr
hSliceIndex
@@ -98,6 +99,9 @@ type
of hFieldPtr:
fieldPtrBase*: HirNode
fieldName*: string
of hFieldAccess:
fieldAccessBase*: HirNode
fieldAccessName*: string
of hArrowField:
arrowFieldBase*: HirNode
arrowFieldName*: string
@@ -166,6 +170,11 @@ type
retType*: Type
body*: HirNode
isPublic*: bool
# Closure capture metadata
captureNames*: seq[string]
captureTypes*: seq[Type]
envStructName*: string
envInstanceName*: string
HirEnumVariant* = object
name*: string
@@ -219,6 +228,9 @@ proc hirAlloca*(name: string, typ: Type, loc: SourceLocation): HirNode =
proc hirStore*(ptrNode, value: HirNode, loc: SourceLocation): HirNode =
HirNode(kind: hStore, storePtr: ptrNode, storeValue: value, typ: makeVoid(), loc: loc)
proc hirAssign*(target, value: HirNode, loc: SourceLocation): HirNode =
HirNode(kind: hAssign, assignOp: tkAssign, assignTarget: target, assignValue: value, typ: makeVoid(), loc: loc)
proc hirLoad*(ptrNode: HirNode, typ: Type, loc: SourceLocation): HirNode =
HirNode(kind: hLoad, loadPtr: ptrNode, typ: typ, loc: loc)