From 4b26a910af273e6c7882250465aa99d645f32949 Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Sun, 14 Feb 2021 14:30:22 -0800 Subject: [PATCH] Have the compiler passed the entire set of documents + state to facilitate moving the higher-level compilation logic into the compiler We're moving a bunch of the compilation logic building up inside Engine into the compiler --- app/Engine/Cfnpp.php | 70 +++++++++++++++-------------------------- app/Engine/ICompile.php | 23 +++----------- 2 files changed, 29 insertions(+), 64 deletions(-) diff --git a/app/Engine/Cfnpp.php b/app/Engine/Cfnpp.php index 88d1fd5..185a784 100644 --- a/app/Engine/Cfnpp.php +++ b/app/Engine/Cfnpp.php @@ -16,12 +16,6 @@ use App\Dom\NodeFunction; */ class Cfnpp implements ICompile { - /** - * Current document state. - * @var Document - */ - protected $document; - /** * Functions that can be called by the document. * @@ -44,20 +38,6 @@ class Cfnpp implements ICompile */ protected $merge_functions; - public function __construct(IOptions $options) - { - if (!($options instanceof CfnppOptions)) - { - throw new \Exception('Cfnpp compiler requires CfnppOptions'); - } - - $this->document = new Document(); - $this->functions = []; - - $cfnpp_functions = new CfnppFunctions($this, $options); - $cfnpp_functions->register($this); - } - /** * Register a function that can be called by the document. * @@ -84,37 +64,37 @@ class Cfnpp implements ICompile } /** - * Set the current state that new compilations will be applied to. + * Compile a set of documents and return the result. * - * @param Document $document + * @param Document[] $documents + * @param IOptions $options * @return void */ - public function setDocument(Document $document): void + public function compile(array $documents, IOptions $options): Document { - $this->document = $document; - } + if (!($options instanceof CfnppOptions)) + { + throw new \Exception('Cfnpp compiler requires CfnppOptions'); + } - /** - * Get the document representing the current state. - * - * @return Document - */ - public function getDocument(): Document - { - return $this->document; - } + // Initialize state + $document = new Document(); + $this->functions = []; + $this->merge_functions = []; - /** - * Apply a new document to the current state. - * - * @param Document $document - * @return void - */ - public function compile(Document $document): void - { - $this->runMergeFunctions($this->document, $document); - $this->merge($this->document, $document); - $this->runFunctions($this->document); + // Register built-in functions + $cfnpp_functions = new CfnppFunctions($this, $options); + $cfnpp_functions->register($this); + + // Process each passed document + foreach ($documents as $next_document) + { + $this->runMergeFunctions($document, $next_document); + $this->merge($document, $next_document); + $this->runFunctions($document); + } + + return $document; } /** diff --git a/app/Engine/ICompile.php b/app/Engine/ICompile.php index 8ca3746..d4e6868 100644 --- a/app/Engine/ICompile.php +++ b/app/Engine/ICompile.php @@ -12,26 +12,11 @@ namespace App\Engine; interface ICompile { /** - * Set the base document all our compilations are run against. + * Given a set of documents, perform all compilation steps and return the result. * - * @param \App\Dom\Document $document + * @param \App\Dom\Document[] $documents documents to compile + * @param IOptions $options * @return void */ - public function setDocument(\App\Dom\Document $document): void; - - /** - * Perform transform process on the document held by this class, - * using the one passed in as instructions. - * - * @param \App\Dom\Document $document document to compile - * @return void - */ - public function compile(\App\Dom\Document $document): void; - - /** - * Retrieve the current result. - * - * @return \App\Dom\Document - */ - public function getDocument(): \App\Dom\Document; + public function compile(array $documents, IOptions $options): \App\Dom\Document; }