Skip to content

Commit

Permalink
Add level / displacement types
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Nov 4, 2021
1 parent 81e7c40 commit bcf4780
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ void mkPath(Value & v, const char * s)

inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
{
for (size_t l = var.level; l; --l, env = env->up) ;
for (auto l = var.level; l; --l, env = env->up) ;

if (!var.fromWith) return env->values[var.displ];

Expand Down Expand Up @@ -1060,7 +1060,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
/* The recursive attributes are evaluated in the new
environment, while the inherited attributes are evaluated
in the original environment. */
size_t displ = 0;
Displacement displ = 0;
for (auto & i : attrs) {
Value * vAttr;
if (hasOverrides && !i.second.inherited) {
Expand Down Expand Up @@ -1136,7 +1136,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
/* The recursive attributes are evaluated in the new environment,
while the inherited attributes are evaluated in the original
environment. */
size_t displ = 0;
Displacement displ = 0;
for (auto & i : attrs->attrs)
env2.values[displ++] = i.second.e->maybeThunk(state, i.second.inherited ? env : env2);

Expand Down Expand Up @@ -1283,7 +1283,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
Env & env2(allocEnv(size));
env2.up = vCur.lambda.env;

size_t displ = 0;
Displacement displ = 0;

if (!lambda.hasFormals())
env2.values[displ++] = args[0];
Expand Down
10 changes: 5 additions & 5 deletions src/libexpr/nixexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void ExprVar::bindVars(const StaticEnv & env)
/* Check whether the variable appears in the environment. If so,
set its level and displacement. */
const StaticEnv * curEnv;
unsigned int level;
Level level;
int withLevel = -1;
for (curEnv = &env, level = 0; curEnv; curEnv = curEnv->up, level++) {
if (curEnv->isWith) {
Expand Down Expand Up @@ -326,7 +326,7 @@ void ExprAttrs::bindVars(const StaticEnv & env)
if (recursive) {
dynamicEnv = &newEnv;

unsigned int displ = 0;
Displacement displ = 0;
for (auto & i : attrs)
newEnv.vars.emplace_back(i.first, i.second.displ = displ++);

Expand Down Expand Up @@ -359,7 +359,7 @@ void ExprLambda::bindVars(const StaticEnv & env)
(hasFormals() ? formals->formals.size() : 0) +
(arg.empty() ? 0 : 1));

unsigned int displ = 0;
Displacement displ = 0;

if (!arg.empty()) newEnv.vars.emplace_back(arg, displ++);

Expand Down Expand Up @@ -387,7 +387,7 @@ void ExprLet::bindVars(const StaticEnv & env)
{
StaticEnv newEnv(false, &env, attrs->attrs.size());

unsigned int displ = 0;
Displacement displ = 0;
for (auto & i : attrs->attrs)
newEnv.vars.emplace_back(i.first, i.second.displ = displ++);

Expand All @@ -405,7 +405,7 @@ void ExprWith::bindVars(const StaticEnv & env)
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;
Level level;
prevWith = 0;
for (curEnv = &env, level = 1; curEnv; curEnv = curEnv->up, level++)
if (curEnv->isWith) {
Expand Down
11 changes: 7 additions & 4 deletions src/libexpr/nixexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ struct ExprPath : Expr
Value * maybeThunk(EvalState & state, Env & env);
};

typedef uint32_t Level;
typedef uint32_t Displacement;

struct ExprVar : Expr
{
Pos pos;
Expand All @@ -148,8 +151,8 @@ struct ExprVar : Expr
value is obtained by getting the attribute named `name' from
the set stored in the environment that is `level' levels up
from the current one.*/
unsigned int level;
unsigned int displ;
Level level;
Displacement displ;

ExprVar(const Symbol & name) : name(name) { };
ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
Expand Down Expand Up @@ -183,7 +186,7 @@ struct ExprAttrs : Expr
bool inherited;
Expr * e;
Pos pos;
unsigned int displ; // displacement
Displacement displ; // displacement
AttrDef(Expr * e, const Pos & pos, bool inherited=false)
: inherited(inherited), e(e), pos(pos) { };
AttrDef() { };
Expand Down Expand Up @@ -352,7 +355,7 @@ struct StaticEnv
const StaticEnv * up;

// Note: these must be in sorted order.
typedef std::vector<std::pair<Symbol, unsigned int>> Vars;
typedef std::vector<std::pair<Symbol, Displacement>> Vars;
Vars vars;

StaticEnv(bool isWith, const StaticEnv * up, size_t expectedSize = 0) : isWith(isWith), up(up) {
Expand Down

0 comments on commit bcf4780

Please sign in to comment.