Namespace Kitab\Compiler\IntermediateRepresentation
This namespace contains all the Intermediate Representations the parser will compile into. They represent an abstraction of the code holding only the data required by the target.
See the global workflow to get understand where the Intermediate Representation (IR) happens.
Into IR with a visitor
The parser analyses a file, and a resulting Abstract Syntax Tree (AST)
is generated on success. The AST is then transformed into the IR by
applying a visitor on it. The
Kitab\Compiler\IntermediateRepresentation\Into class acts as a
visitor on the AST to transform some nodes in the AST into IR. When
the transformation finishes, the IR can be retrived by collecting it.
Examples
Let $phpParser be an instance of
the PHP-Parser parser, thus:
// Parse PHP code.
$statements = $phpParser->parse('<?php function f(int $x): int { return $x << 2; }');
// Prepare the visitor to transform the AST into IR.
$intoIR = new Kitab\Compiler\IntermediateRepresentation\Into('example.php');
// Prepare the visitor logic.
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor($intoIR);
// Visitor and transform.
$traverser->traverse($statements);
// Collect the result.
$intermediateRepresentation = $intoIR->collect();
assert($intermediateRepresentation instanceof Kitab\Compiler\IntermediateRepresentation\File);
Classes
-
ConstantA constant intermediate representation.
-
TypeA type intermediate representation.
-
Class_A class intermediate representation.
-
DocumentationA documentation intermediate representation.
-
FileA file intermediate representation.
-
IntoA visitor to transform an Abstract Syntax Tree (AST) into an Intermediate Representation (IR).
-
MethodA method intermediate representation.
-
AttributeAn attribute intermediate representation.
-
Interface_An interface intermediate representation.
-
Function_A named function intermediate representation.
-
EntityAn entity intermediate representation.
-
Trait_A trait intermediate representation.
-
ParameterA parameter intermediate representation.
Interfaces
-
HasMethodsAn interface to represent an entity with methods.