* Simplify the evaluator.

This commit is contained in:
Eelco Dolstra 2003-06-17 13:37:44 +00:00
parent c739e20585
commit a7ab242fb4
3 changed files with 50 additions and 63 deletions

View File

@ -186,30 +186,31 @@ static void checkPlatform(string platform)
} }
string printExpr(Expr e)
{
char * s = ATwriteToString(e);
return s;
}
/* Throw an exception with an error message containing the given /* Throw an exception with an error message containing the given
aterm. */ aterm. */
static Error badTerm(const string & msg, Expr e) static Error badTerm(const string & msg, Expr e)
{ {
char * s = ATwriteToString(e); return Error(msg + ", in `" + printExpr(e) + "'");
return Error(msg + ", in `" + s + "'");
} }
/* Hash an expression. Hopefully the representation used by Hash hashExpr(Expr e)
ATwriteToString() won't change, otherwise all hashes will
change. */
static Hash hashExpr(Expr e)
{ {
char * s = ATwriteToString(e); return hashString(printExpr(e));
debug(s);
return hashString(s);
} }
/* Evaluate an expression; the result must be a string. */ /* Evaluate an expression; the result must be a string. */
static string evalString(Expr e) static string evalString(Expr e)
{ {
e = evalValue(e).e; e = evalValue(e);
char * s; char * s;
if (ATmatch(e, "Str(<str>)", &s)) return s; if (ATmatch(e, "Str(<str>)", &s)) return s;
else throw badTerm("string value expected", e); else throw badTerm("string value expected", e);
@ -220,10 +221,10 @@ static string evalString(Expr e)
non-expression reference. */ non-expression reference. */
static Hash evalExternal(Expr e) static Hash evalExternal(Expr e)
{ {
EvalResult r = evalValue(e); e = evalValue(e);
char * s; char * s;
if (ATmatch(r.e, "External(<str>)", &s)) return r.h; if (ATmatch(e, "External(<str>)", &s)) return parseHash(s);
else throw badTerm("external non-expression value expected", r.e); else throw badTerm("external non-expression value expected", e);
} }
@ -238,7 +239,7 @@ void evalArgs(ATermList args, ATermList & argsNF, Environment & env)
throw badTerm("invalid argument", arg); throw badTerm("invalid argument", arg);
string name = evalString(eName); string name = evalString(eName);
eVal = evalValue(eVal).e; eVal = evalValue(eVal);
char * s; char * s;
if (ATmatch(eVal, "Str(<str>)", &s)) { if (ATmatch(eVal, "Str(<str>)", &s)) {
@ -256,9 +257,8 @@ void evalArgs(ATermList args, ATermList & argsNF, Environment & env)
/* Evaluate an expression. */ /* Evaluate an expression. */
EvalResult evalValue(Expr e) Expr evalValue(Expr e)
{ {
EvalResult r;
char * s; char * s;
Expr eBuildPlatform, eProg; Expr eBuildPlatform, eProg;
ATermList args; ATermList args;
@ -267,21 +267,19 @@ EvalResult evalValue(Expr e)
if (ATmatch(e, "Str(<str>)", &s) || if (ATmatch(e, "Str(<str>)", &s) ||
ATmatch(e, "Bool(True)") || ATmatch(e, "Bool(True)") ||
ATmatch(e, "Bool(False)")) ATmatch(e, "Bool(False)"))
{ return e;
r.e = e;
}
/* External expressions. */ /* External expressions. */
/* External non-expressions. */ /* External non-expressions. */
else if (ATmatch(e, "External(<str>)", &s)) { if (ATmatch(e, "External(<str>)", &s)) {
r.e = e; parseHash(s); /* i.e., throw exception if not valid */
r.h = parseHash(s); return e;
} }
/* Execution primitive. */ /* Execution primitive. */
else if (ATmatch(e, "Exec(<term>, <term>, [<list>])", if (ATmatch(e, "Exec(<term>, <term>, [<list>])",
&eBuildPlatform, &eProg, &args)) &eBuildPlatform, &eProg, &args))
{ {
string buildPlatform = evalString(eBuildPlatform); string buildPlatform = evalString(eBuildPlatform);
@ -312,12 +310,9 @@ EvalResult evalValue(Expr e)
(string) sourceHash + "-nf", buildPlatform, prog, env); (string) sourceHash + "-nf", buildPlatform, prog, env);
} }
r.e = ATmake("External(<str>)", ((string) targetHash).c_str()); return ATmake("External(<str>)", ((string) targetHash).c_str());
r.h = targetHash;
} }
/* Barf. */ /* Barf. */
else throw badTerm("invalid expression", e); throw badTerm("invalid expression", e);
return r;
} }

View File

@ -18,69 +18,63 @@ using namespace std;
| Bool(b) -- boolean constant | Bool(b) -- boolean constant
| App(e, e) -- application | App(e, e) -- application
| Lam(x, e) -- lambda abstraction | Lam(x, e) -- lambda abstraction
| Exec(platform, e, [(s, e)]) | Exec(platform, e, [Arg(e, e)])
-- primitive; execute e with args e* on platform -- primitive; execute e with args e* on platform
; ;
Semantics Semantics
Each rules given as eval(e) => (e', h'), i.e., expression e has a Each rule given as eval(e) => e', i.e., expression e has a normal
normal form e' with hash code h'. evalE = fst . eval. evalH = snd form e'.
. eval.
eval(Hash(h)) => eval(loadExpr(h)) eval(Hash(h)) => eval(loadExpr(h))
eval(External(h)) => (External(h), h) eval(External(h)) => External(h) # idem for Str, Bool
eval(Str(s)@e) => (e, 0) # idem for Bool
eval(App(e1, e2)) => eval(App(e1', e2)) eval(App(e1, e2)) => eval(App(e1', e2))
where e1' = evalE(e1) where e1' = eval(e1)
eval(App(Lam(var, body), arg)@in) => eval(App(Lam(var, body), arg)) =>
eval(subst(var, arg, body))@out eval(subst(var, arg, body))
[AND write out to storage, and dbNFs[hash(in)] = hash(out) ???]
eval(Exec(platform, prog, args)@e) => eval(Exec(platform, prog, args)) =>
(External(h), h) (External(h), h)
where where
hIn = hashExpr(e) fn = ... name of the output (random or by hashing expr) ...
fn = ... form name involving hIn ...
h = h =
if exec(evalE(platform) => Str(...) if exec( fn
, getFile(evalH(prog)) , eval(platform) => Str(...)
, getFile(eval(prog))
, map(makeArg . eval, args) , map(makeArg . eval, args)
) then ) then
hashExternal(fn) hashExternal(fn)
else else
undef undef
... register ...
makeArg((argn, (External(h), h))) => (argn, getFile(h)) makeArg(Arg(Str(nm), (External(h), h))) => (nm, getFile(h))
makeArg((argn, (Str(s), _))) => (argn, s) makeArg(Arg(Str(nm), (Str(s), _))) => (nm, s)
makeArg((argn, (Bool(True), _))) => (argn, "1") makeArg(Arg(Str(nm), (Bool(True), _))) => (nm, "1")
makeArg((argn, (Bool(False), _))) => (argn, undef) makeArg(Arg(Str(nm), (Bool(False), _))) => (nm, undef)
getFile :: Hash -> FileName getFile :: Hash -> FileName
loadExpr :: Hash -> FileName loadExpr :: Hash -> FileName
hashExpr :: Expr -> Hash hashExpr :: Expr -> Hash
hashExternal :: FileName -> Hash hashExternal :: FileName -> Hash
exec :: Platform -> FileName -> [(String, String)] -> Status exec :: FileName -> Platform -> FileName -> [(String, String)] -> Status
*/ */
typedef ATerm Expr; typedef ATerm Expr;
struct EvalResult
{
Expr e;
Hash h;
};
/* Evaluate an expression. */ /* Evaluate an expression. */
EvalResult evalValue(Expr e); Expr evalValue(Expr e);
/* Return a canonical textual representation of an expression. */
string printExpr(Expr e);
/* Hash an expression. */
Hash hashExpr(Expr e);
#endif /* !__EVAL_H */ #endif /* !__EVAL_H */

View File

@ -12,10 +12,8 @@
void evalTest(Expr e) void evalTest(Expr e)
{ {
EvalResult r = evalValue(e); e = evalValue(e);
cout << (string) hashExpr(e) << ": " << printExpr(e) << endl;
char * s = ATwriteToString(r.e);
cout << (string) r.h << ": " << s << endl;
} }