Skip to content

Commit

Permalink
Merge pull request #171 from xp-framework/refactor/lookup
Browse files Browse the repository at this point in the history
Refactor scope lookup from Result to CodeGen
  • Loading branch information
thekid authored Jul 15, 2023
2 parents e2d056a + e428a1c commit 64a32d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
30 changes: 30 additions & 0 deletions src/main/php/lang/ast/CodeGen.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace lang\ast;

use lang\ast\emit\{Reflection, Declaration, Incomplete};

class CodeGen {
private $id= 0;
public $scope= [];
Expand Down Expand Up @@ -32,4 +34,32 @@ public function search($node, $kind) {
}
}
}

/**
* Looks up a given type
*
* @param string $type
* @return lang.ast.emit.Type
*/
public function lookup($type) {
$enclosing= $this->scope[0] ?? null;

if ('self' === $type || 'static' === $type) {
return new Declaration($enclosing->type);
} else if ('parent' === $type) {
return $enclosing->type->parent ? $this->lookup($enclosing->type->parent->literal()) : null;
}

foreach ($this->scope as $scope) {
if ($scope->type->name && $type === $scope->type->name->literal()) {
return new Declaration($scope->type);
}
}

if (class_exists($type) || interface_exists($type) || trait_exists($type) || enum_exists($type)) {
return new Reflection($type);
} else {
return new Incomplete($type);
}
}
}
10 changes: 3 additions & 7 deletions src/main/php/lang/ast/emit/Declaration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use lang\ast\nodes\{EnumCase, Property};

class Declaration extends Type {
private $type, $result;
private $type;

static function __static() { }

/**
* @param lang.ast.nodes.TypeDeclaration $type
* @param lang.ast.Result $result
*/
public function __construct($type, $result) {
/** @param lang.ast.nodes.TypeDeclaration $type */
public function __construct($type) {
$this->type= $type;
$this->result= $result;
}

/** @return string */
Expand Down
21 changes: 2 additions & 19 deletions src/main/php/lang/ast/emit/GeneratedCode.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,11 @@ public function temp() {
/**
* Looks up a given type
*
* @deprecated Use `CodeGen::lookup()` instead!
* @param string $type
* @return lang.ast.emit.Type
*/
public function lookup($type) {
$enclosing= $this->codegen->scope[0] ?? null;

if ('self' === $type || 'static' === $type) {
return new Declaration($enclosing->type, $this);
} else if ('parent' === $type) {
return $enclosing->type->parent ? $this->lookup($enclosing->type->parent->literal()) : null;
}

foreach ($this->codegen->scope as $scope) {
if ($scope->type->name && $type === $scope->type->name->literal()) {
return new Declaration($scope->type, $this);
}
}

if (class_exists($type) || interface_exists($type) || trait_exists($type) || enum_exists($type)) {
return new Reflection($type);
} else {
return new Incomplete($type);
}
return $this->codegen->lookup($type);
}
}
4 changes: 2 additions & 2 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function isConstant($result, $node) {
return (
$node->member instanceof Literal &&
is_string($node->type) &&
!$result->lookup($node->type)->rewriteEnumCase($node->member->expression)
!$result->codegen->lookup($node->type)->rewriteEnumCase($node->member->expression)
);
} else if ($node instanceof BinaryExpression) {
return $this->isConstant($result, $node->left) && $this->isConstant($result, $node->right);
Expand Down Expand Up @@ -1064,7 +1064,7 @@ protected function emitScope($result, $scope) {
$scope->member instanceof Literal &&
is_string($scope->type) &&
'class' !== $scope->member->expression &&
$result->lookup($scope->type)->rewriteEnumCase($scope->member->expression)
$result->codegen->lookup($scope->type)->rewriteEnumCase($scope->member->expression)
) {
$result->out->write('$'.$scope->member->expression);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function lookup_self() {
$r= new GeneratedCode(new MemoryOutputStream());
$context= $r->codegen->enter(new InType(new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1)));

Assert::equals(new Declaration($context->type, $r), $r->lookup('self'));
Assert::equals(new Declaration($context->type), $r->lookup('self'));
}

#[Test]
Expand All @@ -73,7 +73,7 @@ public function lookup_named() {
$r= new GeneratedCode(new MemoryOutputStream());
$context= $r->codegen->enter(new InType(new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1)));

Assert::equals(new Declaration($context->type, $r), $r->lookup('\\T'));
Assert::equals(new Declaration($context->type), $r->lookup('\\T'));
}

#[Test]
Expand Down

0 comments on commit 64a32d3

Please sign in to comment.