Add some preparation for sub program execution

This commit is contained in:
Joop Schilder 2020-05-08 12:22:34 +02:00
parent e2b8f1d7aa
commit 96a4705cce
2 changed files with 24 additions and 11 deletions

View File

@ -9,6 +9,14 @@ use Program\Word;
use RuntimeException;
use SplStack;
/*
* executing a program:
* - load parts program
* - execute line by line
* - load sub program
* - execute lines from subprogram
* - back to parts program
*/
class Machine
{
public State $state;
@ -47,19 +55,23 @@ class Machine
throw new RuntimeException('No program loaded');
}
foreach ($this->activeProgram as $block) {
// Do we apply a block or a word?
// Answer: depends! Some commands need
// more information from other words.
// TODO: Interpret commands such as preparation of a work cycle
$words = array_map(fn(Word $w) => $w->toString(), $block->words);
$words = implode("\t", $words);
$words = "N{$block->N}\t$words";
foreach ($block->words as $word) {
/** @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');
print("\n $words\n\n");
dump($this->state);
readline();
}

View File

@ -52,6 +52,7 @@ class State
$this->workCycleSelection->apply($word->value);
// TODO Handle other G commands
return;
}
if ($word->register === 'M') {