about summary refs log tree commit diff
path: root/src/eval.hh
blob: bddc9f5d9578ca1843f81902ce968c39cc6fb1c8 (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
#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
      | Exec(platform, e, [(s, e)])
          -- primitive; execute e with args e* on platform
      ;

   Semantics

   Each rules given as eval(e) => (e', h'), i.e., expression e has a
   normal form e' with hash code h'.  evalE = fst . eval.  evalH = snd
   . eval.

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

   eval(External(h)) => (External(h), h)

   eval(Str(s)@e) => (e, 0) # idem for Bool

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

   eval(App(Lam(var, body), arg)@in) =>
     eval(subst(var, arg, body))@out
     [AND write out to storage, and dbNFs[hash(in)] = hash(out) ???]

   eval(Exec(platform, prog, args)@e) =>
     (External(h), h)
     where
       hIn = hashExpr(e)

       fn = ... form name involving hIn ...

       h =
         if exec(evalE(platform) => Str(...)
                , getFile(evalH(prog))
                , map(makeArg . eval, args)
                ) then
           hashExternal(fn)
         else
           undef

   makeArg((argn, (External(h), h))) => (argn, getFile(h))
   makeArg((argn, (Str(s), _))) => (argn, s)
   makeArg((argn, (Bool(True), _))) => (argn, "1")
   makeArg((argn, (Bool(False), _))) => (argn, undef)

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

typedef ATerm Expr;


struct EvalResult 
{
    Expr e;
    Hash h;
};


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


#endif /* !__EVAL_H */