nix-super/src/eval.hh

81 lines
1.9 KiB
C++
Raw Normal View History

#ifndef __EVAL_H
#define __EVAL_H
extern "C" {
#include <aterm2.h>
}
#include "hash.hh"
using namespace std;
/* Abstract syntax of Nix values:
e := Hash(h) -- reference to expression value
| External(h) -- reference to non-expression value
| Str(s) -- string constant
| Bool(b) -- boolean constant
| App(e, e) -- application
| Lam(x, e) -- lambda abstraction
2003-06-17 16:37:44 +03:00
| Exec(platform, e, [Arg(e, e)])
-- primitive; execute e with args e* on platform
;
Semantics
2003-06-17 16:37:44 +03:00
Each rule given as eval(e) => e', i.e., expression e has a normal
form e'.
eval(Hash(h)) => eval(loadExpr(h))
2003-06-17 16:37:44 +03:00
eval(External(h)) => External(h) # idem for Str, Bool
eval(App(e1, e2)) => eval(App(e1', e2))
2003-06-17 16:37:44 +03:00
where e1' = eval(e1)
2003-06-17 16:37:44 +03:00
eval(App(Lam(var, body), arg)) =>
eval(subst(var, arg, body))
2003-06-17 16:37:44 +03:00
eval(Exec(platform, prog, args)) =>
(External(h), h)
where
2003-06-17 16:37:44 +03:00
fn = ... name of the output (random or by hashing expr) ...
h =
2003-06-17 16:37:44 +03:00
if exec( fn
, eval(platform) => Str(...)
, getFile(eval(prog))
, map(makeArg . eval, args)
) then
hashExternal(fn)
else
undef
2003-06-17 16:37:44 +03:00
... register ...
2003-06-17 16:37:44 +03:00
makeArg(Arg(Str(nm), (External(h), h))) => (nm, getFile(h))
makeArg(Arg(Str(nm), (Str(s), _))) => (nm, s)
makeArg(Arg(Str(nm), (Bool(True), _))) => (nm, "1")
makeArg(Arg(Str(nm), (Bool(False), _))) => (nm, undef)
getFile :: Hash -> FileName
loadExpr :: Hash -> FileName
hashExpr :: Expr -> Hash
hashExternal :: FileName -> Hash
2003-06-17 16:37:44 +03:00
exec :: FileName -> Platform -> FileName -> [(String, String)] -> Status
*/
typedef ATerm Expr;
2003-06-17 16:37:44 +03:00
/* Evaluate an expression. */
Expr evalValue(Expr e);
2003-06-17 16:37:44 +03:00
/* Return a canonical textual representation of an expression. */
string printExpr(Expr e);
2003-06-17 16:37:44 +03:00
/* Hash an expression. */
Hash hashExpr(Expr e);
#endif /* !__EVAL_H */