Permalink
Browse files

Added pragma.enable("for-must-match")

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...
1 parent a0e86dc commit a0b3b599cf267b3138eea5f5fb83f27cebd28373 tal committed Oct 26, 2011
View
1 src/bin/resources/org/erights/e/elang/syntax/syntax-props-0.9.txt
@@ -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
View
6 src/bin/resources/org/erights/e/elang/syntax/syntax-props-default.txt
@@ -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 ####################
View
46 src/jsrc/org/erights/e/elang/syntax/ENodeBuilder.java
@@ -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(),

0 comments on commit a0b3b59

Please sign in to comment.