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;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct Env_ * Env;
|
|
|
|
typedef struct Value_ * Value;
|
|
|
|
|
|
|
|
typedef std::map<string, Value> Bindings;
|
|
|
|
|
|
|
|
|
|
|
|
struct Env_
|
|
|
|
{
|
|
|
|
Env up;
|
|
|
|
Bindings bindings;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
tInt = 1,
|
|
|
|
tAttrs,
|
2010-03-24 12:06:05 +01:00
|
|
|
tThunk,
|
|
|
|
tLambda
|
2010-03-23 18:30:50 +01:00
|
|
|
} ValueType;
|
|
|
|
|
|
|
|
|
|
|
|
struct Value_
|
|
|
|
{
|
|
|
|
ValueType type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int integer;
|
|
|
|
Bindings * attrs;
|
|
|
|
struct {
|
|
|
|
Env env;
|
|
|
|
Expr expr;
|
|
|
|
} thunk;
|
2010-03-24 12:06:05 +01:00
|
|
|
struct {
|
|
|
|
Env env;
|
|
|
|
Pattern pat;
|
|
|
|
Expr body;
|
|
|
|
} lambda;
|
2010-03-23 18:30:50 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & str, Value_ & v)
|
|
|
|
{
|
|
|
|
switch (v.type) {
|
|
|
|
case tInt:
|
|
|
|
str << v.integer;
|
|
|
|
break;
|
|
|
|
case tAttrs:
|
|
|
|
str << "{ ";
|
|
|
|
foreach (Bindings::iterator, i, *v.attrs) {
|
|
|
|
str << i->first << " = " << *i->second << "; ";
|
|
|
|
}
|
|
|
|
str << "}";
|
|
|
|
break;
|
|
|
|
case tThunk:
|
|
|
|
str << "<CODE>";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-24 13:11:38 +01:00
|
|
|
void eval(Env env, Expr e, Value v);
|
2010-03-23 18:30:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
void forceValue(Value v)
|
|
|
|
{
|
|
|
|
if (v->type != tThunk) return;
|
2010-03-24 13:11:38 +01:00
|
|
|
eval(v->thunk.env, v->thunk.expr, v);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Value lookupVar(Env env, const string & name)
|
|
|
|
{
|
|
|
|
for ( ; env; env = env->up) {
|
|
|
|
Value v = env->bindings[name];
|
|
|
|
if (v) return v;
|
|
|
|
}
|
|
|
|
throw Error("undefined variable");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-24 13:11:38 +01:00
|
|
|
unsigned long nrValues = 0;
|
|
|
|
|
|
|
|
Value allocValue()
|
|
|
|
{
|
|
|
|
nrValues++;
|
|
|
|
return new Value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void eval(Env env, Expr e, Value v)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
|
|
|
printMsg(lvlError, format("eval: %1%") % e);
|
|
|
|
|
|
|
|
ATerm name;
|
|
|
|
if (matchVar(e, name)) {
|
2010-03-24 13:11:38 +01:00
|
|
|
Value v2 = lookupVar(env, aterm2String(name));
|
|
|
|
forceValue(v2);
|
|
|
|
*v = *v2;
|
|
|
|
return;
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int n;
|
|
|
|
if (matchInt(e, n)) {
|
|
|
|
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)) {
|
|
|
|
v->type = tAttrs;
|
|
|
|
v->attrs = new Bindings;
|
|
|
|
ATerm e2, pos;
|
|
|
|
for (ATermIterator i(es); i; ++i) {
|
|
|
|
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
|
2010-03-24 13:11:38 +01:00
|
|
|
Value v2 = allocValue();
|
2010-03-23 18:30:50 +01:00
|
|
|
v2->type = tThunk;
|
|
|
|
v2->thunk.env = env;
|
|
|
|
v2->thunk.expr = e2;
|
|
|
|
(*v->attrs)[aterm2String(name)] = v2;
|
|
|
|
}
|
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)) {
|
|
|
|
Env env2 = new Env_;
|
|
|
|
env2->up = env;
|
|
|
|
|
|
|
|
v->type = tAttrs;
|
|
|
|
v->attrs = &env2->bindings;
|
|
|
|
ATerm name, e2, pos;
|
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
|
|
|
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
|
2010-03-24 13:11:38 +01:00
|
|
|
Value v2 = allocValue();
|
2010-03-23 18:30:50 +01:00
|
|
|
v2->type = tThunk;
|
|
|
|
v2->thunk.env = env2;
|
|
|
|
v2->thunk.expr = e2;
|
|
|
|
env2->bindings[aterm2String(name)] = v2;
|
|
|
|
}
|
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
|
|
|
Expr 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-23 18:30:50 +01:00
|
|
|
if (v->type != tAttrs) throw TypeError("expected attribute set");
|
|
|
|
Value v2 = (*v->attrs)[aterm2String(name)];
|
|
|
|
if (!v2) throw TypeError("attribute not found");
|
|
|
|
forceValue(v2);
|
2010-03-24 13:11:38 +01:00
|
|
|
*v = *v2;
|
|
|
|
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)) {
|
|
|
|
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);
|
|
|
|
if (v->type != tLambda) throw TypeError("expected function");
|
|
|
|
if (!matchVarPat(v->lambda.pat, name)) throw Error("not implemented");
|
2010-03-24 12:06:05 +01:00
|
|
|
|
2010-03-24 13:11:38 +01:00
|
|
|
Value arg_ = allocValue();
|
2010-03-24 12:06:05 +01:00
|
|
|
arg_->type = tThunk;
|
|
|
|
arg_->thunk.env = env;
|
|
|
|
arg_->thunk.expr = arg;
|
|
|
|
|
|
|
|
Env env2 = new Env_;
|
|
|
|
env2->up = env;
|
|
|
|
env2->bindings[aterm2String(name)] = arg_;
|
|
|
|
|
2010-03-24 13:11:38 +01:00
|
|
|
eval(env2, v->lambda.body, v);
|
|
|
|
return;
|
2010-03-24 12:06:05 +01:00
|
|
|
}
|
|
|
|
|
2010-03-23 18:30:50 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doTest(string s)
|
|
|
|
{
|
|
|
|
EvalState state;
|
|
|
|
Expr e = parseExprFromString(state, s, "/");
|
|
|
|
printMsg(lvlError, format("%1%") % e);
|
2010-03-24 13:11:38 +01:00
|
|
|
Value_ v;
|
|
|
|
eval(0, e, &v);
|
|
|
|
printMsg(lvlError, format("result: %1%") % v);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run(Strings args)
|
|
|
|
{
|
|
|
|
printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value_));
|
|
|
|
|
|
|
|
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-24 13:11:38 +01:00
|
|
|
|
2010-03-23 18:30:50 +01:00
|
|
|
//Expr e = parseExprFromString(state, "let x = \"a\"; in x + \"b\"", "/");
|
|
|
|
//Expr e = parseExprFromString(state, "(x: x + \"b\") \"a\"", "/");
|
|
|
|
//Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/");
|
|
|
|
//Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/");
|
2010-03-24 13:11:38 +01:00
|
|
|
|
|
|
|
printMsg(lvlError, format("alloced %1% values") % nrValues);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string programId = "eval-test";
|