Add some support code for nix-repl

This commit is contained in:
Eelco Dolstra 2013-09-02 18:34:04 +02:00
parent 92077b4547
commit 57d18df7d0
3 changed files with 26 additions and 12 deletions

View file

@ -143,8 +143,8 @@ EvalState::EvalState()
, sIgnoreNulls(symbols.create("__ignoreNulls")) , sIgnoreNulls(symbols.create("__ignoreNulls"))
, repair(false) , repair(false)
, baseEnv(allocEnv(128)) , baseEnv(allocEnv(128))
, baseEnvDispl(0)
, staticBaseEnv(false, 0) , staticBaseEnv(false, 0)
, baseEnvDispl(0)
{ {
nrEnvs = nrValuesInEnvs = nrValues = nrListElems = 0; nrEnvs = nrValuesInEnvs = nrValues = nrListElems = 0;
nrAttrsets = nrOpUpdates = nrOpUpdateValuesCopied = 0; nrAttrsets = nrOpUpdates = nrOpUpdateValuesCopied = 0;
@ -456,6 +456,13 @@ void EvalState::evalFile(const Path & path, Value & v)
} }
void EvalState::resetFileCache()
{
fileEvalCache.clear();
parseTrees.clear();
}
void EvalState::eval(Expr * e, Value & v) void EvalState::eval(Expr * e, Value & v)
{ {
e->eval(*this, baseEnv, v); e->eval(*this, baseEnv, v);

View file

@ -130,12 +130,15 @@ public:
Expr * parseExprFromFile(Path path); Expr * parseExprFromFile(Path path);
/* Parse a Nix expression from the specified string. */ /* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(const string & s, const Path & basePath, StaticEnv & staticEnv);
Expr * parseExprFromString(const string & s, const Path & basePath); Expr * parseExprFromString(const string & s, const Path & basePath);
/* Evaluate an expression read from the given file to normal /* Evaluate an expression read from the given file to normal
form. */ form. */
void evalFile(const Path & path, Value & v); void evalFile(const Path & path, Value & v);
void resetFileCache();
/* Look up a file in the search path. */ /* Look up a file in the search path. */
Path findFile(const string & path); Path findFile(const string & path);
@ -184,21 +187,19 @@ public:
path. Nothing is copied to the store. */ path. Nothing is copied to the store. */
Path coerceToPath(Value & v, PathSet & context); Path coerceToPath(Value & v, PathSet & context);
private: public:
/* The base environment, containing the builtin functions and /* The base environment, containing the builtin functions and
values. */ values. */
Env & baseEnv; Env & baseEnv;
unsigned int baseEnvDispl;
public:
/* The same, but used during parsing to resolve variables. */ /* The same, but used during parsing to resolve variables. */
StaticEnv staticBaseEnv; // !!! should be private StaticEnv staticBaseEnv; // !!! should be private
private: private:
unsigned int baseEnvDispl;
void createBaseEnv(); void createBaseEnv();
void addConstant(const string & name, Value & v); void addConstant(const string & name, Value & v);
@ -212,8 +213,8 @@ private:
friend class ExprAttrs; friend class ExprAttrs;
friend class ExprLet; friend class ExprLet;
Expr * parse(const char * text, Expr * parse(const char * text, const Path & path,
const Path & path, const Path & basePath); const Path & basePath, StaticEnv & staticEnv);
public: public:

View file

@ -481,7 +481,7 @@ namespace nix {
Expr * EvalState::parse(const char * text, Expr * EvalState::parse(const char * text,
const Path & path, const Path & basePath) const Path & path, const Path & basePath, StaticEnv & staticEnv)
{ {
yyscan_t scanner; yyscan_t scanner;
ParseData data(*this); ParseData data(*this);
@ -496,7 +496,7 @@ Expr * EvalState::parse(const char * text,
if (res) throw ParseError(data.error); if (res) throw ParseError(data.error);
try { try {
data.result->bindVars(staticBaseEnv); data.result->bindVars(staticEnv);
} catch (Error & e) { } catch (Error & e) {
throw ParseError(format("%1%, in `%2%'") % e.msg() % path); throw ParseError(format("%1%, in `%2%'") % e.msg() % path);
} }
@ -527,7 +527,7 @@ Expr * EvalState::parseExprFromFile(Path path)
tree cache. */ tree cache. */
Expr * e = parseTrees[path]; Expr * e = parseTrees[path];
if (!e) { if (!e) {
e = parse(readFile(path).c_str(), path, dirOf(path)); e = parse(readFile(path).c_str(), path, dirOf(path), staticBaseEnv);
parseTrees[path] = e; parseTrees[path] = e;
} }
@ -535,9 +535,15 @@ Expr * EvalState::parseExprFromFile(Path path)
} }
Expr * EvalState::parseExprFromString(const string & s, const Path & basePath, StaticEnv & staticEnv)
{
return parse(s.c_str(), "(string)", basePath, staticEnv);
}
Expr * EvalState::parseExprFromString(const string & s, const Path & basePath) Expr * EvalState::parseExprFromString(const string & s, const Path & basePath)
{ {
return parse(s.c_str(), "(string)", basePath); return parseExprFromString(s, basePath, staticBaseEnv);
} }