diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 2e00b42ff..b37959a2e 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -82,7 +82,7 @@ ref EvalCommand::getEvalState() if (expr.staticenv) { auto vm = mapStaticEnvBindings(*expr.staticenv.get(), env); - runRepl(evalState, *vm); + runRepl(evalState, ref(&error), *vm); } }; } diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index 0d847d255..e27ee2e9e 100644 --- a/src/libcmd/command.hh +++ b/src/libcmd/command.hh @@ -314,6 +314,8 @@ void printClosureDiff( void runRepl( ref evalState, + std::optional> debugError, const std::map & extraEnv); + } diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 910f0f694..0eea2389b 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -50,6 +50,8 @@ struct NixRepl ref state; Bindings * autoArgs; + std::optional> debugError; + Strings loadedFiles; const static int envSize = 32768; @@ -70,6 +72,7 @@ struct NixRepl void loadFile(const Path & path); void loadFlake(const std::string & flakeRef); void initEnv(); + void loadFiles(); void reloadFiles(); void addAttrsToScope(Value & attrs); void addVarToScope(const Symbol & name, Value * v); @@ -199,6 +202,9 @@ namespace { void NixRepl::mainLoop(const std::vector & files) { + std::cout << "iinitial mainLoop; " << std::endl; + // printStaticEnvBindings(*staticEnv, 0); + string error = ANSI_RED "error:" ANSI_NORMAL " "; notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n"); @@ -207,7 +213,7 @@ void NixRepl::mainLoop(const std::vector & files) loadedFiles.push_back(i); } - reloadFiles(); + loadFiles(); if (!loadedFiles.empty()) notice(""); // Allow nix-repl specific settings in .inputrc @@ -225,6 +231,9 @@ void NixRepl::mainLoop(const std::vector & files) std::string input; + std::cout << "pre MAINLOOP; " << std::endl; + // printStaticEnvBindings(*staticEnv, 0); + while (true) { // When continuing input from previous lines, don't print a prompt, just align to the same // number of chars as the prompt. @@ -415,21 +424,40 @@ bool NixRepl::processLine(string line) std::cout << "The following commands are available:\n" << "\n" - << " Evaluate and print expression\n" - << " = Bind expression to variable\n" - << " :a Add attributes from resulting set to scope\n" - << " :b Build derivation\n" - << " :e Open package or function in $EDITOR\n" - << " :i Build derivation, then install result into current profile\n" - << " :l Load Nix expression and add it to scope\n" - << " :lf Load Nix flake and add it to scope\n" - << " :p Evaluate and print expression recursively\n" - << " :q Exit nix-repl\n" - << " :r Reload all files\n" - << " :s Build dependencies of derivation, then start nix-shell\n" - << " :t Describe result of evaluation\n" - << " :u Build derivation, then start nix-shell\n" - << " :doc Show documentation of a builtin function\n"; + << " Evaluate and print expression\n" + << " = Bind expression to variable\n" + << " :a Add attributes from resulting set to scope\n" + << " :b Build derivation\n" + << " :e Open package or function in $EDITOR\n" + << " :i Build derivation, then install result into current profile\n" + << " :l Load Nix expression and add it to scope\n" + << " :lf Load Nix flake and add it to scope\n" + << " :p Evaluate and print expression recursively\n" + << " :q Exit nix-repl\n" + << " :r Reload all files\n" + << " :s Build dependencies of derivation, then start nix-shell\n" + << " :t Describe result of evaluation\n" + << " :u Build derivation, then start nix-shell\n" + << " :doc Show documentation of a builtin function\n" + << " :d Debug mode commands\n" + << " :d stack Show call stack\n" + << " :d stack Detail for step N\n" + << " :d error Show current error\n"; + } + + else if (command == ":d" || command == ":debug") { + std::cout << "debug: '" << arg << "'" << std::endl; + if (arg == "stack") { + } + else if (arg == "error") { + if (this->debugError.has_value()) { + showErrorInfo(std::cout, (*debugError)->info(), true); + } + else + { + notice("error information not available"); + } + } } else if (command == ":a" || command == ":add") { @@ -561,11 +589,13 @@ bool NixRepl::processLine(string line) line[p + 1] != '=' && isVarName(name = removeWhitespace(string(line, 0, p)))) { + std::cout << "isvarname" << std::endl; Expr * e = parseString(string(line, p + 1)); Value *v = new Value(*state->allocValue()); v->mkThunk(env, e); addVarToScope(state->symbols.create(name), v); } else { + std::cout << "evalstring" << std::endl; Value v; evalString(line, v); printValue(std::cout, v, 1) << std::endl; @@ -623,6 +653,12 @@ void NixRepl::reloadFiles() { initEnv(); + loadFiles(); +} + + +void NixRepl::loadFiles() +{ Strings old = loadedFiles; loadedFiles.clear(); @@ -649,12 +685,28 @@ void NixRepl::addVarToScope(const Symbol & name, Value * v) { if (displ >= envSize) throw Error("environment full; cannot add more variables"); + if (auto oldVar = staticEnv->find(name); oldVar != staticEnv->vars.end()) + staticEnv->vars.erase(oldVar); staticEnv->vars.emplace_back(name, displ); staticEnv->sort(); env->values[displ++] = v; varNames.insert((string) name); + notice("Added variable to scope: %1%", name); + } +// version from master. +// void NixRepl::addVarToScope(const Symbol & name, Value & v) +// { +// if (displ >= envSize) +// throw Error("environment full; cannot add more variables"); +// if (auto oldVar = staticEnv.find(name); oldVar != staticEnv.vars.end()) +// staticEnv.vars.erase(oldVar); +// staticEnv.vars.emplace_back(name, displ); +// staticEnv.sort(); +// env->values[displ++] = &v; +// varNames.insert((string) name); +// } Expr * NixRepl::parseString(string s) { @@ -665,8 +717,11 @@ Expr * NixRepl::parseString(string s) void NixRepl::evalString(string s, Value & v) { + std::cout << "pre partstirns:l" << std::endl; Expr * e = parseString(s); + std::cout << "pre e->eval" << std::endl; e->eval(*state, *env, v); + std::cout << "prev fv" << std::endl; state->forceValue(v); } @@ -817,10 +872,13 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m void runRepl( ref evalState, + std::optional> debugError, const std::map & extraEnv) { auto repl = std::make_unique(evalState); + repl->debugError = debugError; + repl->initEnv(); std::set names; @@ -834,6 +892,9 @@ void runRepl( printError(hintfmt("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)).str()); // printError("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)); + std::cout << " pre repl->mainLoop({});" << std::endl; + // printStaticEnvBindings(*repl->staticEnv, 0); + repl->mainLoop({}); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 485c2df83..4b3e7d69a 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -26,6 +26,7 @@ typedef void (* PrimOpFun) (EvalState & state, const Pos & pos, Value * * args, extern std::function debuggerHook; void printStaticEnvBindings(const Expr &expr); +void printStaticEnvBindings(const StaticEnv &se, int lvl = 0); struct PrimOp { diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 58af0df7d..066dc4ecc 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -21,6 +21,7 @@ #include "nixexpr.hh" #include "eval.hh" #include "globals.hh" +#include namespace nix { @@ -615,6 +616,10 @@ Expr * EvalState::parse(const char * text, FileOrigin origin, if (res) throw ParseError(data.error.value()); + std::cout << " data.result->bindVars(staticEnv); " << std::endl; + + // printStaticEnvBindings(*staticEnv, 0); + data.result->bindVars(staticEnv); return data.result;