Skip to content

Commit

Permalink
Merge pull request phalcon#42 from Cinderella-Man/empty-function
Browse files Browse the repository at this point in the history
Empty function
  • Loading branch information
Phalcon committed Dec 23, 2013
2 parents 7a45445 + 43680be commit fc86504
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
9 changes: 7 additions & 2 deletions Library/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ public function compile(CompilationContext $compilationContext)

$namespace = $compilationContext->config->get('namespace');

$abstractFlag = '0';
if ($this->isAbstract()) {
$abstractFlag = 'ZEND_ACC_EXPLICIT_ABSTRACT_CLASS';
}

/**
* Register the class with extends + interfaces
*/
Expand All @@ -556,7 +561,7 @@ public function compile(CompilationContext $compilationContext)

if ($this->getType() == 'class') {
$codePrinter->output('ZEPHIR_REGISTER_CLASS_EX(' . $this->getNCNamespace() . ', ' . $this->getName() . ', ' . $namespace . ', ' .
strtolower($this->getSCName($namespace)) . ', ' . $classEntry . ', ' . $methodEntry . ', 0);');
strtolower($this->getSCName($namespace)) . ', ' . $classEntry . ', ' . $methodEntry . ', ' . $abstractFlag . ');');
$codePrinter->outputBlankLine();
} else {
$codePrinter->output('ZEPHIR_REGISTER_INTERFACE_EX(' . $this->getNCNamespace() . ', ' . $this->getName() . ', ' . $namespace . ', ' .
Expand All @@ -566,7 +571,7 @@ public function compile(CompilationContext $compilationContext)
} else {
if ($this->getType() == 'class') {
$codePrinter->output('ZEPHIR_REGISTER_CLASS(' . $this->getNCNamespace() . ', ' . $this->getName() . ', ' . $namespace . ', ' .
strtolower($this->getSCName($namespace)) . ', ' . $methodEntry . ', 0);');
strtolower($this->getSCName($namespace)) . ', ' . $methodEntry . ', ' . $abstractFlag . ');');
} else {
$codePrinter->output('ZEPHIR_REGISTER_INTERFACE(' . $this->getNCNamespace() . ', ' . $this->getName() . ', ' . $namespace . ', ' .
strtolower($this->getSCName($namespace)) . ', ' . $methodEntry . ');');
Expand Down
2 changes: 1 addition & 1 deletion Library/Operators/Other/CloneOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function compile($expression, CompilationContext $compilationContext)

$clonedVariable = $compilationContext->symbolTable->getVariableForRead($exprCompiledVariable->getCode(), $compilationContext, $expression);
if ($clonedVariable->getType() != 'variable') {
throw new CompiledException("Variable type: " . $variable->getType() . " cannot be cloned");
throw new CompiledException("Variable type: " . $exprVariable->getType() . " cannot be cloned");
}

if ($clonedVariable->hasDifferentDynamicType(array('undefined', 'object', 'null'))) {
Expand Down
47 changes: 45 additions & 2 deletions Library/Operators/Other/EmptyOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,53 @@ class EmptyOperator extends BaseOperator
*/
public function compile($expression, CompilationContext $compilationContext)
{

$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('int', '(0 == 0)', $expression);
return $this->evaluateVariableExpression($expression, $compilationContext);
}

/**
* Evaluates variable expressions like
* if empty $a
* if !empty $b
*
* it will create something around below lines:
* ZEPHIR_IS_EMPTY(var)
*
* @param array $expression
* @param \CompilationContext $compilationContext
* @return \CompiledExpression
*/
protected function evaluateVariableExpression(
$expression,
CompilationContext $compilationContext
) {

switch (true) {
case isset($expression['left']['left']['value'])
&& isset($expression['left']['left']['type'])
&& $expression['left']['left']['type'] == 'variable':

$name = $expression['left']['left']['value'];

break;

default:
throw new Exception(
'Empty syntax not supported'
);
}

$variable = $compilationContext->symbolTable->getVariableForWrite(
$name,
$compilationContext,
$expression['left']['left']
);

return new CompiledExpression(
'bool',
'ZEPHIR_IS_EMPTY(' . $variable->getName() . ')',
$expression
);
}
}
4 changes: 2 additions & 2 deletions Library/SymbolTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ public function getVariableForRead($name, CompilationContext $compilationContext

/**
* Return a variable in the symbol table, it will be used for a write operation
* Some variables aren't writables themselves but their members do
* Some variables aren't writable themselves but their members do
*
* @param string $name
* @param array $statement
* @param CompilationContext $compilationContext
* @param array $statement
* @return \Variable
*/
public function getVariableForWrite($name, $compilationContext, $statement=null)
Expand Down
22 changes: 14 additions & 8 deletions test/flow.zep
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ class Flow
if b {
let c = 1;
if c {
return 1;
return 654;
} else {
return 0;
return -1;
}
} else {
return 0;
return -2;
}
} else {
return 0;
return -3;
}
}

Expand All @@ -142,7 +142,7 @@ class Flow
if b {
let c = 1;
if c {
return 1;
return 987;
} else {
return 0;
}
Expand All @@ -158,26 +158,32 @@ class Flow
{
int a, b;
let a = 1, b = 2;
if a + b { return 1; }
if a + b { return -12; }
return 0;
}

public function testIf14()
{
var a, b;
let a = 1, b = 2;
if a + b { return 1; }
if a + b { return 74; }
return 0;
}

public function testIf15()
{
var a, b, c;
let a = 1, b = 2, c = 3;
if a + b + c { return 1; }
if a + b + c { return 89; }
return 0;
}

public function testIf16(var a)
{
if empty(a) { return true; }
return false;
}

public function testLoop1()
{
var a;
Expand Down
10 changes: 9 additions & 1 deletion unit-tests/flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
assert($t->testIf7() === 1);
assert($t->testIf8() === 0);
assert($t->testIf9() === 1);
assert($t->testIf10() === 1);
assert($t->testIf10() === 654);
assert($t->testIf12() === 987);
assert($t->testIf13() === -12);
assert($t->testIf14() === 74);
assert($t->testIf15() === 89);
assert($t->testIf16(array()) === false);
assert($t->testIf16('') === true);
assert($t->testIf16(null) === true);
assert($t->testIf16(' ') === false);

assert($t->testLoop1() === true);
assert($t->testLoop2() === 5);
Expand Down

0 comments on commit fc86504

Please sign in to comment.