From 403cb9327f5c298cb6a85a87241962df4a90857b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 30 Oct 2003 16:48:26 +0000 Subject: [PATCH] * Factor out evaluation into a separate file. --- src/fix-ng/Makefile.am | 2 +- src/fix-ng/eval.cc | 45 +++++++++++++++++++++++++++++ src/fix-ng/eval.hh | 31 ++++++++++++++++++++ src/fix-ng/fix.cc | 65 +++++++----------------------------------- 4 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 src/fix-ng/eval.cc create mode 100644 src/fix-ng/eval.hh 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 + +#include "fix-expr.hh" + + +typedef map NormalForms; +//typedef map PkgPaths; +//typedef map 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,36 +1,14 @@ #include #include -#include "parser.hh" #include "globals.hh" #include "normalise.hh" #include "shared.hh" +#include "expr.hh" +#include "eval.hh" -typedef map NormalForms; -typedef map PkgPaths; -typedef map 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;