From 3999ef97ffd2391150d2ce16fc6c7ec25cbb88be Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 1 Sep 2024 18:04:12 +0100 Subject: [PATCH] Register function into globals even if it's a closure --- src/compiler.cpp | 6 ++++++ test/function.vli | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/compiler.cpp b/src/compiler.cpp index 4cacedf..a85dbe4 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -679,6 +679,12 @@ Result Compiler::compile_fn(Context& context, Symbol& op, TRY(ex_res.add_opcode(Oc::MakeClosure, {0, (int64_t)reg}, {0, (int64_t)reg + (int64_t)ctx.closures.size() + 1})); + if (context.toplevel && !name.is()) { + int64_t gname = TRY(context.add_const(name)); + TRY(ex_res.add_opcode(Oc::GlobalStore, {1, (int64_t)gname}, + {0, (int64_t)reg})); + } + context.maxreg = reg + 1; ex_res.reg = reg; diff --git a/test/function.vli b/test/function.vli index ce7dc78..fcd3e0e 100644 --- a/test/function.vli +++ b/test/function.vli @@ -26,3 +26,10 @@ 1)) 43)) ) + + +(let ((x 42)) + (fn foo() x) + ) + +(assert (= (foo) 42))