Initial unfinished work on generic functions

This commit is contained in:
Mattia Giambirtone 2022-06-20 09:39:54 +02:00
parent 95880b7ba2
commit 985ceed075
1 changed files with 17 additions and 4 deletions

View File

@ -53,6 +53,10 @@ type
case kind: TypeKind:
of Function:
name: string
# Unfortunately we need to pollute
# the type system with AST nodes due
# to how we handle generics
funNode: FunDecl
isLambda: bool
isGenerator: bool
isCoroutine: bool
@ -946,6 +950,15 @@ proc generateCall(self: Compiler, fn: Name, args: seq[Expression]) =
if fn.valueType.isBuiltinFunction:
self.handleBuiltinFunction(fn, args)
return
if any(fn.valueType.args, proc (arg: tuple[name: string, kind: Type]): bool = arg[1].kind == Generic):
# The function has generic arguments! We need to compile a version
# of it with the right type data
# We don't want to cause *any* interference to
# other objects, so we just play it safe
var node = fn.valueType.funNode.deepCopy()
for argument in node.arguments:
self.emitFunction(fn)
self.emitByte(LoadReturnAddress)
let pos = self.chunk.code.len()
@ -1080,9 +1093,9 @@ proc declareName(self: Compiler, node: Declaration, mutable: bool = false) =
owner: self.currentModule,
valueType: Type(kind: Function,
name: node.name.token.lexeme,
returnType: self.inferType(
node.returnType),
args: @[]),
returnType: self.inferType(node.returnType),
args: @[],
funNode: node),
codePos: self.chunk.code.len(),
name: node.name,
isLet: false,
@ -1687,7 +1700,7 @@ proc funDecl(self: Compiler, node: FunDecl) =
## Compiles function declarations
var function = self.currentFunction
self.declareName(node)
if node.generics.len() > 0:
if node.generics.len() < 0:
# We can't know the type of
# generic arguments yet, so
# we wait for the function to