* Refactoring to support domain checks.

This commit is contained in:
Eelco Dolstra 2006-07-24 15:16:03 +00:00
parent 9c3099d328
commit 57751fdb55
10 changed files with 36 additions and 29 deletions

View File

@ -37,15 +37,17 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
ATermVector defsUsed; ATermVector defsUsed;
ATermList recAttrs = ATempty; ATermList recAttrs = ATempty;
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
Expr name, def = 0; Expr name, def; DefaultValue def2; ATerm dummy;
if (!matchNoDefFormal(*i, name) && !matchDefFormal(*i, name, def)) if (!matchFormal(*i, name, dummy, def2)) abort(); /* can't happen */
abort(); /* can't happen */ if (!matchDefaultValue(def2, def)) def = 0;
if (subs[name] == 0) { if (subs[name] == 0) {
if (def == 0) throw TypeError(format("the argument named `%1%' required by the function is missing") if (def == 0) throw TypeError(format("the argument named `%1%' required by the function is missing")
% aterm2String(name)); % aterm2String(name));
defsUsed.push_back(name); defsUsed.push_back(name);
recAttrs = ATinsert(recAttrs, makeBind(name, def, makeNoPos())); recAttrs = ATinsert(recAttrs, makeBind(name, def, makeNoPos()));
} }
/* !!! check that the argument are in the valid values list,
if present */
} }
/* Make a recursive attribute set out of the (argument-name, /* Make a recursive attribute set out of the (argument-name,
@ -64,8 +66,8 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
/* One or more actual arguments were not declared as formal /* One or more actual arguments were not declared as formal
arguments. Find out which. */ arguments. Find out which. */
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
Expr name, def; Expr name; ATerm d1, d2;
matchNoDefFormal(*i, name) || matchDefFormal(*i, name, def); matchFormal(*i, name, d1, d2);
subs.remove(name); subs.remove(name);
} }
throw TypeError(format("the function does not expect an argument named `%1%'") throw TypeError(format("the function does not expect an argument named `%1%'")

View File

@ -121,12 +121,11 @@ static void getDerivations(EvalState & state, Expr e,
ATerm body, pos; ATerm body, pos;
if (matchFunction(e, formals, body, pos)) { if (matchFunction(e, formals, body, pos)) {
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
Expr name, def; Expr name, def; ATerm values, def2;
if (matchNoDefFormal(*i, name)) if (!matchFormal(*i, name, values, def2)) abort();
if (!matchDefaultValue(def2, def))
throw TypeError(format("cannot auto-call a function that has an argument without a default value (`%1%')") throw TypeError(format("cannot auto-call a function that has an argument without a default value (`%1%')")
% aterm2String(name)); % aterm2String(name));
else if (!matchDefFormal(*i, name, def))
abort(); /* can't happen */
} }
getDerivations(state, getDerivations(state,
makeCall(e, makeAttrs(ATermMap(0))), makeCall(e, makeAttrs(ATermMap(0))),

View File

@ -45,8 +45,13 @@ Inherit | Expr ATermList Pos | ATerm |
Scope | | Expr | Scope | | Expr |
NoDefFormal | string | ATerm | Formal | string ValidValues DefaultValue | ATerm |
DefFormal | string Expr | ATerm |
ValidValues | ATermList | ValidValues |
UnrestrictedValues | | ValidValues |
DefaultValue | Expr | DefaultValue |
NoDefaultValue | | DefaultValue |
True | | ATerm | True | | ATerm |
False | | ATerm | False | | ATerm |

View File

@ -134,9 +134,8 @@ Expr substitute(const Substitution & subs, Expr e)
if (matchFunction(e, formals, body, pos)) { if (matchFunction(e, formals, body, pos)) {
ATermMap map(ATgetLength(formals)); ATermMap map(ATgetLength(formals));
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
if (!matchNoDefFormal(*i, name) && ATerm d1, d2;
!matchDefFormal(*i, name, def)) if (!matchFormal(*i, name, d1, d2)) abort();
abort();
map.set(name, makeRemoved()); map.set(name, makeRemoved());
} }
Substitution subs2(&subs, &map); Substitution subs2(&subs, &map);
@ -221,16 +220,14 @@ static void checkVarDefs2(set<Expr> & done, const ATermMap & defs, Expr e)
else if (matchFunction(e, formals, body, pos)) { else if (matchFunction(e, formals, body, pos)) {
ATermMap defs2(defs); ATermMap defs2(defs);
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
Expr deflt; Expr d1, d2;
if (!matchNoDefFormal(*i, name) && if (!matchFormal(*i, name, d1, d2)) abort();
!matchDefFormal(*i, name, deflt))
abort();
defs2.set(name, (ATerm) ATempty); defs2.set(name, (ATerm) ATempty);
} }
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
Expr deflt; Expr dummy, deflt;
set<Expr> done2; set<Expr> done2;
if (matchDefFormal(*i, name, deflt)) if (matchFormal(*i, name, dummy, deflt)) /* !!! check dummy */
checkVarDefs2(done2, defs2, deflt); checkVarDefs2(done2, defs2, deflt);
} }
set<Expr> done2; set<Expr> done2;

View File

@ -19,6 +19,9 @@ MakeError(TypeError, EvalError)
normals forms efficiently. */ normals forms efficiently. */
typedef ATerm Expr; typedef ATerm Expr;
typedef ATerm DefaultValue;
typedef ATerm ValidValues;
typedef ATerm Pos; typedef ATerm Pos;

View File

@ -118,10 +118,8 @@ static void checkAttrSets(ATerm e)
ATermMap names(ATgetLength(formals)); ATermMap names(ATgetLength(formals));
for (ATermIterator i(formals); i; ++i) { for (ATermIterator i(formals); i; ++i) {
ATerm name; ATerm name;
Expr deflt; ATerm d1, d2;
if (!matchNoDefFormal(*i, name) && if (!matchFormal(*i, name, d1, d2)) abort();
!matchDefFormal(*i, name, deflt))
abort();
if (names.get(name)) if (names.get(name))
throw EvalError(format("duplicate formal function argument `%1%' at %2%") throw EvalError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name) % showPos(pos)); % aterm2String(name) % showPos(pos));

View File

@ -16,6 +16,8 @@
#include "lexer-tab.h" #include "lexer-tab.h"
typedef ATerm Expr; typedef ATerm Expr;
typedef ATerm ValidValues;
typedef ATerm DefaultValue;
typedef ATerm Pos; typedef ATerm Pos;
#include "nixexpr-ast.hh" #include "nixexpr-ast.hh"
@ -203,8 +205,9 @@ formals
; ;
formal formal
: ID { $$ = makeNoDefFormal($1); } : ID { $$ = makeFormal($1, makeUnrestrictedValues(), makeNoDefaultValue()); }
| ID '?' expr { $$ = makeDefFormal($1, $3); } // | ID ':' '[' expr_list ']' { $$ = makeDefFormal($1, $3); }
| ID '?' expr { $$ = makeFormal($1, makeUnrestrictedValues(), makeDefaultValue($3)); }
; ;
%% %%

View File

@ -1 +1 @@
Function([NoDefFormal("x"),NoDefFormal("y"),NoDefFormal("z")],OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),Pos("(string)",1,2)) Function([Formal("x",UnrestrictedValues,NoDefaultValue),Formal("y",UnrestrictedValues,NoDefaultValue),Formal("z",UnrestrictedValues,NoDefaultValue)],OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),Pos("(string)",1,2))

View File

@ -1 +1 @@
Function([NoDefFormal("stdenv"),NoDefFormal("fetchurl")],Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1"),Pos("(string)",4,7)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2"),Pos("(string)",6,8)),Bind("md5",Str("7e935a42428d63a387b3c048be0f2756"),Pos("(string)",7,8))])),Pos("(string)",5,6))])),Pos("(string)",1,2)) Function([Formal("stdenv",UnrestrictedValues,NoDefaultValue),Formal("fetchurl",UnrestrictedValues,NoDefaultValue)],Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1"),Pos("(string)",4,7)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2"),Pos("(string)",6,8)),Bind("md5",Str("7e935a42428d63a387b3c048be0f2756"),Pos("(string)",7,8))])),Pos("(string)",5,6))])),Pos("(string)",1,2))

View File

@ -1 +1 @@
Function([DefFormal("localServer",Var("false")),DefFormal("httpServer",Var("false")),DefFormal("sslSupport",Var("false")),DefFormal("pythonBindings",Var("false")),DefFormal("javaSwigBindings",Var("false")),DefFormal("javahlBindings",Var("false")),NoDefFormal("stdenv"),NoDefFormal("fetchurl"),DefFormal("openssl",Var("null")),DefFormal("httpd",Var("null")),DefFormal("db4",Var("null")),NoDefFormal("expat"),DefFormal("swig",Var("null")),DefFormal("j2sdk",Var("null"))],Assert(OpNEq(Var("expat"),Var("null")),Assert(OpImpl(Var("localServer"),OpNEq(Var("db4"),Var("null"))),Assert(OpImpl(Var("httpServer"),OpAnd(OpNEq(Var("httpd"),Var("null")),OpEq(Select(Var("httpd"),"expat"),Var("expat")))),Assert(OpImpl(Var("sslSupport"),OpAnd(OpNEq(Var("openssl"),Var("null")),OpImpl(Var("httpServer"),OpEq(Select(Var("httpd"),"openssl"),Var("openssl"))))),Assert(OpImpl(Var("pythonBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"pythonSupport"))),Assert(OpImpl(Var("javaSwigBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"javaSupport"))),Assert(OpImpl(Var("javahlBindings"),OpNEq(Var("j2sdk"),Var("null"))),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("subversion-1.1.1"),Pos("(string)",20,7)),Bind("builder",Path("/foo/bar"),Pos("(string)",22,10)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2"),Pos("(string)",24,8)),Bind("md5",Str("a180c3fe91680389c210c99def54d9e0"),Pos("(string)",25,8))])),Pos("(string)",23,6)),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),Pos("(string)",31,10)),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),Pos("(string)",33,10)),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),Pos("(string)",34,8)),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),Pos("(string)",35,6)),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),Pos("(string)",36,7)),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),Pos("(string)",37,9)),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),Pos("(string)",38,8)),Bind("expat",Var("expat"),Pos("(string)",41,10)),Bind("localServer",Var("localServer"),Pos("(string)",41,10)),Bind("httpServer",Var("httpServer"),Pos("(string)",41,10)),Bind("sslSupport",Var("sslSupport"),Pos("(string)",41,10)),Bind("pythonBindings",Var("pythonBindings"),Pos("(string)",41,10)),Bind("javaSwigBindings",Var("javaSwigBindings"),Pos("(string)",41,10)),Bind("javahlBindings",Var("javahlBindings"),Pos("(string)",41,10))])),Pos("(string)",17,7)),Pos("(string)",16,7)),Pos("(string)",15,7)),Pos("(string)",14,7)),Pos("(string)",13,7)),Pos("(string)",12,7)),Pos("(string)",11,7)),Pos("(string)",1,2)) Function([Formal("localServer",UnrestrictedValues,DefaultValue(Var("false"))),Formal("httpServer",UnrestrictedValues,DefaultValue(Var("false"))),Formal("sslSupport",UnrestrictedValues,DefaultValue(Var("false"))),Formal("pythonBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("javaSwigBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("javahlBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("stdenv",UnrestrictedValues,NoDefaultValue),Formal("fetchurl",UnrestrictedValues,NoDefaultValue),Formal("openssl",UnrestrictedValues,DefaultValue(Var("null"))),Formal("httpd",UnrestrictedValues,DefaultValue(Var("null"))),Formal("db4",UnrestrictedValues,DefaultValue(Var("null"))),Formal("expat",UnrestrictedValues,NoDefaultValue),Formal("swig",UnrestrictedValues,DefaultValue(Var("null"))),Formal("j2sdk",UnrestrictedValues,DefaultValue(Var("null")))],Assert(OpNEq(Var("expat"),Var("null")),Assert(OpImpl(Var("localServer"),OpNEq(Var("db4"),Var("null"))),Assert(OpImpl(Var("httpServer"),OpAnd(OpNEq(Var("httpd"),Var("null")),OpEq(Select(Var("httpd"),"expat"),Var("expat")))),Assert(OpImpl(Var("sslSupport"),OpAnd(OpNEq(Var("openssl"),Var("null")),OpImpl(Var("httpServer"),OpEq(Select(Var("httpd"),"openssl"),Var("openssl"))))),Assert(OpImpl(Var("pythonBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"pythonSupport"))),Assert(OpImpl(Var("javaSwigBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"javaSupport"))),Assert(OpImpl(Var("javahlBindings"),OpNEq(Var("j2sdk"),Var("null"))),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("subversion-1.1.1"),Pos("(string)",20,7)),Bind("builder",Path("/foo/bar"),Pos("(string)",22,10)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2"),Pos("(string)",24,8)),Bind("md5",Str("a180c3fe91680389c210c99def54d9e0"),Pos("(string)",25,8))])),Pos("(string)",23,6)),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),Pos("(string)",31,10)),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),Pos("(string)",33,10)),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),Pos("(string)",34,8)),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),Pos("(string)",35,6)),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),Pos("(string)",36,7)),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),Pos("(string)",37,9)),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),Pos("(string)",38,8)),Bind("expat",Var("expat"),Pos("(string)",41,10)),Bind("localServer",Var("localServer"),Pos("(string)",41,10)),Bind("httpServer",Var("httpServer"),Pos("(string)",41,10)),Bind("sslSupport",Var("sslSupport"),Pos("(string)",41,10)),Bind("pythonBindings",Var("pythonBindings"),Pos("(string)",41,10)),Bind("javaSwigBindings",Var("javaSwigBindings"),Pos("(string)",41,10)),Bind("javahlBindings",Var("javahlBindings"),Pos("(string)",41,10))])),Pos("(string)",17,7)),Pos("(string)",16,7)),Pos("(string)",15,7)),Pos("(string)",14,7)),Pos("(string)",13,7)),Pos("(string)",12,7)),Pos("(string)",11,7)),Pos("(string)",1,2))