feat: struct and tuple patterns in match (bootstrap + selfhost)
Support destructuring in match arms:
- Tuple: (a, b) binds subject._0 / _1
- Struct: Point { x: px, y: py } and shorthand Point { x, y }
Bootstrap: matchPatternBindings for pkTuple/pkStruct; register local
tuple typedefs from function bodies. Selfhost: parse, Sema_BindPattern,
Lcx_PatternBindings with scope defines. Fix operator-overload path that
crashed when typeName was null after pattern binds.
Example: examples/struct_tuple_pat.bux. Selfhost-loop IDENTICAL.
This commit is contained in:
+17
-2
@@ -814,8 +814,23 @@ proc extractPatternBindings(sema: var Sema, pat: Pattern, scope: Scope, subjectT
|
||||
else:
|
||||
sema.extractPatternBindings(elem, scope, elemTy)
|
||||
of pkStruct:
|
||||
for f in pat.patStructFields:
|
||||
sema.extractPatternBindings(f.pattern, scope)
|
||||
# Resolve field types from struct declaration when possible
|
||||
var fieldTypes = initTable[string, Type]()
|
||||
var structName = pat.patStructName
|
||||
if structName.len == 0 and subjectType != nil and subjectType.kind == tkNamed:
|
||||
structName = subjectType.name
|
||||
if structName.len > 0:
|
||||
let ssym = sema.globalScope.lookup(structName)
|
||||
if ssym != nil and ssym.decl != nil and ssym.decl.kind == dkStruct:
|
||||
for f in ssym.decl.declStructFields:
|
||||
fieldTypes[f.name] = sema.resolveType(f.ftype)
|
||||
for entry in pat.patStructFields:
|
||||
let fty = if fieldTypes.hasKey(entry.name): fieldTypes[entry.name] else: makeUnknown()
|
||||
if entry.pattern != nil and entry.pattern.kind == pkIdent:
|
||||
let sym = Symbol(kind: skVar, name: entry.pattern.patIdent, typ: fty, isMutable: false)
|
||||
discard scope.define(sym)
|
||||
else:
|
||||
sema.extractPatternBindings(entry.pattern, scope, fty)
|
||||
of pkGuarded:
|
||||
sema.extractPatternBindings(pat.patGuardedInner, scope, subjectType)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user