-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement pretty printer and a test to confirm basics are working
- Loading branch information
Showing
10 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":1,"defects":[],"times":{"Tests\\ScannerTest::it_expects_single_token_when_source_is_empty":0.005}} | ||
{"version":1,"defects":{"Tests\\AstPrinterTest::it_expects_to_pretty_print_tree":7},"times":{"Tests\\ScannerTest::it_expects_single_token_when_source_is_empty":0.005,"Tests\\AstPrinterTest::it_expects_to_pretty_print_tree":0.03,"Tests\\AstPrinterTest::it_expects_missing_parenthesis_and_additional_space_not_to_match":0.004,"Tests\\AstPrinterTest::it_expects_the_printed_expression_to_match_exactly":0.002}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Waffle; | ||
|
||
use Waffle\Grammar\Expr; | ||
use Waffle\Grammar\Expressions\Assign; | ||
use Waffle\Grammar\Expressions\Binary; | ||
use Waffle\Grammar\Expressions\Call; | ||
use Waffle\Grammar\Expressions\EThis; | ||
use Waffle\Grammar\Expressions\Get; | ||
use Waffle\Grammar\Expressions\Grouping; | ||
use Waffle\Grammar\Expressions\Literal; | ||
use Waffle\Grammar\Expressions\Logical; | ||
use Waffle\Grammar\Expressions\Set; | ||
use Waffle\Grammar\Expressions\Super; | ||
use Waffle\Grammar\Expressions\Unary; | ||
use Waffle\Grammar\Expressions\Variable; | ||
|
||
class AstPrinter implements Grammar\Expressions\Visitor | ||
{ | ||
public function printExpr(Expr $expr): string | ||
{ | ||
return $expr->accept($this); | ||
} | ||
|
||
public function visitAssignExpr(Assign $expr) | ||
{ | ||
// TODO: Implement visitAssignExpr() method. | ||
} | ||
|
||
public function visitBinaryExpr(Binary $expr) | ||
{ | ||
return $this->parenthesize( | ||
$expr->operator->lexeme, | ||
$expr->left, | ||
$expr->right | ||
); | ||
} | ||
|
||
public function visitCallExpr(Call $expr) | ||
{ | ||
// TODO: Implement visitCallExpr() method. | ||
} | ||
|
||
public function visitGetExpr(Get $expr) | ||
{ | ||
// TODO: Implement visitGetExpr() method. | ||
} | ||
|
||
public function visitGroupingExpr(Grouping $expr) | ||
{ | ||
return $this->parenthesize( | ||
"group", | ||
$expr->expression | ||
); | ||
} | ||
|
||
public function visitLiteralExpr(Literal $expr) | ||
{ | ||
if (is_null($expr->value)) { | ||
return "nil"; | ||
} | ||
return (string)$expr->value; | ||
} | ||
|
||
public function visitLogicalExpr(Logical $expr) | ||
{ | ||
// TODO: Implement visitLogicalExpr() method. | ||
} | ||
|
||
public function visitSetExpr(Set $expr) | ||
{ | ||
// TODO: Implement visitSetExpr() method. | ||
} | ||
|
||
public function visitSuperExpr(Super $expr) | ||
{ | ||
// TODO: Implement visitSuperExpr() method. | ||
} | ||
|
||
public function visitThisExpr(EThis $expr) | ||
{ | ||
// TODO: Implement visitThisExpr() method. | ||
} | ||
|
||
public function visitUnaryExpr(Unary $expr) | ||
{ | ||
return $this->parenthesize( | ||
$expr->operator->lexeme, | ||
$expr->right | ||
); | ||
} | ||
|
||
public function visitVariableExpr(Variable $variable) | ||
{ | ||
// TODO: Implement visitVariableExpr() method. | ||
} | ||
|
||
private function parenthesize(string $name, Expr...$exprs): string | ||
{ | ||
$parts = []; | ||
$parts[] = "("; | ||
$parts[] = $name; | ||
foreach ($exprs as $expr) { | ||
$parts[] = " "; | ||
$parts[] = $expr->accept($this); | ||
} | ||
$parts[] = ")"; | ||
|
||
return implode($parts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Waffle\AstPrinter; | ||
use Waffle\Grammar\Expr; | ||
use Waffle\Grammar\Expressions\Binary; | ||
use Waffle\Grammar\Expressions\Grouping; | ||
use Waffle\Grammar\Expressions\Literal; | ||
use Waffle\Grammar\Expressions\Unary; | ||
use Waffle\Token; | ||
use Waffle\TokenType; | ||
|
||
class AstPrinterTest extends TestCase | ||
{ | ||
private AstPrinter $printer; | ||
private Expr $expr; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->printer = new AstPrinter(); | ||
$this->expr = new Binary( | ||
new Unary( | ||
new Token(TokenType::MINUS, "-", null, 1), | ||
new Literal(123) | ||
), | ||
new Token(TokenType::STAR, "*", null, 1), | ||
new Grouping( | ||
new Literal(45.67) | ||
) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_expects_missing_parenthesis_and_additional_space_not_to_match(): void | ||
{ | ||
$this->assertNotEquals( | ||
"* (- 123) (group 45.67) )", | ||
$this->printer->printExpr($this->expr) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_expects_the_printed_expression_to_match_exactly(): void | ||
{ | ||
$this->assertEquals( | ||
"(* (- 123) (group 45.67))", | ||
$this->printer->printExpr($this->expr) | ||
); | ||
} | ||
} |