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
13 changes: 13 additions & 0 deletions src/main/jay/grammars/php.jay
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ use xp\compiler\ast\DynamicInstanceCreationNode;
use xp\compiler\ast\DynamicInstanceOfNode;
use xp\compiler\ast\DynamicVariableReferenceNode;
use xp\compiler\ast\NoopNode;
use xp\compiler\ast\YieldNode;
%}

%left ','
%left '(' ')'
%right T_YIELD
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%right '=' T_ADD_EQUAL T_SUB_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_SHR T_SHL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL
%left '?' ':'
%nonassoc T_DOUBLE_ARROW
%left '|'
%left '^'
%left '&'
Expand Down Expand Up @@ -143,6 +146,7 @@ use xp\compiler\ast\NoopNode;
%token T_IN 364
%token T_BREAK 365
%token T_CONTINUE 366
%token T_YIELD 367

%token T_IF 370
%token T_ELSE 371
Expand Down Expand Up @@ -591,6 +595,15 @@ expression:
| T_CLONE expression {
$$= $yyLex->create(new CloneNode($2)); break;
}
| T_YIELD {
$$= $yyLex->create(new YieldNode());
}
| T_YIELD expression {
$$= $yyLex->create(new YieldNode($2));
}
| T_YIELD expression T_DOUBLE_ARROW expression {
$$= $yyLex->create(new YieldNode($4, $2));
}
| assignment {
$$= $yyLex->create(new AssignmentNode($1));
}
Expand Down
19 changes: 16 additions & 3 deletions src/main/jay/grammars/xp.jay
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ use xp\compiler\ast\LambdaNode;
use xp\compiler\ast\WithNode;
use xp\compiler\ast\ArmNode;
use xp\compiler\ast\BracedExpressionNode;
use xp\compiler\ast\YieldNode;
%}

%left ','
%nonassoc T_ARROW
%right T_YIELD
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%right '=' T_ADD_EQUAL T_SUB_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL
%left '?' ':'
%left '?'
%nonassoc ':'
%left '|'
%left '^'
%left '&'
Expand All @@ -90,7 +93,7 @@ use xp\compiler\ast\BracedExpressionNode;
%nonassoc T_INSTANCEOF
%right '~' T_INC T_DEC
%right '['
%right T_AS
%right T_AS
%nonassoc T_NEW T_CLONE

%token T_WORD 260
Expand Down Expand Up @@ -143,6 +146,7 @@ use xp\compiler\ast\BracedExpressionNode;
%token T_IN 364
%token T_BREAK 365
%token T_CONTINUE 366
%token T_YIELD 367

%token T_IF 370
%token T_ELSE 371
Expand Down Expand Up @@ -823,7 +827,16 @@ expression:
}
}
| T_CLONE expression {
$$= $yyLex->create(new CloneNode($2)); break;
$$= $yyLex->create(new CloneNode($2));
}
| T_YIELD {
$$= $yyLex->create(new YieldNode());
}
| T_YIELD expression {
$$= $yyLex->create(new YieldNode($2));
}
| T_YIELD expression ':' expression {
$$= $yyLex->create(new YieldNode($4, $2));
}
| expression T_AS paramtyperef {
$$= $yyLex->create(new CastNode(array_merge($3, array('expression' => $1))));
Expand Down
6 changes: 3 additions & 3 deletions src/main/php/xp/compiler/Runner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* Adds path to source path (source path will equal classpath initially)
* </li>
* <li>-E [emitter]:
* Use emitter, defaults to "php5.4"
* Use emitter, defaults to "php5.5"
* </li>
* <li>-p [profile[,profile[,...]]]:
* Use compiler profiles (defaults to ["default"]) - xp/compiler/{profile}.xcp.ini
Expand Down Expand Up @@ -180,8 +180,8 @@ public static function main(array $args) {
$manager->setSourcePaths(\xp::$classpath);

// Handle arguments
$profiles= array('default');
$emitter= 'php5.4';
$profiles= ['default'];
$emitter= 'php5.5';
$result= function($success) { return $success ? 0 : 1; };
$files= array();
$listener= new DefaultDiagnosticListener(Console::$out);
Expand Down
23 changes: 23 additions & 0 deletions src/main/php/xp/compiler/ast/YieldNode.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace xp\compiler\ast;

/**
* The "yield" statement
*/
class YieldNode extends Node {
public $value= null;
public $key= null;

public function __construct($value= null, $key= null) {
$this->value= $value;
$this->key= $key;
}

/**
* Returns a hashcode
*
* @return string
*/
public function hashCode() {
return 'yield '.($this->key ? $this->key->hashCode().' => ' : '').($this->value ? $this->value->hashCode() : '');
}
}
10 changes: 9 additions & 1 deletion src/main/php/xp/compiler/emit/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,14 @@ protected abstract function emitReturn($b, $return);
* @param xp.compiler.ast.SilenceOperatorNode silenced
*/
protected abstract function emitSilenceOperator($b, $silenced);

/**
* Emit a yield node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.YieldNode yield
*/
protected abstract function emitYield($b, $yield);

/**
* Emit a single node
Expand Down Expand Up @@ -742,7 +750,7 @@ protected function format($code, $message, Node $context= null) {
$pos= $context->position;
} else { // Try to determine last context node from backtrace
$pos= array(0, 0);
foreach (create(new \lang\Throwable(null))->getStackTrace() as $element) {
foreach ((new \lang\Throwable(null))->getStackTrace() as $element) {
if (
'emit' == substr($element->method, 0, 4) &&
sizeof($element->args) > 1 &&
Expand Down
10 changes: 10 additions & 0 deletions src/main/php/xp/compiler/emit/php/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,16 @@ protected function emitSilenceOperator($b, $silenced) {
$this->scope[0]->setType($silenced, $this->scope[0]->typeOf($silenced->expression));
}

/**
* Emit a yield node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.YieldNode yield
*/
protected function emitYield($b, $yield) {
$this->error('V505', 'Yield not supported in '.$this->getClassName());
}

/**
* Emit all given nodes
*
Expand Down
24 changes: 24 additions & 0 deletions src/main/php/xp/compiler/emit/php/V55Emitter.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace xp\compiler\emit\php;

/**
* Emits sourcecode using PHP 5.5 sourcecode
*/
class V55Emitter extends V54Emitter {

/**
* Emit a yield node
*
* @param xp.compiler.emit.Buffer b
* @param xp.compiler.ast.YieldNode yield
*/
protected function emitYield($b, $yield) {
$b->append('yield ');
if ($yield->key) {
$this->emitOne($b, $yield->key);
$b->append(' => ');
$this->emitOne($b, $yield->value);
} else if ($yield->value) {
$this->emitOne($b, $yield->value);
}
}
}
1 change: 1 addition & 0 deletions src/main/php/xp/compiler/syntax/php/Lexer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Lexer extends \text\parser\generic\AbstractLexer {
'while' => Parser::T_WHILE,
'break' => Parser::T_BREAK,
'continue' => Parser::T_CONTINUE,
'yield' => Parser::T_YIELD,

'if' => Parser::T_IF,
'else' => Parser::T_ELSE,
Expand Down
Loading