Hoa central
Public Member Functions | List of all members
Hoa\Praspel\Visitor\Compiler Class Reference
Inheritance diagram for Hoa\Praspel\Visitor\Compiler:

Public Member Functions

 visit (Visitor\Element $element, &$handle=null, $eldnah=null)
 
- Public Member Functions inherited from Hoa\Visitor\Visit
 visit (Element $element, &$handle=null, $eldnah=null)
 

Detailed Description

Class .

Compile the model to PHP code.

Definition at line 51 of file Compiler.php.

Member Function Documentation

Hoa\Praspel\Visitor\Compiler::visit ( Visitor\Element  $element,
$handle = null,
  $eldnah = null 
)

Visit an element.

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed

Definition at line 61 of file Compiler.php.

65  {
66  $out = null;
67 
68  // Hoa\Praspel.
69 
70  if ($element instanceof Praspel\Model\Specification) {
71  $variable = '$' . $element->getId();
72  $out = $variable . ' = new \Hoa\Praspel\Model\Specification();' . "\n";
73 
74  foreach ($element::getAllowedClauses() as $clause) {
75  if (true === $element->clauseExists($clause)) {
76  $out .= $element->getClause($clause)->accept(
77  $this,
78  $handle,
79  $eldnah
80  );
81  }
82  }
83  } elseif ($element instanceof Praspel\Model\Is) {
84  $variable = '$' . $element->getParent()->getId();
85  $out =
86  "\n" .
87  $variable . '->getClause(\'is\')->setProperty(' .
88  $element->getProperty() .
89  ');' . "\n";
90  } elseif ($element instanceof Praspel\Model\Declaration) {
91  $variable = '$' . ($eldnah ?: $element->getId());
92  $out =
93  "\n" .
94  $variable . ' = $' . $element->getParent()->getId() .
95  '->getClause(\'' . $element->getName() . '\');' . "\n";
96 
97  foreach ($element->getLocalVariables() as $var) {
98  $out .= $var->accept($this, $handle, $eldnah);
99  }
100 
101  foreach ($element->getPredicates() as $predicate) {
102  $out .= $variable . '->predicate(\'' . $predicate . '\');' . "\n";
103  }
104  } elseif ($element instanceof Praspel\Model\Variable) {
105  $variable = '$' . ($eldnah ?: $element->getClause()->getId());
106  $name = $element->getName();
107  $start = $variable . '[\'' . $name . '\']';
108 
109  if (true === $element->isLocal()) {
110  $out .= $variable . '->let[\'' . $name . '\']';
111  } else {
112  $out .= $start;
113  }
114 
115  if (null !== $alias = $element->getAlias()) {
116  $out .= '->domainof(\'' . $alias . '\');' . "\n";
117  } else {
118  $out .=
119  '->in = ' .
120  $element->getDomains()->accept($this, $handle, $eldnah) .
121  ';' . "\n";
122  }
123 
124  $constraints = $element->getConstraints();
125 
126  if (isset($constraints['is'])) {
127  $out .=
128  $start . '->is(\'' .
129  implode('\', \'', $constraints['is']) . '\');' .
130  "\n";
131  }
132 
133  if (isset($constraints['contains'])) {
134  foreach ($constraints['contains'] as $contains) {
135  $out .= $start . '->contains(' . $contains . ');' . "\n";
136  }
137  }
138 
139  if (isset($constraints['key'])) {
140  foreach ($constraints['key'] as $pairs) {
141  $out .=
142  $start . '->key(' . $pairs[0] . ')->in = ' .
143  $pairs[1] . ';' . "\n";
144  }
145  }
146  } elseif ($element instanceof Praspel\Model\Throwable) {
147  $parent = '$' . $element->getParent()->getId();
148  $_variable = $element->getId();
149  $variable = '$' . $_variable;
150  $out =
151  "\n" .
152  $variable . ' = ' . $parent .
153  '->getClause(\'throwable\');' . "\n";
154 
155  foreach ($element as $identifier) {
156  $exception = $element[$identifier];
157  $start = $variable . '[\'' . $identifier . '\']';
158  $out .= $start . ' = \'' . $exception->getInstanceName() . '\';' . "\n";
159 
160  if (false === $element->isDisjointed()) {
161  if (null !== $with = $element->getWith()) {
162  $temp = $_variable . '_' . $identifier . '_with';
163  $out .=
164  '$' . $temp . ' = ' .
165  $variable . '->newWith();' . "\n";
166 
167  foreach ($with->getLocalVariables() as $var) {
168  $out .= $var->accept($this, $handle, $temp);
169  }
170 
171  foreach ($with->getPredicates() as $predicate) {
172  $out .=
173  '$' . $temp . '->predicate(\'' . $predicate .
174  '\');' . "\n";
175  }
176 
177  $out .= $start . '->setWith($' . $temp . ');' . "\n";
178  }
179  } else {
180  $out .=
181  $start . '->disjunctionWith(\'' .
182  $exception->getDisjunction() . '\');' . "\n";
183  }
184  }
185  } elseif ($element instanceof Praspel\Model\DefaultBehavior) {
186  $out =
187  "\n" .
188  '$' . $element->getId() . ' = $' .
189  $element->getParent()->getId() .
190  '->getClause(\'default\')' . "\n";
191 
192  foreach ($element::getAllowedClauses() as $clause) {
193  if (true === $element->clauseExists($clause)) {
194  $out .= $element->getClause($clause)->accept(
195  $this,
196  $handle,
197  $eldnah
198  );
199  }
200  }
201  } elseif ($element instanceof Praspel\Model\Behavior) {
202  $out =
203  "\n" .
204  '$' . $element->getId() . ' = $' .
205  $element->getParent()->getId() .
206  '->getClause(\'behavior\')' .
207  '->get(\'' . $element->getIdentifier() . '\');' . "\n";
208 
209  foreach ($element::getAllowedClauses() as $clause) {
210  if (true === $element->clauseExists($clause)) {
211  $out .= $element->getClause($clause)->accept(
212  $this,
213  $handle,
214  $eldnah
215  );
216  }
217  }
218  } elseif ($element instanceof Praspel\Model\Description) {
219  $parent = '$' . $element->getParent()->getId();
220  $variable = '$' . $element->getId();
221  $out =
222  "\n" .
223  $variable . ' = ' . $parent .
224  '->getClause(\'description\');' . "\n";
225 
226  foreach ($element as $example) {
227  $out .=
228  $variable . '[] = \'' .
229  preg_replace('#(?<!\\\)\'#', '\\\'', $example) .
230  '\';' . "\n";
231  }
232  } elseif ($element instanceof Praspel\Model\Collection) {
233  foreach ($element as $el) {
234  $out .= $el->accept($this, $handle, $eldnah);
235  }
236  }
237 
238  // Hoa\Realdom.
239 
240  elseif ($element instanceof Realdom\Disjunction) {
241  $realdoms = $element->getUnflattenedRealdoms();
242 
243  if (!empty($realdoms)) {
244  $oout = [];
245 
246  foreach ($realdoms as $realdom) {
247  if ($realdom instanceof Realdom\IRealdom\Constant) {
248  $oout[] =
249  'const(' .
250  $realdom->accept($this, $handle, $eldnah) .
251  ')';
252  } else {
253  $oout[] = $realdom->accept($this, $handle, $eldnah);
254  }
255  }
256 
257  $out .= 'realdom()->' . implode('->or->', $oout);
258  }
259  } elseif ($element instanceof Realdom) {
260  if ($element instanceof Realdom\IRealdom\Constant) {
261  if ($element instanceof Realdom\_Array) {
262  $oout = [];
263 
264  foreach ($element['pairs'] as $pair) {
265  $_oout = null;
266 
267  foreach ($pair as $_pair) {
268  if (null !== $_oout) {
269  $_oout .= ', ';
270  }
271 
272  $_oout .= $_pair->accept($this, $handle, $eldnah);
273  }
274 
275  $oout[] = 'array(' . $_oout . ')';
276  }
277 
278  $out .= 'array(' . implode(', ', $oout) . ')';
279  } else {
280  $out .= $element->getConstantRepresentation();
281  }
282  } else {
283  $oout = [];
284 
285  foreach ($element->getArguments() as $argument) {
286  $oout[] = $argument->accept($this, $handle, $eldnah);
287  }
288 
289  $out .=
290  $element->getName() .
291  '(' . implode(', ', $oout) . ')';
292  }
293  } elseif ($element instanceof Realdom\Crate\Constant) {
294  $holder = $element->getHolder();
295  $praspel = $element->getPraspelRepresentation();
296  $out .=
297  '$' . $element->getDeclaration()->getId() .
298  '[\'' . $praspel() . '\']';
299  } elseif ($element instanceof Realdom\Crate\Variable) {
300  $holder = $element->getVariable();
301 
302  if ($holder instanceof Praspel\Model\Variable\Implicit) {
303  $out .=
304  'variable($' . $holder->getClause()->getId() .
305  '->getImplicitVariable(\'' . $holder->getName() .
306  '\'))';
307  } else {
308  $out .=
309  'variable($' . $holder->getClause()->getId() .
310  '->getVariable(\'' . $holder->getName() . '\', true))';
311  }
312  } else {
313  throw new Praspel\Exception\Compiler(
314  '%s is not yet implemented.',
315  0,
316  get_class($element)
317  );
318  }
319 
320  return $out;
321  }

The documentation for this class was generated from the following file: