diff --git a/src/frontend/compiler/targets/bytecode/target.nim b/src/frontend/compiler/targets/bytecode/target.nim index 1ea10c5..a325466 100644 --- a/src/frontend/compiler/targets/bytecode/target.nim +++ b/src/frontend/compiler/targets/bytecode/target.nim @@ -120,6 +120,7 @@ proc newBytecodeCompiler*(replMode: bool = false): BytecodeCompiler = result.depth = 0 result.lines = @[] result.jumps = @[] + result.modules = newTable[string, Name]() result.lambdas = @[] result.currentFunction = nil result.replMode = replMode @@ -1170,11 +1171,10 @@ method unary(self: BytecodeCompiler, node: UnaryExpr, compile: bool = true): Typ let fn = Type(kind: Function, returnType: Type(kind: Any), args: @[("", self.inferOrError(node.a), default)]) - let impl = self.match(node.token.lexeme, fn, node) - result = impl.valueType - if impl.isGeneric: - result = self.specialize(result, @[node.a]) - result = result.returnType + var impl = self.match(node.token.lexeme, fn, node) + if impl.valueType.isAuto: + impl = self.prepareAutoFunction(impl, fn.args) + result = impl.valueType.returnType if compile: self.generateCall(impl, @[node.a], impl.line) @@ -1183,11 +1183,10 @@ method binary(self: BytecodeCompiler, node: BinaryExpr, compile: bool = true): T ## Compiles all binary expressions var default: Expression let fn = Type(kind: Function, returnType: Type(kind: Any), args: @[("", self.inferOrError(node.a), default), ("", self.inferOrError(node.b), default)]) - let impl = self.match(node.token.lexeme, fn, node) - result = impl.valueType - if impl.isGeneric: - result = self.specialize(result, @[node.a, node.b]) - result = result.returnType + var impl = self.match(node.token.lexeme, fn, node) + if impl.valueType.isAuto: + impl = self.prepareAutoFunction(impl, fn.args) + result = impl.valueType.returnType if compile: self.generateCall(impl, @[node.a, node.b], impl.line) @@ -1280,7 +1279,7 @@ method assignment(self: BytecodeCompiler, node: ASTNode, compile: bool = true): method call(self: BytecodeCompiler, node: CallExpr, compile: bool = true): Type {.discardable.} = - ## Compiles code to call a chain of function calls + ## Compiles function calls var args: seq[tuple[name: string, kind: Type, default: Expression]] = @[] var argExpr: seq[Expression] = @[] var default: Expression @@ -1306,14 +1305,12 @@ method call(self: BytecodeCompiler, node: CallExpr, compile: bool = true): Type # Calls like hi() var impl = self.match(IdentExpr(node.callee).name.lexeme, Type(kind: Function, returnType: Type(kind: All), args: args), node) result = impl.valueType - if impl.isGeneric: - result = self.specialize(result, argExpr) if impl.valueType.isAuto: impl = self.prepareAutoFunction(impl, args) result = impl.valueType if result.fun.kind == NodeKind.lambdaExpr: self.lambdaExpr(LambdaExpr(result.fun), compile=compile) - elif not impl.valueType.compiled: + if not impl.valueType.compiled: self.funDecl(FunDecl(result.fun), impl) result = result.returnType if compile: @@ -1353,13 +1350,15 @@ method call(self: BytecodeCompiler, node: CallExpr, compile: bool = true): Type result = result.returnType of NodeKind.getItemExpr: var node = GetItemExpr(node.callee) - let impl = self.match(node.name.token.lexeme, - self.getItemExpr(node, compile=false, matching=Type(kind: Function, args: args, returnType: Type(kind: All))), node) - result = impl.valueType - if impl.isGeneric: - result = self.specialize(result, argExpr) - result = result.returnType - self.generateCall(impl, argExpr, node.token.line) + let impl = self.getItemExpr(node, compile=false, matching=Type(kind: Function, args: args, returnType: Type(kind: All))) + var fn: Name + for name in self.names: + if name.valueType == impl: + fn = name + break + result = impl.returnType + if compile: + self.generateCall(fn, argExpr, node.token.line) of NodeKind.lambdaExpr: var node = LambdaExpr(node.callee) var impl = self.lambdaExpr(node, compile=compile) @@ -1394,7 +1393,7 @@ method getItemExpr(self: BytecodeCompiler, node: GetItemExpr, compile: bool = tr for name in values: if self.compare(name.valueType, matching): result = name.valueType - return + break if len(values) == 1: result = values[0].valueType else: @@ -1689,6 +1688,9 @@ proc importStmt(self: BytecodeCompiler, node: ImportStmt, compile: bool = true) # its public names to us for name in self.findInModule("", module): name.exportedTo.incl(self.currentModule.path) + # We also need to export public names from other modules + # that we have explicitly exported because imports are + # compiled only once for module in self.modules.values(): if self.currentModule.path in module.exportedTo: for name in self.findInModule("", module): diff --git a/src/peon/stdlib/builtins/misc.pn b/src/peon/stdlib/builtins/misc.pn index 01d0f9a..7d4cfbc 100644 --- a/src/peon/stdlib/builtins/misc.pn +++ b/src/peon/stdlib/builtins/misc.pn @@ -2,11 +2,6 @@ import values; -fn clock*: float { - #pragma[magic: "SysClock64"] -} - - fn print*[T: any](x: T) { #pragma[magic: "print"] }