about summary refs log tree commit diff
path: root/src/eval.hh
blob: 2e764b1bd59547d8e2a9b8b5850915144fac49ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef __EVAL_H
#define __EVAL_H

extern "C" {
#include <aterm2.h>
}

#include "hash.hh"

using namespace std;


/* Abstract syntax of Nix values:

   e := Deref(e) -- external expression
      | Hash(h) -- value reference
      | Str(s) -- string constant
      | Bool(b) -- boolean constant
      | Var(x) -- variable
      | App(e, e) -- application
      | Lam(x, e) -- lambda abstraction
      | Exec(platform, e, [Arg(e, e)])
          -- primitive; execute e with args e* on platform
      ;

   TODO: Deref(e) allows computed external expressions, which might be
   too expressive; perhaps this should be Deref(h).

   Semantics

   Each rule given as eval(e) => e', i.e., expression e has a normal
   form e'.

   eval(Deref(Hash(h))) => eval(loadExpr(h))

   eval(Hash(h)) => Hash(h) # idem for Str, Bool

   eval(App(e1, e2)) => eval(App(e1', e2))
     where e1' = eval(e1)

   eval(App(Lam(var, body), arg)) =>
     eval(subst(var, arg, body))

   eval(Exec(platform, prog, args)) => Hash(h)
     where
       fn = ... name of the output (random or by hashing expr) ...
       h =
         if exec( fn 
                , eval(platform) => Str(...)
                , getFile(eval(prog))
                , map(makeArg . eval, args)
                ) then
           hashPath(fn)
         else
           undef
       ... register ...

   makeArg(Arg(Str(nm), (Hash(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)

   subst(x, e1, e2) is defined as a generic topdown term
   traversal of e2, replacing each `Var(x)' with e1, and not
   descending into `Lam(x, _)'.

   Note: all stored expressions must be closed. !!! ugly

   getFile :: Hash -> FileName
   loadExpr :: Hash -> FileName
   hashExpr :: Expr -> Hash 
   hashPath :: FileName -> Hash
   exec :: FileName -> Platform -> FileName -> [(String, String)] -> Status
*/

typedef ATerm Expr;


/* Evaluate an expression. */
Expr evalValue(Expr e);

/* Return a canonical textual representation of an expression. */
string printExpr(Expr e);

/* Perform variable substitution. */
Expr substExpr(string x, Expr rep, Expr e);

/* Hash an expression. */
Hash hashExpr(Expr e);


#endif /* !__EVAL_H */