Add a command :t for showing the type of an expression

This commit is contained in:
Eelco Dolstra 2013-09-02 16:00:48 +00:00
parent 287c88ca59
commit 3202206d1d

View file

@ -30,6 +30,7 @@ struct NixRepl
void processLine(string line); void processLine(string line);
void addVar(const Symbol & name, Value * v); void addVar(const Symbol & name, Value * v);
Expr * parseString(string s); Expr * parseString(string s);
void evalString(string s, Value & v);
}; };
@ -91,22 +92,26 @@ void NixRepl::mainLoop()
void NixRepl::processLine(string line) void NixRepl::processLine(string line)
{ {
if (string(line, 0, 2) == ":a") { if (string(line, 0, 2) == ":a") {
Expr * e = parseString(string(line, 2));
Value v; Value v;
e->eval(state, *env, v); evalString(string(line, 2), v);
state.forceAttrs(v); state.forceAttrs(v);
foreach (Bindings::iterator, i, *v.attrs) foreach (Bindings::iterator, i, *v.attrs)
addVar(i->name, i->value); addVar(i->name, i->value);
} }
else if (string(line, 0, 2) == ":t") {
Value v;
evalString(string(line, 2), v);
std::cout << showType(v) << std::endl;
}
else if (string(line, 0, 1) == ":") { else if (string(line, 0, 1) == ":") {
throw Error(format("unknown command %1%") % string(line, 0, 2)); throw Error(format("unknown command %1%") % string(line, 0, 2));
} }
else { else {
Expr * e = parseString(line);
Value v; Value v;
e->eval(state, *env, v); evalString(line, v);
state.strictForceValue(v); state.strictForceValue(v);
std::cout << v << std::endl; std::cout << v << std::endl;
} }
@ -127,6 +132,14 @@ Expr * NixRepl::parseString(string s)
} }
void NixRepl::evalString(string s, Value & v)
{
Expr * e = parseString(s);
e->eval(state, *env, v);
state.forceValue(v);
}
void run(nix::Strings args) void run(nix::Strings args)
{ {
NixRepl repl; NixRepl repl;