about summary refs log tree commit diff
path: root/src/libexpr/eval-test.cc
blob: a7786561e81e56ba038748b22fb556663a598611 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "nixexpr.hh"
#include "parser.hh"
#include "hash.hh"
#include "util.hh"

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace nix;


void doTest(EvalState & state, string s)
{
    Expr * e = parseExprFromString(state, s, absPath("."));
    std::cerr << ">>>>> " << *e << std::endl;
    Value v;
    state.eval(e, v);
    state.strictForceValue(v);
    printMsg(lvlError, format("result: %1%") % v);
}


void run(Strings args)
{
    SymbolTable t;

    printMsg(lvlError, format("size of symbol: %1% bytes") % sizeof(Symbol));
    
    Symbol s1 = t.create("foo");
    Symbol s2 = t.create("foo");
    Symbol s3 = t.create("bar");
    Symbol s4 = t.create("foo");

    assert(s1 == s2);
    assert(s1 == s4);
    assert(s1 != s3);

    std::map<Symbol, int> m;

    m[s1] = 123;
    m[s3] = 456;

    std::cout << m[s1] << std::endl;
    std::cout << m[s2] << std::endl;
    std::cout << m[s3] << std::endl;
    std::cout << m[s4] << std::endl;

    EvalState state;

    printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
    printMsg(lvlError, format("size of int AST node: %1% bytes") % sizeof(ExprInt));
    printMsg(lvlError, format("size of attrset AST node: %1% bytes") % sizeof(ExprAttrs));

    doTest(state, "123");
    doTest(state, "{ x = 1; y = 2; }");
    doTest(state, "{ x = 1; y = 2; }.y");
    doTest(state, "let x = 1; y = 2; z = 3; in let a = 4; in y");
    doTest(state, "rec { x = 1; y = x; }.x");
    doTest(state, "(x: x) 1");
    doTest(state, "(x: y: y) 1 2");
    doTest(state, "x: x");
    doTest(state, "({x, y}: x) { x = 1; y = 2; }");
    doTest(state, "({x, y}@args: args.x) { x = 1; y = 2; }");
    doTest(state, "(args@{x, y}: args.x) { x = 1; y = 2; }");
    doTest(state, "({x ? 1}: x) { }");
    doTest(state, "({x ? 1, y ? x}: y) { x = 2; }");
    doTest(state, "({x, y, ...}: x) { x = 1; y = 2; z = 3; }");
    doTest(state, "({x, y, ...}@args: args.z) { x = 1; y = 2; z = 3; }");
    //doTest(state, "({x ? y, y ? x}: y) { }");
    doTest(state, "let x = 1; in x");
    doTest(state, "let { x = 1; body = x; }");
    doTest(state, "with { x = 1; }; x");
    doTest(state, "let x = 2; in with { x = 1; }; x"); // => 2
    doTest(state, "with { x = 1; }; with { x = 2; }; x"); // => 2
    doTest(state, "with { x = 1; }; with { y = 2; }; x"); // => 1
    doTest(state, "[ 1 2 3 ]");
    doTest(state, "[ 1 2 ] ++ [ 3 4 5 ]");
    doTest(state, "123 == 123");
    doTest(state, "123 == 456");
    doTest(state, "let id = x: x; in [1 2] == [(id 1) (id 2)]");
    doTest(state, "let id = x: x; in [1 2] == [(id 1) (id 3)]");
    doTest(state, "[1 2] == [3 (let x = x; in x)]");
    doTest(state, "{ x = 1; y.z = 2; } == { y = { z = 2; }; x = 1; }");
    doTest(state, "{ x = 1; y = 2; } == { x = 2; }");
    doTest(state, "{ x = [ 1 2 ]; } == { x = [ 1 ] ++ [ 2 ]; }");
    doTest(state, "1 != 1");
    doTest(state, "true");
    doTest(state, "builtins.true");
    doTest(state, "true == false");
    doTest(state, "__head [ 1 2 3 ]");
    doTest(state, "__add 1 2");
    doTest(state, "null");
    doTest(state, "\"foo\"");
    doTest(state, "''\n  foo\n  bar\n    ''");
    doTest(state, "let s = \"bar\"; in \"foo${s}\"");
    doTest(state, "if true then 1 else 2");
    doTest(state, "if false then 1 else 2");
    doTest(state, "if false || true then 1 else 2");
    doTest(state, "!(true || false)");
    doTest(state, "let x = x; in if true || x then 1 else 2");
    doTest(state, "http://nixos.org/");
    doTest(state, "/etc/passwd");
    //doTest(state, "import ./foo.nix");
    doTest(state, "map (x: __add 1 x) [ 1 2 3 ]");
    doTest(state, "map (builtins.add 1) [ 1 2 3 ]");
    doTest(state, "builtins.hasAttr \"x\" { x = 1; }");
    doTest(state, "let x = 1; as = { inherit x; y = as.x; }; in as.y");
    doTest(state, "let x = 1; as = rec { inherit x; y = x; }; in as.y");
    doTest(state, "let as = { x = 1; }; bs = rec { inherit (as) x; y = x; }; in bs.y");
    doTest(state, "let as = rec { inherit (y) x; y = { x = 1; }; }; in as.x");
    doTest(state, "let x = 1; in let inherit x; in x");
    doTest(state, "with { x = 1; }; let inherit x; y = x; in y");
    doTest(state, "builtins.toXML 123");
    doTest(state, "builtins.toXML { a.b = \"x\" + \"y\"; c = [ 1 2 ] ++ [ 3 4 ]; }");

    state.printStats();
}


void printHelp()
{
}


string programId = "eval-test";