Skip to content
Browse files

Allow single-argument untyped blocks as patterns

{ a -> a } should be a matching block, but previously was not. Now, it
will be treated as one at runtime (but not compile a pattern object).
  • Loading branch information...
1 parent 9733de0 commit 72d4e6fbae549594ac167bc3002caefe680c8c14 @mwh committed May 16, 2012
Showing with 26 additions and 3 deletions.
  1. +8 −1 genjs.grace
  2. +6 −2 gracelib.c
  3. +2 −0 tests/t124_matchingbare.out
  4. +10 −0 tests/t124_matchingbare_test.grace
View
9 genjs.grace
@@ -219,10 +219,17 @@ method compileblock(o) {
out(" if (Grace_isTrue(match)) \{")
out(" var bindings = callmethod(match, \"bindings\");")
out(" var rv = callmethod(this, \"applyIndirectly\", bindings);")
- out(" return new GraceSuccessfulMatch(rv, bindings);")
+ out(" return new GraceSuccessfulMatch(rv, []);")
out(" \}")
out(" return new GraceFailedMatch(rv);")
out(" \}")
+ } else {
+ if (o.params.size == 1) then {
+ out(" block{myc}.methods[\"match\"] = function(o) \{")
+ out(" var r = this.real.apply(this.receiver, arguments);")
+ out(" return new GraceSuccessfulMatch(r, []);")
+ out(" \}")
+ }
}
out(" block" ++ myc ++ ".receiver = this;")
out(" block{myc}.className = 'block<{modname}:{o.line}>';")
View
8 gracelib.c
@@ -2886,8 +2886,12 @@ Object Block_applyIndirectly(Object self, int nargs, Object *args, int flags) {
}
Object Block_match(Object self, int nargs, Object *args, int flags) {
struct BlockObject *bo = (struct BlockObject*)self;
- if (!bo->data[1])
- die("block is not a matching block");
+ if (!bo->data[1]) {
+ if (nargs != 1)
+ die("block is not a matching block");
+ Object r = callmethod(self, "apply", nargs, args);
+ return alloc_SuccessfulMatch(r, NULL);
+ }
Object pattern = bo->data[1];
Object match = callmethod(pattern, "match", 1, args);
if (!istrue(match))
View
2 tests/t124_matchingbare.out
@@ -0,0 +1,2 @@
+TWO
+FALLTHROUGH 1
View
10 tests/t124_matchingbare_test.grace
@@ -0,0 +1,10 @@
+method tryMatch(x) {
+ match(x)
+ case { y : String -> print "STRING" }
+ case { 2 -> print "TWO" }
+ case { y -> print "FALLTHROUGH {y}" }
+}
+
+tryMatch 2
+tryMatch 1
+

0 comments on commit 72d4e6f

Please sign in to comment.
Something went wrong with that request. Please try again.