Skip to content

Commit

Permalink
refactory checking for symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
tealang committed Sep 5, 2024
1 parent 75f02e9 commit 8e91285
Show file tree
Hide file tree
Showing 17 changed files with 720 additions and 762 deletions.
54 changes: 24 additions & 30 deletions compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,45 +269,39 @@ private function check_ast()
private function check_ast_for_unit(Unit $unit, ASTChecker $normal_checker)
{
$native_checker = ASTChecker::get_native_checker();
$programs = $unit->programs;

foreach ($unit->programs as $program) {
// 1st, check all ns usings
foreach ($programs as $program) {
if ($program->is_native) {
$native_checker->collect_program_uses($program);
$native_checker->check_all_usings($program);
}
else {
$normal_checker->collect_program_uses($program);
$normal_checker->check_all_usings($program);
}
}

$native_programs = [];
$normal_programs = [];
foreach ($unit->programs as $program) {
if ($program->is_native) {
$native_programs[] = $program;
}
elseif ($program->name === '__package') {
self::echo_start(" - {$program->file}", LF);
$normal_checker->check_program($program);
}
else {
$normal_programs[] = $program;
}
// 2nd, attach symbols, and collecting auto usings
foreach ($programs as $program) {
self::echo_start(" - {$program->file}", LF);
$checker = $program->is_native ? $native_checker : $normal_checker;
$checker->collect_program_uses($program);
}

// the Native programs
foreach ($native_programs as $program) {
$native_checker->check_all_usings($program);
// 3td, check statements
$package_program = $programs['__package'] ?? null;
if ($package_program) {
$normal_checker->check_program($package_program);
}

foreach ($native_programs as $program) {
self::echo_start(" - {$program->file}", LF);
$native_checker->check_all_declarations($program);
foreach ($programs as $program) {
// self::echo_start(" - {$program->file}", LF);
$program->is_native and $native_checker->check_program($program);
}

// the Tea programs
foreach ($normal_programs as $program) {
self::echo_start(" - {$program->file}", LF);
$normal_checker->check_program($program);
foreach ($programs as $program) {
// self::echo_start(" - {$program->file}", LF);
!$program->is_native and $normal_checker->check_program($program);
}
}

Expand Down Expand Up @@ -552,14 +546,14 @@ private function render_public_header()
$program->uses = $this->header_program->uses;

foreach ($this->unit->symbols as $symbol) {
$declaration = $symbol->declaration;
$belong_program = $declaration->program;
$modifier = $declaration->modifier ?? null;
$decl = $symbol->declaration;
$belong_program = $decl->program;
$modifier = $decl->modifier ?? null;
if (($modifier === _PUBLIC)
&& $belong_program->unit === $this->unit
&& !$belong_program->is_external
) {
$program->append_declaration($declaration);
$program->append_declaration($decl);
}
}

Expand Down
46 changes: 29 additions & 17 deletions compiler/ast/base/Identifiable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function get_unit()
return $this->symbol->declaration->unit;
}

// public function get_declaration()
// {
// $decl = $this->symbol->declaration;
// if ($decl instanceof UseDeclaration) {
// $decl = $decl->source_declaration;
// }

// return $decl;
// }

public function is_assignable()
{
$decl = $this->symbol->declaration;
Expand Down Expand Up @@ -89,15 +99,15 @@ public static function create_with_symbol(Symbol $symbol)

public function is_based_with(IType $target)
{
if (!$this->symbol->declaration instanceof ClassKindredDeclaration) {
return false;
$curr_decl = $this->symbol->declaration;
if (!$curr_decl instanceof ClassKindredDeclaration) {
$is = false;
}

if ($this->symbol->declaration->find_based_with_symbol($target->symbol) !== null) {
return true;
else {
$is = $curr_decl->find_based_with_symbol($target->symbol) !== null;
}

return false;
return $is;
}

public function is_same_or_based_with(IType $target)
Expand All @@ -108,20 +118,22 @@ public function is_same_or_based_with(IType $target)
public function is_accept_single_type(IType $target)
{
if ($target->has_null and !$this->nullable and !$this->has_null) {
return false;
$is = false;
}

if ($target instanceof NoneType) {
return $this->nullable;
elseif ($target instanceof NoneType) {
$is = $this->nullable;
}
else {
$is = $target->symbol === $this->symbol
// || $target === TypeFactory::$_none
// for check BuiltinTypeClassDeclaration like String
// can not use symbol to compare BuiltinTypeClassDeclaration, because of the symbol maybe 'this'
|| $this->symbol->declaration === $target->symbol->declaration
|| $target->is_based_with($this)
;
}

return $target->symbol === $this->symbol
// || $target === TypeFactory::$_none
// for check BuiltinTypeClassDeclaration like String
// can not use symbol to compare BuiltinTypeClassDeclaration, because of the symbol maybe 'this'
|| $this->symbol->declaration === $target->symbol->declaration
|| $target->is_based_with($this)
;
return $is;
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/ast/base/Program.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function __construct(string $file, Unit $unit)
$this->name = $this->generate_name();
}

public function append_declaration(IDeclaration $declaration)
public function append_declaration(IDeclaration $decl)
{
$this->declarations[(string)$declaration->name] = $declaration;
$this->declarations[(string)$decl->name] = $decl;
}

public function append_depends_native_program(Program $program)
Expand Down
13 changes: 9 additions & 4 deletions compiler/ast/base/Symbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ class Symbol
*/
public $declaration;

public function __construct(IDeclaration $declaration)
/**
* @var ?UseDeclaration
*/
public $using;

public function __construct(IDeclaration $decl)
{
$this->name = $declaration->name;
$this->declaration = $declaration;
// $declaration->symbol = $this;
$this->name = $decl->name;
$this->declaration = $decl;
// $decl->symbol = $this;
}
}

Expand Down
49 changes: 24 additions & 25 deletions compiler/ast/base/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,33 @@ public function unite_type(IType $target): IType {

public function is_accept_single_type(IType $target) {
if ($target->has_null and !$this->nullable and !$this->has_null) {
return false;
$is = false;
}

if ($target instanceof NoneType) {
return $this->nullable;
elseif ($target instanceof NoneType) {
$is = $this->nullable;
}

if ($target === $this) {
return true;
elseif ($target === $this) {
$is = true;
}

// for the builtin classes
if ($target instanceof ClassKindredIdentifier && $this->symbol === $target->symbol) {
return true;
elseif ($target instanceof ClassKindredIdentifier && $this->symbol === $target->symbol) {
$is = true;
}

if (!$target instanceof static) {
return false;
elseif (!$target instanceof static) {
$is = false;
}

// if current value type is Any, then accept any types
if ($this->generic_type === null || $this->generic_type === TypeFactory::$_any) {
return true;
elseif ($this->generic_type === null || $this->generic_type === TypeFactory::$_any) {
$is = true;
}

if ($target->generic_type === null) {
return false;
elseif ($target->generic_type === null) {
$is = false;
}
else {
$is = $this->generic_type->is_accept_type($target->generic_type);
}

return $this->generic_type->is_accept_type($target->generic_type);
return $is;
}

public function is_same_with(IType $target) {
Expand Down Expand Up @@ -504,15 +501,17 @@ class IterableType extends SingleGenericType {

public function is_same_or_based_with(IType $target) {
if ($this->symbol !== $target->symbol and $target->symbol !== TypeFactory::$_iterable->symbol) {
return false;
$is = false;
}

if ($this->generic_type === null or $target->generic_type === null) {
return $this->generic_type === $target->generic_type
elseif ($this->generic_type === null or $target->generic_type === null) {
$is = $this->generic_type === $target->generic_type
|| ($this->generic_type ?? $target->generic_type) instanceof AnyType;
}
else {
$is = $this->generic_type->is_same_or_based_with($target->generic_type);
}

return $this->generic_type->is_same_or_based_with($target->generic_type);
return $is;
}
}

Expand Down
33 changes: 17 additions & 16 deletions compiler/ast/declaration/BaseDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ trait DeclarationTrait {

public $uses = [];

public $defer_check_identifiers = [];
public $unknow_identifiers = [];

public $tailing_newlines;

public function set_depends_to_unit_level()
{
foreach ($this->defer_check_identifiers as $identifier) {
$declaration = $identifier->symbol->declaration;
if (!$declaration->is_unit_level) {
$declaration->is_unit_level = true;
$declaration->set_depends_to_unit_level();
foreach ($this->unknow_identifiers as $identifier) {
$decl = $identifier->symbol->declaration;
if (!$decl->is_unit_level) {
$decl->is_unit_level = true;
$decl->set_depends_to_unit_level();
}
}
}
Expand All @@ -103,27 +103,28 @@ public function append_use_declaration(UseDeclaration $use)
}
}

public function set_defer_check_identifier(Identifiable $identifier)
public function append_unknow_identifier(PlainIdentifier $identifier)
{
$this->defer_check_identifiers[$identifier->name] = $identifier;
$this->unknow_identifiers[] = $identifier;
}

public function remove_defer_check_for_key(string $key)
public function remove_unknow_identifier(PlainIdentifier $identifier)
{
if (isset($this->defer_check_identifiers[$key])) {
unset($this->defer_check_identifiers[$key]);
$idx = array_search($identifier, $this->unknow_identifiers, true);
if ($idx !== false) {
unset($this->unknow_identifiers[$idx]);
}
}

public function append_defer_check_identifiers(IDeclaration $declaration)
public function append_unknow_identifiers_from_declaration(IDeclaration $decl)
{
if (!$declaration->defer_check_identifiers) {
if (!$decl->unknow_identifiers) {
return;
}

$this->defer_check_identifiers = array_merge(
$this->defer_check_identifiers,
$declaration->defer_check_identifiers
$this->unknow_identifiers = array_merge(
$this->unknow_identifiers,
$decl->unknow_identifiers
);
}
}
Expand Down
18 changes: 13 additions & 5 deletions compiler/ast/declaration/ClassKindredDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,23 @@ public function find_based_with_symbol(Symbol $symbol)

// check the implements interfaces
foreach ($this->bases as $based) {
$based_symbol = $based->symbol;
if ($based_symbol === $symbol || $based_symbol->declaration->find_based_with_symbol($symbol) ) {
if ($this->is_identifier_based_with_symbol($based, $symbol)) {
$result = $based;
break;
}
}

return $result;
}

private static function is_identifier_based_with_symbol(PlainIdentifier $based, Symbol $symbol)
{
$based_symbol = $based->symbol;
$is = $based_symbol === $symbol
|| $based_symbol->declaration->find_based_with_symbol($symbol);

return $is;
}
}

class ClassDeclaration extends ClassKindredDeclaration implements ICallableDeclaration
Expand All @@ -127,7 +135,7 @@ class ClassDeclaration extends ClassKindredDeclaration implements ICallableDecla
public function find_based_with_symbol(Symbol $symbol)
{
// check the extends class first
if ($this->inherits !== null and $result = $this->find_based_with_symbol_in_inherits($this->inherits, $symbol)) {
if ($this->inherits and $result = $this->find_based_with_symbol_in_inherits($this->inherits, $symbol)) {
// no any
}
else {
Expand All @@ -137,12 +145,12 @@ public function find_based_with_symbol(Symbol $symbol)
return $result;
}

private function find_based_with_symbol_in_inherits($inherits, $symbol)
private function find_based_with_symbol_in_inherits(PlainIdentifier $inherits, Symbol $symbol)
{
$inherits_symbol = $inherits->symbol;
$result = null;

// 当引用的unit中类所继承类 和 当前引用的类 为同一个第三方unit的类时,symbol会有所不同,这时需比较declaration
// symbol in difference packages would be not same, but declaration is
if ($inherits_symbol === $symbol || $inherits_symbol->declaration === $symbol->declaration) {
$result = $inherits;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/coder/BaseCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ public function render_function_declaration(FunctionDeclaration $node)

// public function render_function_block(FunctionBlock $node)
// {
// $declaration = $this->generate_function_header($node);
// $decl = $this->generate_function_header($node);
// $body = $this->render_function_body($node);

// return "{$declaration} $body";
// return "{$decl} $body";
// }

public function render_anonymous_function(AnonymousFunction $node)
Expand Down
Loading

0 comments on commit 8e91285

Please sign in to comment.