Concat any exprs

This commit is contained in:
Kevin Leung
2016-12-07 10:08:47 +08:00
parent f4fb7d63c3
commit 38aa0f4dc4
2 changed files with 14 additions and 3 deletions

View File

@@ -389,11 +389,15 @@ class Exprs {
default: e.pos.makeFailure(NOT_A_FUNCTION);
}
static public function concat(e:Expr, with:Expr, ?pos)
static public function concat(e:Expr, with:Expr, ?pos) {
if(pos == null) pos = e.pos;
return
switch [e.expr, with.expr] {
case [EBlock(e1), EBlock(e2)]: Success(EBlock(e1.concat(e2)).at(pos == null ? e.pos : pos));
default: e.pos.makeFailure(NOT_A_BLOCK);
case [EBlock(e1), EBlock(e2)]: Success(EBlock(e1.concat(e2)).at(pos));
case [EBlock(e1), e2]: Success(EBlock(e1.concat([with])).at(pos));
case [e1, EBlock(e2)]: Success(EBlock([e].concat(e2)).at(pos));
default: Success(EBlock([e, with]).at(pos));
}
}
static inline var NOT_AN_INT = "integer constant expected";

View File

@@ -80,4 +80,11 @@ class Exprs extends Base {
])
);
}
function testConcat() {
exprEq(macro {a; b;}, (macro a).concat(macro b).sure());
exprEq(macro {a; b; c;}, (macro {a; b;}).concat(macro c).sure());
exprEq(macro {a; b; c;}, (macro a).concat(macro {b; c;}).sure());
exprEq(macro {a; b; c; d;}, (macro {a; b;}).concat(macro {c; d;}).sure());
}
}