Added options to yield.

This commit is contained in:
back2dos
2013-11-02 03:02:18 +01:00
parent d5784f685f
commit aefe622434
4 changed files with 12 additions and 9 deletions

View File

@@ -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(); [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: 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 ### Position tools
- `function sanitize(pos:Position):Position` - `function sanitize(pos:Position):Position`

View File

@@ -4,8 +4,8 @@
"license": "MIT", "license": "MIT",
"tags": ["tink", "macro", "utility"], "tags": ["tink", "macro", "utility"],
"description": "The macro toolkit ;)", "description": "The macro toolkit ;)",
"version": "0.1.1-beta", "version": "0.2.1-beta",
"releasenote": "Fixed ClassBuilder.hasConstructor", "releasenote": "Added options to yield",
"contributors": ["back2dos"], "contributors": ["back2dos"],
"dependencies": { "dependencies": {
"tink_core": "1.0.0-beta.4" "tink_core": "1.0.0-beta.4"

View File

@@ -144,9 +144,9 @@ class Exprs {
t; t;
}).typeof(); }).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) inline function rec(e)
return yield(e, yielder); return yield(e, yielder, options);
return return
if (e == null || e.expr == null) e; if (e == null || e.expr == null) e;
else switch (e.expr) { else switch (e.expr) {
@@ -164,9 +164,9 @@ class Exprs {
for (c in cases) for (c in cases)
c.expr = rec(c.expr); c.expr = rec(c.expr);
ESwitch(e, cases, rec(edef)).at(e.pos); 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); 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); EWhile(cond, rec(body), normal).at(e.pos);
case EBreak, EContinue: e; case EBreak, EContinue: e;
case EBinop(OpArrow, value, jump) if (jump.expr == EContinue || jump.expr == EBreak): case EBinop(OpArrow, value, jump) if (jump.expr == EContinue || jump.expr == EBreak):

View File

@@ -36,13 +36,14 @@ class Exprs extends Base {
function testYield() { function testYield() {
function yielder(e) return macro @yield $e; function yielder(e) return macro @yield $e;
function test(x:Expr, e:Expr) function test(x:Expr, e:Expr, ?options)
exprEq(x, e.yield(yielder)); exprEq(x, e.yield(yielder, options));
test(macro @yield foo, macro foo); test(macro @yield foo, macro foo);
test(macro @yield (foo), macro (foo)); test(macro @yield (foo), macro (foo));
test(macro for (_) @yield foo, macro for (_) foo); test(macro for (_) @yield foo, macro for (_) foo);
test(macro while (_) @yield foo, macro while (_) 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]); test(macro @yield [while (_) foo], macro [while (_) foo]);
} }