From 96a4705cce9f359db9c61f1ac1a3b9ff0338434f Mon Sep 17 00:00:00 2001 From: Joop Schilder Date: Fri, 8 May 2020 12:22:34 +0200 Subject: [PATCH] Add some preparation for sub program execution --- src/Automata/Machine.php | 34 +++++++++++++++++++++++----------- src/Automata/State.php | 1 + 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Automata/Machine.php b/src/Automata/Machine.php index b9bf4aa..8961e71 100644 --- a/src/Automata/Machine.php +++ b/src/Automata/Machine.php @@ -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(); } diff --git a/src/Automata/State.php b/src/Automata/State.php index 0e49c28..215a4b5 100644 --- a/src/Automata/State.php +++ b/src/Automata/State.php @@ -52,6 +52,7 @@ class State $this->workCycleSelection->apply($word->value); // TODO Handle other G commands + return; } if ($word->register === 'M') {