From 1096d17b65834a7e1ff29d1afdf09536cc9d7a8d Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 31 Mar 2022 09:37:36 -0600 Subject: [PATCH] show 'with' bindings as well as static --- src/libcmd/repl.cc | 10 +++--- src/libexpr/eval.cc | 76 ++++++++++++++++++++++++++++++++++----------- src/libexpr/eval.hh | 4 +-- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 2e3a3b6df..5de4cdf76 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -511,7 +511,7 @@ bool NixRepl::processLine(string line) ++iter, ++idx) { if (idx == this->debugTraceIndex) { - printStaticEnvBindings(iter->expr); + printEnvBindings(iter->expr, iter->env); break; } } @@ -533,7 +533,7 @@ bool NixRepl::processLine(string line) { std::cout << "\n" << ANSI_BLUE << idx << ANSI_NORMAL << ": "; showDebugTrace(std::cout, *iter); - printStaticEnvBindings(iter->expr); + printEnvBindings(iter->expr, iter->env); loadDebugTraceEnv(*iter); break; } @@ -1010,14 +1010,14 @@ void runRepl( repl->initEnv(); // add 'extra' vars. - std::set names; + // std::set names; for (auto & [name, value] : extraEnv) { // names.insert(ANSI_BOLD + name + ANSI_NORMAL); - names.insert(name); + // names.insert(name); repl->addVarToScope(repl->state->symbols.create(name), *value); } - printError(hintfmt("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)).str()); + // printError(hintfmt("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)).str()); repl->mainLoop({}); } diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index cc0380d4a..7c67f2ea8 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -699,22 +699,46 @@ std::optional EvalState::getDoc(Value & v) return {}; } -void printStaticEnvBindings(const StaticEnv &se, int lvl) + +// just for the current level of StaticEnv, not the whole chain. +void printStaticEnvBindings(const StaticEnv &se) +{ + std::cout << ANSI_MAGENTA; + for (auto i = se.vars.begin(); i != se.vars.end(); ++i) + { + std::cout << i->first << " "; + } + std::cout << ANSI_NORMAL; + std::cout << std::endl; +} + +// just for the current level of Env, not the whole chain. +void printWithBindings(const Env &env) +{ + if (env.type == Env::HasWithAttrs) + { + std::cout << "with: "; + std::cout << ANSI_MAGENTA; + Bindings::iterator j = env.values[0]->attrs->begin(); + while (j != env.values[0]->attrs->end()) { + std::cout << j->name << " "; + ++j; + } + std::cout << ANSI_NORMAL; + std::cout << std::endl; + } +} + +void printEnvBindings(const StaticEnv &se, const Env &env, int lvl) { std::cout << "Env level " << lvl << std::endl; - if (se.up) { - std::cout << ANSI_MAGENTA; - for (auto i = se.vars.begin(); i != se.vars.end(); ++i) - { - std::cout << i->first << " "; - } - std::cout << ANSI_NORMAL; - + if (se.up && env.up) { + std::cout << "static: "; + printStaticEnvBindings(se); + printWithBindings(env); std::cout << std::endl; - std::cout << std::endl; - - printStaticEnvBindings(*se.up, ++lvl); + printEnvBindings(*se.up, *env.up, ++lvl); } else { @@ -727,18 +751,19 @@ void printStaticEnvBindings(const StaticEnv &se, int lvl) } std::cout << ANSI_NORMAL; std::cout << std::endl; + printWithBindings(env); // probably nothing there for the top level. std::cout << std::endl; } - } -void printStaticEnvBindings(const Expr &expr) +// TODO: add accompanying env for With stuff. +void printEnvBindings(const Expr &expr, const Env &env) { // just print the names for now if (expr.staticenv) { - printStaticEnvBindings(*expr.staticenv.get(), 0); + printEnvBindings(*expr.staticenv.get(), env, 0); } } @@ -750,11 +775,26 @@ void mapStaticEnvBindings(const StaticEnv &se, const Env &env, valmap & vm) if (env.up && se.up) { mapStaticEnvBindings(*se.up, *env.up,vm); - // iterate through staticenv bindings and add them. auto map = valmap(); - for (auto iter = se.vars.begin(); iter != se.vars.end(); ++iter) + if (env.type == Env::HasWithAttrs) { - map[iter->first] = env.values[iter->second]; + std::cout << "(env.type == Env::HasWithAttrs)" << std::endl; + Bindings::iterator j = env.values[0]->attrs->begin(); + while (j != env.values[0]->attrs->end()) { + // std::cout << "adding : " << j->name << std::endl; + map[j->name] = j->value; + // if (countCalls) attrSelects[*j->pos]++; + // return j->value; + ++j; + } + } + else + { + // iterate through staticenv bindings and add them. + for (auto iter = se.vars.begin(); iter != se.vars.end(); ++iter) + { + map[iter->first] = env.values[iter->second]; + } } vm.merge(map); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 9de3475e8..1c569fc36 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -25,8 +25,8 @@ enum RepairFlag : bool; typedef void (* PrimOpFun) (EvalState & state, const Pos & pos, Value * * args, Value & v); -void printStaticEnvBindings(const Expr &expr); -void printStaticEnvBindings(const StaticEnv &se, int lvl = 0); +void printEnvBindings(const Expr &expr, const Env &env); +void printEnvBindings(const StaticEnv &se, const Env &env, int lvl = 0); struct PrimOp {