Skip to content

Commit 0645052

Browse files
committed
Adapt to AST type refactoring
See xp-framework/ast#39
1 parent b94c6c4 commit 0645052

9 files changed

+56
-30
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"keywords": ["module", "xp"],
88
"require" : {
99
"xp-framework/core": "^11.0 | ^10.0",
10-
"xp-framework/ast": "^8.1",
10+
"xp-framework/ast": "dev-refactor/types as 9.0.0",
1111
"php" : ">=7.0.0"
1212
},
1313
"require-dev" : {

src/main/php/lang/ast/emit/Declaration.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function name() { return ltrim($this->type->name, '\\'); }
2828
public function rewriteEnumCase($member) {
2929
if (!self::$ENUMS && 'enum' === $this->type->kind) {
3030
return ($this->type->body[$member] ?? null) instanceof EnumCase;
31-
} else if ('class' === $this->type->kind && '\\lang\\Enum' === $this->type->parent) {
31+
} else if ('class' === $this->type->kind && $this->type->parent && '\\lang\\Enum' === $this->type->parent->literal()) {
3232
return ($this->type->body['$'.$member] ?? null) instanceof Property;
3333
}
3434
return false;

src/main/php/lang/ast/emit/GeneratedCode.class.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function lookup($type) {
7070
if ('self' === $type || 'static' === $type) {
7171
return new Declaration($this->type[0], $this);
7272
} else if ('parent' === $type) {
73-
return $this->lookup($this->type[0]->parent);
73+
return $this->lookup($this->type[0]->parent->literal());
7474
}
7575

7676
foreach ($this->type as $enclosing) {
77-
if ($type === $enclosing->name) return new Declaration($enclosing, $this);
77+
if ($enclosing->name && $type === $enclosing->name->literal()) return new Declaration($enclosing, $this);
7878
}
7979

8080
return new Reflection($type);

src/main/php/lang/ast/emit/PHP.class.php

+40-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
UnpackExpression,
1515
Variable
1616
};
17-
use lang\ast\types\{IsUnion, IsFunction, IsArray, IsMap, IsNullable};
17+
use lang\ast\types\{IsUnion, IsFunction, IsArray, IsMap, IsNullable, IsExpression};
1818
use lang\ast\{Emitter, Node, Type, Result};
1919

2020
abstract class PHP extends Emitter {
@@ -373,9 +373,17 @@ protected function emitEnum($result, $enum) {
373373

374374
$enum->comment && $this->emitOne($result, $enum->comment);
375375
$enum->annotations && $this->emitOne($result, $enum->annotations);
376-
$result->at($enum->declared)->out->write('enum '.$this->declaration($enum->name));
376+
$result->at($enum->declared)->out->write('enum '.$enum->declaration());
377377
$enum->base && $result->out->write(':'.$enum->base);
378-
$enum->implements && $result->out->write(' implements '.implode(', ', $enum->implements));
378+
379+
if ($enum->implements) {
380+
$list= '';
381+
foreach ($enum->implements as $type) {
382+
$list.= ', '.$type->literal();
383+
}
384+
$result->out->write(' implements '.substr($list, 2));
385+
}
386+
379387
$result->out->write('{');
380388

381389
foreach ($enum->body as $member) {
@@ -386,7 +394,7 @@ protected function emitEnum($result, $enum) {
386394
$result->out->write('static function __init() {');
387395
$this->emitInitializations($result, $result->locals[0]);
388396
$this->emitMeta($result, $enum->name, $enum->annotations, $enum->comment);
389-
$result->out->write('}} '.$enum->name.'::__init();');
397+
$result->out->write('}} '.$enum->name->literal().'::__init();');
390398
array_shift($result->type);
391399
}
392400

@@ -397,9 +405,17 @@ protected function emitClass($result, $class) {
397405

398406
$class->comment && $this->emitOne($result, $class->comment);
399407
$class->annotations && $this->emitOne($result, $class->annotations);
400-
$result->at($class->declared)->out->write(implode(' ', $class->modifiers).' class '.$this->declaration($class->name));
401-
$class->parent && $result->out->write(' extends '.$class->parent);
402-
$class->implements && $result->out->write(' implements '.implode(', ', $class->implements));
408+
$result->at($class->declared)->out->write(implode(' ', $class->modifiers).' class '.$class->declaration());
409+
$class->parent && $result->out->write(' extends '.$class->parent->literal());
410+
411+
if ($class->implements) {
412+
$list= '';
413+
foreach ($class->implements as $type) {
414+
$list.= ', '.$type->literal();
415+
}
416+
$result->out->write(' implements '.substr($list, 2));
417+
}
418+
403419
$result->out->write('{');
404420
foreach ($class->body as $member) {
405421
$this->emitOne($result, $member);
@@ -444,7 +460,7 @@ protected function emitClass($result, $class) {
444460
$result->out->write('static function __init() {');
445461
$this->emitInitializations($result, $result->locals[0]);
446462
$this->emitMeta($result, $class->name, $class->annotations, $class->comment);
447-
$result->out->write('}} '.$class->name.'::__init();');
463+
$result->out->write('}} '.$class->name->literal().'::__init();');
448464
array_shift($result->type);
449465
$result->locals= [];
450466
}
@@ -507,7 +523,7 @@ protected function emitInterface($result, $interface) {
507523

508524
$interface->comment && $this->emitOne($result, $interface->comment);
509525
$interface->annotations && $this->emitOne($result, $interface->annotations);
510-
$result->at($interface->declared)->out->write('interface '.$this->declaration($interface->name));
526+
$result->at($interface->declared)->out->write('interface '.$interface->declaration());
511527
$interface->parents && $result->out->write(' extends '.implode(', ', $interface->parents));
512528
$result->out->write('{');
513529
foreach ($interface->body as $member) {
@@ -523,7 +539,7 @@ protected function emitTrait($result, $trait) {
523539

524540
$trait->comment && $this->emitOne($result, $trait->comment);
525541
$trait->annotations && $this->emitOne($result, $trait->annotations);
526-
$result->at($trait->declared)->out->write('trait '.$this->declaration($trait->name));
542+
$result->at($trait->declared)->out->write('trait '.$trait->declaration());
527543
$result->out->write('{');
528544
foreach ($trait->body as $member) {
529545
$this->emitOne($result, $member);
@@ -904,12 +920,12 @@ protected function emitArguments($result, $arguments) {
904920
}
905921

906922
protected function emitNew($result, $new) {
907-
if ($new->type instanceof Node) {
923+
if ($new->type instanceof IsExpression) {
908924
$result->out->write('new (');
909-
$this->emitOne($result, $new->type);
925+
$this->emitOne($result, $new->type->expression);
910926
$result->out->write(')(');
911927
} else {
912-
$result->out->write('new '.$new->type.'(');
928+
$result->out->write('new '.$new->type->literal().'(');
913929
}
914930

915931
$this->emitArguments($result, $new->arguments);
@@ -925,12 +941,19 @@ protected function emitNewClass($result, $new) {
925941

926942
// Allow "extends self" to reference enclosing class (except if this
927943
// class is an anonymous class!)
928-
if ('self' === $new->definition->parent && $result->type && $result->type[0]->name) {
929-
$result->out->write(' extends '.$result->type[0]->name);
944+
if ($result->type && $result->type[0]->name && $new->definition->parent && 'self' === $new->definition->parent->name()) {
945+
$result->out->write(' extends '.$result->type[0]->name->literal());
930946
} else if ($new->definition->parent) {
931-
$result->out->write(' extends '.$new->definition->parent);
947+
$result->out->write(' extends '.$new->definition->parent->literal());
948+
}
949+
950+
if ($new->definition->implements) {
951+
$list= '';
952+
foreach ($new->definition->implements as $type) {
953+
$list.= ', '.$type->literal();
954+
}
955+
$result->out->write(' implements '.substr($list, 2));
932956
}
933-
$new->definition->implements && $result->out->write(' implements '.implode(', ', $new->definition->implements));
934957

935958
array_unshift($result->type, $new->definition);
936959
$result->out->write('{');

src/main/php/lang/ast/emit/php/XpMeta.class.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ private function comment($comment) {
6666
}
6767

6868
/** Emit xp::$meta */
69-
protected function emitMeta($result, $name, $annotations, $comment) {
70-
if (null === $name) {
69+
protected function emitMeta($result, $type, $annotations, $comment) {
70+
if (null === $type) {
7171
$result->out->write('\xp::$meta[strtr(self::class, "\\\\", ".")]= [');
7272
} else {
73-
$result->out->write('\xp::$meta[\''.strtr(ltrim($name, '\\'), '\\', '.').'\']= [');
73+
$result->out->write('\xp::$meta[\''.strtr($type->name(), '\\', '.').'\']= [');
7474
}
7575
$result->out->write('"class" => [');
7676
$this->attributes($result, $annotations, []);

src/test/php/lang/ast/unittest/emit/DeclarationTest.class.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
use lang\ast\emit\Declaration;
44
use lang\ast\nodes\{ClassDeclaration, Property};
5+
use lang\ast\types\IsValue;
56
use unittest\{Assert, Test, Expect};
67

78
class DeclarationTest {
89
private $type;
910

1011
#[Before]
1112
public function type() {
12-
$this->type= new ClassDeclaration([], '\\T', '\\lang\\Enum', [], [
13+
$this->type= new ClassDeclaration([], '\\T', new IsValue('\\lang\\Enum'), [], [
1314
'$ONE' => new Property(['public', 'static'], 'ONE', null, null, [], null, 1)
1415
]);
1516
}

src/test/php/lang/ast/unittest/emit/GeneratedCodeTest.class.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use io\streams\MemoryOutputStream;
44
use lang\ast\emit\{GeneratedCode, Declaration, Escaping, Reflection};
55
use lang\ast\nodes\ClassDeclaration;
6+
use lang\ast\types\IsValue;
67
use lang\{Value, ClassNotFoundException};
78
use unittest\{Assert, Expect, Test};
89

@@ -46,23 +47,23 @@ public function writes_epilog_on_closing() {
4647
#[Test]
4748
public function lookup_self() {
4849
$r= new GeneratedCode(new MemoryOutputStream());
49-
$r->type[0]= new ClassDeclaration([], '\\T', null, [], [], null, null, 1);
50+
$r->type[0]= new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1);
5051

5152
Assert::equals(new Declaration($r->type[0], $r), $r->lookup('self'));
5253
}
5354

5455
#[Test]
5556
public function lookup_parent() {
5657
$r= new GeneratedCode(new MemoryOutputStream());
57-
$r->type[0]= new ClassDeclaration([], '\\T', '\\lang\\Value', [], [], null, null, 1);
58+
$r->type[0]= new ClassDeclaration([], new IsValue('\\T'), new IsValue('\\lang\\Value'), [], [], null, null, 1);
5859

5960
Assert::equals(new Reflection(Value::class), $r->lookup('parent'));
6061
}
6162

6263
#[Test]
6364
public function lookup_named() {
6465
$r= new GeneratedCode(new MemoryOutputStream());
65-
$r->type[0]= new ClassDeclaration([], '\\T', null, [], [], null, null, 1);
66+
$r->type[0]= new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1);
6667

6768
Assert::equals(new Declaration($r->type[0], $r), $r->lookup('\\T'));
6869
}

src/test/php/lang/ast/unittest/emit/RewriteClassOnObjectsTest.class.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use lang\ast\emit\{PHP, RewriteClassOnObjects};
44
use lang\ast\nodes\{ScopeExpression, Variable, Literal, ClassDeclaration};
5+
use lang\ast\types\IsValue;
56
use unittest\{Assert, Test};
67

78
class RewriteClassOnObjectsTest extends EmitterTraitTest {
@@ -24,7 +25,7 @@ public function rewrites_type_variable() {
2425
public function does_not_rewrite_type_literal() {
2526
Assert::equals('self::class', $this->emit(
2627
new ScopeExpression('self', new Literal('class')),
27-
[new ClassDeclaration([], '\\T', null, [], [], null, null, 1)]
28+
[new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1)]
2829
));
2930
}
3031
}

src/test/php/lang/ast/unittest/loader/CompilingClassLoaderTest.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function load_uri() {
131131
Assert::equals('Tests', $class->getSimpleName());
132132
}
133133

134-
#[Test, Expect(class: ClassFormatException::class, withMessage: 'Compiler error: Expected "{", have "(end)"')]
134+
#[Test, Expect(class: ClassFormatException::class, withMessage: 'Compiler error: Expected "type name", have "(end)"')]
135135
public function load_class_with_syntax_errors() {
136136
$this->compile(['Errors' => "<?php\nclass"], function($loader, $types) {
137137
return $loader->loadClass($types['Errors']);

0 commit comments

Comments
 (0)