fix: C backend, parser, sema, HIR lowering for all examples
- C backend: strip c8/c16/c32 string prefixes in emitted C - C backend: resolve imports to fully-qualified names (Std_Io_PrintLine) - Parser: fix infinite loop on < in comparisons vs generic calls - Parser: fix enum patterns without parens (Option::None) - Parser: fix match expressions inside blocks - Sema: extract pattern bindings for match arms - HIR: lower match expressions to if-else chains with enum tag checks - HIR/C backend: support block expressions as function return values - Makefile: add integration tests for all 9 examples - Add pattern_matching.bux example
This commit is contained in:
+26
-1
@@ -211,6 +211,29 @@ proc collectGlobals*(sema: var Sema) =
|
||||
proc checkExpr(sema: var Sema, expr: Expr, scope: Scope): Type
|
||||
proc checkStmt(sema: var Sema, stmt: Stmt, scope: Scope): Type
|
||||
|
||||
proc extractPatternBindings(sema: var Sema, pat: Pattern, scope: Scope) =
|
||||
## Add pattern-bound identifiers to scope with unknown type (best-effort)
|
||||
if pat == nil: return
|
||||
case pat.kind
|
||||
of pkIdent:
|
||||
let sym = Symbol(kind: skVar, name: pat.patIdent, typ: makeUnknown(), isMutable: false)
|
||||
discard scope.define(sym)
|
||||
of pkEnum:
|
||||
for arg in pat.patEnumArgs:
|
||||
sema.extractPatternBindings(arg, scope)
|
||||
for nf in pat.patEnumNamed:
|
||||
sema.extractPatternBindings(nf.pattern, scope)
|
||||
of pkTuple:
|
||||
for elem in pat.patTupleElements:
|
||||
sema.extractPatternBindings(elem, scope)
|
||||
of pkStruct:
|
||||
for f in pat.patStructFields:
|
||||
sema.extractPatternBindings(f.pattern, scope)
|
||||
of pkGuarded:
|
||||
sema.extractPatternBindings(pat.patGuardedInner, scope)
|
||||
else:
|
||||
discard
|
||||
|
||||
proc checkExprList(sema: var Sema, exprs: seq[Expr], scope: Scope): seq[Type] =
|
||||
for e in exprs:
|
||||
result.add(sema.checkExpr(e, scope))
|
||||
@@ -522,7 +545,9 @@ proc checkExpr(sema: var Sema, expr: Expr, scope: Scope): Type =
|
||||
let subjectType = sema.checkExpr(expr.exprMatchSubject, scope)
|
||||
var resultType = makeUnknown()
|
||||
for arm in expr.exprMatchArms:
|
||||
let armType = sema.checkExpr(arm.body, scope)
|
||||
var armScope = newScope(scope)
|
||||
sema.extractPatternBindings(arm.pattern, armScope)
|
||||
let armType = sema.checkExpr(arm.body, armScope)
|
||||
if resultType.isUnknown:
|
||||
resultType = armType
|
||||
elif armType != resultType and not armType.isUnknown:
|
||||
|
||||
Reference in New Issue
Block a user