75 lines
1.1 KiB
PHP
75 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Program;
|
|
|
|
use RuntimeException;
|
|
|
|
class Block
|
|
{
|
|
public string $N;
|
|
/** @var Word[] */
|
|
public array $words;
|
|
|
|
|
|
public function __construct(string $N)
|
|
{
|
|
$this->N = $N;
|
|
$this->words = [];
|
|
}
|
|
|
|
|
|
public function addWord(Word $word): void
|
|
{
|
|
$this->words[] = $word;
|
|
}
|
|
|
|
|
|
public function has(string $register): bool
|
|
{
|
|
foreach ($this->words as $word) {
|
|
if ($word->register === $register) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public function read(string $register): int
|
|
{
|
|
foreach ($this->words as $index => $word) {
|
|
if ($word->register === $register) {
|
|
return $word->value;
|
|
}
|
|
}
|
|
|
|
throw new RuntimeException("No word in block for register '$register'");
|
|
}
|
|
|
|
|
|
public function pop(string $register): int
|
|
{
|
|
foreach ($this->words as $index => $word) {
|
|
if ($word->register === $register) {
|
|
unset($this->words[$index]);
|
|
|
|
return $word->value;
|
|
}
|
|
}
|
|
|
|
throw new RuntimeException("No word in block for register '$register'");
|
|
}
|
|
|
|
|
|
public function __toString()
|
|
{
|
|
$buffer = "N$this->N\t\t";
|
|
foreach ($this->words as $word) {
|
|
$buffer .= "$word\t";
|
|
}
|
|
|
|
return $buffer;
|
|
}
|
|
}
|