Skip to content

Commit

Permalink
Cleaned up static constant access expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Apr 17, 2018
1 parent c7eb52c commit 70e6efe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 37 deletions.
53 changes: 37 additions & 16 deletions Library/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
| |
| 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 |
| at the following url: https://zephir-lang.com/license.html |
+--------------------------------------------------------------------------+
*/

namespace Zephir;

use Zephir\Compiler\CompilerException;
use Zephir\Expression\Constants;
use Zephir\Expression\StaticConstantAccess;

/**
* ClassConstant
Expand All @@ -23,23 +24,29 @@
*/
class ClassConstant
{
/**
* @var string
*/
protected $name;

/**
* @var array
*/
protected $value;
protected $value = [];

/**
* @var string
*/
protected $docblock;

/**
* ClassConstant constructor
*
* @param $name
* @param $value
* @param $docBlock
* @param string $name
* @param array $value
* @param string $docBlock
*/
public function __construct($name, $value, $docBlock)
public function __construct($name, array $value, $docBlock)
{
$this->name = $name;
$this->value = $value;
Expand Down Expand Up @@ -115,29 +122,33 @@ public function getType()
/**
* Process the value of the class constant if needed
*
* @param compilationContext $compilationContext
* @param CompilationContext $compilationContext
*
* @return void
*
* @throws Exception
*/
public function processValue($compilationContext)
public function processValue(CompilationContext $compilationContext)
{
if ($this->value['type'] == 'constant') {
$constant = new Constants();
$compiledExpression = $constant->compile($this->value, $compilationContext);

$this->value = array(
$this->value = [
'type' => $compiledExpression->getType(),
'value' => $compiledExpression->getCode()
);
];
return;
}

if ($this->value['type'] == 'static-constant-access') {
$expression = new Expression($this->value);
$compiledExpression = $expression->compile($compilationContext);
$staticConstantAccess = new StaticConstantAccess();
$compiledExpression = $staticConstantAccess->compile($this->value, $compilationContext);

$this->value = array(
$this->value = [
'type' => $compiledExpression->getType(),
'value' => $compiledExpression->getCode()
);
'value' => $compiledExpression->getCode(),
];
return;
}
}
Expand All @@ -146,13 +157,23 @@ public function processValue($compilationContext)
* Produce the code to register a class constant
*
* @param CompilationContext $compilationContext
*
* @return void
*
* @throws CompilerException
* @throws Exception
*/
public function compile(CompilationContext $compilationContext)
{
$this->processValue($compilationContext);

$compilationContext->backend->declareConstant($this->value['type'], $this->getName(), isset($this->value['value']) ? $this->value['value'] : null, $compilationContext);
$constanValue = isset($this->value['value']) ? $this->value['value'] : null;

$compilationContext->backend->declareConstant(
$this->value['type'],
$this->getName(),
$constanValue,
$compilationContext
);
}
}
54 changes: 33 additions & 21 deletions Library/Expression/StaticConstantAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
| |
| 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 |
| at the following url: https://zephir-lang.com/license.html |
+--------------------------------------------------------------------------+
*/

namespace Zephir\Expression;

use Zephir\Variable;
use Zephir\ClassConstant;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Compiler\CompilerException;
use Zephir\ClassConstant;
use Zephir\Exception;
use Zephir\Variable;

/**
* StaticConstantAccess
Expand All @@ -26,23 +27,26 @@
*/
class StaticConstantAccess
{
protected $_expecting = true;
/** @var bool */
protected $expecting = true;

protected $_readOnly = false;
/** @var bool */
protected $readOnly = false;

protected $_expectingVariable;
/** @var Variable|null */
protected $expectingVariable;

/**
* Sets if the variable must be resolved into a direct variable symbol
* create a temporary value or ignore the return value
*
* @param boolean $expecting
* @param Variable $expectingVariable
* @param bool $expecting
* @param Variable|null $expectingVariable
*/
public function setExpectReturn($expecting, Variable $expectingVariable = null)
{
$this->_expecting = $expecting;
$this->_expectingVariable = $expectingVariable;
$this->expecting = $expecting;
$this->expectingVariable = $expectingVariable;
}

/**
Expand All @@ -52,27 +56,29 @@ public function setExpectReturn($expecting, Variable $expectingVariable = null)
*/
public function setReadOnly($readOnly)
{
$this->_readOnly = $readOnly;
$this->readOnly = (bool) $readOnly;
}

/**
* Access a static constant class
*
* @param array $expression
* @param array $expression
* @param CompilationContext $compilationContext
*
* @return CompiledExpression
* @throws Exception|CompilerException
*/
public function compile($expression, CompilationContext $compilationContext)
public function compile(array $expression, CompilationContext $compilationContext)
{
$compiler = &$compilationContext->compiler;
$className = $expression['left']['value'];
$constant = $expression['right']['value'];
$constant = $expression['right']['value'];

/**
* Fetch the class definition according to the class where the constant
* is supposed to be declared
*/
if (!in_array($className, array('this', 'self', 'static', 'parent'))) {
if (!in_array($className, ['this', 'self', 'static', 'parent'])) {
$className = $compilationContext->getFullName($className);
if ($compiler->isClass($className) || $compiler->isInterface($className)) {
$classDefinition = $compiler->getClassDefinition($className);
Expand All @@ -84,7 +90,7 @@ public function compile($expression, CompilationContext $compilationContext)
}
}
} else {
if (in_array($className, array('self', 'static', 'this'))) {
if (in_array($className, ['self', 'static', 'this'])) {
$classDefinition = $compilationContext->classDefinition;
} else {
if ($className == 'parent') {
Expand All @@ -104,7 +110,14 @@ public function compile($expression, CompilationContext $compilationContext)
* so we need to check that they effectively do exist
*/
if (!$classDefinition->hasConstant($constant)) {
throw new CompilerException("Class '" . $classDefinition->getCompleteName() . "' does not have a constant called: '" . $constant . "'", $expression);
throw new CompilerException(
sprintf(
"Class '%s' does not have a constant called: '%s'",
$classDefinition->getCompleteName(),
$constant
),
$expression
);
}

/**
Expand All @@ -114,9 +127,9 @@ public function compile($expression, CompilationContext $compilationContext)
/**
* Resolves the symbol that expects the value
*/
if ($this->_expecting) {
if ($this->_expectingVariable) {
$symbolVariable = $this->_expectingVariable;
if ($this->expecting) {
if ($this->expectingVariable) {
$symbolVariable = $this->expectingVariable;
$symbolVariable->initVariant($compilationContext);
} else {
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression);
Expand Down Expand Up @@ -161,7 +174,6 @@ public function compile($expression, CompilationContext $compilationContext)
case 'bool':
case 'null':
break;

default:
$compilationContext->logger->warning($constantDefinition->getName(), 'nonexistent-constant', $expression);
return new CompiledExpression('null', null, $expression);
Expand Down

0 comments on commit 70e6efe

Please sign in to comment.