Skip to content

Commit

Permalink
Updates and small unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Cinderella-Man committed Dec 17, 2013
1 parent ea85739 commit 43680be
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
27 changes: 18 additions & 9 deletions Library/Operators/Other/EmptyOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public function compile($expression, CompilationContext $compilationContext)
{
$compilationContext->headersManager->add('kernel/operators');

if (isset($expression['left']['type'])
&& $expression['left']['type'] == 'variable'
) {
return $this->evaluateVariableExpression($expression, $compilationContext);
}

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

/**
Expand All @@ -61,10 +55,25 @@ protected function evaluateVariableExpression(
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(
$expression['left']['value'],
$name,
$compilationContext,
$expression['left']
$expression['left']['left']
);

return new CompiledExpression(
Expand Down
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 43680be

Please sign in to comment.