Skip to content

Commit

Permalink
Merge pull request #5 from preprocess/feature/new-sigil
Browse files Browse the repository at this point in the history
New sigil
  • Loading branch information
assertchris authored Sep 10, 2018
2 parents 5ecc9e2 + 69b926e commit cef3c5d
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 335 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
"type": "pre-macro",
"name": "pre/async",
"license": "MIT",
"autoload": {
"files": [
"source/functions.php"
]
},
"require": {
"pre/plugin": "^0.10.0"
"pre/plugin": "^0.11.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0"
},
"extra": {
"macros": ["source/macros.yay"]
"macros": [
"source/macros.yay"
]
}
}
66 changes: 66 additions & 0 deletions source/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Pre\Async;

use Yay\Ast;

function closure($ast) {
if (function_exists("\\Amp\\resolve")) {
$append = new Ast("amp1");
$append->push(new Ast());
$ast->append($append);
} else {
$append = new Ast("amp2");
$append->push(new Ast());
$ast->append($append);
}

$defined = [];
$yields = false;

foreach ($ast->{"functionArguments"} as $node) {
$name = $node["functionArgument"]["functionArgumentName"]->value();
$defined[$name] = true;
}

$bound = false;
$scope = new Ast("scope");

$pushed = [];

foreach ($ast->{"body"} as $token) {
$name = $token->value();

if (in_array($token->value(), ["yield", "await"])) {
$yields = true;
continue;
}

if (!$token->is(T_VARIABLE)) {
continue;
}

if (isset($defined[$name]) || isset($pushed[$name])) {
continue;
}

if (substr($name, 1) === "this") {
continue;
}

$pushed[$name] = true;
$scope->push(new Ast("var", $token));
$bound = true;
}

if ($bound) {
$ast->append($scope);
}

if (!$yields) {
$append = new Ast("yields");
$append->push(new Ast());

$ast->append($append);
}
}
Loading

0 comments on commit cef3c5d

Please sign in to comment.