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
-
Constant
A constant intermediate representation.
-
Type
A type intermediate representation.
-
Class_
A class intermediate representation.
-
Documentation
A documentation intermediate representation.
-
File
A file intermediate representation.
-
Into
A visitor to transform an Abstract Syntax Tree (AST) into an Intermediate Representation (IR).
-
Method
A method intermediate representation.
-
Attribute
An attribute intermediate representation.
-
Interface_
An interface intermediate representation.
-
Function_
A named function intermediate representation.
-
Entity
An entity intermediate representation.
-
Trait_
A trait intermediate representation.
-
Parameter
A parameter intermediate representation.
Interfaces
-
HasMethods
An interface to represent an entity with methods.