This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added support for while, do...while and for loops.

  • Loading branch information...
jmalloc committed Aug 20, 2012
1 parent 5df7056 commit 42cf0adc3d30e8d49147a5b8ef043f66469a747e
Showing with 170 additions and 17 deletions.
  1. +2 −6 composer.lock
  2. +41 −0 lib/Icecave/Rasta/Renderer.php
  3. +127 −11 test/suite/Icecave/Rasta/RendererTest.php
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -73,15 +73,18 @@
use Icecave\Pasta\AST\Identifier;
use Icecave\Pasta\AST\Node;
use Icecave\Pasta\AST\PhpBlock;
+use Icecave\Pasta\AST\Stmt\DoWhileStatement;
use Icecave\Pasta\AST\Stmt\ExpressionStatement;
use Icecave\Pasta\AST\Stmt\ForeachStatement;
+use Icecave\Pasta\AST\Stmt\ForStatement;
use Icecave\Pasta\AST\Stmt\IStatement;
use Icecave\Pasta\AST\Stmt\IfStatement;
use Icecave\Pasta\AST\Stmt\NamespaceStatement;
use Icecave\Pasta\AST\Stmt\ReturnStatement;
use Icecave\Pasta\AST\Stmt\StatementBlock;
use Icecave\Pasta\AST\Stmt\ThrowStatement;
use Icecave\Pasta\AST\Stmt\UseStatement;
+use Icecave\Pasta\AST\Stmt\WhileStatement;
use Icecave\Pasta\AST\SyntaxTree;
use Icecave\Pasta\AST\Type\AbstractMethod;
use Icecave\Pasta\AST\Type\ClassDefinition;
@@ -164,6 +167,14 @@ public function visitPhpBlock(PhpBlock $node, $closeTag = true)
return $code;
}
+ public function visitDoWhileStatement(DoWhileStatement $node)
+ {
+ $code = $this->indent('do' . PHP_EOL);
+ $code .= $this->generateStatementCode($node->statement());
+ $code .= $this->indent('while (' . $node->conditionExpression()->accept($this) . ');' . PHP_EOL);
+ return $code;
+ }
+
public function visitExpressionStatement(ExpressionStatement $node)
{
return $this->indent($node->expression()->accept($this) . ';' . PHP_EOL);
@@ -181,6 +192,29 @@ public function visitForeachStatement(ForeachStatement $node)
return $code;
}
+ public function visitForStatement(ForStatement $node)
+ {
+ $init = '';
+ $cond = '';
+ $iter = '';
+
+ if ($expression = $node->initializeExpression()) {
+ $init = $expression->accept($this);
+ }
+
+ if ($expression = $node->conditionExpression()) {
+ $cond = ' ' . $expression->accept($this);
+ }
+
+ if ($expression = $node->iterationExpression()) {
+ $iter = ' ' . $expression->accept($this);
+ }
+
+ $code = $this->indent('for (' . $init . ';' . $cond . ';' . $iter . ')' . PHP_EOL);
+ $code .= $this->generateStatementCode($node->statement());
+ return $code;
+ }
+
public function visitIfStatement(IfStatement $node)
{
$code = $this->indent('if (' . $node->condition()->accept($this) . ')' . PHP_EOL);
@@ -237,6 +271,13 @@ public function visitUseStatement(UseStatement $node)
return $code;
}
+ public function visitWhileStatement(WhileStatement $node)
+ {
+ $code = $this->indent('while (' . $node->conditionExpression()->accept($this) . ')' . PHP_EOL);
+ $code .= $this->generateStatementCode($node->statement());
+ return $code;
+ }
+
public function visitSyntaxTree(SyntaxTree $node)
{
$children = $node->children();
@@ -17,7 +17,9 @@
use Icecave\Pasta\AST\Identifier;
use Icecave\Pasta\AST\Node;
use Icecave\Pasta\AST\PhpBlock;
+use Icecave\Pasta\AST\Stmt\DoWhileStatement;
use Icecave\Pasta\AST\Stmt\ExpressionStatement;
+use Icecave\Pasta\AST\Stmt\ForStatement;
use Icecave\Pasta\AST\Stmt\ForeachStatement;
use Icecave\Pasta\AST\Stmt\IStatement;
use Icecave\Pasta\AST\Stmt\IfStatement;
@@ -26,6 +28,7 @@
use Icecave\Pasta\AST\Stmt\StatementBlock;
use Icecave\Pasta\AST\Stmt\ThrowStatement;
use Icecave\Pasta\AST\Stmt\UseStatement;
+use Icecave\Pasta\AST\Stmt\WhileStatement;
use Icecave\Pasta\AST\SyntaxTree;
use Icecave\Pasta\AST\Type\AbstractMethod;
use Icecave\Pasta\AST\Type\ClassDefinition;
@@ -125,14 +128,29 @@ public function testVisitPhpBlockShortTag()
$this->assertSame('<' . '?' . PHP_EOL . '?' . '>', $code);
}
- public function testExpressionStatement()
+ public function testVisitDoWhileStatement()
+ {
+ $node = new DoWhileStatement(
+ new Literal(true),
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'do' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $expected .= 'while (true);' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitExpressionStatement()
{
$node = new ExpressionStatement(new Literal(123));
$code = $node->accept($this->_renderer);
$this->assertSame('123;' . PHP_EOL, $code);
}
- public function testForeachStatement()
+ public function testVisitForeachStatement()
{
$node = new ForeachStatement(
new Variable(new Identifier('thing')),
@@ -148,7 +166,7 @@ public function testForeachStatement()
$this->assertSame($expected, $code);
}
- public function testForeachStatementWithoutKey()
+ public function testVisitForeachStatementWithoutKey()
{
$node = new ForeachStatement(
new Variable(new Identifier('thing')),
@@ -164,7 +182,91 @@ public function testForeachStatementWithoutKey()
$this->assertSame($expected, $code);
}
- public function testIfStatement()
+ public function testVisitForStatement()
+ {
+ $node = new ForStatement(
+ new Literal(1),
+ new Literal(2),
+ new Literal(3),
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'for (1; 2; 3)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitForStatementWithOnlyInitializeExpression()
+ {
+ $index = new Variable(new Identifier('index'));
+ $node = new ForStatement(
+ new Literal(1),
+ null,
+ null,
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'for (1;;)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitForStatementWithOnlyConditionExpression()
+ {
+ $index = new Variable(new Identifier('index'));
+ $node = new ForStatement(
+ null,
+ new Literal(2),
+ null,
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'for (; 2;)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitForStatementWithOnlyIterationExpression()
+ {
+ $index = new Variable(new Identifier('index'));
+ $node = new ForStatement(
+ null,
+ null,
+ new Literal(3),
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'for (;; 3)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitForStatementWithNoExpressions()
+ {
+ $index = new Variable(new Identifier('index'));
+ $node = new ForStatement(
+ null,
+ null,
+ null,
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'for (;;)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
+ public function testVisitIfStatement()
{
$node = new IfStatement(new Literal(true));
$code = $node->accept($this->_renderer);
@@ -175,7 +277,7 @@ public function testIfStatement()
$this->assertSame($expected, $code);
}
- public function testIfStatementElse()
+ public function testVisitIfStatementElse()
{
$node = new IfStatement(
new Literal(true),
@@ -193,7 +295,7 @@ public function testIfStatementElse()
$this->assertSame($expected, $code);
}
- public function testIfStatementElseIf()
+ public function testVisitIfStatementElseIf()
{
$node = new IfStatement(
new Literal(true),
@@ -211,7 +313,7 @@ public function testIfStatementElseIf()
$this->assertSame($expected, $code);
}
- public function testIfStatementSingleStatement()
+ public function testVisitIfStatementSingleStatement()
{
$node = new IfStatement(
new Literal(true),
@@ -224,7 +326,7 @@ public function testIfStatementSingleStatement()
$this->assertSame($expected, $code);
}
- public function testIfStatementElseSingleStatement()
+ public function testVisitIfStatementElseSingleStatement()
{
$node = new IfStatement(
new Literal(true),
@@ -247,14 +349,14 @@ public function testVisitNamespaceStatement()
$this->assertSame('namespace Foo\Bar\Spam;' . PHP_EOL . PHP_EOL, $code);
}
- public function testReturnStatement()
+ public function testVisitReturnStatement()
{
$node = new ReturnStatement;
$code = $node->accept($this->_renderer);
$this->assertSame('return;' . PHP_EOL, $code);
}
- public function testReturnStatementWithExpression()
+ public function testVisitReturnStatementWithExpression()
{
$node = new ReturnStatement(new Literal(123));
$code = $node->accept($this->_renderer);
@@ -285,7 +387,7 @@ public function testVisitStatementBlockEmpty()
$this->assertSame($expected, $code);
}
- public function testThrowStatement()
+ public function testVisitThrowStatement()
{
$node = new ThrowStatement(new Literal(123));
$code = $node->accept($this->_renderer);
@@ -299,6 +401,20 @@ public function testVisitUseStatement()
$this->assertSame('use Foo\Bar\Spam;' . PHP_EOL, $code);
}
+ public function testVisitWhileStatement()
+ {
+ $node = new WhileStatement(
+ new Literal(true),
+ new StatementBlock
+ );
+ $code = $node->accept($this->_renderer);
+
+ $expected = 'while (true)' . PHP_EOL;
+ $expected .= '{' . PHP_EOL;
+ $expected .= '}' . PHP_EOL;
+ $this->assertSame($expected, $code);
+ }
+
public function testVisitUseStatementWithAlias()
{
$node = new UseStatement(QualifiedIdentifier::fromString('Foo\Bar\Spam'), new Identifier('Alias'));

0 comments on commit 42cf0ad

Please sign in to comment.