Skip to content

xp-framework/ast

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XP AST

Build Status on TravisCI XP Framework Module BSD Licence Required PHP 5.6+ Supports PHP 7.0+ Supports HHVM 3.4+ Latest Stable Version

Abstract syntax tree library used for Compile-time metaprogramming.

Example

Register transformations to be used by the XP Compiler. This can be done in your library's module.xp, for instance.

use lang\ast\transform\Transformations;
use lang\ast\nodes\Method;
use lang\ast\nodes\Signature;
use lang\ast\Code;

Transformations::register('class', function($class) {
  if ($class->value->annotation('getters')) {
    foreach ($class->value->properties() as $property) {
      $class->value->inject(new Method(
        ['public'],
        $property->name,
        new Signature([], $property->type),
        [new Code('return $this->'.$property->name.';')]
      ));
    }
  }
  yield $class;
});

When compiling the following sourcecode, getters for the id and name members will automatically be added.

<?php

<<getters>>
class Person {
  private int $id;
  private string $name;

  public function __construct(int $id, string $name) {
    $this->id= $id;
    $this->name= $name;
  }
}