This commit is contained in:
Ben Burdette 2021-12-20 12:32:21 -07:00
parent c151a9b426
commit f317019edd
5 changed files with 86 additions and 17 deletions

View file

@ -82,7 +82,7 @@ ref<EvalState> EvalCommand::getEvalState()
if (expr.staticenv) if (expr.staticenv)
{ {
auto vm = mapStaticEnvBindings(*expr.staticenv.get(), env); auto vm = mapStaticEnvBindings(*expr.staticenv.get(), env);
runRepl(evalState, *vm); runRepl(evalState, ref<const Error>(&error), *vm);
} }
}; };
} }

View file

@ -314,6 +314,8 @@ void printClosureDiff(
void runRepl( void runRepl(
ref<EvalState> evalState, ref<EvalState> evalState,
std::optional<ref<const Error>> debugError,
const std::map<std::string, Value *> & extraEnv); const std::map<std::string, Value *> & extraEnv);
} }

View file

@ -50,6 +50,8 @@ struct NixRepl
ref<EvalState> state; ref<EvalState> state;
Bindings * autoArgs; Bindings * autoArgs;
std::optional<ref<const Error>> debugError;
Strings loadedFiles; Strings loadedFiles;
const static int envSize = 32768; const static int envSize = 32768;
@ -70,6 +72,7 @@ struct NixRepl
void loadFile(const Path & path); void loadFile(const Path & path);
void loadFlake(const std::string & flakeRef); void loadFlake(const std::string & flakeRef);
void initEnv(); void initEnv();
void loadFiles();
void reloadFiles(); void reloadFiles();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol & name, Value * v); void addVarToScope(const Symbol & name, Value * v);
@ -199,6 +202,9 @@ namespace {
void NixRepl::mainLoop(const std::vector<std::string> & files) void NixRepl::mainLoop(const std::vector<std::string> & files)
{ {
std::cout << "iinitial mainLoop; " << std::endl;
// printStaticEnvBindings(*staticEnv, 0);
string error = ANSI_RED "error:" ANSI_NORMAL " "; string error = ANSI_RED "error:" ANSI_NORMAL " ";
notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n"); notice("Welcome to Nix " + nixVersion + ". Type :? for help.\n");
@ -207,7 +213,7 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
loadedFiles.push_back(i); loadedFiles.push_back(i);
} }
reloadFiles(); loadFiles();
if (!loadedFiles.empty()) notice(""); if (!loadedFiles.empty()) notice("");
// Allow nix-repl specific settings in .inputrc // Allow nix-repl specific settings in .inputrc
@ -225,6 +231,9 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)
std::string input; std::string input;
std::cout << "pre MAINLOOP; " << std::endl;
// printStaticEnvBindings(*staticEnv, 0);
while (true) { while (true) {
// When continuing input from previous lines, don't print a prompt, just align to the same // When continuing input from previous lines, don't print a prompt, just align to the same
// number of chars as the prompt. // number of chars as the prompt.
@ -429,7 +438,26 @@ bool NixRepl::processLine(string line)
<< " :s <expr> Build dependencies of derivation, then start nix-shell\n" << " :s <expr> Build dependencies of derivation, then start nix-shell\n"
<< " :t <expr> Describe result of evaluation\n" << " :t <expr> Describe result of evaluation\n"
<< " :u <expr> Build derivation, then start nix-shell\n" << " :u <expr> Build derivation, then start nix-shell\n"
<< " :doc <expr> Show documentation of a builtin function\n"; << " :doc <expr> Show documentation of a builtin function\n"
<< " :d <cmd> Debug mode commands\n"
<< " :d stack Show call stack\n"
<< " :d stack <int> 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") { else if (command == ":a" || command == ":add") {
@ -561,11 +589,13 @@ bool NixRepl::processLine(string line)
line[p + 1] != '=' && line[p + 1] != '=' &&
isVarName(name = removeWhitespace(string(line, 0, p)))) isVarName(name = removeWhitespace(string(line, 0, p))))
{ {
std::cout << "isvarname" << std::endl;
Expr * e = parseString(string(line, p + 1)); Expr * e = parseString(string(line, p + 1));
Value *v = new Value(*state->allocValue()); Value *v = new Value(*state->allocValue());
v->mkThunk(env, e); v->mkThunk(env, e);
addVarToScope(state->symbols.create(name), v); addVarToScope(state->symbols.create(name), v);
} else { } else {
std::cout << "evalstring" << std::endl;
Value v; Value v;
evalString(line, v); evalString(line, v);
printValue(std::cout, v, 1) << std::endl; printValue(std::cout, v, 1) << std::endl;
@ -623,6 +653,12 @@ void NixRepl::reloadFiles()
{ {
initEnv(); initEnv();
loadFiles();
}
void NixRepl::loadFiles()
{
Strings old = loadedFiles; Strings old = loadedFiles;
loadedFiles.clear(); loadedFiles.clear();
@ -649,12 +685,28 @@ void NixRepl::addVarToScope(const Symbol & name, Value * v)
{ {
if (displ >= envSize) if (displ >= envSize)
throw Error("environment full; cannot add more variables"); 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->vars.emplace_back(name, displ);
staticEnv->sort(); staticEnv->sort();
env->values[displ++] = v; env->values[displ++] = v;
varNames.insert((string) name); 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) Expr * NixRepl::parseString(string s)
{ {
@ -665,8 +717,11 @@ Expr * NixRepl::parseString(string s)
void NixRepl::evalString(string s, Value & v) void NixRepl::evalString(string s, Value & v)
{ {
std::cout << "pre partstirns:l" << std::endl;
Expr * e = parseString(s); Expr * e = parseString(s);
std::cout << "pre e->eval" << std::endl;
e->eval(*state, *env, v); e->eval(*state, *env, v);
std::cout << "prev fv" << std::endl;
state->forceValue(v); state->forceValue(v);
} }
@ -817,10 +872,13 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
void runRepl( void runRepl(
ref<EvalState> evalState, ref<EvalState> evalState,
std::optional<ref<const Error>> debugError,
const std::map<std::string, Value *> & extraEnv) const std::map<std::string, Value *> & extraEnv)
{ {
auto repl = std::make_unique<NixRepl>(evalState); auto repl = std::make_unique<NixRepl>(evalState);
repl->debugError = debugError;
repl->initEnv(); repl->initEnv();
std::set<std::string> names; std::set<std::string> names;
@ -834,6 +892,9 @@ void runRepl(
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());
// printError("The following extra variables are in scope: %s\n", concatStringsSep(", ", names)); // 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({}); repl->mainLoop({});
} }

View file

@ -26,6 +26,7 @@ typedef void (* PrimOpFun) (EvalState & state, const Pos & pos, Value * * args,
extern std::function<void(const Error & error, const Env & env, const Expr & expr)> debuggerHook; extern std::function<void(const Error & error, const Env & env, const Expr & expr)> debuggerHook;
void printStaticEnvBindings(const Expr &expr); void printStaticEnvBindings(const Expr &expr);
void printStaticEnvBindings(const StaticEnv &se, int lvl = 0);
struct PrimOp struct PrimOp
{ {

View file

@ -21,6 +21,7 @@
#include "nixexpr.hh" #include "nixexpr.hh"
#include "eval.hh" #include "eval.hh"
#include "globals.hh" #include "globals.hh"
#include <iostream>
namespace nix { namespace nix {
@ -615,6 +616,10 @@ Expr * EvalState::parse(const char * text, FileOrigin origin,
if (res) throw ParseError(data.error.value()); if (res) throw ParseError(data.error.value());
std::cout << " data.result->bindVars(staticEnv); " << std::endl;
// printStaticEnvBindings(*staticEnv, 0);
data.result->bindVars(staticEnv); data.result->bindVars(staticEnv);
return data.result; return data.result;