From a69534fc217666d53a418605de0ebb0879cbb2f7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 29 Oct 2004 11:22:49 +0000 Subject: * Drop ATmake / ATMatcher also in handling store expressions. --- src/Makefile.am | 2 + src/aterm-helper.pl | 147 +++++++++++++++++++++++++++++++++++++++++ src/libexpr/Makefile.am | 11 ++- src/libexpr/aterm-helper.pl | 108 ------------------------------ src/libexpr/constructors.def | 53 --------------- src/libexpr/eval.cc | 4 +- src/libexpr/nixexpr-ast.def | 55 +++++++++++++++ src/libexpr/nixexpr.cc | 4 +- src/libexpr/parser.cc | 2 +- src/libexpr/parser.y | 2 +- src/libexpr/primops.cc | 2 +- src/libmain/shared.cc | 6 ++ src/libstore/Makefile.am | 9 ++- src/libstore/storeexpr-ast.def | 7 ++ src/libstore/storeexpr.cc | 61 ++++++++--------- src/libutil/Makefile.am | 7 -- src/libutil/aterm.cc | 89 ------------------------- src/libutil/aterm.hh | 42 ------------ src/libutil/test-aterm.cc | 66 ------------------ src/nix-env/main.cc | 12 ++-- src/nix-instantiate/main.cc | 3 +- 21 files changed, 276 insertions(+), 416 deletions(-) create mode 100755 src/aterm-helper.pl delete mode 100755 src/libexpr/aterm-helper.pl delete mode 100644 src/libexpr/constructors.def create mode 100644 src/libexpr/nixexpr-ast.def create mode 100644 src/libstore/storeexpr-ast.def delete mode 100644 src/libutil/test-aterm.cc (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 5637382172a2..089f532203b3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,8 @@ SUBDIRS = bin2c boost libutil libstore libmain nix-store nix-hash \ libexpr nix-instantiate nix-env log2xml +EXTRA_DIST = aterm-helper.pl + SETUID_PROGS = nix-store nix-instantiate nix-env install-exec-hook: if SETUID_HACK diff --git a/src/aterm-helper.pl b/src/aterm-helper.pl new file mode 100755 index 000000000000..917b50852aa6 --- /dev/null +++ b/src/aterm-helper.pl @@ -0,0 +1,147 @@ +#! /usr/bin/perl -w + +# This program generates C/C++ code for efficiently manipulating +# ATerms. It generates functions to build and match ATerms according +# to a set of constructor definitions defined in a file read from +# standard input. A constructor is defined by a line with the +# following format: +# +# SYM | ARGS | TYPE | FUN? +# +# where SYM is the name of the constructor, ARGS is a +# whitespace-separated list of argument types, TYPE is the type of the +# resulting ATerm (which should be `ATerm' or a type synonym for +# `ATerm'), and the optional FUN is used to construct the names of the +# build and match functions (it defaults to SYM; overriding it is +# useful if there are overloaded constructors, e.g., with different +# arities). Note that SYM may be empty. +# +# A line of the form +# +# VAR = EXPR +# +# causes a ATerm variable to be generated that is initialised to the +# value EXPR. +# +# Finally, a line of the form +# +# init NAME +# +# causes the initialisation function to be called `NAME'. This +# function must be called before any of the build/match functions or +# the generated variables are used. + +die if scalar @ARGV != 2; + +my $syms = ""; +my $init = ""; +my $initFun = "init"; + +open HEADER, ">$ARGV[0]"; +open IMPL, ">$ARGV[1]"; + +while () { + next if (/^\s*$/); + + if (/^\s*(\w*)\s*\|([^\|]*)\|\s*(\w+)\s*\|\s*(\w+)?/) { + my $const = $1; + my @types = split ' ', $2; + my $result = $3; + my $funname = $4; + $funname = $const unless defined $funname; + + my $formals = ""; + my $formals2 = ""; + my $args = ""; + my $unpack = ""; + my $n = 1; + foreach my $type (@types) { + $args .= ", "; + if ($type eq "string") { +# $args .= "(ATerm) ATmakeAppl0(ATmakeAFun((char *) e$n, 0, ATtrue))"; +# $type = "const char *"; + $type = "ATerm"; + $args .= "e$n"; + # !!! in the matcher, we should check that the + # argument is a string (i.e., a nullary application). + } elsif ($type eq "int") { + $args .= "(ATerm) ATmakeInt(e$n)"; + } elsif ($type eq "ATermList" || $type eq "ATermBlob") { + $args .= "(ATerm) e$n"; + } else { + $args .= "e$n"; + } + $formals .= ", " if $formals ne ""; + $formals .= "$type e$n"; + $formals2 .= ", "; + $formals2 .= "$type & e$n"; + my $m = $n - 1; + # !!! more checks here + if ($type eq "int") { + $unpack .= " e$n = ATgetInt((ATermInt) ATgetArgument(e, $m));\n"; + } elsif ($type eq "ATermList") { + $unpack .= " e$n = (ATermList) ATgetArgument(e, $m);\n"; + } elsif ($type eq "ATermBlob") { + $unpack .= " e$n = (ATermBlob) ATgetArgument(e, $m);\n"; + } else { + $unpack .= " e$n = ATgetArgument(e, $m);\n"; + } + $n++; + } + + my $arity = scalar @types; + + print HEADER "extern AFun sym$funname;\n\n"; + + print IMPL "AFun sym$funname = 0;\n"; + + print HEADER "static inline $result make$funname($formals) {\n"; + print HEADER " return (ATerm) ATmakeAppl$arity(sym$funname$args);\n"; + print HEADER "}\n\n"; + + print HEADER "#ifdef __cplusplus\n"; + print HEADER "static inline bool match$funname(ATerm e$formals2) {\n"; + print HEADER " if (ATgetType(e) != AT_APPL || ATgetAFun(e) != sym$funname) return false;\n"; + print HEADER "$unpack"; + print HEADER " return true;\n"; + print HEADER "}\n"; + print HEADER "#endif\n\n\n"; + + $init .= " sym$funname = ATmakeAFun(\"$const\", $arity, ATfalse);\n"; + $init .= " ATprotectAFun(sym$funname);\n"; + } + + elsif (/^\s*(\w+)\s*=\s*(.*)$/) { + my $name = $1; + my $value = $2; + print HEADER "extern ATerm $name;\n"; + print IMPL "ATerm $name = 0;\n"; + $init .= " $name = $value;\n"; + } + + elsif (/^\s*init\s+(\w+)\s*$/) { + $initFun = $1; + } + + else { + die "bad line: `$_'"; + } +} + +print HEADER "void $initFun();\n\n"; + +print HEADER "static inline ATerm string2ATerm(const char * s) {\n"; +print HEADER " return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));\n"; +print HEADER "}\n\n"; + +print HEADER "static inline const char * aterm2String(ATerm t) {\n"; +print HEADER " return (const char *) ATgetName(ATgetAFun(t));\n"; +print HEADER "}\n\n"; + +print IMPL "\n"; +print IMPL "void $initFun() {\n"; +print IMPL "$init"; +print IMPL "}\n"; + +close HEADER; +close IMPL; diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index a52b4710d235..d7de30855c11 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -3,10 +3,9 @@ noinst_LIBRARIES = libexpr.a libexpr_a_SOURCES = nixexpr.cc nixexpr.hh parser.cc parser.hh \ eval.cc eval.hh primops.cc \ lexer-tab.c lexer-tab.h parser-tab.c parser-tab.h \ - constructors.hh + nixexpr-ast.hh -EXTRA_DIST = lexer.l parser.y constructors.def constructors.cc \ - aterm-helper.pl +EXTRA_DIST = lexer.l parser.y nixexpr-ast.def nixexpr-ast.cc AM_CXXFLAGS = \ -I.. ${bdb_include} ${aterm_include} -I../libutil -I../libstore @@ -27,10 +26,10 @@ lexer-tab.c lexer-tab.h: lexer.l # ATerm helper function generation. -constructors.cc constructors.hh: aterm-helper.pl constructors.def - $(perl) aterm-helper.pl constructors.hh constructors.cc < constructors.def +nixexpr-ast.cc nixexpr-ast.hh: ../aterm-helper.pl nixexpr-ast.def + $(perl) ../aterm-helper.pl nixexpr-ast.hh nixexpr-ast.cc < nixexpr-ast.def -nixexpr.hh: constructors.hh +nixexpr.hh: nixexpr-ast.hh CLEANFILES = diff --git a/src/libexpr/aterm-helper.pl b/src/libexpr/aterm-helper.pl deleted file mode 100755 index 9a1a931a91b6..000000000000 --- a/src/libexpr/aterm-helper.pl +++ /dev/null @@ -1,108 +0,0 @@ -#! /usr/bin/perl -w - -die if scalar @ARGV != 2; - -my $syms = ""; -my $init = ""; - -open HEADER, ">$ARGV[0]"; -open IMPL, ">$ARGV[1]"; - -while () { - next if (/^\s*$/); - - if (/^\s*(\w+)\s*\|([^\|]*)\|\s*(\w+)\s*\|\s*(\w+)?/) { - my $const = $1; - my @types = split ' ', $2; - my $result = $3; - my $funname = $4; - $funname = $const unless defined $funname; - - my $formals = ""; - my $formals2 = ""; - my $args = ""; - my $unpack = ""; - my $n = 1; - foreach my $type (@types) { - $args .= ", "; - if ($type eq "string") { -# $args .= "(ATerm) ATmakeAppl0(ATmakeAFun((char *) e$n, 0, ATtrue))"; -# $type = "const char *"; - $type = "ATerm"; - $args .= "e$n"; - } elsif ($type eq "int") { - $args .= "(ATerm) ATmakeInt(e$n)"; - } elsif ($type eq "ATermList" || $type eq "ATermBlob") { - $args .= "(ATerm) e$n"; - } else { - $args .= "e$n"; - } - $formals .= ", " if $formals ne ""; - $formals .= "$type e$n"; - $formals2 .= ", "; - $formals2 .= "$type & e$n"; - my $m = $n - 1; - if ($type eq "int") { - $unpack .= " e$n = ATgetInt((ATermInt) ATgetArgument(e, $m));\n"; - } elsif ($type eq "ATermList") { - $unpack .= " e$n = (ATermList) ATgetArgument(e, $m);\n"; - } elsif ($type eq "ATermBlob") { - $unpack .= " e$n = (ATermBlob) ATgetArgument(e, $m);\n"; - } else { - $unpack .= " e$n = ATgetArgument(e, $m);\n"; - } - $n++; - } - - my $arity = scalar @types; - - print HEADER "extern AFun sym$funname;\n\n"; - - print IMPL "AFun sym$funname = 0;\n"; - - print HEADER "static inline $result make$funname($formals) {\n"; - print HEADER " return (ATerm) ATmakeAppl$arity(sym$funname$args);\n"; - print HEADER "}\n\n"; - - print HEADER "#ifdef __cplusplus\n"; - print HEADER "static inline bool match$funname(ATerm e$formals2) {\n"; - print HEADER " if (ATgetType(e) != AT_APPL || ATgetAFun(e) != sym$funname) return false;\n"; - print HEADER "$unpack"; - print HEADER " return true;\n"; - print HEADER "}\n"; - print HEADER "#endif\n\n\n"; - - $init .= " sym$funname = ATmakeAFun(\"$const\", $arity, ATfalse);\n"; - $init .= " ATprotectAFun(sym$funname);\n"; - } - - elsif (/^\s*(\w+)\s*=\s*(.*)$/) { - my $name = $1; - my $value = $2; - print HEADER "extern ATerm $name;\n"; - print IMPL "ATerm $name = 0;\n"; - $init .= " $name = $value;\n"; - } - - else { - die "bad line: `$_'"; - } -} - -print HEADER "void initSyms();\n\n"; - -print HEADER "static inline ATerm string2ATerm(const char * s) {\n"; -print HEADER " return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s, 0, ATtrue));\n"; -print HEADER "}\n\n"; - -print HEADER "static inline const char * aterm2String(ATerm t) {\n"; -print HEADER " return (const char *) ATgetName(ATgetAFun(t));\n"; -print HEADER "}\n\n"; - -print IMPL "\n"; -print IMPL "void initSyms() {\n"; -print IMPL "$init"; -print IMPL "}\n"; - -close HEADER; -close IMPL; diff --git a/src/libexpr/constructors.def b/src/libexpr/constructors.def deleted file mode 100644 index 497cd33c336d..000000000000 --- a/src/libexpr/constructors.def +++ /dev/null @@ -1,53 +0,0 @@ -Pos | string int int | Pos | -NoPos | | Pos | - -Function | ATermList Expr Pos | Expr | -Function1 | string Expr Pos | Expr | -Assert | Expr Expr Pos | Expr | -With | Expr Expr Pos | Expr | -If | Expr Expr Expr | Expr | -OpNot | Expr | Expr | -OpEq | Expr Expr | Expr | -OpNEq | Expr Expr | Expr | -OpAnd | Expr Expr | Expr | -OpOr | Expr Expr | Expr | -OpImpl | Expr Expr | Expr | -OpUpdate | Expr Expr | Expr | -SubPath | Expr Expr | Expr | -OpHasAttr | Expr string | Expr | -OpPlus | Expr Expr | Expr | -Call | Expr Expr | Expr | -Select | Expr string | Expr | -Var | string | Expr | -Int | int | Expr | -Str | string | Expr | -Path | string | Expr | -Uri | string | Expr | -List | ATermList | Expr | -BlackHole | | Expr | -Undefined | | Expr | -PrimOp | int ATermBlob ATermList | Expr | -Attrs | ATermList | Expr | -Closed | Expr | Expr | -Rec | ATermList ATermList | Expr | -Bool | ATerm | Expr | -Null | | Expr | - -Bind | string Expr Pos | ATerm | -Bind | string Expr | ATerm | Bind2 -Inherit | Expr ATermList Pos | ATerm | - -Scope | | Expr | - -NoDefFormal | string | ATerm | -DefFormal | string Expr | ATerm | - -True | | ATerm | -False | | ATerm | - -PrimOpDef | int ATermBlob | ATerm | - -AttrRHS | Expr Pos | ATerm | - -eTrue = makeBool(makeTrue()) -eFalse = makeBool(makeFalse()) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 88362333c29d..cef346002962 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1,6 +1,6 @@ #include "eval.hh" #include "parser.hh" -#include "constructors.hh" +#include "nixexpr-ast.hh" EvalState::EvalState() @@ -10,7 +10,7 @@ EvalState::EvalState() nrEvaluated = nrCached = 0; - initSyms(); + initNixExprHelpers(); addPrimOps(); } diff --git a/src/libexpr/nixexpr-ast.def b/src/libexpr/nixexpr-ast.def new file mode 100644 index 000000000000..19601847cf32 --- /dev/null +++ b/src/libexpr/nixexpr-ast.def @@ -0,0 +1,55 @@ +init initNixExprHelpers + +Pos | string int int | Pos | +NoPos | | Pos | + +Function | ATermList Expr Pos | Expr | +Function1 | string Expr Pos | Expr | +Assert | Expr Expr Pos | Expr | +With | Expr Expr Pos | Expr | +If | Expr Expr Expr | Expr | +OpNot | Expr | Expr | +OpEq | Expr Expr | Expr | +OpNEq | Expr Expr | Expr | +OpAnd | Expr Expr | Expr | +OpOr | Expr Expr | Expr | +OpImpl | Expr Expr | Expr | +OpUpdate | Expr Expr | Expr | +SubPath | Expr Expr | Expr | +OpHasAttr | Expr string | Expr | +OpPlus | Expr Expr | Expr | +Call | Expr Expr | Expr | +Select | Expr string | Expr | +Var | string | Expr | +Int | int | Expr | +Str | string | Expr | +Path | string | Expr | +Uri | string | Expr | +List | ATermList | Expr | +BlackHole | | Expr | +Undefined | | Expr | +PrimOp | int ATermBlob ATermList | Expr | +Attrs | ATermList | Expr | +Closed | Expr | Expr | +Rec | ATermList ATermList | Expr | +Bool | ATerm | Expr | +Null | | Expr | + +Bind | string Expr Pos | ATerm | +Bind | string Expr | ATerm | Bind2 +Inherit | Expr ATermList Pos | ATerm | + +Scope | | Expr | + +NoDefFormal | string | ATerm | +DefFormal | string Expr | ATerm | + +True | | ATerm | +False | | ATerm | + +PrimOpDef | int ATermBlob | ATerm | + +AttrRHS | Expr Pos | ATerm | + +eTrue = makeBool(makeTrue()) +eFalse = makeBool(makeFalse()) diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index cbfb5576bf04..56e053fd7ae4 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -2,8 +2,8 @@ #include "storeexpr.hh" -#include "constructors.hh" -#include "constructors.cc" +#include "nixexpr-ast.hh" +#include "nixexpr-ast.cc" ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct) diff --git a/src/libexpr/parser.cc b/src/libexpr/parser.cc index a0a6c01df762..8a199c00263f 100644 --- a/src/libexpr/parser.cc +++ b/src/libexpr/parser.cc @@ -7,7 +7,7 @@ #include "aterm.hh" #include "parser.hh" -#include "constructors.hh" +#include "nixexpr-ast.hh" struct ParseData diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 2864b1600e73..8573697b634d 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -18,7 +18,7 @@ typedef ATerm Expr; typedef ATerm Pos; -#include "constructors.hh" +#include "nixexpr-ast.hh" void setParseResult(void * data, ATerm t); void parseError(void * data, char * error, int line, int column); diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index e230d35ce6b1..830a2c095839 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1,7 +1,7 @@ #include "normalise.hh" #include "eval.hh" #include "globals.hh" -#include "constructors.hh" +#include "nixexpr-ast.hh" /* Load and evaluate an expression from path specified by the diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 73388c96e8cd..f608afd84898 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -55,6 +55,9 @@ void checkStoreNotSymlink(Path path) } +void initStoreExprHelpers(); + + /* Initialize and reorder arguments, then call the actual argument processor. */ static void initAndRun(int argc, char * * argv) @@ -101,6 +104,9 @@ static void initAndRun(int argc, char * * argv) string lt = getEnv("NIX_LOG_TYPE"); if (lt != "") setLogType(lt); + /* ATerm stuff. !!! find a better place to put this */ + initStoreExprHelpers(); + /* Put the arguments in a vector. */ Strings args, remaining; while (argc--) args.push_back(*argv++); diff --git a/src/libstore/Makefile.am b/src/libstore/Makefile.am index 31735e8936c7..7e0f32d1f836 100644 --- a/src/libstore/Makefile.am +++ b/src/libstore/Makefile.am @@ -5,7 +5,14 @@ libstore_a_SOURCES = \ normalise.cc misc.cc normalise.hh \ globals.cc globals.hh db.cc db.hh \ references.cc references.hh pathlocks.cc pathlocks.hh \ - gc.cc gc.hh + gc.cc gc.hh storeexpr-ast.hh + +EXTRA_DIST = storeexpr-ast.def storeexpr-ast.cc AM_CXXFLAGS = -Wall \ -I.. ${bdb_include} ${aterm_include} -I../libutil + +storeexpr-ast.cc storeexpr-ast.hh: ../aterm-helper.pl storeexpr-ast.def + $(perl) ../aterm-helper.pl storeexpr-ast.hh storeexpr-ast.cc < storeexpr-ast.def + +storeexpr.cc: storeexpr-ast.hh \ No newline at end of file diff --git a/src/libstore/storeexpr-ast.def b/src/libstore/storeexpr-ast.def new file mode 100644 index 000000000000..9d2433dbe4be --- /dev/null +++ b/src/libstore/storeexpr-ast.def @@ -0,0 +1,7 @@ +init initStoreExprHelpers + +Closure | ATermList ATermList | ATerm | +Derive | ATermList ATermList string string ATermList ATermList | ATerm | + +| string string | ATerm | EnvBinding | +| string ATermList | ATerm | ClosureElem | diff --git a/src/libstore/storeexpr.cc b/src/libstore/storeexpr.cc index 29f271de884b..0b92be0e7117 100644 --- a/src/libstore/storeexpr.cc +++ b/src/libstore/storeexpr.cc @@ -2,6 +2,9 @@ #include "globals.hh" #include "store.hh" +#include "storeexpr-ast.hh" +#include "storeexpr-ast.cc" + Hash hashTerm(ATerm t) { @@ -29,10 +32,11 @@ Path writeTerm(ATerm t, const string & suffix) static void parsePaths(ATermList paths, PathSet & out) { - ATMatcher m; for (ATermIterator i(paths); i; ++i) { - string s; - if (!(atMatch(m, *i) >> s)) + if (ATgetType(*i) != AT_APPL) + throw badTerm("not a path", *i); + string s = aterm2String(*i); + if (s.size() == 0 || s[0] != '/') throw badTerm("not a path", *i); out.insert(s); } @@ -69,21 +73,20 @@ static void checkClosure(const Closure & closure) static bool parseClosure(ATerm t, Closure & closure) { ATermList roots, elems; - ATMatcher m; - if (!(atMatch(m, t) >> "Closure" >> roots >> elems)) + if (!matchClosure(t, roots, elems)) return false; parsePaths(roots, closure.roots); for (ATermIterator i(elems); i; ++i) { - string path; + ATerm path; ATermList refs; - if (!(atMatch(m, *i) >> "" >> path >> refs)) + if (!matchClosureElem(*i, path, refs)) throw badTerm("not a closure element", *i); ClosureElem elem; parsePaths(refs, elem.refs); - closure.elems[path] = elem; + closure.elems[aterm2String(path)] = elem; } checkClosure(closure); @@ -93,32 +96,29 @@ static bool parseClosure(ATerm t, Closure & closure) static bool parseDerivation(ATerm t, Derivation & derivation) { - ATMatcher m; ATermList outs, ins, args, bnds; - string builder, platform; + ATerm builder, platform; - if (!(atMatch(m, t) >> "Derive" >> outs >> ins >> platform - >> builder >> args >> bnds)) + if (!matchDerive(t, outs, ins, platform, builder, args, bnds)) return false; parsePaths(outs, derivation.outputs); parsePaths(ins, derivation.inputs); - derivation.builder = builder; - derivation.platform = platform; + derivation.builder = aterm2String(builder); + derivation.platform = aterm2String(platform); for (ATermIterator i(args); i; ++i) { - string s; - if (!(atMatch(m, *i) >> s)) + if (ATgetType(*i) != AT_APPL) throw badTerm("string expected", *i); - derivation.args.push_back(s); + derivation.args.push_back(aterm2String(*i)); } for (ATermIterator i(bnds); i; ++i) { - string s1, s2; - if (!(atMatch(m, *i) >> "" >> s1 >> s2)) + ATerm s1, s2; + if (!matchEnvBinding(*i, s1, s2)) throw badTerm("tuple of strings expected", *i); - derivation.env[s1] = s2; + derivation.env[aterm2String(s1)] = aterm2String(s2); } return true; @@ -142,7 +142,7 @@ static ATermList unparsePaths(const PathSet & paths) ATermList l = ATempty; for (PathSet::const_iterator i = paths.begin(); i != paths.end(); i++) - l = ATinsert(l, ATmake("", i->c_str())); + l = ATinsert(l, string2ATerm(i->c_str())); return ATreverse(l); } @@ -155,11 +155,11 @@ static ATerm unparseClosure(const Closure & closure) for (ClosureElems::const_iterator i = closure.elems.begin(); i != closure.elems.end(); i++) elems = ATinsert(elems, - ATmake("(, )", - i->first.c_str(), + makeClosureElem( + string2ATerm(i->first.c_str()), unparsePaths(i->second.refs))); - return ATmake("Closure(, )", roots, elems); + return makeClosure(roots, elems); } @@ -168,20 +168,21 @@ static ATerm unparseDerivation(const Derivation & derivation) ATermList args = ATempty; for (Strings::const_iterator i = derivation.args.begin(); i != derivation.args.end(); i++) - args = ATinsert(args, ATmake("", i->c_str())); + args = ATinsert(args, string2ATerm(i->c_str())); ATermList env = ATempty; for (StringPairs::const_iterator i = derivation.env.begin(); i != derivation.env.end(); i++) env = ATinsert(env, - ATmake("(, )", - i->first.c_str(), i->second.c_str())); + makeEnvBinding( + string2ATerm(i->first.c_str()), + string2ATerm(i->second.c_str()))); - return ATmake("Derive(, , , , , )", + return makeDerive( unparsePaths(derivation.outputs), unparsePaths(derivation.inputs), - derivation.platform.c_str(), - derivation.builder.c_str(), + string2ATerm(derivation.platform.c_str()), + string2ATerm(derivation.builder.c_str()), ATreverse(args), ATreverse(env)); } diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am index bd19bfa83a15..a7ff1b6e0358 100644 --- a/src/libutil/Makefile.am +++ b/src/libutil/Makefile.am @@ -4,10 +4,3 @@ libutil_a_SOURCES = util.cc util.hh hash.cc hash.hh \ archive.cc archive.hh md5.c md5.h aterm.cc aterm.hh AM_CXXFLAGS = -Wall -I.. ${aterm_include} - -check_PROGRAMS = test-aterm - -test_aterm_SOURCES = test-aterm.cc -test_aterm_LDADD = ./libutil.a ../boost/format/libformat.a \ - ${aterm_lib} - diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc index fb734b3a0847..77bf8dab4c12 100644 --- a/src/libutil/aterm.cc +++ b/src/libutil/aterm.cc @@ -16,95 +16,6 @@ ostream & operator << (ostream & stream, ATerm e) } -ATMatcher & atMatch(ATMatcher & pos, ATerm t) -{ - pos.t = t; - pos.pos = ATMatcher::funPos; - return pos; -} - - -static inline bool failed(const ATMatcher & pos) -{ - return pos.pos == ATMatcher::failPos; -} - - -static inline ATMatcher & fail(ATMatcher & pos) -{ - pos.pos = ATMatcher::failPos; - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, ATerm & out) -{ - out = 0; - if (failed(pos)) return pos; - if (pos.pos == ATMatcher::funPos || - ATgetType(pos.t) != AT_APPL || - pos.pos >= (int) ATgetArity(ATgetAFun(pos.t))) - return fail(pos); - out = ATgetArgument(pos.t, pos.pos); - pos.pos++; - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, string & out) -{ - out = ""; - if (pos.pos == ATMatcher::funPos) { - if (ATgetType(pos.t) != AT_APPL) return fail(pos); - out = ATgetName(ATgetAFun(pos.t)); - pos.pos = 0; - } else { - ATerm t; - pos = pos >> t; - if (failed(pos)) return pos; - if (ATgetType(t) != AT_APPL || - ATgetArity(ATgetAFun(t)) != 0) - return fail(pos); - out = ATgetName(ATgetAFun(t)); - } - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, const string & s) -{ - string s2; - pos = pos >> s2; - if (failed(pos)) return pos; - if (s != s2) return fail(pos); - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, int & n) -{ - n = 0; - ATerm t; - pos = pos >> t; - if (failed(pos)) return pos; - if (ATgetType(t) != AT_INT) return fail(pos); - n = ATgetInt((ATermInt) t); - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, ATermList & out) -{ - out = 0; - ATerm t; - pos = pos >> t; - if (failed(pos)) return pos; - if (ATgetType(t) != AT_LIST) return fail(pos); - out = (ATermList) t; - return pos; -} - - Error badTerm(const format & f, ATerm t) { char * s = ATwriteToString(t); diff --git a/src/libutil/aterm.hh b/src/libutil/aterm.hh index 577b784be202..883d20c63ad1 100644 --- a/src/libutil/aterm.hh +++ b/src/libutil/aterm.hh @@ -36,48 +36,6 @@ public: }; -/* Type-safe matching. */ - -struct ATMatcher -{ - ATerm t; - int pos; - const static int failPos = -2; - const static int funPos = -1; - - ATMatcher() : t(0), pos(failPos) - { - } - - operator bool() const - { - return pos != failPos; - } -}; - -/* Initiate matching of a term. */ -ATMatcher & atMatch(ATMatcher & pos, ATerm t); - -/* Get the next argument of an application. */ -ATMatcher & operator >> (ATMatcher & pos, ATerm & out); - -/* Get the name of the function symbol of an application, or the next - argument of an application as a string. */ -ATMatcher & operator >> (ATMatcher & pos, string & out); - -/* Like the previous, but check that the string is equal to the given - string. */ -ATMatcher & operator >> (ATMatcher & pos, const string & s); - -/* Get the next argument of an application, and verify that it is a - integer. */ -ATMatcher & operator >> (ATMatcher & pos, int & n); - -/* Get the next argument of an application, and verify that it is a - list. */ -ATMatcher & operator >> (ATMatcher & pos, ATermList & out); - - /* Throw an exception with an error message containing the given aterm. */ Error badTerm(const format & f, ATerm t); diff --git a/src/libutil/test-aterm.cc b/src/libutil/test-aterm.cc deleted file mode 100644 index 325639ca4f30..000000000000 --- a/src/libutil/test-aterm.cc +++ /dev/null @@ -1,66 +0,0 @@ -#include "aterm.hh" -#include - - -void runTests() -{ - verbosity = lvlDebug; - - ATMatcher pos; - - ATerm t = ATmake("Call(Foo, Bar, \"xyz\")"); - - debug(format("term: %1%") % t); - - string fun, arg3; - ATerm lhs, rhs; - - if (!(atMatch(pos, t) >> "Call" >> lhs >> rhs >> arg3)) - throw Error("should succeed"); - if (arg3 != "xyz") throw Error("bad 1"); - - if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> arg3)) - throw Error("should succeed"); - if (fun != "Call") throw Error("bad 2"); - if (arg3 != "xyz") throw Error("bad 3"); - - if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> "xyz")) - throw Error("should succeed"); - - if (atMatch(pos, t) >> fun >> lhs >> rhs >> "abc") - throw Error("should fail"); - - if (atMatch(pos, t) >> "Call" >> lhs >> rhs >> "abc") - throw Error("should fail"); - - t = ATmake("X([A, B, C], \"abc\")"); - - ATerm t1, t2, t3; - if (atMatch(pos, t) >> "X" >> t1 >> t2 >> t3) - throw Error("should fail"); - if (!(atMatch(pos, t) >> "X" >> t1 >> t2)) - throw Error("should succeed"); - ATermList ts; - if (!(atMatch(pos, t) >> "X" >> ts >> t2)) - throw Error("should succeed"); - if (ATgetLength(ts) != 3) - throw Error("bad"); - if (atMatch(pos, t) >> "X" >> t1 >> ts) - throw Error("should fail"); -} - - -int main(int argc, char * * argv) -{ - ATerm bottomOfStack; - ATinit(argc, argv, &bottomOfStack); - - try { - runTests(); - } catch (Error & e) { - printMsg(lvlError, format("error: %1%") % e.msg()); - return 1; - } - - return 0; -} diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc index c25cdb60caf9..527dafbf7811 100644 --- a/src/nix-env/main.cc +++ b/src/nix-env/main.cc @@ -6,7 +6,7 @@ #include "parser.hh" #include "eval.hh" #include "help.txt.hh" -#include "constructors.hh" +#include "nixexpr-ast.hh" #include #include @@ -481,20 +481,22 @@ typedef list Table; void printTable(Table & table) { - int nrColumns = table.size() > 0 ? table.front().size() : 0; + unsigned int nrColumns = table.size() > 0 ? table.front().size() : 0; - vector widths; + vector widths; widths.resize(nrColumns); for (Table::iterator i = table.begin(); i != table.end(); ++i) { assert(i->size() == nrColumns); - Strings::iterator j; int column; + Strings::iterator j; + unsigned int column; for (j = i->begin(), column = 0; j != i->end(); ++j, ++column) if (j->size() > widths[column]) widths[column] = j->size(); } for (Table::iterator i = table.begin(); i != table.end(); ++i) { - Strings::iterator j; int column; + Strings::iterator j; + unsigned int column; for (j = i->begin(), column = 0; j != i->end(); ++j, ++column) { cout << *j; diff --git a/src/nix-instantiate/main.cc b/src/nix-instantiate/main.cc index 907f7257a4e9..7decc2259c7c 100644 --- a/src/nix-instantiate/main.cc +++ b/src/nix-instantiate/main.cc @@ -6,7 +6,7 @@ #include "shared.hh" #include "eval.hh" #include "parser.hh" -#include "constructors.hh" +#include "nixexpr-ast.hh" #include "help.txt.hh" @@ -28,7 +28,6 @@ static Expr evalStdin(EvalState & state, bool parseOnly) static void printDrvPaths(EvalState & state, Expr e) { - ATMatcher m; ATermList es; /* !!! duplication w.r.t. parseDerivations in nix-env */ -- cgit 1.4.1