diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-10-30 16:48:26 +0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-10-30 16:48:26 +0000 |
commit | 403cb9327f5c298cb6a85a87241962df4a90857b (patch) | |
tree | 7845c52eef1ce3289cd8a32e083542dcdfceecc8 /src/fix-ng | |
parent | 9f8f39aa3cdb54532a85e41f14985fc6a530fb36 (diff) |
* Factor out evaluation into a separate file.
Diffstat (limited to 'src/fix-ng')
-rw-r--r-- | src/fix-ng/Makefile.am | 2 | ||||
-rw-r--r-- | src/fix-ng/eval.cc | 45 | ||||
-rw-r--r-- | src/fix-ng/eval.hh | 31 | ||||
-rw-r--r-- | src/fix-ng/fix.cc | 65 |
4 files changed, 87 insertions, 56 deletions
diff --git a/src/fix-ng/Makefile.am b/src/fix-ng/Makefile.am index 88f1f4fe9..b0e90450e 100644 --- a/src/fix-ng/Makefile.am +++ b/src/fix-ng/Makefile.am @@ -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 diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc new file mode 100644 index 000000000..c0f4680a4 --- /dev/null +++ b/src/fix-ng/eval.cc @@ -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); +} diff --git a/src/fix-ng/eval.hh b/src/fix-ng/eval.hh new file mode 100644 index 000000000..5fcb648a7 --- /dev/null +++ b/src/fix-ng/eval.hh @@ -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 */ diff --git a/src/fix-ng/fix.cc b/src/fix-ng/fix.cc index 05e27c506..fb98dc697 100644 --- a/src/fix-ng/fix.cc +++ b/src/fix-ng/fix.cc @@ -1,37 +1,15 @@ #include <map> #include <iostream> -#include "parser.hh" #include "globals.hh" #include "normalise.hh" #include "shared.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); +#include "expr.hh" +#include "eval.hh" #if 0 +#if 0 static Path searchPath(const Paths & searchDirs, const Path & relPath) { if (string(relPath, 0, 1) == "/") return 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; |