philips-cnc6600-interpreter/src/Program/Block.php

49 lines
680 B
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 valueOf(string $register): int
{
foreach ($this->words as $word) {
if ($word->register === $register) {
return $word->value;
}
}
throw new RuntimeException("No word in block for register '$register'");
}
public function __toString()
{
$buffer = "$this->N\t";
foreach ($this->words as $word) {
$buffer .= "$word\t";
}
return $buffer;
}
}