2010-03-23 18:30:50 +01:00
|
|
|
#include "nixexpr.hh"
|
|
|
|
#include "parser.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "nixexpr-ast.hh"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
struct Env;
|
|
|
|
struct Value;
|
2010-03-23 18:30:50 +01:00
|
|
|
|
2010-03-25 14:10:04 +01:00
|
|
|
typedef ATerm Sym;
|
|
|
|
|
|
|
|
typedef std::map<Sym, Value> Bindings;
|
2010-03-23 18:30:50 +01:00
|
|
|
|
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
struct Env
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
2010-03-24 13:41:08 +01:00
|
|
|
Env * up;
|
2010-03-23 18:30:50 +01:00
|
|
|
Bindings bindings;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
tInt = 1,
|
|
|
|
tAttrs,
|
2010-03-25 16:38:37 +01:00
|
|
|
tList,
|
2010-03-24 12:06:05 +01:00
|
|
|
tThunk,
|
2010-03-25 00:40:00 +01:00
|
|
|
tLambda,
|
2010-03-25 13:51:14 +01:00
|
|
|
tCopy,
|
|
|
|
tBlackhole
|
2010-03-23 18:30:50 +01:00
|
|
|
} ValueType;
|
|
|
|
|
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
struct Value
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
|
|
|
ValueType type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int integer;
|
|
|
|
Bindings * attrs;
|
2010-03-25 16:38:37 +01:00
|
|
|
struct {
|
|
|
|
unsigned int length;
|
|
|
|
Value * elems;
|
|
|
|
} list;
|
2010-03-23 18:30:50 +01:00
|
|
|
struct {
|
2010-03-24 13:41:08 +01:00
|
|
|
Env * env;
|
2010-03-23 18:30:50 +01:00
|
|
|
Expr expr;
|
|
|
|
} thunk;
|
2010-03-24 12:06:05 +01:00
|
|
|
struct {
|
2010-03-24 13:41:08 +01:00
|
|
|
Env * env;
|
2010-03-24 12:06:05 +01:00
|
|
|
Pattern pat;
|
|
|
|
Expr body;
|
|
|
|
} lambda;
|
2010-03-25 00:40:00 +01:00
|
|
|
Value * val;
|
2010-03-23 18:30:50 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
std::ostream & operator << (std::ostream & str, Value & v)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
|
|
|
switch (v.type) {
|
|
|
|
case tInt:
|
|
|
|
str << v.integer;
|
|
|
|
break;
|
|
|
|
case tAttrs:
|
|
|
|
str << "{ ";
|
2010-03-24 13:41:08 +01:00
|
|
|
foreach (Bindings::iterator, i, *v.attrs)
|
2010-03-25 16:38:37 +01:00
|
|
|
str << aterm2String(i->first) << " = " << i->second << "; ";
|
2010-03-23 18:30:50 +01:00
|
|
|
str << "}";
|
|
|
|
break;
|
2010-03-25 16:38:37 +01:00
|
|
|
case tList:
|
|
|
|
str << "[ ";
|
|
|
|
for (unsigned int n = 0; n < v.list.length; ++n)
|
|
|
|
str << v.list.elems[n] << " ";
|
|
|
|
str << "]";
|
|
|
|
break;
|
2010-03-23 18:30:50 +01:00
|
|
|
case tThunk:
|
|
|
|
str << "<CODE>";
|
|
|
|
break;
|
2010-03-25 00:40:00 +01:00
|
|
|
case tLambda:
|
|
|
|
str << "<LAMBDA>";
|
|
|
|
break;
|
2010-03-23 18:30:50 +01:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 00:40:00 +01:00
|
|
|
static void eval(Env * env, Expr e, Value & v);
|
2010-03-23 18:30:50 +01:00
|
|
|
|
|
|
|
|
2010-03-25 15:51:04 +01:00
|
|
|
static void forceValue(Value & v)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
2010-03-25 13:51:14 +01:00
|
|
|
if (v.type == tThunk) {
|
|
|
|
v.type = tBlackhole;
|
|
|
|
eval(v.thunk.env, v.thunk.expr, v);
|
|
|
|
}
|
2010-03-25 00:40:00 +01:00
|
|
|
else if (v.type == tCopy) {
|
|
|
|
forceValue(*v.val);
|
|
|
|
v = *v.val;
|
|
|
|
}
|
2010-03-25 13:51:14 +01:00
|
|
|
else if (v.type == tBlackhole)
|
|
|
|
throw EvalError("infinite recursion encountered");
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 15:51:04 +01:00
|
|
|
static Value * lookupWith(Env * env, Sym name)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
2010-03-25 15:51:04 +01:00
|
|
|
if (!env) return 0;
|
|
|
|
Value * v = lookupWith(env->up, name);
|
|
|
|
if (v) return v;
|
|
|
|
Bindings::iterator i = env->bindings.find(sWith);
|
|
|
|
if (i == env->bindings.end()) return 0;
|
|
|
|
Bindings::iterator j = i->second.attrs->find(name);
|
|
|
|
if (j != i->second.attrs->end()) return &j->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Value * lookupVar(Env * env, Sym name)
|
|
|
|
{
|
|
|
|
/* First look for a regular variable binding for `name'. */
|
|
|
|
for (Env * env2 = env; env2; env2 = env2->up) {
|
|
|
|
Bindings::iterator i = env2->bindings.find(name);
|
|
|
|
if (i != env2->bindings.end()) return &i->second;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
2010-03-25 15:51:04 +01:00
|
|
|
|
|
|
|
/* Otherwise, look for a `with' attribute set containing `name'.
|
|
|
|
Outer `withs' take precedence (i.e. `with {x=1;}; with {x=2;};
|
|
|
|
x' evaluates to 1). */
|
|
|
|
Value * v = lookupWith(env, name);
|
|
|
|
if (v) return v;
|
|
|
|
|
|
|
|
/* Alternative implementation where the inner `withs' take
|
|
|
|
precedence (i.e. `with {x=1;}; with {x=2;}; x' evaluates to
|
|
|
|
2). */
|
|
|
|
#if 0
|
|
|
|
for (Env * env2 = env; env2; env2 = env2->up) {
|
|
|
|
Bindings::iterator i = env2->bindings.find(sWith);
|
|
|
|
if (i == env2->bindings.end()) continue;
|
|
|
|
Bindings::iterator j = i->second.attrs->find(name);
|
|
|
|
if (j != i->second.attrs->end()) return &j->second;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-23 18:30:50 +01:00
|
|
|
throw Error("undefined variable");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 16:38:37 +01:00
|
|
|
unsigned long nrValues = 0, nrEnvs = 0;
|
2010-03-24 13:41:08 +01:00
|
|
|
|
2010-03-25 15:51:04 +01:00
|
|
|
static Env * allocEnv()
|
2010-03-24 13:11:38 +01:00
|
|
|
{
|
2010-03-24 13:41:08 +01:00
|
|
|
nrEnvs++;
|
|
|
|
return new Env;
|
2010-03-24 13:11:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-25 00:40:00 +01:00
|
|
|
static void eval(Env * env, Expr e, Value & v)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
|
|
|
printMsg(lvlError, format("eval: %1%") % e);
|
|
|
|
|
2010-03-25 14:10:04 +01:00
|
|
|
Sym name;
|
2010-03-23 18:30:50 +01:00
|
|
|
if (matchVar(e, name)) {
|
2010-03-25 14:10:04 +01:00
|
|
|
Value * v2 = lookupVar(env, name);
|
2010-03-24 13:41:08 +01:00
|
|
|
forceValue(*v2);
|
|
|
|
v = *v2;
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int n;
|
|
|
|
if (matchInt(e, n)) {
|
2010-03-24 13:41:08 +01:00
|
|
|
v.type = tInt;
|
|
|
|
v.integer = n;
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ATermList es;
|
|
|
|
if (matchAttrs(e, es)) {
|
2010-03-24 13:41:08 +01:00
|
|
|
v.type = tAttrs;
|
|
|
|
v.attrs = new Bindings;
|
2010-03-23 18:30:50 +01:00
|
|
|
ATerm e2, pos;
|
|
|
|
for (ATermIterator i(es); i; ++i) {
|
|
|
|
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
|
2010-03-25 14:10:04 +01:00
|
|
|
Value & v2 = (*v.attrs)[name];
|
2010-03-24 13:41:08 +01:00
|
|
|
nrValues++;
|
|
|
|
v2.type = tThunk;
|
|
|
|
v2.thunk.env = env;
|
|
|
|
v2.thunk.expr = e2;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
if (matchRec(e, rbnds, nrbnds)) {
|
2010-03-24 13:41:08 +01:00
|
|
|
Env * env2 = allocEnv();
|
2010-03-23 18:30:50 +01:00
|
|
|
env2->up = env;
|
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
v.type = tAttrs;
|
|
|
|
v.attrs = &env2->bindings;
|
2010-03-23 18:30:50 +01:00
|
|
|
ATerm name, e2, pos;
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
|
|
|
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
|
2010-03-25 14:10:04 +01:00
|
|
|
Value & v2 = env2->bindings[name];
|
2010-03-24 13:41:08 +01:00
|
|
|
nrValues++;
|
|
|
|
v2.type = tThunk;
|
|
|
|
v2.thunk.env = env2;
|
|
|
|
v2.thunk.expr = e2;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
2010-03-24 13:11:38 +01:00
|
|
|
|
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
2010-03-25 16:38:37 +01:00
|
|
|
Expr e1, e2;
|
2010-03-23 18:30:50 +01:00
|
|
|
if (matchSelect(e, e2, name)) {
|
2010-03-24 13:11:38 +01:00
|
|
|
eval(env, e2, v);
|
2010-03-24 13:41:08 +01:00
|
|
|
if (v.type != tAttrs) throw TypeError("expected attribute set");
|
2010-03-25 14:10:04 +01:00
|
|
|
Bindings::iterator i = v.attrs->find(name);
|
2010-03-24 13:41:08 +01:00
|
|
|
if (i == v.attrs->end()) throw TypeError("attribute not found");
|
|
|
|
forceValue(i->second);
|
|
|
|
v = i->second;
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
2010-03-24 12:06:05 +01:00
|
|
|
Pattern pat; Expr body; Pos pos;
|
|
|
|
if (matchFunction(e, pat, body, pos)) {
|
2010-03-24 13:41:08 +01:00
|
|
|
v.type = tLambda;
|
|
|
|
v.lambda.env = env;
|
|
|
|
v.lambda.pat = pat;
|
|
|
|
v.lambda.body = body;
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-24 12:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Expr fun, arg;
|
|
|
|
if (matchCall(e, fun, arg)) {
|
2010-03-24 13:11:38 +01:00
|
|
|
eval(env, fun, v);
|
2010-03-24 13:41:08 +01:00
|
|
|
if (v.type != tLambda) throw TypeError("expected function");
|
2010-03-24 12:06:05 +01:00
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
Env * env2 = allocEnv();
|
2010-03-24 12:06:05 +01:00
|
|
|
env2->up = env;
|
|
|
|
|
2010-03-25 13:19:41 +01:00
|
|
|
ATermList formals; ATerm ellipsis;
|
|
|
|
|
|
|
|
if (matchVarPat(v.lambda.pat, name)) {
|
2010-03-25 14:10:04 +01:00
|
|
|
Value & vArg = env2->bindings[name];
|
2010-03-25 13:19:41 +01:00
|
|
|
vArg.type = tThunk;
|
|
|
|
vArg.thunk.env = env;
|
|
|
|
vArg.thunk.expr = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (matchAttrsPat(v.lambda.pat, formals, ellipsis, name)) {
|
|
|
|
Value * vArg;
|
|
|
|
Value vArg_;
|
|
|
|
|
|
|
|
if (name == sNoAlias)
|
|
|
|
vArg = &vArg_;
|
|
|
|
else
|
2010-03-25 14:10:04 +01:00
|
|
|
vArg = &env2->bindings[name];
|
2010-03-25 13:19:41 +01:00
|
|
|
|
|
|
|
eval(env, arg, *vArg);
|
|
|
|
if (vArg->type != tAttrs) throw TypeError("expected attribute set");
|
|
|
|
|
2010-03-25 13:45:23 +01:00
|
|
|
/* For each formal argument, get the actual argument. If
|
|
|
|
there is no matching actual argument but the formal
|
|
|
|
argument has a default, use the default. */
|
|
|
|
unsigned int attrsUsed = 0;
|
2010-03-25 13:19:41 +01:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
2010-03-25 14:10:04 +01:00
|
|
|
Expr def; Sym name;
|
2010-03-25 13:19:41 +01:00
|
|
|
DefaultValue def2;
|
|
|
|
if (!matchFormal(*i, name, def2)) abort(); /* can't happen */
|
|
|
|
|
2010-03-25 14:10:04 +01:00
|
|
|
Bindings::iterator j = vArg->attrs->find(name);
|
2010-03-25 13:45:23 +01:00
|
|
|
|
2010-03-25 14:10:04 +01:00
|
|
|
Value & v = env2->bindings[name];
|
2010-03-25 13:45:23 +01:00
|
|
|
|
|
|
|
if (j == vArg->attrs->end()) {
|
|
|
|
if (!matchDefaultValue(def2, def)) def = 0;
|
|
|
|
if (def == 0) throw TypeError(format("the argument named `%1%' required by the function is missing")
|
|
|
|
% aterm2String(name));
|
|
|
|
v.type = tThunk;
|
|
|
|
v.thunk.env = env2;
|
|
|
|
v.thunk.expr = def;
|
|
|
|
} else {
|
|
|
|
attrsUsed++;
|
|
|
|
v.type = tCopy;
|
|
|
|
v.val = &j->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that each actual argument is listed as a formal
|
|
|
|
argument (unless the attribute match specifies a
|
|
|
|
`...'). TODO: show the names of the
|
|
|
|
expected/unexpected arguments. */
|
|
|
|
if (ellipsis == eFalse && attrsUsed != vArg->attrs->size())
|
|
|
|
throw TypeError("function called with unexpected argument");
|
2010-03-25 00:40:00 +01:00
|
|
|
}
|
2010-03-25 13:19:41 +01:00
|
|
|
|
|
|
|
else abort();
|
2010-03-25 00:40:00 +01:00
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
eval(env2, v.lambda.body, v);
|
2010-03-24 13:11:38 +01:00
|
|
|
return;
|
2010-03-24 12:06:05 +01:00
|
|
|
}
|
|
|
|
|
2010-03-25 15:51:04 +01:00
|
|
|
Expr attrs;
|
|
|
|
if (matchWith(e, attrs, body, pos)) {
|
|
|
|
Env * env2 = allocEnv();
|
|
|
|
env2->up = env;
|
|
|
|
|
|
|
|
Value & vAttrs = env2->bindings[sWith];
|
|
|
|
eval(env, attrs, vAttrs);
|
|
|
|
if (vAttrs.type != tAttrs) throw TypeError("`with' should evaluate to an attribute set");
|
|
|
|
|
|
|
|
eval(env2, body, v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-25 16:38:37 +01:00
|
|
|
if (matchList(e, es)) {
|
|
|
|
v.type = tList;
|
|
|
|
v.list.length = ATgetLength(es);
|
|
|
|
v.list.elems = new Value[v.list.length]; // !!! check destructor
|
|
|
|
for (unsigned int n = 0; n < v.list.length; ++n, es = ATgetNext(es)) {
|
|
|
|
v.list.elems[n].type = tThunk;
|
|
|
|
v.list.elems[n].thunk.env = env;
|
|
|
|
v.list.elems[n].thunk.expr = ATgetFirst(es);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchOpConcat(e, e1, e2)) {
|
|
|
|
Value v1; eval(env, e1, v1);
|
|
|
|
if (v1.type != tList) throw TypeError("list expecteed");
|
|
|
|
Value v2; eval(env, e2, v2);
|
|
|
|
if (v2.type != tList) throw TypeError("list expecteed");
|
|
|
|
v.type = tList;
|
|
|
|
v.list.length = v1.list.length + v2.list.length;
|
|
|
|
v.list.elems = new Value[v.list.length];
|
|
|
|
/* !!! This loses sharing with the original lists. We could
|
|
|
|
use a tCopy node, but that would use more memory. */
|
|
|
|
for (unsigned int n = 0; n < v1.list.length; ++n)
|
|
|
|
v.list.elems[n] = v1.list.elems[n];
|
|
|
|
for (unsigned int n = 0; n < v2.list.length; ++n)
|
|
|
|
v.list.elems[n + v1.list.length] = v2.list.elems[n];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error("unsupported term");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void strictEval(Env * env, Expr e, Value & v)
|
|
|
|
{
|
|
|
|
eval(env, e, v);
|
|
|
|
|
|
|
|
if (v.type == tAttrs) {
|
|
|
|
foreach (Bindings::iterator, i, *v.attrs)
|
|
|
|
forceValue(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (v.type == tList) {
|
|
|
|
for (unsigned int n = 0; n < v.list.length; ++n)
|
|
|
|
forceValue(v.list.elems[n]);
|
|
|
|
}
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doTest(string s)
|
|
|
|
{
|
|
|
|
EvalState state;
|
|
|
|
Expr e = parseExprFromString(state, s, "/");
|
2010-03-25 00:40:00 +01:00
|
|
|
printMsg(lvlError, format(">>>>> %1%") % e);
|
2010-03-24 13:41:08 +01:00
|
|
|
Value v;
|
2010-03-25 16:38:37 +01:00
|
|
|
strictEval(0, e, v);
|
2010-03-24 13:11:38 +01:00
|
|
|
printMsg(lvlError, format("result: %1%") % v);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run(Strings args)
|
|
|
|
{
|
2010-03-24 13:41:08 +01:00
|
|
|
printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
|
2010-03-23 18:30:50 +01:00
|
|
|
|
|
|
|
doTest("123");
|
|
|
|
doTest("{ x = 1; y = 2; }");
|
|
|
|
doTest("{ x = 1; y = 2; }.y");
|
|
|
|
doTest("rec { x = 1; y = x; }.y");
|
2010-03-24 12:06:05 +01:00
|
|
|
doTest("(x: x) 1");
|
|
|
|
doTest("(x: y: y) 1 2");
|
2010-03-25 00:40:00 +01:00
|
|
|
doTest("x: x");
|
|
|
|
doTest("({x, y}: x) { x = 1; y = 2; }");
|
|
|
|
doTest("({x, y}@args: args.x) { x = 1; y = 2; }");
|
2010-03-25 13:19:41 +01:00
|
|
|
doTest("(args@{x, y}: args.x) { x = 1; y = 2; }");
|
2010-03-25 13:45:23 +01:00
|
|
|
doTest("({x ? 1}: x) { }");
|
|
|
|
doTest("({x ? 1, y ? x}: y) { x = 2; }");
|
|
|
|
doTest("({x, y, ...}: x) { x = 1; y = 2; z = 3; }");
|
|
|
|
doTest("({x, y, ...}@args: args.z) { x = 1; y = 2; z = 3; }");
|
2010-03-25 13:51:14 +01:00
|
|
|
//doTest("({x ? y, y ? x}: y) { }");
|
2010-03-25 14:10:04 +01:00
|
|
|
doTest("let x = 1; in x");
|
2010-03-25 15:51:04 +01:00
|
|
|
doTest("with { x = 1; }; x");
|
|
|
|
doTest("let x = 2; in with { x = 1; }; x"); // => 2
|
|
|
|
doTest("with { x = 1; }; with { x = 2; }; x"); // => 1
|
2010-03-25 16:38:37 +01:00
|
|
|
doTest("[ 1 2 3 ]");
|
|
|
|
doTest("[ 1 2 ] ++ [ 3 4 5 ]");
|
2010-03-24 13:11:38 +01:00
|
|
|
|
|
|
|
printMsg(lvlError, format("alloced %1% values") % nrValues);
|
2010-03-24 13:41:08 +01:00
|
|
|
printMsg(lvlError, format("alloced %1% environments") % nrEnvs);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string programId = "eval-test";
|