Skip to content

Commit

Permalink
Small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Jul 31, 2023
1 parent 4d4da8c commit 2acffe8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
68 changes: 22 additions & 46 deletions src/Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
use GoPhp\GoValue\Map\MapBuilder;
use GoPhp\GoValue\Map\MapLookupValue;
use GoPhp\GoValue\PointerValue;
use GoPhp\GoValue\RecoverableInvokable;
use GoPhp\GoValue\Sequence;
use GoPhp\GoValue\Slice\SliceBuilder;
use GoPhp\GoValue\Slice\SliceValue;
Expand Down Expand Up @@ -125,7 +124,6 @@
final class Interpreter
{
private static None $noneJump;

private Ast $ast;
private Iota $iota;
private PanicPointer $panicPointer;
Expand All @@ -151,15 +149,12 @@ final class Interpreter
*/
public function __construct(
string $source,
?BuiltinProvider $builtin = null,
array $argv = [],
?BuiltinProvider $builtin = null,
?ErrorHandler $errorHandler = null,
StreamProvider $streams = new StdStreamProvider(),
FuncTypeValidator $entryPointValidator = new ZeroArityValidator(
DEFAULT_ENTRY_POINT_FUNC_NAME,
DEFAULT_ENTRY_POINT_PACK_NAME
),
FuncTypeValidator $initValidator = new ZeroArityValidator(DEFAULT_INITIALIZER_FUNC_NAME),
FuncTypeValidator $entryPointValidator = new ZeroArityValidator(ENTRY_POINT_FUNC, ENTRY_POINT_PACKAGE),
FuncTypeValidator $initValidator = new ZeroArityValidator(INITIALIZER_FUNC),
EnvVarSet $envVars = new EnvVarSet(),
bool $toplevel = false,
) {
Expand Down Expand Up @@ -217,7 +212,7 @@ public function run(): ExitCode
);
}

$call = new InvokableCall($this->entryPoint, $this->argv);
$call = new InvokableCall($this->entryPoint, Argv::fromEmpty());
$this->callFunc($call);
} catch (RuntimeError|PanicError $error) {
$this->errorHandler->onError($error);
Expand All @@ -236,51 +231,34 @@ private function evalDeclsInOrder(): void
}

$mapping = [
TypeDecl::class => [],
ConstDecl::class => [],
VarDecl::class => [],
TypeDecl::class => [],
FuncDecl::class => [],
MethodDecl::class => [],
];

foreach ($this->ast->decls as $decl) {
$key = match (true) {
$decl instanceof ConstDecl,
$decl instanceof VarDecl,
$decl instanceof TypeDecl,
$decl instanceof FuncDecl,
$decl instanceof MethodDecl, => $decl::class,
default => throw RuntimeError::nonDeclarationOnTopLevel(),
};
if (!isset($mapping[$decl::class])) {
throw RuntimeError::nonDeclarationOnTopLevel();
}

$mapping[$key][] = $decl;
$mapping[$decl::class][] = $decl;
}

$this->scopeResolver->enterPackageScope();

foreach ($mapping[TypeDecl::class] as $decl) {
/** @var TypeDecl $decl */
$this->evalTypeDeclStmt($decl);
}

foreach ($mapping[ConstDecl::class] as $decl) {
/** @var ConstDecl $decl */
$this->evalConstDeclStmt($decl);
}

foreach ($mapping[VarDecl::class] as $decl) {
/** @var VarDecl $decl */
$this->evalVarDeclStmt($decl);
}

foreach ($mapping[FuncDecl::class] as $decl) {
/** @var FuncDecl $decl */
$this->evalFuncDeclStmt($decl);
}

foreach ($mapping[MethodDecl::class] as $decl) {
/** @var MethodDecl $decl */
$this->evalMethodDeclStmt($decl);
foreach ($mapping as $decls) {
foreach ($decls as $decl) {
/** @psalm-suppress all */
(match ($decl::class) {
TypeDecl::class => $this->evalTypeDeclStmt(...),
ConstDecl::class => $this->evalConstDeclStmt(...),
VarDecl::class => $this->evalVarDeclStmt(...),
FuncDecl::class => $this->evalFuncDeclStmt(...),
MethodDecl::class => $this->evalMethodDeclStmt(...),
})($decl);
}
}

foreach ($this->initializers->get() as $initializer) {
Expand Down Expand Up @@ -655,10 +633,8 @@ private function callFunc(InvokableCall $fn): GoValue
$this->panicPointer->panic = $panic;
$this->releaseDeferredStack();

if ($this->panicPointer->panic === null) {
if ($fn->func instanceof RecoverableInvokable) {
return $fn->func->zeroReturnValue();
}
if ($this->panicPointer->panic === null && ($recover = $fn->tryRecover()) !== null) {
return $recover;
}

throw $panic;
Expand Down
12 changes: 10 additions & 2 deletions src/InvokableCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@

use GoPhp\GoValue\GoValue;
use GoPhp\GoValue\Invokable;
use GoPhp\GoValue\RecoverableInvokable;

final class InvokableCall
{
public function __construct(
public readonly Invokable $func,
public readonly Argv $argv,
private readonly Invokable $func,
private readonly Argv $argv,
) {}

public function __invoke(): GoValue
{
return ($this->func)($this->argv);
}

public function tryRecover(): ?GoValue
{
return $this->func instanceof RecoverableInvokable
? $this->func->zeroReturnValue()
: null;
}
}
6 changes: 3 additions & 3 deletions src/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
*
* @internal
*/
const DEFAULT_ENTRY_POINT_PACK_NAME = 'main';
const ENTRY_POINT_PACKAGE = 'main';

/**
* Default name of the main function
*
* @internal
*/
const DEFAULT_ENTRY_POINT_FUNC_NAME = 'main';
const ENTRY_POINT_FUNC = 'main';

/**
* Default name of the init function
*
* @internal
*/
const DEFAULT_INITIALIZER_FUNC_NAME = 'init';
const INITIALIZER_FUNC = 'init';

/**
* @internal
Expand Down

0 comments on commit 2acffe8

Please sign in to comment.