Skip to content

Commit

Permalink
Added better amp-specific generation
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Jun 30, 2017
1 parent b0dfd50 commit 35a02c5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"require": {
"pre/plugin": "^0.7.3"
},
"autoload": {
"files": [
"src/constants.php"
]
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0"
},
Expand Down
9 changes: 0 additions & 9 deletions src/constants.php

This file was deleted.

68 changes: 54 additions & 14 deletions src/macros.yay
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,21 @@ macro ·recursion {
·token("}"),
·_()·scope,
·_()·simple,
·_()·yields
·_()·yields,
·_()·amp1,
·_()·amp2
)
} >> function($ast) {
if (function_exists("\\Amp\\resolve")) {
$append = new \Yay\Ast("·amp1");
$append->push(new \Yay\Ast());
$ast->append($append);
} else {
$append = new \Yay\Ast("·amp2");
$append->push(new \Yay\Ast());
$ast->append($append);
}

$defined = [];
$yields = false;

Expand Down Expand Up @@ -175,18 +187,26 @@ macro ·recursion {
·scope ?·{
··trim(·modifiers ···( ) { ·modifier }) function ·function(·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignmentEq ·argAssignmentVal } }): \Amp\Promise
{
return call_user_func(PRE_ASYNC_WRAPPER, function () use (·scope ···(, ) { &·var }) {
·body ·yields ?·{yield;}
});
return ··trim(
·amp1 ?· { \Amp\resolve }
·amp2 ?· { \Amp\call }
(function () use (·scope ···(, ) { &·var }) {
·body ·yields ?·{yield;}
});
)
}
}

·simple ?·{
··trim(·modifiers ···( ) { ·modifier }) function ·function(·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignmentEq ·argAssignmentVal } }): \Amp\Promise
{
return call_user_func(PRE_ASYNC_WRAPPER, function () {
·body ·yields ?·{yield;}
});
return ··trim(
·amp1 ?· { \Amp\resolve }
·amp2 ?· { \Amp\call }
(function () {
·body ·yields ?·{yield;}
});
)
}
}
)}
Expand Down Expand Up @@ -238,9 +258,21 @@ macro ·recursion {
·token("}"),
·_()·scope,
·_()·simple,
·_()·yields
·_()·yields,
·_()·amp1,
·_()·amp2
)
} >> function($ast) {
if (function_exists("\\Amp\\resolve")) {
$append = new \Yay\Ast("·amp1");
$append->push(new \Yay\Ast());
$ast->append($append);
} else {
$append = new \Yay\Ast("·amp2");
$append->push(new \Yay\Ast());
$ast->append($append);
}

$defined = [];
$yields = false;

Expand Down Expand Up @@ -297,17 +329,25 @@ macro ·recursion {
} >> {··trim(
·scope ?·{
[·scope ···(, ) { ·var = ·var ?? null}, "fn" => function (·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignmentEq ·argAssignmentVal } }) use (·scope ···(, ) { &·var }): \Amp\Promise {
return call_user_func(PRE_ASYNC_WRAPPER, function () use (·scope ···(, ) { &·var }) {
·body ·yields ?·{yield;}
});
return ··trim(
·amp1 ?· { \Amp\resolve }
·amp2 ?· { \Amp\call }
(function () use (·scope ···(, ) { &·var }) {
·body ·yields ?·{yield;}
});
)
}]["fn"]
}

·simple ?·{
function (·args ···(, ) { ·arg ··· { ·argType ·argName ·argAssignmentEq ·argAssignVal } }): \Amp\Promise {
return call_user_func(PRE_ASYNC_WRAPPER, function () {
·body ·yields ?·{yield;}
});
return ··trim(
·amp1 ?· { \Amp\resolve }
·amp2 ?· { \Amp\call }
(function () {
·body ·yields ?·{yield;}
});
)
}
}
)}
12 changes: 6 additions & 6 deletions tests/specs/async.spec
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,42 @@ class AsyncClass
{
public function first(array $data = []): \Amp\Promise
{
return call_user_func(PRE_ASYNC_WRAPPER, function () {
return \Amp\call(function () {
yield \Amp\File\get("path/to/file");
});
}

public function second(): \Amp\Promise
{
return call_user_func(PRE_ASYNC_WRAPPER, function () {
return \Amp\call(function () {
return "boo!";
yield;
});
}

public static function complex(): \Amp\Promise
{
return call_user_func(PRE_ASYNC_WRAPPER, function () {
return \Amp\call(function () {
yield;
});
}
}

$first = function (): \Amp\Promise {
return call_user_func(PRE_ASYNC_WRAPPER, function () {
return \Amp\call(function () {
yield "here";
});
};

$second = [$thing = $thing ?? null, "fn" => function () use (&$thing): \Amp\Promise {
return call_user_func(PRE_ASYNC_WRAPPER, function () use (&$thing) {
return \Amp\call(function () use (&$thing) {
$this->something();
yield $thing;
});
}]["fn"];

$third = [$thing = $thing ?? null, "fn" => function () use (&$thing): \Amp\Promise {
return call_user_func(PRE_ASYNC_WRAPPER, function () use (&$thing) {
return \Amp\call(function () use (&$thing) {
yield $thing;
});
}]["fn"];

0 comments on commit 35a02c5

Please sign in to comment.