* Simplify @-patterns: only {attrs}@name' or name@{attrs}' are now

allowed.  So `name1@name2', `{attrs1}@{attrs2}' and so on are now no
  longer legal.  This is no big loss because they were not useful
  anyway.

  This also changes the output of builtins.toXML for @-patterns
  slightly.
This commit is contained in:
Eelco Dolstra 2010-03-25 12:19:41 +00:00
parent 7482349fe8
commit 8a10360c91
13 changed files with 83 additions and 173 deletions

View file

@ -111,91 +111,6 @@ Env * allocEnv()
}
static bool patternIsStrict(Pattern pat)
{
ATerm name, ellipsis, pat1, pat2;
ATermList formals;
if (matchVarPat(pat, name)) return false;
else if (matchAttrsPat(pat, formals, ellipsis)) return true;
else if (matchAtPat(pat, pat1, pat2))
return patternIsStrict(pat1) || patternIsStrict(pat2);
else abort();
}
static void bindVarPats(Pattern pat, Env & newEnv,
Env * argEnv, Expr argExpr, Value * & vArg)
{
Pattern pat1, pat2;
if (matchAtPat(pat, pat1, pat2)) {
bindVarPats(pat1, newEnv, argEnv, argExpr, vArg);
bindVarPats(pat2, newEnv, argEnv, argExpr, vArg);
return;
}
ATerm name;
if (!matchVarPat(pat, name)) abort();
if (vArg) {
Value & v = newEnv.bindings[aterm2String(name)];
v.type = tCopy;
v.val = vArg;
} else {
vArg = &newEnv.bindings[aterm2String(name)];
vArg->type = tThunk;
vArg->thunk.env = argEnv;
vArg->thunk.expr = argExpr;
}
}
static void bindAttrPats(Pattern pat, Env & newEnv,
Value & vArg, Value * & vArgInEnv)
{
Pattern pat1, pat2;
if (matchAtPat(pat, pat1, pat2)) {
bindAttrPats(pat1, newEnv, vArg, vArgInEnv);
bindAttrPats(pat2, newEnv, vArg, vArgInEnv);
return;
}
ATerm name;
if (matchVarPat(pat, name)) {
if (vArgInEnv) {
Value & v = newEnv.bindings[aterm2String(name)];
v.type = tCopy;
v.val = vArgInEnv;
} else {
vArgInEnv = &newEnv.bindings[aterm2String(name)];
*vArgInEnv = vArg;
}
return;
}
ATerm ellipsis;
ATermList formals;
if (matchAttrsPat(pat, formals, ellipsis)) {
for (ATermIterator i(formals); i; ++i) {
Expr name, def;
DefaultValue def2;
if (!matchFormal(*i, name, def2)) abort(); /* can't happen */
Bindings::iterator j = vArg.attrs->find(aterm2String(name));
if (j == vArg.attrs->end())
throw TypeError(format("the argument named `%1%' required by the function is missing")
% aterm2String(name));
Value & v = newEnv.bindings[aterm2String(name)];
v.type = tCopy;
v.val = &j->second;
}
return;
}
abort();
}
static void eval(Env * env, Expr e, Value & v)
{
printMsg(lvlError, format("eval: %1%") % e);
@ -279,17 +194,45 @@ static void eval(Env * env, Expr e, Value & v)
Env * env2 = allocEnv();
env2->up = env;
if (patternIsStrict(v.lambda.pat)) {
Value vArg;
eval(env, arg, vArg);
if (vArg.type != tAttrs) throw TypeError("expected attribute set");
Value * vArg2 = 0;
bindAttrPats(v.lambda.pat, *env2, vArg, vArg2);
} else {
Value * vArg = 0;
bindVarPats(v.lambda.pat, *env2, env, arg, vArg);
ATermList formals; ATerm ellipsis;
if (matchVarPat(v.lambda.pat, name)) {
Value & vArg = env2->bindings[aterm2String(name)];
vArg.type = tThunk;
vArg.thunk.env = env;
vArg.thunk.expr = arg;
}
else if (matchAttrsPat(v.lambda.pat, formals, ellipsis, name)) {
Value * vArg;
Value vArg_;
if (name == sNoAlias)
vArg = &vArg_;
else
vArg = &env2->bindings[aterm2String(name)];
eval(env, arg, *vArg);
if (vArg->type != tAttrs) throw TypeError("expected attribute set");
for (ATermIterator i(formals); i; ++i) {
Expr name, def;
DefaultValue def2;
if (!matchFormal(*i, name, def2)) abort(); /* can't happen */
Bindings::iterator j = vArg->attrs->find(aterm2String(name));
if (j == vArg->attrs->end())
throw TypeError(format("the argument named `%1%' required by the function is missing")
% aterm2String(name));
Value & v = env2->bindings[aterm2String(name)];
v.type = tCopy;
v.val = &j->second;
}
}
else abort();
eval(env2, v.lambda.body, v);
return;
}
@ -319,13 +262,10 @@ void run(Strings args)
doTest("rec { x = 1; y = x; }.y");
doTest("(x: x) 1");
doTest("(x: y: y) 1 2");
doTest("(x@y: x) 1");
doTest("(x@y: y) 2");
doTest("(x@y@z: y) 3");
doTest("x: x");
doTest("({x, y}: x) { x = 1; y = 2; }");
doTest("({x, y}@args: args.x) { x = 1; y = 2; }");
doTest("({x, y}@args@args2: args2.x) { x = 1; y = 2; }");
doTest("(args@{x, y}: args.x) { x = 1; y = 2; }");
//Expr e = parseExprFromString(state, "let x = \"a\"; in x + \"b\"", "/");
//Expr e = parseExprFromString(state, "(x: x + \"b\") \"a\"", "/");

View file

@ -85,16 +85,17 @@ static void patternMatch(EvalState & state,
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
if (matchVarPat(pat, name))
subs.set(name, arg);
else if (matchAttrsPat(pat, formals, ellipsis)) {
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
arg = evalExpr(state, arg);
if (name != sNoAlias) subs.set(name, arg);
/* Get the actual arguments. */
ATermMap attrs;
queryAllAttrs(arg, attrs);
@ -131,11 +132,6 @@ static void patternMatch(EvalState & state,
% aterm2String(attrs.begin()->key));
}
else if (matchAtPat(pat, pat1, pat2)) {
patternMatch(state, pat1, arg, subs, subsRecursive);
patternMatch(state, pat2, arg, subs, subsRecursive);
}
else abort();
}
@ -425,12 +421,11 @@ Path coerceToPath(EvalState & state, Expr e, PathSet & context)
Expr autoCallFunction(Expr e, const ATermMap & args)
{
Pattern pat;
ATerm body, pos;
ATerm body, pos, name;
ATermList formals;
ATermBool ellipsis;
/* !!! this should be more general */
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) {
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis, name)) {
ATermMap actualArgs(ATgetLength(formals));
for (ATermIterator i(formals); i; ++i) {

View file

@ -44,23 +44,19 @@ static void printPatternAsXML(Pattern pat, XMLWriter & doc)
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
if (matchVarPat(pat, name))
doc.writeEmptyElement("varpat", singletonAttrs("name", aterm2String(name)));
else if (matchAttrsPat(pat, formals, ellipsis)) {
XMLOpenElement _(doc, "attrspat");
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
XMLAttrs attrs;
if (name != sNoAlias) attrs["name"] = aterm2String(name);
if (ellipsis == eTrue) attrs["ellipsis"] = "1";
XMLOpenElement _(doc, "attrspat", attrs);
for (ATermIterator i(formals); i; ++i) {
Expr name; ATerm dummy;
if (!matchFormal(*i, name, dummy)) abort();
doc.writeEmptyElement("attr", singletonAttrs("name", aterm2String(name)));
}
if (ellipsis == eTrue) doc.writeEmptyElement("ellipsis");
}
else if (matchAtPat(pat, pat1, pat2)) {
XMLOpenElement _(doc, "at");
printPatternAsXML(pat1, doc);
printPatternAsXML(pat2, doc);
}
}

View file

@ -77,8 +77,7 @@ Inherit | Expr ATermList Pos | ATerm |
Scope | | Expr |
VarPat | string | Pattern |
AttrsPat | ATermList ATermBool | Pattern | # bool = `...'
AtPat | Pattern Pattern | Pattern |
AttrsPat | ATermList ATermBool string | Pattern | # bool = `...'
Formal | string DefaultValue | ATerm |
@ -95,3 +94,4 @@ AttrRHS | Expr Pos | ATerm |
eTrue = makeBool(makeTrue())
eFalse = makeBool(makeFalse())
sOverrides = toATerm("__overrides")
sNoAlias = toATerm("")

View file

@ -114,23 +114,19 @@ static void varsBoundByPattern(ATermMap & map, Pattern pat)
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
/* Use makeRemoved() so that it can be used directly in
substitute(). */
if (matchVarPat(pat, name))
map.set(name, makeRemoved());
else if (matchAttrsPat(pat, formals, ellipsis)) {
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
if (name != sNoAlias) map.set(name, makeRemoved());
for (ATermIterator i(formals); i; ++i) {
ATerm d1;
if (!matchFormal(*i, name, d1)) abort();
map.set(name, makeRemoved());
}
}
else if (matchAtPat(pat, pat1, pat2)) {
varsBoundByPattern(map, pat1);
varsBoundByPattern(map, pat2);
}
else abort();
}

View file

@ -140,32 +140,30 @@ static Expr fixAttrs(bool recursive, ATermList as)
static void checkPatternVars(ATerm pos, ATermMap & map, Pattern pat)
{
ATerm name;
ATerm name = sNoAlias;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
if (matchVarPat(pat, name)) {
if (map.get(name))
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name) % showPos(pos));
map.set(name, name);
}
else if (matchAttrsPat(pat, formals, ellipsis)) {
if (matchAttrsPat(pat, formals, ellipsis, name)) {
for (ATermIterator i(formals); i; ++i) {
ATerm d1;
if (!matchFormal(*i, name, d1)) abort();
ATerm d1, name2;
if (!matchFormal(*i, name2, d1)) abort();
if (map.get(name2))
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name2) % showPos(pos));
map.set(name2, name2);
}
}
else matchVarPat(pat, name);
if (name != sNoAlias) {
if (map.get(name))
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
% aterm2String(name) % showPos(pos));
map.set(name, name);
}
}
else if (matchAtPat(pat, pat1, pat2)) {
checkPatternVars(pos, map, pat1);
checkPatternVars(pos, map, pat2);
}
else abort();
}
static void checkPatternVars(ATerm pos, Pattern pat)
@ -323,7 +321,7 @@ static void freeAndUnprotect(void * p)
%type <t> start expr expr_function expr_if expr_op
%type <t> expr_app expr_select expr_simple bind inheritsrc formal
%type <t> pattern pattern2
%type <t> pattern
%type <ts> binds ids attrpath expr_list string_parts ind_string_parts
%type <formals> formals
%token <t> ID INT STR IND_STR PATH URI
@ -433,13 +431,10 @@ ind_string_parts
;
pattern
: pattern2 '@' pattern { $$ = makeAtPat($1, $3); }
| pattern2
;
pattern2
: ID { $$ = makeVarPat($1); }
| '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse); }
| '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse, sNoAlias); }
| '{' formals '}' '@' ID { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse, $5); }
| ID '@' '{' formals '}' { $$ = makeAttrsPat($4.formals, $4.ellipsis ? eTrue : eFalse, $1); }
;
binds

View file

@ -846,19 +846,14 @@ static void attrsInPattern(ATermMap & map, Pattern pat)
{
ATerm name;
ATermList formals;
Pattern pat1, pat2;
ATermBool ellipsis;
if (matchAttrsPat(pat, formals, ellipsis)) {
if (matchAttrsPat(pat, formals, ellipsis, name)) {
for (ATermIterator i(formals); i; ++i) {
ATerm def;
if (!matchFormal(*i, name, def)) abort();
map.set(name, makeAttrRHS(makeBool(def != constNoDefaultValue), makeNoPos()));
}
}
else if (matchAtPat(pat, pat1, pat2)) {
attrsInPattern(map, pat1);
attrsInPattern(map, pat2);
}
}

View file

@ -1 +1 @@
Str("abcxyzDDDDEFghijk",[])
Str("abcxyzDDDDEFijk",[])

View file

@ -6,8 +6,6 @@ let
h = {x ? "d", y ? x, z ? args.x}@args: x + y + z;
i = args@args2: args.x + args2.y;
j = {x, y, z, ...}: x + y + z;
in
@ -15,5 +13,4 @@ in
g {x = "x"; y = "y"; z = "z";} +
h {x = "D";} +
h {x = "D"; y = "E"; z = "F";} +
i {x = "g"; y = "h";} +
j {x = "i"; y = "j"; z = "k"; bla = "bla"; foo = "bar";}

View file

@ -6,14 +6,11 @@
</attr>
<attr name="at">
<function>
<at>
<varpat name="args" />
<attrspat>
<attrspat name="args">
<attr name="x" />
<attr name="y" />
<attr name="z" />
</attrspat>
</at>
</function>
</attr>
<attr name="b">
@ -24,11 +21,10 @@
</attr>
<attr name="ellipsis">
<function>
<attrspat>
<attrspat ellipsis="1">
<attr name="x" />
<attr name="y" />
<attr name="z" />
<ellipsis />
</attrspat>
</function>
</attr>

View file

@ -1 +1 @@
Function(AttrsPat([Formal("x",NoDefaultValue),Formal("y",NoDefaultValue),Formal("z",NoDefaultValue)],Bool(False)),OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),NoPos)
Function(AttrsPat([Formal("x",NoDefaultValue),Formal("y",NoDefaultValue),Formal("z",NoDefaultValue)],Bool(False),""),OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),NoPos)

View file

@ -1 +1 @@
Function(AttrsPat([Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue)],Bool(False)),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1",[]),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("7e935a42428d63a387b3c048be0f2756",[]),NoPos),Bind("url",Str("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2",[]),NoPos)])),NoPos)])),NoPos)
Function(AttrsPat([Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue)],Bool(False),""),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1",[]),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("7e935a42428d63a387b3c048be0f2756",[]),NoPos),Bind("url",Str("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2",[]),NoPos)])),NoPos)])),NoPos)

View file

@ -1 +1 @@
Function(AttrsPat([Formal("localServer",DefaultValue(Var("false"))),Formal("httpServer",DefaultValue(Var("false"))),Formal("sslSupport",DefaultValue(Var("false"))),Formal("pythonBindings",DefaultValue(Var("false"))),Formal("javaSwigBindings",DefaultValue(Var("false"))),Formal("javahlBindings",DefaultValue(Var("false"))),Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue),Formal("openssl",DefaultValue(Var("null"))),Formal("httpd",DefaultValue(Var("null"))),Formal("db4",DefaultValue(Var("null"))),Formal("expat",NoDefaultValue),Formal("swig",DefaultValue(Var("null"))),Formal("j2sdk",DefaultValue(Var("null")))],Bool(False)),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("builder",Path("/foo/bar"),NoPos),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),NoPos),Bind("expat",Var("expat"),NoPos),Bind("httpServer",Var("httpServer"),NoPos),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),NoPos),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),NoPos),Bind("javaSwigBindings",Var("javaSwigBindings"),NoPos),Bind("javahlBindings",Var("javahlBindings"),NoPos),Bind("localServer",Var("localServer"),NoPos),Bind("name",Str("subversion-1.1.1",[]),NoPos),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),NoPos),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),NoPos),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),NoPos),Bind("pythonBindings",Var("pythonBindings"),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("a180c3fe91680389c210c99def54d9e0",[]),NoPos),Bind("url",Str("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2",[]),NoPos)])),NoPos),Bind("sslSupport",Var("sslSupport"),NoPos),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),NoPos)])),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos)
Function(AttrsPat([Formal("localServer",DefaultValue(Var("false"))),Formal("httpServer",DefaultValue(Var("false"))),Formal("sslSupport",DefaultValue(Var("false"))),Formal("pythonBindings",DefaultValue(Var("false"))),Formal("javaSwigBindings",DefaultValue(Var("false"))),Formal("javahlBindings",DefaultValue(Var("false"))),Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue),Formal("openssl",DefaultValue(Var("null"))),Formal("httpd",DefaultValue(Var("null"))),Formal("db4",DefaultValue(Var("null"))),Formal("expat",NoDefaultValue),Formal("swig",DefaultValue(Var("null"))),Formal("j2sdk",DefaultValue(Var("null")))],Bool(False),""),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("builder",Path("/foo/bar"),NoPos),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),NoPos),Bind("expat",Var("expat"),NoPos),Bind("httpServer",Var("httpServer"),NoPos),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),NoPos),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),NoPos),Bind("javaSwigBindings",Var("javaSwigBindings"),NoPos),Bind("javahlBindings",Var("javahlBindings"),NoPos),Bind("localServer",Var("localServer"),NoPos),Bind("name",Str("subversion-1.1.1",[]),NoPos),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),NoPos),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),NoPos),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),NoPos),Bind("pythonBindings",Var("pythonBindings"),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("a180c3fe91680389c210c99def54d9e0",[]),NoPos),Bind("url",Str("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2",[]),NoPos)])),NoPos),Bind("sslSupport",Var("sslSupport"),NoPos),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),NoPos)])),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos)