From 2ca2d11006f2cd067ba02989415534a00fc328c5 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 27 Feb 2014 00:54:48 +0100 Subject: [PATCH] change extractor syntax (closes #2676) --- matcher.ml | 8 +++---- tests/unit/TestMatch.hx | 24 ++++++++++----------- tests/unit/issues/Issue2676.hx | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 tests/unit/issues/Issue2676.hx diff --git a/matcher.ml b/matcher.ml index 0223efb6ef6..856001cd714 100644 --- a/matcher.ml +++ b/matcher.ml @@ -1017,12 +1017,12 @@ let transform_extractors mctx stl cases = | EBinop(OpArrow,_,_) when !in_or -> error "Extractors in or patterns are not allowed" (pos e) | EBinop(OpArrow, e1, e2) -> - let p = pos e in let ec = EConst (Ident ("__ex" ^ string_of_int (!exc))),snd e in - let ecall = match fst e1 with - | ECall((EField((EConst(Ident "_"),_),s),_), el) -> ECall((EField(ec,s),p),el),p - | _ -> ECall(e1,[ec]),p + let rec map_left e = match fst e with + | EConst(Ident "_") -> ec + | _ -> Ast.map_expr map_left e in + let ecall = map_left e1 in ex := (ecall,e2) :: !ex; incr exc; ec diff --git a/tests/unit/TestMatch.hx b/tests/unit/TestMatch.hx index 18aadc5b0ef..ecf1106bbd7 100644 --- a/tests/unit/TestMatch.hx +++ b/tests/unit/TestMatch.hx @@ -462,7 +462,7 @@ class TestMatch extends Test { function f(i) { return switch(i) { case 1,2,3: 1; - case even => true: 2; + case _.even() => true: 2; case 4: throw "unreachable"; case _: 3; } @@ -484,9 +484,9 @@ class TestMatch extends Test { function f(t:MiniType) { return switch (t) { - case MTString(deref => "Foo", []): "Foo"; - case MTString(deref => "Bar" | "Baz", _): "BarBaz"; - case MTInt(deref => i, []): 'Int:$i'; + case MTString(_.deref() => "Foo", []): "Foo"; + case MTString(_.deref() => "Bar" | "Baz", _): "BarBaz"; + case MTInt(_.deref() => i, []): 'Int:$i'; case MTString(_): "OtherString"; case _: "Other"; } @@ -503,7 +503,7 @@ class TestMatch extends Test { function g(i : Array) { return switch(i) { case [x]: 1; - case isPair => Some(p) : p.a+p.b; + case isPair(_) => Some(p) : p.a+p.b; case arr: 3; } } @@ -520,7 +520,7 @@ class TestMatch extends Test { var i = 9; var r = switch(i) { case 1: 1; - case anon.odd => true: 2; + case anon.odd(_) => true: 2; case 9: 3; case _: 4; } @@ -531,8 +531,8 @@ class TestMatch extends Test { function check(i) { return switch(i) { case 1: 1; - case mul.bind(4) => 8: 2; - case mul.bind(5) => 15: 3; + case mul(_, 4) => 8: 2; + case mul(_, 5) => 15: 3; case _: 4; } } @@ -556,10 +556,10 @@ class TestMatch extends Test { function h(i : Array) { return switch(i) { case [x]: 1; - case isPair => Some({ a : a, b : b }) if (a < 0): 42; - case isPair => Some({ a : is(even) => Some(a), b : b }) : a+b; - case isPair => Some({ a : isNot(even) => Some(a), b : b }) : a*b; - case testArgs.bind(1, "foo") => "[99,98,97]": 99; + case isPair(_) => Some({ a : a, b : b }) if (a < 0): 42; + case isPair(_) => Some({ a : is(even)(_) => Some(a), b : b }) : a+b; + case isPair(_) => Some({ a : isNot(even)(_) => Some(a), b : b }) : a*b; + case testArgs(1, "foo", _) => "[99,98,97]": 99; case arr: 3; } } diff --git a/tests/unit/issues/Issue2676.hx b/tests/unit/issues/Issue2676.hx new file mode 100644 index 00000000000..899c5cd94e8 --- /dev/null +++ b/tests/unit/issues/Issue2676.hx @@ -0,0 +1,39 @@ +package unit.issues; +import unit.Test; + + +class Issue2676 extends Test { + function test() { + function match1(s:String) { + return switch(s) { + case "foo": 1; + case _.toUpperCase() => "BAR": 2; + case _ + "," + _ => "abc,abc": 3; + case _: 4; + } + } + + eq(1, match1("foo")); + eq(4, match1("foo2")); + eq(2, match1("bAr")); + eq(2, match1("BAR")); + eq(2, match1("bar")); + eq(4, match1("barz")); + eq(3, match1("abc")); + eq(4, match1("ab")); + + // check side effect handling + function func(i1:Int, i2:Int, i3:Int) { + return '$i1;$i2;$i3'; + } + + var i = 0; + + var s = switch(9) { + case func(i++, i++, i++) => "0;1;2": "ok"; + case _: "not ok"; + } + + eq("ok", s); + } +} \ No newline at end of file