From e2bb39006ab8a50dea42e17e8bed686d889a353b Mon Sep 17 00:00:00 2001 From: Joop Schilder Date: Sun, 10 May 2020 21:27:50 +0200 Subject: [PATCH] Fix parser error that made it ignore the last line --- src/Automata/InstructionSet.php | 3 ++- src/Automata/Machine.php | 6 ++---- src/Program/ProgramParser.php | 5 ++++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Automata/InstructionSet.php b/src/Automata/InstructionSet.php index a094953..8b4cef5 100644 --- a/src/Automata/InstructionSet.php +++ b/src/Automata/InstructionSet.php @@ -20,6 +20,7 @@ class InstructionSet public function apply(Block $block): void { + print("$block\n"); $words = $block->words; while ($word = array_shift($words)) { $method = "{$word}"; @@ -32,7 +33,7 @@ class InstructionSet } - public function G22(Block $block): void + protected function G22(Block $block): void { $number = $block->valueOf('X'); $this->machine->SPMC->loadProgram($number); diff --git a/src/Automata/Machine.php b/src/Automata/Machine.php index 3cadab1..34e2259 100644 --- a/src/Automata/Machine.php +++ b/src/Automata/Machine.php @@ -40,12 +40,10 @@ class Machine public function step(): void { - $block = $this->AMC->next(); - if (is_null($block) && $this->AMC === $this->SPMC) { - // SP ended, back to PP + if ($this->AMC === $this->SPMC && count($this->AMC) === 0) { $this->AMC = $this->PPMC; - $block = $this->AMC->next(); } + $block = $this->AMC->next(); $this->IS->apply($block); } diff --git a/src/Program/ProgramParser.php b/src/Program/ProgramParser.php index 88c5d55..7d83f82 100644 --- a/src/Program/ProgramParser.php +++ b/src/Program/ProgramParser.php @@ -61,7 +61,7 @@ class ProgramParser } $program = new Program($firstWord->value); $lineBuffer = null; - foreach ($words as $word) { + while ($word = array_shift($words)) { if ($word->register === 'N') { if (!is_null($lineBuffer)) { $program->addBlock($lineBuffer); @@ -71,6 +71,9 @@ class ProgramParser } $lineBuffer->addWord($word); } + if (!is_null($lineBuffer)) { + $program->addBlock($lineBuffer); + } return $program; }