On reload, wipe the environment

This commit is contained in:
Eelco Dolstra 2013-09-09 17:06:14 +02:00
parent 498f8b0485
commit e91160021f

View file

@ -29,6 +29,7 @@ struct NixRepl
Strings loadedFiles; Strings loadedFiles;
const static int envSize = 32768;
StaticEnv staticEnv; StaticEnv staticEnv;
Env * env; Env * env;
int displ; int displ;
@ -43,6 +44,7 @@ struct NixRepl
bool getLine(string & line); bool getLine(string & line);
bool processLine(string line); bool processLine(string line);
void loadFile(const Path & path); void loadFile(const Path & path);
void initEnv();
void reloadFiles(); void reloadFiles();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol & name, Value & v); void addVarToScope(const Symbol & name, Value & v);
@ -75,10 +77,6 @@ NixRepl::NixRepl()
{ {
curDir = absPath("."); curDir = absPath(".");
env = &state.allocEnv(32768);
env->up = &state.baseEnv;
displ = 0;
store = openStore(); store = openStore();
} }
@ -90,10 +88,8 @@ void NixRepl::mainLoop(const Strings & args)
foreach (Strings::const_iterator, i, args) foreach (Strings::const_iterator, i, args)
loadedFiles.push_back(*i); loadedFiles.push_back(*i);
if (!loadedFiles.empty()) { reloadFiles();
reloadFiles(); if (!loadedFiles.empty()) std::cout << std::endl;
std::cout << std::endl;
}
using_history(); using_history();
read_history(0); read_history(0);
@ -383,8 +379,20 @@ void NixRepl::loadFile(const Path & path)
} }
void NixRepl::initEnv()
{
env = &state.allocEnv(envSize);
env->up = &state.baseEnv;
displ = 0;
varNames.clear();
staticEnv.vars.clear();
}
void NixRepl::reloadFiles() void NixRepl::reloadFiles()
{ {
initEnv();
Strings old = loadedFiles; Strings old = loadedFiles;
loadedFiles.clear(); loadedFiles.clear();
@ -407,6 +415,8 @@ void NixRepl::addAttrsToScope(Value & attrs)
void NixRepl::addVarToScope(const Symbol & name, Value & v) void NixRepl::addVarToScope(const Symbol & name, Value & v)
{ {
if (displ >= envSize)
throw Error("environment full; cannot add more variables");
staticEnv.vars[name] = displ; staticEnv.vars[name] = displ;
env->values[displ++] = &v; env->values[displ++] = &v;
varNames.insert((string) name); varNames.insert((string) name);