Skip to content
This repository was archived by the owner on Sep 9, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/jay/grammars/php.jay
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ expression:
| '@' expression {
$$= $yyLex->create(new SilenceOperatorNode($2));
}
| T_FUNCTION '(' lambda_input ')' '{' statements_opt '}' {
$$= $yyLex->create(new LambdaNode($3, (array)$6));
| T_FUNCTION '(' lambda_input ')' lambda_uses_opt '{' statements_opt '}' {
$$= $yyLex->create(new LambdaNode($3, (array)$7, $5));
}
| '(' expression ')' chain_opt {
if ($4) {
Expand All @@ -689,6 +689,11 @@ lambda_input_parameter:
T_VARIABLE initialization_opt { $$= array('name' => $1); $2 && $$['default']= $2; }
;

lambda_uses_opt:
/* empty */ { $$= array(); }
| T_USE '(' lambda_input_parameters ')' { $$= $3; }
;

literal:
scalar
| T_ARRAY '(' { $p= $yyLex->position; } map_or_list ')' { $4->position= $p; $4->type= NULL; $$= $4; }
Expand Down
5 changes: 4 additions & 1 deletion src/main/php/xp/compiler/ast/LambdaNode.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
class LambdaNode extends Node {
public $parameters;
public $statements;
public $uses;

/**
* Constructor
*
* @param xp.compiler.ast.Node[] parameters
* @param xp.compiler.ast.Node[] statements
* @param xp.compiler.ast.Node[] uses
*/
public function __construct(array $parameters, array $statements) {
public function __construct(array $parameters, array $statements, $uses= null) {
$this->parameters= $parameters;
$this->statements= $statements;
$this->uses= $uses;
}
}
31 changes: 18 additions & 13 deletions src/main/php/xp/compiler/emit/php/V54Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,6 @@ protected function emitArm($b, $arm) {
*/
protected function emitLambda($b, $lambda) {

// Capture all local variables and parameters of containing scope which
// are also used inside the lambda by value.
$finder= new \xp\compiler\ast\LocalVariableFinder();
foreach ($finder->variablesIn((array)$this->scope[0]->routine->body) as $variable) {
$finder->including($variable);
}
foreach ($this->scope[0]->routine->parameters as $param) {
$finder->including($param['name']);
}
$finder->excluding('*');

// Parameters
$b->append('function(');
$s= sizeof($lambda->parameters)- 1;
Expand All @@ -236,8 +225,24 @@ protected function emitLambda($b, $lambda) {
}
$b->append(')');

// Use variables
if ($capture= $finder->variablesIn($lambda->statements)) {
// If not explicitely stated: Capture all local variables and parameters of
// containing scope which are also used inside the lambda by value.
if (null === $lambda->uses) {
$finder= new \xp\compiler\ast\LocalVariableFinder();
foreach ($finder->variablesIn((array)$this->scope[0]->routine->body) as $variable) {
$finder->including($variable);
}
foreach ($this->scope[0]->routine->parameters as $param) {
$finder->including($param['name']);
}
$finder->excluding('*');

// Use variables
if ($capture= $finder->variablesIn($lambda->statements)) {
$b->append(' use($')->append(implode(', $', $capture))->append(')');
}
} else if ($lambda->uses) {
$capture= array_map(function($var) { return $var->name; }, $lambda->uses);
$b->append(' use($')->append(implode(', $', $capture))->append(')');
}

Expand Down
Loading