* Store values in environments.

This commit is contained in:
Eelco Dolstra 2010-03-24 12:41:08 +00:00
parent b70bd8fe56
commit 0fd3648d34

View file

@ -9,15 +9,15 @@
using namespace nix; using namespace nix;
typedef struct Env_ * Env; struct Env;
typedef struct Value_ * Value; struct Value;
typedef std::map<string, Value> Bindings; typedef std::map<string, Value> Bindings;
struct Env_ struct Env
{ {
Env up; Env * up;
Bindings bindings; Bindings bindings;
}; };
@ -30,7 +30,7 @@ typedef enum {
} ValueType; } ValueType;
struct Value_ struct Value
{ {
ValueType type; ValueType type;
union union
@ -38,11 +38,11 @@ struct Value_
int integer; int integer;
Bindings * attrs; Bindings * attrs;
struct { struct {
Env env; Env * env;
Expr expr; Expr expr;
} thunk; } thunk;
struct { struct {
Env env; Env * env;
Pattern pat; Pattern pat;
Expr body; Expr body;
} lambda; } lambda;
@ -50,7 +50,7 @@ struct Value_
}; };
std::ostream & operator << (std::ostream & str, Value_ & v) std::ostream & operator << (std::ostream & str, Value & v)
{ {
switch (v.type) { switch (v.type) {
case tInt: case tInt:
@ -58,9 +58,8 @@ std::ostream & operator << (std::ostream & str, Value_ & v)
break; break;
case tAttrs: case tAttrs:
str << "{ "; str << "{ ";
foreach (Bindings::iterator, i, *v.attrs) { foreach (Bindings::iterator, i, *v.attrs)
str << i->first << " = " << *i->second << "; "; str << i->first << " = " << i->second << "; ";
}
str << "}"; str << "}";
break; break;
case tThunk: case tThunk:
@ -73,21 +72,21 @@ std::ostream & operator << (std::ostream & str, Value_ & v)
} }
void eval(Env env, Expr e, Value v); void eval(Env * env, Expr e, Value & v);
void forceValue(Value v) void forceValue(Value & v)
{ {
if (v->type != tThunk) return; if (v.type != tThunk) return;
eval(v->thunk.env, v->thunk.expr, v); eval(v.thunk.env, v.thunk.expr, v);
} }
Value lookupVar(Env env, const string & name) Value * lookupVar(Env * env, const string & name)
{ {
for ( ; env; env = env->up) { for ( ; env; env = env->up) {
Value v = env->bindings[name]; Bindings::iterator i = env->bindings.find(name);
if (v) return v; if (i != env->bindings.end()) return &i->second;
} }
throw Error("undefined variable"); throw Error("undefined variable");
} }
@ -95,63 +94,65 @@ Value lookupVar(Env env, const string & name)
unsigned long nrValues = 0; unsigned long nrValues = 0;
Value allocValue() unsigned long nrEnvs = 0;
Env * allocEnv()
{ {
nrValues++; nrEnvs++;
return new Value_; return new Env;
} }
void eval(Env env, Expr e, Value v) void eval(Env * env, Expr e, Value & v)
{ {
printMsg(lvlError, format("eval: %1%") % e); printMsg(lvlError, format("eval: %1%") % e);
ATerm name; ATerm name;
if (matchVar(e, name)) { if (matchVar(e, name)) {
Value v2 = lookupVar(env, aterm2String(name)); Value * v2 = lookupVar(env, aterm2String(name));
forceValue(v2); forceValue(*v2);
*v = *v2; v = *v2;
return; return;
} }
int n; int n;
if (matchInt(e, n)) { if (matchInt(e, n)) {
v->type = tInt; v.type = tInt;
v->integer = n; v.integer = n;
return; return;
} }
ATermList es; ATermList es;
if (matchAttrs(e, es)) { if (matchAttrs(e, es)) {
v->type = tAttrs; v.type = tAttrs;
v->attrs = new Bindings; v.attrs = new Bindings;
ATerm e2, pos; ATerm e2, pos;
for (ATermIterator i(es); i; ++i) { for (ATermIterator i(es); i; ++i) {
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */ if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
Value v2 = allocValue(); Value & v2 = (*v.attrs)[aterm2String(name)];
v2->type = tThunk; nrValues++;
v2->thunk.env = env; v2.type = tThunk;
v2->thunk.expr = e2; v2.thunk.env = env;
(*v->attrs)[aterm2String(name)] = v2; v2.thunk.expr = e2;
} }
return; return;
} }
ATermList rbnds, nrbnds; ATermList rbnds, nrbnds;
if (matchRec(e, rbnds, nrbnds)) { if (matchRec(e, rbnds, nrbnds)) {
Env env2 = new Env_; Env * env2 = allocEnv();
env2->up = env; env2->up = env;
v->type = tAttrs; v.type = tAttrs;
v->attrs = &env2->bindings; v.attrs = &env2->bindings;
ATerm name, e2, pos; ATerm name, e2, pos;
for (ATermIterator i(rbnds); i; ++i) { for (ATermIterator i(rbnds); i; ++i) {
if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */ if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
Value v2 = allocValue(); Value & v2 = env2->bindings[aterm2String(name)];
v2->type = tThunk; nrValues++;
v2->thunk.env = env2; v2.type = tThunk;
v2->thunk.expr = e2; v2.thunk.env = env2;
env2->bindings[aterm2String(name)] = v2; v2.thunk.expr = e2;
} }
return; return;
@ -160,39 +161,39 @@ void eval(Env env, Expr e, Value v)
Expr e2; Expr e2;
if (matchSelect(e, e2, name)) { if (matchSelect(e, e2, name)) {
eval(env, e2, v); eval(env, e2, v);
if (v->type != tAttrs) throw TypeError("expected attribute set"); if (v.type != tAttrs) throw TypeError("expected attribute set");
Value v2 = (*v->attrs)[aterm2String(name)]; Bindings::iterator i = v.attrs->find(aterm2String(name));
if (!v2) throw TypeError("attribute not found"); if (i == v.attrs->end()) throw TypeError("attribute not found");
forceValue(v2); forceValue(i->second);
*v = *v2; v = i->second;
return; return;
} }
Pattern pat; Expr body; Pos pos; Pattern pat; Expr body; Pos pos;
if (matchFunction(e, pat, body, pos)) { if (matchFunction(e, pat, body, pos)) {
v->type = tLambda; v.type = tLambda;
v->lambda.env = env; v.lambda.env = env;
v->lambda.pat = pat; v.lambda.pat = pat;
v->lambda.body = body; v.lambda.body = body;
return; return;
} }
Expr fun, arg; Expr fun, arg;
if (matchCall(e, fun, arg)) { if (matchCall(e, fun, arg)) {
eval(env, fun, v); eval(env, fun, v);
if (v->type != tLambda) throw TypeError("expected function"); if (v.type != tLambda) throw TypeError("expected function");
if (!matchVarPat(v->lambda.pat, name)) throw Error("not implemented"); if (!matchVarPat(v.lambda.pat, name)) throw Error("not implemented");
Value arg_ = allocValue(); Env * env2 = allocEnv();
arg_->type = tThunk;
arg_->thunk.env = env;
arg_->thunk.expr = arg;
Env env2 = new Env_;
env2->up = env; env2->up = env;
env2->bindings[aterm2String(name)] = arg_;
Value & arg_ = env2->bindings[aterm2String(name)];
nrValues++;
arg_.type = tThunk;
arg_.thunk.env = env;
arg_.thunk.expr = arg;
eval(env2, v->lambda.body, v); eval(env2, v.lambda.body, v);
return; return;
} }
@ -205,15 +206,15 @@ void doTest(string s)
EvalState state; EvalState state;
Expr e = parseExprFromString(state, s, "/"); Expr e = parseExprFromString(state, s, "/");
printMsg(lvlError, format("%1%") % e); printMsg(lvlError, format("%1%") % e);
Value_ v; Value v;
eval(0, e, &v); eval(0, e, v);
printMsg(lvlError, format("result: %1%") % v); printMsg(lvlError, format("result: %1%") % v);
} }
void run(Strings args) void run(Strings args)
{ {
printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value_)); printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
doTest("123"); doTest("123");
doTest("{ x = 1; y = 2; }"); doTest("{ x = 1; y = 2; }");
@ -228,6 +229,7 @@ void run(Strings args)
//Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/"); //Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/");
printMsg(lvlError, format("alloced %1% values") % nrValues); printMsg(lvlError, format("alloced %1% values") % nrValues);
printMsg(lvlError, format("alloced %1% environments") % nrEnvs);
} }