2010-03-23 18:30:50 +01:00
|
|
|
#include "nixexpr.hh"
|
|
|
|
#include "parser.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
2010-04-12 20:30:11 +02:00
|
|
|
#include <iostream>
|
2010-03-23 18:30:50 +01:00
|
|
|
#include <cstdlib>
|
2010-03-28 20:27:07 +02:00
|
|
|
#include <cstring>
|
2010-03-23 18:30:50 +01:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
2010-04-12 11:14:27 +02:00
|
|
|
void doTest(EvalState & state, string s)
|
2010-03-23 18:30:50 +01:00
|
|
|
{
|
2010-04-13 14:25:42 +02:00
|
|
|
Expr * e = parseExprFromString(state, s, absPath("."));
|
2010-04-12 20:30:11 +02:00
|
|
|
std::cerr << ">>>>> " << *e << std::endl;
|
2010-03-24 13:41:08 +01:00
|
|
|
Value v;
|
2010-04-07 15:55:46 +02:00
|
|
|
state.eval(e, v);
|
|
|
|
state.strictForceValue(v);
|
2010-03-24 13:11:38 +01:00
|
|
|
printMsg(lvlError, format("result: %1%") % v);
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run(Strings args)
|
|
|
|
{
|
2010-04-13 14:25:42 +02:00
|
|
|
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;
|
|
|
|
|
2010-04-12 11:14:27 +02:00
|
|
|
EvalState state;
|
2010-04-12 20:30:11 +02:00
|
|
|
|
2010-03-24 13:41:08 +01:00
|
|
|
printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
|
2010-04-12 20:30:11 +02:00
|
|
|
printMsg(lvlError, format("size of int AST node: %1% bytes") % sizeof(ExprInt));
|
|
|
|
printMsg(lvlError, format("size of attrset AST node: %1% bytes") % sizeof(ExprAttrs));
|
2010-04-14 16:42:32 +02:00
|
|
|
|
2010-04-12 11:14:27 +02:00
|
|
|
doTest(state, "123");
|
|
|
|
doTest(state, "{ x = 1; y = 2; }");
|
|
|
|
doTest(state, "{ x = 1; y = 2; }.y");
|
2010-04-14 16:42:32 +02:00
|
|
|
doTest(state, "let x = 1; y = 2; z = 3; in let a = 4; in y");
|
|
|
|
doTest(state, "rec { x = 1; y = x; }.x");
|
2010-04-12 11:14:27 +02:00
|
|
|
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");
|
2010-04-12 23:21:24 +02:00
|
|
|
doTest(state, "let { x = 1; body = x; }");
|
2010-04-12 11:14:27 +02:00
|
|
|
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"); // => 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)]");
|
2010-04-13 01:33:23 +02:00
|
|
|
doTest(state, "{ x = 1; y.z = 2; } == { y = { z = 2; }; x = 1; }");
|
2010-04-12 11:14:27 +02:00
|
|
|
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");
|
2010-04-12 23:21:24 +02:00
|
|
|
doTest(state, "\"foo\"");
|
2010-04-13 00:03:27 +02:00
|
|
|
doTest(state, "''\n foo\n bar\n ''");
|
2010-04-12 23:21:24 +02:00
|
|
|
doTest(state, "let s = \"bar\"; in \"foo${s}\"");
|
2010-04-12 11:14:27 +02:00
|
|
|
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");
|
2010-04-12 23:21:24 +02:00
|
|
|
doTest(state, "!(true || false)");
|
2010-04-12 11:14:27 +02:00
|
|
|
doTest(state, "let x = x; in if true || x then 1 else 2");
|
2010-04-12 20:30:11 +02:00
|
|
|
doTest(state, "http://nixos.org/");
|
2010-04-12 11:14:27 +02:00
|
|
|
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 ]");
|
2010-04-12 23:21:24 +02:00
|
|
|
doTest(state, "builtins.hasAttr \"x\" { x = 1; }");
|
2010-04-12 11:14:27 +02:00
|
|
|
doTest(state, "let x = 1; as = rec { inherit x; y = as.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, "builtins.toXML 123");
|
2010-04-13 01:33:23 +02:00
|
|
|
doTest(state, "builtins.toXML { a.b = \"x\" + \"y\"; c = [ 1 2 ] ++ [ 3 4 ]; }");
|
2010-04-12 11:14:27 +02:00
|
|
|
|
|
|
|
state.printStats();
|
2010-03-23 18:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string programId = "eval-test";
|