Skip to content

Commit

Permalink
Implement proc
Browse files Browse the repository at this point in the history
  • Loading branch information
m3m0r7 committed Sep 25, 2023
1 parent 36bac06 commit 412851b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/VM/Core/Runtime/Executor/Debugger/DebugFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RubyVM\VM\Core\Runtime\Executor\Debugger;

use RubyVM\VM\Core\Helper\ClassHelper;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\Runtime\Executor\Operation\Operand;
use RubyVM\VM\Core\Runtime\RubyClass;

Expand All @@ -16,9 +17,13 @@ public function __toString(): string

$result = [];
foreach ($targetItems as $index => $item) {
if ($item instanceof RubyClassInterface) {
$result[] = ClassHelper::nameBy($item->entity()) . "({$item->entity()})";
continue;
}
$result[] = match ($item::class) {
Operand::class => match (($item->operand)::class) {
RubyClass::class => ClassHelper::nameBy($item->operand->entity) . "({$item->operand->entity})",
RubyClass::class => ClassHelper::nameBy($item->operand->entity()) . "({$item->operand->entity()})",
default => ClassHelper::nameBy($item->operand),
},
default => 'unknown',
Expand Down
4 changes: 2 additions & 2 deletions src/VM/Core/Runtime/Executor/EnvironmentTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace RubyVM\VM\Core\Runtime\Executor;

use RubyVM\VM\Core\Criterion\Entry\AbstractEntries;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\Runtime\Executor\Debugger\DebugFormat;
use RubyVM\VM\Core\Runtime\RubyClass;
use RubyVM\VM\Exception\LocalTableException;

class EnvironmentTable extends AbstractEntries
Expand All @@ -20,7 +20,7 @@ class EnvironmentTable extends AbstractEntries

public function verify(mixed $value): bool
{
return $value instanceof RubyClass;
return $value instanceof RubyClassInterface;
}

public function get(mixed $index): mixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function process(ContextInterface|RubyClassInterface ...$arguments): Proc
...$this->translateForArguments(...$arguments),
);

if ($result instanceof \RubyVM\VM\Core\Runtime\RubyClass) {
if ($result instanceof RubyClassInterface) {
$this->context->vmStack()->push(new Operand($result));
}

Expand Down
73 changes: 73 additions & 0 deletions src/VM/Core/Runtime/Lambda.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace RubyVM\VM\Core\Runtime;

use RubyVM\VM\Core\Runtime\Entity\Class_;
use RubyVM\VM\Core\Runtime\Entity\EntityInterface;
use RubyVM\VM\Core\Runtime\Essential\MainInterface;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface;
use RubyVM\VM\Core\Runtime\Executor\Executor;
use RubyVM\VM\Core\YARV\Criterion\InstructionSequence\InstructionSequenceInterface;
use RubyVM\VM\Core\YARV\Criterion\ShouldBeRubyClass;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;

class Lambda implements MainInterface
{
use ShouldBeRubyClass;

protected ?EntityInterface $entity = null;

public function __construct(private InstructionSequenceInterface $instructionSequence)
{
}

public function entity(): EntityInterface
{
return $this->entity ??= Class_::createBy(new StringSymbol('lambda'));
}

public function __toString(): string
{
return 'lambda';
}

public function call(RubyClassInterface ...$arguments): RubyClassInterface|null
{
$executor = new Executor(
kernel: $this->context->kernel(),

Check failure on line 40 in src/VM/Core/Runtime/Lambda.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method kernel() on RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface|null.
rubyClass: $this->context->self(),

Check failure on line 41 in src/VM/Core/Runtime/Lambda.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method self() on RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface|null.
instructionSequence: $this->instructionSequence,

Check failure on line 42 in src/VM/Core/Runtime/Lambda.php

View workflow job for this annotation

GitHub Actions / lint

Parameter $instructionSequence of class RubyVM\VM\Core\Runtime\Executor\Executor constructor expects RubyVM\VM\Core\YARV\Criterion\InstructionSequence\InstructionSequence, RubyVM\VM\Core\YARV\Criterion\InstructionSequence\InstructionSequenceInterface given.
option: $this->context->option(),

Check failure on line 43 in src/VM/Core/Runtime/Lambda.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method option() on RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface|null.
debugger: $this->context->debugger(),

Check failure on line 44 in src/VM/Core/Runtime/Lambda.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method debugger() on RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface|null.
previousContext: $this->context,
);

$localTableSize = $this
->instructionSequence
->body()
->info()
->localTableSize();

$argumentSize = count($arguments);
for ($i = 0; $i < $argumentSize; $i++) {
$executor
->context()
->environmentTable()
->set(
Option::VM_ENV_DATA_SIZE + $i,
$arguments[$i],
);
}

$result = $executor->execute();

if ($result->threw) {
throw $result->threw;
}

return $result->returnValue;
}
}
10 changes: 10 additions & 0 deletions src/VM/Core/Runtime/Provider/ProvideBasicClassMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use RubyVM\VM\Core\Runtime\Entity\Nil;
use RubyVM\VM\Core\Runtime\Entity\String_;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface;
use RubyVM\VM\Core\Runtime\Lambda;
use RubyVM\VM\Core\Runtime\UserlandHeapSpace;
use RubyVM\VM\Core\YARV\Essential\Symbol\ArraySymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\NilSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\RangeSymbol;
Expand Down Expand Up @@ -56,4 +59,11 @@ public function inspect(): RubyClassInterface
return String_::createBy($string)
->toBeRubyClass();
}

public function lambda(ContextInterface $context): RubyClassInterface
{
return (new Lambda($context->instructionSequence()))
->setRuntimeContext($this->context)
->setUserlandHeapSpace(new UserlandHeapSpace());
}
}
2 changes: 1 addition & 1 deletion src/VM/Core/Runtime/Provider/ProvideExtendedMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait ProvideExtendedMethodCall
use CallBlockHelper;

/**
* @param (ContextInterface|RubyClass)[] $arguments
* @param (ContextInterface|RubyClassInterface)[] $arguments
*/
public function __call(string $name, array $arguments): ExecutedResult|RubyClassInterface|null
{
Expand Down

0 comments on commit 412851b

Please sign in to comment.