From ee896160b399757741544c19d8be91d1799f69c7 Mon Sep 17 00:00:00 2001 From: Joop Schilder Date: Sun, 10 May 2020 13:55:25 +0200 Subject: [PATCH] Prepare for block level program handling --- src/Automata/Machine.php | 34 +++++++++++--------------- src/Automata/Storage/ProgramMemory.php | 13 +++++++--- src/Automata/Storage/ToolMemory.php | 11 +++++++++ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Automata/Machine.php b/src/Automata/Machine.php index 8961e71..344e216 100644 --- a/src/Automata/Machine.php +++ b/src/Automata/Machine.php @@ -4,6 +4,7 @@ namespace Automata; use Automata\Storage\ProgramMemory; use Automata\Storage\ToolMemory; +use Program\Block; use Program\Program; use Program\Word; use RuntimeException; @@ -55,26 +56,19 @@ class Machine throw new RuntimeException('No program loaded'); } foreach ($this->activeProgram as $block) { - /** @var Word[] $words */ - $words = $block->words; - while ($word = array_shift($words)) { - if ('G22' === $word->toString()) { - $identifier = array_shift($words); - if ($identifier->register !== 'X') { - throw new RuntimeException('Expected X9xxx after G22'); - } - $subProgram = $this->subProgramMemory->read($identifier->value); - $this->programStack->push($subProgram); - system('clear'); - dump($subProgram); - readline(); - continue; - } - $this->state->apply($word); - system('clear'); - dump($this->state); - readline(); - } + $this->executeBlock($block); + } + } + + private function executeBlock(Block $block): void + { + /** @var Word[] $words */ + $words = $block->words; + while ($word = array_shift($words)) { + $this->state->apply($word); + system('clear'); + dump($this->state); + readline(); } } diff --git a/src/Automata/Storage/ProgramMemory.php b/src/Automata/Storage/ProgramMemory.php index f565302..0261be1 100644 --- a/src/Automata/Storage/ProgramMemory.php +++ b/src/Automata/Storage/ProgramMemory.php @@ -13,9 +13,7 @@ class ProgramMemory public function save(Program $program): void { - if ($program->N > 9998 || $program->N < 9001) { - throw new InvalidArgumentException('Program number must be in range 9001 - 9998'); - } + $this->guardInvalidProgramNumber($program->N); $this->memory[$program->N] = $program; } @@ -24,4 +22,13 @@ class ProgramMemory { 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'); + } + } + } diff --git a/src/Automata/Storage/ToolMemory.php b/src/Automata/Storage/ToolMemory.php index 2ad8c2c..a94d145 100644 --- a/src/Automata/Storage/ToolMemory.php +++ b/src/Automata/Storage/ToolMemory.php @@ -3,6 +3,7 @@ namespace Automata\Storage; use Automata\Tool; +use InvalidArgumentException; class ToolMemory { @@ -12,6 +13,7 @@ class ToolMemory public function save(Tool $tool): void { + $this->guardInvalidToolNumber($tool->T); $this->memory[$tool->T] = $tool; } @@ -20,4 +22,13 @@ class ToolMemory { return @$this->memory[$T]; } + + + private function guardInvalidToolNumber(int $T): void + { + if ($T < 1 || $T > 64) { + throw new InvalidArgumentException('Tool number must be in range 1 - 64'); + } + } + }