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();
```
- `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`

View File

@@ -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"

View File

@@ -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):

View File

@@ -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]);
}