Skip to content

Commit

Permalink
Initial implementation of inclusive ranges and exclusive ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Nov 18, 2014
1 parent 7835713 commit a1d1fea
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 124 deletions.
67 changes: 67 additions & 0 deletions Library/Builder/Operators/CastOperatorBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
+--------------------------------------------------------------------------+
| Zephir Language |
+--------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Zephir Team and contributors |
+--------------------------------------------------------------------------+
| This source file is subject the MIT license, that is bundled with |
| this package in the file LICENSE, and is available through the |
| world-wide-web at the following url: |
| http://zephir-lang.com/license.html |
| |
| If you did not receive a copy of the MIT license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+--------------------------------------------------------------------------+
*/

namespace Zephir\Builder\Operators;

use Zephir\Builder\Operators\AbstractOperatorBuilder;

/**
* CastOperatorBuilder
*
* Allows to manually build a 'cast' operator AST node
*/
class CastOperatorBuilder extends AbstractOperatorBuilder
{
protected $leftOperand;

protected $rightOperand;

/**
* @param $left
* @param $right
* @param null $file
* @param int $line
* @param int $char
*/
public function __construct($left, $right, $file = null, $line = 0, $char = 0)
{
$this->leftOperand = $left;
$this->rightOperand = $right;
$this->file = $file;
$this->line = $line;
$this->char = $char;
}

/**
* Returns a builder definition
*
* @return array
*/
public function get()
{
return array(
'type' => 'cast',
'left' => $this->leftOperand,
'right' => $this->rightOperand->get(),
'file' => $this->file,
'line' => $this->line,
'char' => $this->char
);
}
}
8 changes: 4 additions & 4 deletions Library/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class Compiler
{
const VERSION = '0.5.8a';
const VERSION = '0.5.9a';

/**
* @var CompilerFile[]
Expand Down Expand Up @@ -112,7 +112,7 @@ public function __construct(Config $config, Logger $logger)
$this->fileSystem = new FileSystem();
$this->checkRequires();
}

/**
* Check require extensions orther when build your extension
*/
Expand All @@ -127,13 +127,13 @@ protected function checkRequires()
$collection_error .= $value . ", ";
}
}

if ($collection_error != PHP_EOL . "\tCould not load Extension : ") {
$collection_error .= PHP_EOL . "\tYou must add extensions above before build this extension!";
throw new Exception($collection_error);
}
}

}

/**
Expand Down
7 changes: 7 additions & 0 deletions Library/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use Zephir\Operators\Other\RequireOperator;
use Zephir\Operators\Other\TypeOfOperator;
use Zephir\Operators\Other\CastOperator;
use Zephir\Operators\Other\RangeInclusiveOperator;

use Zephir\Expression\Closure;
use Zephir\Expression\ClosureArrow;
Expand Down Expand Up @@ -561,6 +562,12 @@ public function compile(CompilationContext $compilationContext)
$expr->setExpectReturn($this->_expecting, $this->_expectingVariable);
return $expr->compile($expression, $compilationContext);

case 'irange':
$expr = new RangeInclusiveOperator();
$expr->setReadOnly($this->isReadOnly());
$expr->setExpectReturn($this->_expecting, $this->_expectingVariable);
return $expr->compile($expression, $compilationContext);

case 'list':
if ($expression['left']['type'] == 'list') {
$compilationContext->logger->warning("Unnecessary extra parentheses", "extra-parentheses", $expression);
Expand Down
19 changes: 19 additions & 0 deletions Library/Operators/Other/CastOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
class CastOperator extends BaseOperator
{
/**
* Compiles a type cast operation
*
* @param $expression
* @param CompilationContext $compilationContext
* @return bool|CompiledExpression
Expand Down Expand Up @@ -230,6 +232,23 @@ public function compile($expression, CompilationContext $compilationContext)
}
break;

case 'array':
switch ($resolved->getType()) {
case 'variable':
$compilationContext->headersManager->add('kernel/operators');
$symbolVariable = $compilationContext->symbolTable->getTempVariable('array', $compilationContext, $expression);
$symbolVariable->setMustInitNull(true);
$symbolVariable->setIsInitialized(true, $compilationContext, $expression);
$compilationContext->codePrinter->output('zephir_get_arrval(' . $symbolVariable->getName() . ', ' . $resolved->getCode() . ');');
if ($symbolVariable->isTemporal()) {
$symbolVariable->setIdle(true);
}
return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
default:
throw new CompilerException("Cannot cast: " . $resolved->getType() . " to " . $expression['left'], $expression);
}
break;

case 'object':

switch ($resolved->getType()) {
Expand Down
68 changes: 68 additions & 0 deletions Library/Operators/Other/RangeInclusiveOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
+--------------------------------------------------------------------------+
| Zephir Language |
+--------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Zephir Team and contributors |
+--------------------------------------------------------------------------+
| This source file is subject the MIT license, that is bundled with |
| this package in the file LICENSE, and is available through the |
| world-wide-web at the following url: |
| http://zephir-lang.com/license.html |
| |
| If you did not receive a copy of the MIT license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+--------------------------------------------------------------------------+
*/

namespace Zephir\Operators\Other;

use Zephir\Operators\BaseOperator;
use Zephir\CompilationContext;
use Zephir\Expression;
use Zephir\CompilerException;
use Zephir\CompiledExpression;
use Zephir\Builder\FunctionCallBuilder;
use Zephir\Builder\Operators\CastOperatorBuilder;

/**
* RangeInclusive
*
* Inclusive range operator
*/
class RangeInclusiveOperator extends BaseOperator
{
/**
* @param array $expression
* @param CompilationContext $compilationContext
* @return CompiledExpression
* @throws CompilerException
*/
public function compile(array $expression, CompilationContext $compilationContext)
{

if (!isset($expression['left'])) {
throw new CompilerException("Invalid 'left' operand for 'irange' expression", $expression['left']);
}

if (!isset($expression['right'])) {
throw new CompilerException("Invalid 'right' operand for 'irange' expression", $expression['right']);
}

$builder = new FunctionCallBuilder('range', array(
array('parameter' => $expression['left']),
array('parameter' => $expression['right'])
));

/**
* Implicit type coercing
*/
$castBuilder = new CastOperatorBuilder('array', $builder);

$expression = new Expression($castBuilder->get());
$expression->setReadOnly($this->_readOnly);
return $expression->compile($compilationContext);
}
}
2 changes: 2 additions & 0 deletions Library/Passes/CallGathererPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public function passExpression(array $expression)
case 'greater':
case 'greater-equal':
case 'less-equal':
case 'irange':
case 'erange':
$this->passExpression($expression['left']);
$this->passExpression($expression['right']);
break;
Expand Down
4 changes: 4 additions & 0 deletions Library/Passes/LocalContextPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public function passLetStatement(array $statement)
case 'closure':
case 'closure-arrow':
case 'reference':
case 'irange':
case 'erange':
$this->markVariableNoLocal($assignment['variable']);
break;

Expand Down Expand Up @@ -343,6 +345,8 @@ public function passExpression(array $expression)
case 'bitwise_xor':
case 'bitwise_shiftleft':
case 'bitwise_shiftright':
case 'irange':
case 'erange':
$this->passExpression($expression['left']);
$this->passExpression($expression['right']);
break;
Expand Down
2 changes: 2 additions & 0 deletions Library/Passes/MutateGathererPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public function passExpression(array $expression)
case 'bitwise_xor':
case 'bitwise_shiftleft':
case 'bitwise_shiftright':
case 'irange':
case 'erange':
$this->passExpression($expression['left']);
$this->passExpression($expression['right']);
break;
Expand Down
4 changes: 4 additions & 0 deletions Library/Passes/StaticTypeInference.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ public function passExpression(array $expression)
case 'less-equal':
return 'bool';

case 'irange':
case 'erange':
return 'variable';

case 'typeof':
$this->passExpression($expression['left']);
return 'string';
Expand Down
30 changes: 15 additions & 15 deletions ext/test/router.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/test/trie.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a1d1fea

Please sign in to comment.