2017 © Pedro Peláez
 

library ast

AST for the XP Framework

image

xp-framework/ast

AST for the XP Framework

  • Saturday, June 9, 2018
  • by thekid
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,043 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 79 % Grown

The README.md

XP AST

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version, (*1)

Abstract syntax tree library used for XP Compiler., (*2)

Example

use lang\ast\{Language, Tokens};

$tree= Language::named('PHP')->parse(new Tokens('echo PHP_VERSION;'))->tree();

// lang.ast.ParseTree(source: (string))@{
//   scope => lang.ast.Scope {
//     parent => null
//     package => null
//     imports => []
//     types => []
//   }
//   children => [lang.ast.nodes.EchoStatement {
//     kind => "echo"
//     expressions => [lang.ast.nodes.Literal {
//       kind => "literal"
//       expression => "PHP_VERSION"
//       line => 1
//     }]
//     line => 1
//   }]
// }

Compile-time metaprogramming

Register transformations by creating classes inside the lang.ast.syntax.php package - see https://github.com/xp-framework/rfc/issues/327, (*3)

namespace lang\ast\syntax\php;

use lang\ast\Code;
use lang\ast\nodes\{Method, Signature};
use lang\ast\syntax\Extension;
use codegen\Getters;

class CreateGetters implements Extension {

  public function setup($language, $emitter) {
    $emitter->transform('class', function($codegen, $class) {
      if ($class->annotation(Getters::class)) {
        foreach ($class->properties() as $property) {
          $class->declare(new Method(
            ['public'],
            $property->name,
            new Signature([], $property->type),
            [new Code('return $this->'.$property->name)]
          ));
        }
      }
      return $class;
    });
  }
}

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

use codegen\Getters;

#[Getters]
class Person {
  private int $id;
  private string $name;

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

The Versions

09/06 2018

dev-master

9999999-dev http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

02/04 2018

v1.3.0

1.3.0.0 http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

01/04 2018

dev-feature/using

dev-feature/using http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

30/03 2018

v1.2.0

1.2.0.0 http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/03 2018

v1.1.0

1.1.0.0 http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/03 2018

v1.0.1

1.0.1.0 http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

06/11 2017

v1.0.0

1.0.0.0 http://xp-framework.net/

AST for the XP Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp