This commit is contained in:
Eelco Dolstra 2010-03-29 10:13:51 +00:00
parent e3f32ac5af
commit 52090d2418

View file

@ -125,6 +125,9 @@ std::ostream & operator << (std::ostream & str, Value & v)
case tString: case tString:
str << "\"" << v.string.s << "\""; // !!! escaping str << "\"" << v.string.s << "\""; // !!! escaping
break; break;
case tNull:
str << "true";
break;
case tAttrs: case tAttrs:
str << "{ "; str << "{ ";
foreach (Bindings::iterator, i, *v.attrs) foreach (Bindings::iterator, i, *v.attrs)
@ -308,6 +311,16 @@ static Env & allocEnv()
char * p1 = 0, * p2 = 0; char * p1 = 0, * p2 = 0;
static bool evalBool(Env & env, Expr e)
{
Value v;
eval(env, e, v);
if (v.type != tBool)
throw TypeError(format("value is %1% while a Boolean was expected") % showType(v));
return v.boolean;
}
static void eval(Env & env, Expr e, Value & v) static void eval(Env & env, Expr e, Value & v)
{ {
char c; char c;
@ -569,6 +582,17 @@ static void eval(Env & env, Expr e, Value & v)
return; return;
} }
Expr e3;
if (matchIf(e, e1, e2, e3)) {
eval(env, evalBool(env, e1) ? e2 : e3, v);
return;
}
if (matchOpOr(e, e1, e2)) {
mkBool(v, evalBool(env, e1) || evalBool(env, e2));
return;
}
throw Error("unsupported term"); throw Error("unsupported term");
} }
@ -633,6 +657,10 @@ void doTest(string s)
v.type = tBool; v.type = tBool;
v.boolean = false; v.boolean = false;
} }
{
Value & v = baseEnv.bindings[toATerm("null")];
v.type = tNull;
}
/* Add primops to the base environment. */ /* Add primops to the base environment. */
addPrimOp(baseEnv, "__head", 1, prim_head); addPrimOp(baseEnv, "__head", 1, prim_head);
@ -689,6 +717,10 @@ void run(Strings args)
doTest("null"); doTest("null");
doTest("\"foo\""); doTest("\"foo\"");
doTest("let s = \"bar\"; in \"foo${s}\""); doTest("let s = \"bar\"; in \"foo${s}\"");
doTest("if true then 1 else 2");
doTest("if false then 1 else 2");
doTest("if false || true then 1 else 2");
doTest("let x = x; in if true || x then 1 else 2");
printMsg(lvlError, format("alloced %1% values") % nrValues); printMsg(lvlError, format("alloced %1% values") % nrValues);
printMsg(lvlError, format("alloced %1% environments") % nrEnvs); printMsg(lvlError, format("alloced %1% environments") % nrEnvs);