From fb8377547bcb6d4dc6464ca34c0fe433e1cfda44 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 22 Oct 2021 14:49:58 -0600 Subject: [PATCH] more code cleanup --- src/libexpr/eval.cc | 7 +-- src/libexpr/nixexpr.cc | 132 ++++++++++------------------------------- src/libexpr/parser.y | 2 - 3 files changed, 33 insertions(+), 108 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 719dcc1b4..4dde92c0a 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -912,6 +912,9 @@ Env & EvalState::allocEnv(size_t size) nrValuesInEnvs += size; Env * env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *)); env->type = Env::Plain; + + /* We assume that env->values has been cleared by the allocator; maybeThunk() and lookupVar fromWith expect this. */ + return *env; } @@ -925,7 +928,6 @@ Env & fakeEnv(size_t size) return *env; } - void EvalState::mkList(Value & v, size_t size) { v.mkList(size); @@ -1291,7 +1293,6 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v) state.forceAttrs(*vAttrs, pos); if ((j = vAttrs->attrs->find(name)) == vAttrs->attrs->end()) throwEvalError(pos, "attribute '%1%' missing", name, env, this); - // mapBindings(*vAttrs->attrs)); } vAttrs = j->value; pos2 = j->pos; @@ -1419,8 +1420,6 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po "attempt to call something which is not a function but %1%", showType(fun).c_str(), fakeEnv(1), 0); - // fun.env); - // map2("fun", &fun, "arg", &arg)); } ExprLambda & lambda(*fun.lambda.fun); diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index 8f88eabd5..65c7cac1d 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -273,52 +273,36 @@ void ExprVar::bindVars(const std::shared_ptr &env) /* Check whether the variable appears in the environment. If so, set its level and displacement. */ - - // std::cout << "ExprVar::bindVars " << name << std::endl; - - int a = 10; - if (name == "callPackage") { - a++; // try to make code that I can put a breakpoint on... - // std::cout << "meh" << a + 10 << std::endl; - int withLevel = -1; - fromWith = true; - // this->level = withLevel; - } - - { - - const StaticEnv * curEnv; - unsigned int level; - int withLevel = -1; - for (curEnv = env.get(), level = 0; curEnv; curEnv = curEnv->up, level++) { - if (curEnv->isWith) { - if (withLevel == -1) withLevel = level; - } else { - StaticEnv::Vars::const_iterator i = curEnv->vars.find(name); - if (i != curEnv->vars.end()) { - fromWith = false; - this->level = level; - displ = i->second; - return; - } + const StaticEnv * curEnv; + unsigned int level; + int withLevel = -1; + for (curEnv = env.get(), level = 0; curEnv; curEnv = curEnv->up, level++) { + if (curEnv->isWith) { + if (withLevel == -1) withLevel = level; + } else { + StaticEnv::Vars::const_iterator i = curEnv->vars.find(name); + if (i != curEnv->vars.end()) { + fromWith = false; + this->level = level; + displ = i->second; + return; } } - - - /* Otherwise, the variable must be obtained from the nearest - enclosing `with'. If there is no `with', then we can issue an - "undefined variable" error now. */ - if (withLevel == -1) - { - // std::cout << " throw UndefinedVarError({" << std::endl; - throw UndefinedVarError({ - .msg = hintfmt("undefined variable (ExprVar bindvars) '%1%'", name), - .errPos = pos - }); - } - fromWith = true; - this->level = withLevel; } + + /* Otherwise, the variable must be obtained from the nearest + enclosing `with'. If there is no `with', then we can issue an + "undefined variable" error now. */ + if (withLevel == -1) + { + // std::cout << " throw UndefinedVarError({" << std::endl; + throw UndefinedVarError({ + .msg = hintfmt("undefined variable (ExprVar bindvars) '%1%'", name), + .errPos = pos + }); + } + fromWith = true; + this->level = withLevel; } void ExprSelect::bindVars(const std::shared_ptr &env) @@ -349,18 +333,11 @@ void ExprAttrs::bindVars(const std::shared_ptr &env) if (debuggerHook) staticenv = env; - // std::cout << "ExprAttrs::bindVars" << std::endl; - // auto dynamicEnv(env); - if (recursive) { - // std::cout << "recursive" << std::endl; - // dynamicEnv = newEnv.get(); - // also make shared_ptr? auto newEnv = std::shared_ptr(new StaticEnv(false, env.get())); unsigned int displ = 0; for (auto & i : attrs) { - // std::cout << "newenvvar: " << i.first << std::endl; newEnv->vars[i.first] = i.second.displ = displ++; } @@ -373,7 +350,6 @@ void ExprAttrs::bindVars(const std::shared_ptr &env) } } else { - // std::cout << "NOT recursive" << std::endl; for (auto & i : attrs) i.second.e->bindVars(env); @@ -382,9 +358,6 @@ void ExprAttrs::bindVars(const std::shared_ptr &env) i.valueExpr->bindVars(env); } } - - // std::cout << "ExprAttrs::bindVars end" << std::endl; - } void ExprList::bindVars(const std::shared_ptr &env) @@ -401,7 +374,7 @@ void ExprLambda::bindVars(const std::shared_ptr &env) if (debuggerHook) staticenv = env; - auto newEnv = std::shared_ptr(new StaticEnv(false, env.get())); // also make shared_ptr? + auto newEnv = std::shared_ptr(new StaticEnv(false, env.get())); unsigned int displ = 0; @@ -423,7 +396,7 @@ void ExprLet::bindVars(const std::shared_ptr &env) if (debuggerHook) staticenv = env; - auto newEnv = std::shared_ptr(new StaticEnv(false, env.get())); // also make shared_ptr? + auto newEnv = std::shared_ptr(new StaticEnv(false, env.get())); unsigned int displ = 0; for (auto & i : attrs->attrs) @@ -440,7 +413,6 @@ void ExprWith::bindVars(const std::shared_ptr &env) if (debuggerHook) staticenv = env; - // std::cout << " ExprWith::bindVars " << std::endl; /* Does this `with' have an enclosing `with'? If so, record its level so that `lookupVar' can look up variables in the previous `with' if this one doesn't contain the desired attribute. */ @@ -453,54 +425,10 @@ void ExprWith::bindVars(const std::shared_ptr &env) break; } - // std::cout << " ExprWith::bindVars 1" << std::endl; - // attrs->show(std::cout); - // std::cout << std::endl; attrs->bindVars(env); - auto newEnv = std::shared_ptr(new StaticEnv(true, env.get())); // also make shared_ptr? - // std::cout << " ExprWith::bindVars 2" << std::endl; - // std::cout << " body: " << std::endl; - // body->show(std::cout); - // std::cout << std::endl; - - // std::cout << "ExprWith::newenv: (iswith, size); (" << newEnv->isWith << ", " << newEnv->vars.size() << ") " << std::endl; - // for (auto i = newEnv->vars.begin(); i != newEnv->vars.end(); ++i) - // std::cout << "EvalState::parse newEnv " << i->first << std::endl; - - - // std::cout << " body->bindVars(newEnv), iswith: " << newEnv->isWith << std::endl; - body->bindVars(newEnv); - // std::cout << " ExprWith::bindVars 3" << std::endl; -} - -/* -void ExprWith::bindVars(const StaticEnv & env) -{ - // Does this `with' have an enclosing `with'? If so, record its - // level so that `lookupVar' can look up variables in the previous - // `with' if this one doesn't contain the desired attribute. - const StaticEnv * curEnv; - unsigned int level; - prevWith = 0; - for (curEnv = &env, level = 1; curEnv; curEnv = curEnv->up, level++) - if (curEnv->isWith) { - prevWith = level; - break; - } - - attrs->bindVars(env); - std::cout << "ExprWith::bindVars env: " << env.vars.size(); // add std::endl; - for (auto i = env.vars.begin(); i != env.vars.end(); ++i) - { - std::cout << i->first << std::endl; - } - - StaticEnv newEnv(true, &env); + auto newEnv = std::shared_ptr(new StaticEnv(true, env.get())); body->bindVars(newEnv); } -*/ - - void ExprIf::bindVars(const std::shared_ptr &env) { diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index d6d5c85f5..d1e898677 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -22,8 +22,6 @@ #include "eval.hh" #include "globals.hh" -#include - namespace nix { struct ParseData