Refactor, add extensive README.md, add future planning
This commit is contained in:
33
src/Machine/Memory/ProgramMemory.php
Normal file
33
src/Machine/Memory/ProgramMemory.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Machine\Memory;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Program\Program;
|
||||
|
||||
class ProgramMemory
|
||||
{
|
||||
/** @var Program[] */
|
||||
private array $memory = [];
|
||||
|
||||
|
||||
public function write(Program $program): void
|
||||
{
|
||||
$this->guardInvalidProgramNumber($program->N);
|
||||
$this->memory[$program->N] = $program;
|
||||
}
|
||||
|
||||
|
||||
public function read(int $N): ?Program
|
||||
{
|
||||
return @$this->memory[$N];
|
||||
}
|
||||
|
||||
|
||||
private function guardInvalidProgramNumber(int $N): void
|
||||
{
|
||||
if ($N > 9998 || $N < 9001) {
|
||||
throw new InvalidArgumentException('Program number must be in range 9001 - 9998');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user