Skip to content

Commit

Permalink
Refactor code to work without lang.reflect.Package
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jul 30, 2023
1 parent 77c0f24 commit 3c1b795
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/main/php/lang/ast/CompilingClassloader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use lang\ast\emit\Reflection;
use lang\ast\emit\php\XpMeta;
use lang\reflect\Package;
use lang\{
ClassFormatException,
ClassLoader,
Expand Down
13 changes: 6 additions & 7 deletions src/main/php/lang/ast/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use io\streams\OutputStream;
use lang\ast\{Node, Error, Errors};
use lang\reflect\Package;
use lang\{IllegalArgumentException, IllegalStateException, ClassLoader, XPClass};

abstract class Emitter {
Expand All @@ -18,16 +17,16 @@ abstract class Emitter {
*/
public static function forRuntime($runtime, $emitters= []) {
sscanf($runtime, '%[^.:]%*[.:]%d.%d', $engine, $major, $minor);
$p= Package::forName('lang.ast.emit');
$cl= ClassLoader::getDefault();

$engine= strtoupper($engine);
do {
$impl= $engine.$major.$minor;
if ($p->providesClass($impl)) {
if (empty($emitters)) return $p->loadClass($impl);
$impl= "lang.ast.emit.{$engine}{$major}{$minor}";
if ($cl->providesClass($impl)) {
if (empty($emitters)) return $cl->loadClass($impl);

// Extend loaded class, including all given emitters
$extended= ['kind' => 'class', 'extends' => [$p->loadClass($impl)], 'implements' => [], 'use' => []];
$extended= ['kind' => 'class', 'extends' => [$cl->loadClass($impl)], 'implements' => [], 'use' => []];
foreach ($emitters as $class) {
if ($class instanceof XPClass) {
$impl.= ''.strtr($class->getName(), ['.' => '·']);
Expand All @@ -37,7 +36,7 @@ public static function forRuntime($runtime, $emitters= []) {
$extended['use'][]= XPClass::forName($class);
}
}
return ClassLoader::defineType($p->getName().'.'.$impl, $extended, '{}');
return ClassLoader::defineType($impl, $extended, '{}');
}
} while ($minor-- > 0);

Expand Down
22 changes: 19 additions & 3 deletions src/main/php/xp/compiler/Usage.class.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<?php namespace xp\compiler;

use lang\ClassLoader;
use lang\ast\{Language, Emitter};
use lang\reflect\Package;
use util\cmd\Console;

/** @codeCoverageIgnore */
class Usage {
const RUNTIME = 'php';

/**
* Returns XPClass instances for all classes inside a given package
*
* @param string $package
* @return iterable
*/
private static function classesIn($package) {
$offset= -strlen(\xp::CLASS_FILE_EXT);
$cl= ClassLoader::getDefault();
foreach (ClassLoader::getDefault()->packageContents($package) as $item) {
if (0 === substr_compare($item, \xp::CLASS_FILE_EXT, $offset)) {
yield $cl->loadClass($package.'.'.substr($item, 0, $offset));
}
}
}

/** @return int */
public static function main(array $args) {
Console::$err->writeLine('Usage: xp compile <in> [<out>]');
Expand All @@ -21,14 +37,14 @@ public function add($t, $active= false) {
};

$emitter= Emitter::forRuntime(self::RUNTIME.':'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION);
foreach (Package::forName('lang.ast.emit')->getClasses() as $class) {
foreach (self::classesIn('lang.ast.emit') as $class) {
if ($class->isSubclassOf(Emitter::class) && !(MODIFIER_ABSTRACT & $class->getModifiers())) {
$impl->add($class, $class->equals($emitter));
}
}

$language= Language::named(strtoupper(self::RUNTIME));
foreach (Package::forName('lang.ast.syntax')->getClasses() as $class) {
foreach (self::classesIn('lang.ast.syntax') as $class) {
if ($class->isSubclassOf(Language::class) && !(MODIFIER_ABSTRACT & $class->getModifiers())) {
$impl->add($class, $class->isInstance($language));
}
Expand Down

0 comments on commit 3c1b795

Please sign in to comment.