show 'with' bindings as well as static

This commit is contained in:
Ben Burdette 2022-03-31 09:37:36 -06:00
parent c0a567e196
commit 1096d17b65
3 changed files with 65 additions and 25 deletions

View file

@ -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<std::string> names;
// std::set<std::string> 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({});
}

View file

@ -699,22 +699,46 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
return {};
}
void printStaticEnvBindings(const StaticEnv &se, int lvl)
{
std::cout << "Env level " << lvl << std::endl;
if (se.up) {
// 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;
std::cout << std::endl;
}
printStaticEnvBindings(*se.up, ++lvl);
// 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 && env.up) {
std::cout << "static: ";
printStaticEnvBindings(se);
printWithBindings(env);
std::cout << std::endl;
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,12 +775,27 @@ 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();
if (env.type == Env::HasWithAttrs)
{
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);
}

View file

@ -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
{