Browse Source

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
master
Adam Pippin 3 years ago
parent
commit
4b26a910af
  1. 70
      app/Engine/Cfnpp.php
  2. 23
      app/Engine/ICompile.php

70
app/Engine/Cfnpp.php

@ -16,12 +16,6 @@ use App\Dom\NodeFunction;
*/ */
class Cfnpp implements ICompile class Cfnpp implements ICompile
{ {
/**
* Current document state.
* @var Document
*/
protected $document;
/** /**
* Functions that can be called by the document. * Functions that can be called by the document.
* *
@ -44,20 +38,6 @@ class Cfnpp implements ICompile
*/ */
protected $merge_functions; 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. * 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 * @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');
}
/** // Initialize state
* Get the document representing the current state. $document = new Document();
* $this->functions = [];
* @return Document $this->merge_functions = [];
*/
public function getDocument(): Document
{
return $this->document;
}
/** // Register built-in functions
* Apply a new document to the current state. $cfnpp_functions = new CfnppFunctions($this, $options);
* $cfnpp_functions->register($this);
* @param Document $document
* @return void // Process each passed document
*/ foreach ($documents as $next_document)
public function compile(Document $document): void {
{ $this->runMergeFunctions($document, $next_document);
$this->runMergeFunctions($this->document, $document); $this->merge($document, $next_document);
$this->merge($this->document, $document); $this->runFunctions($document);
$this->runFunctions($this->document); }
return $document;
} }
/** /**

23
app/Engine/ICompile.php

@ -12,26 +12,11 @@ namespace App\Engine;
interface ICompile 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 * @return void
*/ */
public function setDocument(\App\Dom\Document $document): void; public function compile(array $documents, IOptions $options): \App\Dom\Document;
/**
* 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;
} }

Loading…
Cancel
Save