Skip to content

Commit

Permalink
Added pragma.enable("for-must-match")
Browse files Browse the repository at this point in the history
When off, this silently does nothing:

  for [a, b] in [1, 2, 3] { foo(a, b) }

When on, it throws an exception.

git-svn-id: svn://svn.synchrona.org/erights/e/trunk@819 bdfa04cf-0718-0410-970a-89734667f18f
  • Loading branch information
tal committed Oct 26, 2011
1 parent a0e86dc commit a0b3b59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Expand Up @@ -17,6 +17,7 @@ e.enable.easy-return=true
e.enable.easy-when=true
e.enable.escape-handler=true
e.enable.exporter=true
e.enable.for-must-match=allow
e.enable.importer=true
e.enable.meta-scope=true
e.enable.noun-string=true
Expand Down
Expand Up @@ -393,6 +393,12 @@ e.enable.thunk=allow

e.enable.hard-when=allow


################ New as of 0.9.4 #######################

# The pattern in a for-loop must match every item or an error is thrown.
e.enable.for-must-match=allow

################ Discontinued Switches ####################


Expand Down
46 changes: 33 additions & 13 deletions src/jsrc/org/erights/e/elang/syntax/ENodeBuilder.java
Expand Up @@ -929,25 +929,45 @@ public EExpr forx(Object assoc,
ensureCommutes(key.staticScope().add(value.staticScope()),
coll.staticScope());

MsgPatt mpatt = methHead(NO_POSER,
"run",
list(finalPattern(kTemp),
finalPattern(vTemp)),
null);

EExpr body = sequence(call(REQUIRE,
"run",
list(noun(fTemp), literal(BAD_FOR))),
ifx(condAnd(matchBind(noun(kTemp),
MsgPatt mpatt;
EExpr innerBody = escape(finalPattern("__continue"),
sequence(bodyExpr, NULL),
null);
EExpr binding;

if (ConstMap.testProp(myProps, "e.enable.for-must-match")) {
/* Do the pattern matching directly in the for-body method. This
* will throw an exception if there is no match. */
mpatt = methHead(NO_POSER,
"run",
list(key, value),
null);
binding = innerBody;
} else {
/* Bind the key and value to temporary values in the for-body method
* and then matchBind them inside the method. This will skip items that
* don't match.
*/
mpatt = methHead(NO_POSER,
"run",
list(finalPattern(kTemp),
finalPattern(vTemp)),
null);
binding = ifx(condAnd(matchBind(noun(kTemp),
NO_POSER,
key),
NO_POSER,
matchBind(noun(vTemp),
NO_POSER,
value)),
escape(finalPattern("__continue"),
sequence(bodyExpr, NULL),
null)));
innerBody);
}

EExpr body = sequence(call(REQUIRE,
"run",
list(noun(fTemp), literal(BAD_FOR))),
binding
);

EExpr closure = object(" For-loop body ",
ignoreOName(),
Expand Down

0 comments on commit a0b3b59

Please sign in to comment.