* Fix builtins.

This commit is contained in:
Eelco Dolstra 2010-04-14 22:59:39 +00:00
parent 81de12bc8f
commit 267dc693d2
4 changed files with 30 additions and 28 deletions

View file

@ -99,6 +99,8 @@ EvalState::EvalState()
, sMeta(symbols.create("meta")) , sMeta(symbols.create("meta"))
, sName(symbols.create("name")) , sName(symbols.create("name"))
, baseEnv(allocEnv(128)) , baseEnv(allocEnv(128))
, baseEnvDispl(0)
, staticBaseEnv(false, 0)
{ {
nrValues = nrEnvs = nrEvaluated = recursionDepth = maxRecursionDepth = 0; nrValues = nrEnvs = nrEvaluated = recursionDepth = maxRecursionDepth = 0;
deepestStack = (char *) -1; deepestStack = (char *) -1;
@ -117,28 +119,24 @@ EvalState::~EvalState()
void EvalState::addConstant(const string & name, Value & v) void EvalState::addConstant(const string & name, Value & v)
{ {
#if 0 staticBaseEnv.vars[symbols.create(name)] = baseEnvDispl;
baseEnv.bindings[symbols.create(name)] = v; baseEnv.values[baseEnvDispl++] = v;
string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name; string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name;
(*baseEnv.bindings[symbols.create("builtins")].attrs)[symbols.create(name2)] = v; (*baseEnv.values[0].attrs)[symbols.create(name2)] = v;
nrValues += 2;
#endif
} }
void EvalState::addPrimOp(const string & name, void EvalState::addPrimOp(const string & name,
unsigned int arity, PrimOp primOp) unsigned int arity, PrimOp primOp)
{ {
#if 0
Value v; Value v;
v.type = tPrimOp; v.type = tPrimOp;
v.primOp.arity = arity; v.primOp.arity = arity;
v.primOp.fun = primOp; v.primOp.fun = primOp;
baseEnv.bindings[symbols.create(name)] = v; staticBaseEnv.vars[symbols.create(name)] = baseEnvDispl;
baseEnv.values[baseEnvDispl++] = v;
string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name; string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name;
(*baseEnv.bindings[symbols.create("builtins")].attrs)[symbols.create(name2)] = v; (*baseEnv.values[0].attrs)[symbols.create(name2)] = v;
nrValues += 2;
#endif
} }

View file

@ -231,6 +231,15 @@ private:
values. */ values. */
Env & baseEnv; Env & baseEnv;
unsigned int baseEnvDispl;
public:
/* The same, but used during parsing to resolve variables. */
StaticEnv staticBaseEnv; // !!! should be private
private:
void createBaseEnv(); void createBaseEnv();
void addConstant(const string & name, Value & v); void addConstant(const string & name, Value & v);

View file

@ -461,8 +461,7 @@ static Expr * parse(EvalState & state, const char * text,
if (res) throw ParseError(data.error); if (res) throw ParseError(data.error);
try { try {
StaticEnv env(false, 0); data.result->bindVars(state.staticBaseEnv);
data.result->bindVars(env);
} catch (Error & e) { } catch (Error & e) {
throw ParseError(format("%1%, in `%2%'") % e.msg() % path); throw ParseError(format("%1%, in `%2%'") % e.msg() % path);
} }

View file

@ -999,15 +999,13 @@ void EvalState::createBaseEnv()
{ {
baseEnv.up = 0; baseEnv.up = 0;
#if 0
Value & builtins = baseEnv.bindings[symbols.create("builtins")];
builtins.type = tAttrs;
builtins.attrs = new Bindings;
#endif
/* Add global constants such as `true' to the base environment. */ /* Add global constants such as `true' to the base environment. */
Value v; Value v;
/* `builtins' must be first! */
mkAttrs(v);
addConstant("builtins", v);
mkBool(v, true); mkBool(v, true);
addConstant("true", v); addConstant("true", v);
@ -1023,14 +1021,6 @@ void EvalState::createBaseEnv()
mkString(v, thisSystem.c_str()); mkString(v, thisSystem.c_str());
addConstant("__currentSystem", v); addConstant("__currentSystem", v);
/* Add a wrapper around the derivation primop that computes the
`drvPath' and `outPath' attributes lazily. */
#if 0
string s = "attrs: let res = derivationStrict attrs; in attrs // { drvPath = res.drvPath; outPath = res.outPath; type = \"derivation\"; }";
mkThunk(v, baseEnv, parseExprFromString(*this, s, "/"));
addConstant("derivation", v);
#endif
// Miscellaneous // Miscellaneous
addPrimOp("import", 1, prim_import); addPrimOp("import", 1, prim_import);
addPrimOp("isNull", 1, prim_isNull); addPrimOp("isNull", 1, prim_isNull);
@ -1053,6 +1043,12 @@ void EvalState::createBaseEnv()
// Derivations // Derivations
addPrimOp("derivationStrict", 1, prim_derivationStrict); addPrimOp("derivationStrict", 1, prim_derivationStrict);
/* Add a wrapper around the derivation primop that computes the
`drvPath' and `outPath' attributes lazily. */
string s = "attrs: let res = derivationStrict attrs; in attrs // { drvPath = res.drvPath; outPath = res.outPath; type = \"derivation\"; }";
mkThunk(v, baseEnv, parseExprFromString(*this, s, "/"));
addConstant("derivation", v);
// Paths // Paths
addPrimOp("__toPath", 1, prim_toPath); addPrimOp("__toPath", 1, prim_toPath);
#if 0 #if 0