From 985ceed075e071819326355781c2080572febe8d Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Mon, 20 Jun 2022 09:39:54 +0200 Subject: [PATCH] Initial unfinished work on generic functions --- src/frontend/compiler.nim | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 8464f63..5ba6d56 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -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