Prepare for block level program handling

This commit is contained in:
Joop Schilder 2020-05-10 13:55:25 +02:00
parent 96a4705cce
commit ee896160b3
3 changed files with 35 additions and 23 deletions

View File

@ -4,6 +4,7 @@ namespace Automata;
use Automata\Storage\ProgramMemory; use Automata\Storage\ProgramMemory;
use Automata\Storage\ToolMemory; use Automata\Storage\ToolMemory;
use Program\Block;
use Program\Program; use Program\Program;
use Program\Word; use Program\Word;
use RuntimeException; use RuntimeException;
@ -55,26 +56,19 @@ class Machine
throw new RuntimeException('No program loaded'); throw new RuntimeException('No program loaded');
} }
foreach ($this->activeProgram as $block) { foreach ($this->activeProgram as $block) {
/** @var Word[] $words */ $this->executeBlock($block);
$words = $block->words; }
while ($word = array_shift($words)) { }
if ('G22' === $word->toString()) {
$identifier = array_shift($words); private function executeBlock(Block $block): void
if ($identifier->register !== 'X') { {
throw new RuntimeException('Expected X9xxx after G22'); /** @var Word[] $words */
} $words = $block->words;
$subProgram = $this->subProgramMemory->read($identifier->value); while ($word = array_shift($words)) {
$this->programStack->push($subProgram); $this->state->apply($word);
system('clear'); system('clear');
dump($subProgram); dump($this->state);
readline(); readline();
continue;
}
$this->state->apply($word);
system('clear');
dump($this->state);
readline();
}
} }
} }

View File

@ -13,9 +13,7 @@ class ProgramMemory
public function save(Program $program): void public function save(Program $program): void
{ {
if ($program->N > 9998 || $program->N < 9001) { $this->guardInvalidProgramNumber($program->N);
throw new InvalidArgumentException('Program number must be in range 9001 - 9998');
}
$this->memory[$program->N] = $program; $this->memory[$program->N] = $program;
} }
@ -24,4 +22,13 @@ class ProgramMemory
{ {
return @$this->memory[$N]; 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');
}
}
} }

View File

@ -3,6 +3,7 @@
namespace Automata\Storage; namespace Automata\Storage;
use Automata\Tool; use Automata\Tool;
use InvalidArgumentException;
class ToolMemory class ToolMemory
{ {
@ -12,6 +13,7 @@ class ToolMemory
public function save(Tool $tool): void public function save(Tool $tool): void
{ {
$this->guardInvalidToolNumber($tool->T);
$this->memory[$tool->T] = $tool; $this->memory[$tool->T] = $tool;
} }
@ -20,4 +22,13 @@ class ToolMemory
{ {
return @$this->memory[$T]; 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');
}
}
} }