-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from m3m0r7/add-object-and-regexp
Implementation of Hash and Regexp
- Loading branch information
Showing
24 changed files
with
468 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\Attribute; | ||
|
||
#[\Attribute] | ||
class BindAliasAs {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject; | ||
|
||
use RubyVM\VM\Core\Runtime\Entity\Class_; | ||
use RubyVM\VM\Core\Runtime\Entity\EntityInterface; | ||
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface; | ||
use RubyVM\VM\Core\YARV\Criterion\ShouldBeRubyClass; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol; | ||
|
||
abstract class BasicObject implements RubyClassInterface | ||
{ | ||
use ShouldBeRubyClass; | ||
|
||
protected ?EntityInterface $entity = null; | ||
|
||
public function entity(): EntityInterface | ||
{ | ||
return $this->entity ??= Class_::createBy(new StringSymbol((string) $this)); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$classNames = explode('\\', static::class); | ||
|
||
return array_pop($classNames); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel; | ||
|
||
use RubyVM\VM\Core\Runtime\BasicObject\BasicObject; | ||
|
||
abstract class Kernel extends BasicObject {} |
9 changes: 9 additions & 0 deletions
9
src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/Enumerable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable; | ||
|
||
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Object_; | ||
|
||
abstract class Enumerable extends Object_ {} |
36 changes: 36 additions & 0 deletions
36
src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/Hash.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable; | ||
|
||
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface; | ||
|
||
class Hash extends Enumerable implements RubyClassInterface, \ArrayAccess | ||
{ | ||
/** | ||
* @param array<SymbolInterface> $hash | ||
*/ | ||
public function __construct(protected array $hash = []) {} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return array_key_exists($offset, $this->hash); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->hash[$offset] ?? null; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
$this->hash[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
unset($this->hash[$offset]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_; | ||
|
||
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Kernel; | ||
|
||
abstract class Object_ extends Kernel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RubyVM\VM\Core\Runtime\Entity; | ||
|
||
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs; | ||
use RubyVM\VM\Core\YARV\Criterion\InstructionSequence\CallInfoInterface; | ||
use RubyVM\VM\Core\YARV\Essential\Symbol\RegExpSymbol; | ||
|
||
class RegExp extends Entity implements EntityInterface | ||
{ | ||
public function __construct(RegExpSymbol $symbol) | ||
{ | ||
$this->symbol = $symbol; | ||
} | ||
|
||
public static function createBy(mixed $value = null, int $option = null): EntityInterface | ||
{ | ||
return new self(new RegExpSymbol($value, $option)); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
// TODO: Convert Ruby regexp to PCRE | ||
return '/' . $this->symbol->valueOf()->valueOf() . '/'; | ||
} | ||
|
||
/** | ||
* @see https://docs.ruby-lang.org/ja/latest/class/Regexp.html#I_--3D--7E | ||
*/ | ||
#[BindAliasAs('=~')] | ||
public function equalsTilde(CallInfoInterface $callInfo, String_|Nil $source): Number|Nil | ||
{ | ||
if ($source instanceof Nil) { | ||
return Nil::createBy(); | ||
} | ||
|
||
preg_match( | ||
(string) $this, | ||
(string) $source, | ||
$match, | ||
PREG_OFFSET_CAPTURE, | ||
); | ||
|
||
if ($match === []) { | ||
return Nil::createBy(); | ||
} | ||
|
||
[, $offset] = $match[0]; | ||
|
||
return Number::createBy($offset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.