diff --git a/composer.json b/composer.json index 6d6cec5..ada73ac 100644 --- a/composer.json +++ b/composer.json @@ -3,24 +3,20 @@ "name": "pre/deferred", "license": "MIT", "require": { - "pre/plugin": "^0.7.3" + "pre/plugin": "^0.10.0" }, "autoload": { "psr-4": { - "Pre\\Deferred\\": "src" + "Pre\\Deferred\\": "source" } }, "require-dev": { "phpunit/phpunit": "^5.0|^6.0" }, "autoload-dev": { - "psr-4": { - "Pre\\Deferred\\": "tests" - } + "files": ["tests/stubs.php"] }, "extra": { - "macros": [ - "src/macros.yay" - ] + "macros": ["source/macros.yay"] } } diff --git a/phpunit.xml b/phpunit.xml index 829414b..5543800 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -2,7 +2,7 @@ > function($ast) { + $bound = false; + $scope = new \Yay\Ast("·scope"); + + $pushed = []; + + foreach ($ast->{"·body"} as $token) { + $name = $token->value(); + + if (!$token->is(T_VARIABLE)) { + continue; + } + + if (isset($pushed[$name])) { + continue; + } + + if (substr($name, 1) === "this") { + continue; + } + + $scope->push(new \Yay\Ast("·var", $token)); + $pushed[$name] = true; + $bound = true; + } + + if ($bound) { + $ast->append($scope); + } else { + $simple = new \Yay\Ast("·simple"); + $simple->push(new \Yay\Ast()); + + $ast->append($simple); + } +} >> { + ··collapse(··trim( + ·scope ?·{ + $deferred = new \Pre\Deferred\Deferred([·scope ···(, ) { ·var = ·var ?? null}, "fn" => function () use (··trim(·scope ···(, ) {&·var})) { + ··trim(·body) + }]["fn"]); + } + + ·simple ?·{ + $deferred = new \Pre\Deferred\Deferred(function () { + ··trim(·body) + }); + } + )) +} + +macro ·recursion { + ·chain( + defer, + ·ns()·function, + ·between( + ·token("("), ·layer()·parameters, ·token(")") + ), + ·token(";") + ) +} >> { + $deferred = ··unsafe(new \Pre\Deferred\Deferred((function (...$parameters) { + return function () use ($parameters) { + ·function(...$parameters); + }; + })(·parameters))); +} diff --git a/src/macros.yay b/src/macros.yay deleted file mode 100644 index dd4e234..0000000 --- a/src/macros.yay +++ /dev/null @@ -1,37 +0,0 @@ -> { - $deferred = new \Pre\Deferred\Deferred(call_user_func(function($context) { - return function() use ($context) { - extract($context); - ·body - }; - }, get_defined_vars())); -} - -macro ·recursion { - ·chain( - defer, - ·ns()·function, - ·between( - ·token("("), ·layer()·parameters, ·token(")") - ), - ·token(";") - ) -} >> { - $deferred = new \Pre\Deferred\Deferred(call_user_func(function() { - $context = func_get_args(); - - return function() use ($context) { - call_user_func_array(··stringify(·function), $context); - }; - }, ·parameters)); -} diff --git a/tests/MacroTest.php b/tests/MacroTest.php index ee7d41e..7e12dbd 100644 --- a/tests/MacroTest.php +++ b/tests/MacroTest.php @@ -1,13 +1,42 @@ format($from))); + $this->assertEquals($this->format($expected), $actual); + } + + private function format($code) { - return __DIR__ . "/specs"; + return " function () use (&$handle) { + fclose($handle); + print "all done"; +}]["fn"]); diff --git a/tests/stubs.php b/tests/stubs.php new file mode 100644 index 0000000..d714f68 --- /dev/null +++ b/tests/stubs.php @@ -0,0 +1,9 @@ +