Skip to content

Commit

Permalink
change extractor syntax (closes HaxeFoundation#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Feb 26, 2014
1 parent 8d79993 commit 2ca2d11
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
8 changes: 4 additions & 4 deletions matcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/TestMatch.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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";
}
Expand All @@ -503,7 +503,7 @@ class TestMatch extends Test {
function g(i : Array<Int>) {
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;
}
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand All @@ -556,10 +556,10 @@ class TestMatch extends Test {
function h(i : Array<Int>) {
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;
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/issues/Issue2676.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 2ca2d11

Please sign in to comment.