Fix parser error that made it ignore the last line

This commit is contained in:
Joop Schilder 2020-05-10 21:27:50 +02:00
parent fda22052b8
commit e2bb39006a
3 changed files with 8 additions and 6 deletions

View File

@ -20,6 +20,7 @@ class InstructionSet
public function apply(Block $block): void public function apply(Block $block): void
{ {
print("$block\n");
$words = $block->words; $words = $block->words;
while ($word = array_shift($words)) { while ($word = array_shift($words)) {
$method = "{$word}"; $method = "{$word}";
@ -32,7 +33,7 @@ class InstructionSet
} }
public function G22(Block $block): void protected function G22(Block $block): void
{ {
$number = $block->valueOf('X'); $number = $block->valueOf('X');
$this->machine->SPMC->loadProgram($number); $this->machine->SPMC->loadProgram($number);

View File

@ -40,12 +40,10 @@ class Machine
public function step(): void public function step(): void
{ {
$block = $this->AMC->next(); if ($this->AMC === $this->SPMC && count($this->AMC) === 0) {
if (is_null($block) && $this->AMC === $this->SPMC) {
// SP ended, back to PP
$this->AMC = $this->PPMC; $this->AMC = $this->PPMC;
$block = $this->AMC->next();
} }
$block = $this->AMC->next();
$this->IS->apply($block); $this->IS->apply($block);
} }

View File

@ -61,7 +61,7 @@ class ProgramParser
} }
$program = new Program($firstWord->value); $program = new Program($firstWord->value);
$lineBuffer = null; $lineBuffer = null;
foreach ($words as $word) { while ($word = array_shift($words)) {
if ($word->register === 'N') { if ($word->register === 'N') {
if (!is_null($lineBuffer)) { if (!is_null($lineBuffer)) {
$program->addBlock($lineBuffer); $program->addBlock($lineBuffer);
@ -71,6 +71,9 @@ class ProgramParser
} }
$lineBuffer->addWord($word); $lineBuffer->addWord($word);
} }
if (!is_null($lineBuffer)) {
$program->addBlock($lineBuffer);
}
return $program; return $program;
} }