* Factor out evaluation into a separate file.

This commit is contained in:
Eelco Dolstra 2003-10-30 16:48:26 +00:00
parent 9f8f39aa3c
commit 403cb9327f
4 changed files with 87 additions and 56 deletions

View File

@ -1,6 +1,6 @@
bin_PROGRAMS = fix-ng
fix_ng_SOURCES = fix-expr.cc parser.cc fix.cc
fix_ng_SOURCES = fix-expr.cc parser.cc eval.cc fix.cc
fix_ng_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \
-L../../externals/inst/lib -ldb_cxx -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm

45
src/fix-ng/eval.cc Normal file
View File

@ -0,0 +1,45 @@
#include "eval.hh"
#include "expr.hh"
#include "parser.hh"
EvalState::EvalState()
{
blackHole = ATmake("BlackHole()");
if (!blackHole) throw Error("cannot build black hole");
}
Expr evalExpr2(EvalState & state, Expr e)
{
return e;
}
Expr evalExpr(EvalState & state, Expr e)
{
Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e));
/* Consult the memo table to quickly get the normal form of
previously evaluated expressions. */
NormalForms::iterator i = state.normalForms.find(e);
if (i != state.normalForms.end()) {
if (i->second == state.blackHole)
throw badTerm("infinite recursion", e);
return i->second;
}
/* Otherwise, evaluate and memoize. */
state.normalForms[e] = state.blackHole;
Expr nf = evalExpr2(state, e);
state.normalForms[e] = nf;
return nf;
}
Expr evalFile(EvalState & state, const Path & path)
{
Nest nest(lvlTalkative, format("evaluating file `%1%'") % path);
Expr e = parseExprFromFile(path);
return evalExpr(state, e);
}

31
src/fix-ng/eval.hh Normal file
View File

@ -0,0 +1,31 @@
#ifndef __EVAL_H
#define __EVAL_H
#include <map>
#include "fix-expr.hh"
typedef map<Expr, Expr> NormalForms;
//typedef map<Path, PathSet> PkgPaths;
//typedef map<Path, Hash> PkgHashes;
struct EvalState
{
NormalForms normalForms;
// PkgPaths pkgPaths;
// PkgHashes pkgHashes; /* normalised package hashes */
Expr blackHole;
EvalState();
};
/* Evaluate an expression to normal form. */
Expr evalExpr(EvalState & state, Expr e);
/* Evaluate an expression read from the given file to normal form. */
Expr evalFile(EvalState & state, const Path & path);
#endif /* !__EVAL_H */

View File

@ -1,36 +1,14 @@
#include <map>
#include <iostream>
#include "parser.hh"
#include "globals.hh"
#include "normalise.hh"
#include "shared.hh"
#include "expr.hh"
#include "eval.hh"
typedef map<ATerm, ATerm> NormalForms;
typedef map<Path, PathSet> PkgPaths;
typedef map<Path, Hash> PkgHashes;
struct EvalState
{
Paths searchDirs;
NormalForms normalForms;
PkgPaths pkgPaths;
PkgHashes pkgHashes; /* normalised package hashes */
Expr blackHole;
EvalState()
{
blackHole = ATmake("BlackHole()");
if (!blackHole) throw Error("cannot build black hole");
}
};
static Expr evalFile(EvalState & state, const Path & path);
static Expr evalExpr(EvalState & state, Expr e);
#if 0
#if 0
static Path searchPath(const Paths & searchDirs, const Path & relPath)
{
@ -380,35 +358,7 @@ static Expr evalExpr2(EvalState & state, Expr e)
/* Barf. */
throw badTerm("invalid expression", e);
}
static Expr evalExpr(EvalState & state, Expr e)
{
Nest nest(lvlVomit, format("evaluating expression: %1%") % printTerm(e));
/* Consult the memo table to quickly get the normal form of
previously evaluated expressions. */
NormalForms::iterator i = state.normalForms.find(e);
if (i != state.normalForms.end()) {
if (i->second == state.blackHole)
throw badTerm("infinite recursion", e);
return i->second;
}
/* Otherwise, evaluate and memoize. */
state.normalForms[e] = state.blackHole;
Expr nf = evalExpr2(state, e);
state.normalForms[e] = nf;
return nf;
}
static Expr evalFile(EvalState & state, const Path & path)
{
Nest nest(lvlTalkative, format("evaluating file `%1%'") % path);
Expr e = parseExprFromFile(path);
return evalExpr(state, e);
}
#endif
static Expr evalStdin(EvalState & state)
@ -444,20 +394,25 @@ void run(Strings args)
Strings files;
bool readStdin = false;
#if 0
state.searchDirs.push_back(".");
state.searchDirs.push_back(nixDataDir + "/fix");
#endif
for (Strings::iterator it = args.begin();
it != args.end(); )
{
string arg = *it++;
#if 0
if (arg == "--includedir" || arg == "-I") {
if (it == args.end())
throw UsageError(format("argument required in `%1%'") % arg);
state.searchDirs.push_back(*it++);
}
else if (arg == "--verbose" || arg == "-v")
else
#endif
if (arg == "--verbose" || arg == "-v")
verbosity = (Verbosity) ((int) verbosity + 1);
else if (arg == "-")
readStdin = true;