From 59f0aaa732d8c6f9d8c398519e39f5ec956ffba9 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 26 Jun 2022 22:06:52 +0300 Subject: [PATCH] defer explain loading to when it's invoked --- docs/commands.md | 4 ++++ src/load.c | 39 ++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 4899b2fc..9331affa 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -18,6 +18,10 @@ Clear the cache of monadic `•Import` calls, resulting in re-evaluating the sou Time the argument expression. `n` specifies the number of times to repeat. Exists to allow not escaping quotes and less overhead for timing very fast & small expressions. +## `)explain expr` / `)e expr` + +Display a syntax breakdown of the expression + ## `)profile expr` / `)profile@frequency expr` Profile the expression at the given sampling frequency, or 5000 samples/second by default diff --git a/src/load.c b/src/load.c index 9dcdbe46..1bea5bee 100644 --- a/src/load.c +++ b/src/load.c @@ -170,15 +170,6 @@ void load_gcFn() { i64 prevEnvPos = comp_currEnvPos; comp_currEnvPos = envCurr-envStart; \ if (CATCH) { POP_COMP; rethrow(); } -B bqn_explain(B str, B path) { - B args = bi_N; - PUSH_COMP; - B c = c2(load_comp, incG(load_compArg), inc(str)); - POP_COMP; - B ret = c2(load_explain, c, str); - return ret; -} - static NOINLINE Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes str,path,args PUSH_COMP; Block* r = load_compObj(c2(comp, incG(compArg), inc(str)), str, path, NULL); @@ -489,12 +480,7 @@ void load_init() { // very last init function load_repr = Get(fmtR, 1); gc_add(load_repr); decG(fmtR); #endif - Block* expl_b = load_compImport("(explain)", - #include "gen/explain" - ); - load_explain = evalFunBlock(expl_b, 0); ptr_dec(expl_b); - gc_add(load_explain); - + gc_enable(); #endif // PRECOMP } @@ -513,6 +499,29 @@ void bqn_exit(i32 code) { exit(code); } +B bqn_explain(B str, B path) { + #if NO_EXPLAIN + thrM("Explainer not included in this CBQN build"); + #else + if (load_explain.u==0) { + B* runtime = harr_ptr(load_rtObj); + Block* expl_b = load_compImport("(explain)", + #include "gen/explain" + ); + load_explain = evalFunBlock(expl_b, 0); ptr_dec(expl_b); + gc_add(load_explain); + } + + B args = bi_N; + PUSH_COMP; + B c = c2(load_comp, incG(load_compArg), inc(str)); + POP_COMP; + B ret = c2(load_explain, c, str); + return ret; + #endif +} + + static void freed_visit(Value* x) { #if CATCH_ERRORS