From aefe6224348bc6327b23ba430491ac5e1d34bcf4 Mon Sep 17 00:00:00 2001 From: back2dos Date: Sat, 2 Nov 2013 03:02:18 +0100 Subject: [PATCH] Added options to yield. --- README.md | 4 +++- haxelib.json | 4 ++-- src/tink/macro/Exprs.hx | 8 ++++---- tests/Exprs.hx | 5 +++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7418c55..eb38fab 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ This is a way to "bounce" out of a macro for a while. Assume you have this expre [block[0], onBounce.bounce(block[1].pos)].toBlock(); ``` -- `function yield(source:Expr, yielder:Expr->Expr):Expr` +- `function yield(source:Expr, yielder:Expr->Expr, ?options:{ ?leaveLoops:Bool }):Expr` This will traverse an expression and will apply the `yielder` to the "leafs", which in this context are the subexpressions that determine a return value. Example: ``` @@ -165,6 +165,8 @@ This will traverse an expression and will apply the `yielder` to the "leafs", wh }); ``` + If you set `options.leaveLoops` to `true`, then loops (both for and while) will be considered leafs. + ### Position tools - `function sanitize(pos:Position):Position` diff --git a/haxelib.json b/haxelib.json index 5b83400..c8fe545 100644 --- a/haxelib.json +++ b/haxelib.json @@ -4,8 +4,8 @@ "license": "MIT", "tags": ["tink", "macro", "utility"], "description": "The macro toolkit ;)", - "version": "0.1.1-beta", - "releasenote": "Fixed ClassBuilder.hasConstructor", + "version": "0.2.1-beta", + "releasenote": "Added options to yield", "contributors": ["back2dos"], "dependencies": { "tink_core": "1.0.0-beta.4" diff --git a/src/tink/macro/Exprs.hx b/src/tink/macro/Exprs.hx index 1255e88..8e63540 100644 --- a/src/tink/macro/Exprs.hx +++ b/src/tink/macro/Exprs.hx @@ -144,9 +144,9 @@ class Exprs { t; }).typeof(); - static public function yield(e:Expr, yielder:Expr->Expr):Expr { + static public function yield(e:Expr, yielder:Expr->Expr, ?options: { ?leaveLoops: Bool }):Expr { inline function rec(e) - return yield(e, yielder); + return yield(e, yielder, options); return if (e == null || e.expr == null) e; else switch (e.expr) { @@ -164,9 +164,9 @@ class Exprs { for (c in cases) c.expr = rec(c.expr); ESwitch(e, cases, rec(edef)).at(e.pos); - case EFor(it, expr): + case EFor(it, expr) if (options == null || options.leaveLoops != true): EFor(it, rec(expr)).at(e.pos); - case EWhile(cond, body, normal): + case EWhile(cond, body, normal) if (options == null || options.leaveLoops != true): EWhile(cond, rec(body), normal).at(e.pos); case EBreak, EContinue: e; case EBinop(OpArrow, value, jump) if (jump.expr == EContinue || jump.expr == EBreak): diff --git a/tests/Exprs.hx b/tests/Exprs.hx index f08b4b5..36d20f9 100644 --- a/tests/Exprs.hx +++ b/tests/Exprs.hx @@ -36,13 +36,14 @@ class Exprs extends Base { function testYield() { function yielder(e) return macro @yield $e; - function test(x:Expr, e:Expr) - exprEq(x, e.yield(yielder)); + function test(x:Expr, e:Expr, ?options) + exprEq(x, e.yield(yielder, options)); test(macro @yield foo, macro foo); test(macro @yield (foo), macro (foo)); test(macro for (_) @yield foo, macro for (_) foo); test(macro while (_) @yield foo, macro while (_) foo); + test(macro @yield while (_) foo, macro while (_) foo, { leaveLoops: true }); test(macro @yield [while (_) foo], macro [while (_) foo]); }