feat: add generics support with monomorphization
- Add ekGenericCall to AST for generic function calls (Max<int>)
- Parse generic type arguments in parsePostfix
- Support generic calls in sema with type parameter substitution
- Implement monomorphization in hir_lower:
- Collect generic function declarations
- Find all generic call sites
- Generate specialized versions with mangled names (Max_int)
- Substitute type parameters with concrete types
- Add generics.bux example
Example:
func Max<T>(a: T, b: T) -> T {
if a > b { return a; }
else { return b; }
}
let m: int = Max<int>(10, 20); // Generates Max_int
This commit is contained in:
@@ -441,6 +441,20 @@ proc parsePostfix(p: var Parser): Expr =
|
||||
discard p.advance()
|
||||
discard p.expect(tkRParen, "expected ')' to close call")
|
||||
left = Expr(kind: ekCall, loc: loc, exprCallCallee: left, exprCallArgs: args)
|
||||
of tkLt:
|
||||
# Generic type arguments: Max<int>(10, 20)
|
||||
if left.kind == ekIdent:
|
||||
discard p.advance()
|
||||
var typeArgs: seq[TypeExpr] = @[]
|
||||
while not p.check(tkGt) and not p.isAtEnd:
|
||||
typeArgs.add(p.parseType())
|
||||
if p.check(tkComma):
|
||||
discard p.advance()
|
||||
discard p.expect(tkGt, "expected '>' to close type arguments")
|
||||
# Store type args in the identifier for later use
|
||||
left = Expr(kind: ekGenericCall, loc: loc, exprGenericCallee: left.exprIdent, exprGenericTypeArgs: typeArgs)
|
||||
else:
|
||||
break
|
||||
of tkLBracket:
|
||||
# Index expression
|
||||
discard p.advance()
|
||||
|
||||
Reference in New Issue
Block a user