aboutsummaryrefslogtreecommitdiffstats
path: root/Visitor/Compiler.php
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-28 12:10:30 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-28 12:10:30 +0200
commit0fce3078006928700d7e65660d49f693dc4293d2 (patch)
tree03636cd45b0422fc7558ef9d8c380c30e13b9069 /Visitor/Compiler.php
parentf7801604c0631304547eee00b68ce3174d40ccdc (diff)
downloadPraspel-0fce3078006928700d7e65660d49f693dc4293d2.zip
Praspel-0fce3078006928700d7e65660d49f693dc4293d2.tar.gz
Praspel-0fce3078006928700d7e65660d49f693dc4293d2.tar.bz2
Move to PSR-1-2 & drop PHP5.3 & `from`/`import`.
Diffstat (limited to 'Visitor/Compiler.php')
-rw-r--r--Visitor/Compiler.php350
1 files changed, 172 insertions, 178 deletions
diff --git a/Visitor/Compiler.php b/Visitor/Compiler.php
index eb033ce..ebf4192 100644
--- a/Visitor/Compiler.php
+++ b/Visitor/Compiler.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -34,246 +34,240 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
-namespace {
+namespace Hoa\Praspel\Visitor;
-from('Hoa')
-
-/**
- * \Hoa\Praspel\Exception\Compiler
- */
--> import('Praspel.Exception.Compiler')
-
-/**
- * \Hoa\Visitor\Visit
- */
--> import('Visitor.Visit');
-
-}
-
-namespace Hoa\Praspel\Visitor {
+use Hoa\Praspel;
+use Hoa\Realdom;
+use Hoa\Visitor;
/**
* Class \Hoa\Praspel\Visitor\Compiler.
*
* Compile the model to PHP code.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Compiler implements \Hoa\Visitor\Visit {
-
+class Compiler implements Visitor\Visit
+{
/**
* Visit an element.
*
- * @access public
* @param \Hoa\Visitor\Element $element Element to visit.
* @param mixed &$handle Handle (reference).
* @param mixed $eldnah Handle (not reference).
* @return mixed
*/
- public function visit ( \Hoa\Visitor\Element $element,
- &$handle = null, $eldnah = null ) {
-
+ public function visit(
+ Visitor\Element $element,
+ &$handle = null,
+ $eldnah = null
+ ) {
$out = null;
// Hoa\Praspel.
- if($element instanceof \Hoa\Praspel\Model\Specification) {
-
+ if ($element instanceof Praspel\Model\Specification) {
$variable = '$' . $element->getId();
$out = $variable . ' = new \Hoa\Praspel\Model\Specification();' . "\n";
- foreach($element::getAllowedClauses() as $clause)
- if(true === $element->clauseExists($clause))
+ foreach ($element::getAllowedClauses() as $clause) {
+ if (true === $element->clauseExists($clause)) {
$out .= $element->getClause($clause)->accept(
$this,
$handle,
$eldnah
);
- }
- elseif($element instanceof \Hoa\Praspel\Model\Is) {
-
+ }
+ }
+ } elseif ($element instanceof Praspel\Model\Is) {
$variable = '$' . $element->getParent()->getId();
- $out = "\n" .
- $variable . '->getClause(\'is\')->setProperty(' .
- $element->getProperty() .
- ');' . "\n";
- }
- elseif($element instanceof \Hoa\Praspel\Model\Declaration) {
-
+ $out =
+ "\n" .
+ $variable . '->getClause(\'is\')->setProperty(' .
+ $element->getProperty() .
+ ');' . "\n";
+ } elseif ($element instanceof Praspel\Model\Declaration) {
$variable = '$' . ($eldnah ?: $element->getId());
- $out = "\n" .
- $variable . ' = $' . $element->getParent()->getId() .
- '->getClause(\'' . $element->getName() . '\');' . "\n";
+ $out =
+ "\n" .
+ $variable . ' = $' . $element->getParent()->getId() .
+ '->getClause(\'' . $element->getName() . '\');' . "\n";
- foreach($element->getLocalVariables() as $var)
+ foreach ($element->getLocalVariables() as $var) {
$out .= $var->accept($this, $handle, $eldnah);
+ }
- foreach($element->getPredicates() as $predicate)
+ foreach ($element->getPredicates() as $predicate) {
$out .= $variable . '->predicate(\'' . $predicate . '\');' . "\n";
- }
- elseif($element instanceof \Hoa\Praspel\Model\Variable) {
-
+ }
+ } elseif ($element instanceof Praspel\Model\Variable) {
$variable = '$' . ($eldnah ?: $element->getClause()->getId());
$name = $element->getName();
$start = $variable . '[\'' . $name . '\']';
- if(true === $element->isLocal())
+ if (true === $element->isLocal()) {
$out .= $variable . '->let[\'' . $name . '\']';
- else
+ } else {
$out .= $start;
+ }
- if(null !== $alias = $element->getAlias())
+ if (null !== $alias = $element->getAlias()) {
$out .= '->domainof(\'' . $alias . '\');' . "\n";
- else
- $out .= '->in = ' .
- $element->getDomains()->accept($this, $handle, $eldnah) .
- ';' . "\n";
+ } else {
+ $out .=
+ '->in = ' .
+ $element->getDomains()->accept($this, $handle, $eldnah) .
+ ';' . "\n";
+ }
$constraints = $element->getConstraints();
- if(isset($constraints['is']))
- $out .= $start . '->is(\'' .
- implode('\', \'', $constraints['is']) . '\');' .
- "\n";
+ if (isset($constraints['is'])) {
+ $out .=
+ $start . '->is(\'' .
+ implode('\', \'', $constraints['is']) . '\');' .
+ "\n";
+ }
- if(isset($constraints['contains']))
- foreach($constraints['contains'] as $contains)
+ if (isset($constraints['contains'])) {
+ foreach ($constraints['contains'] as $contains) {
$out .= $start . '->contains(' . $contains . ');' . "\n";
+ }
+ }
- if(isset($constraints['key']))
- foreach($constraints['key'] as $pairs)
- $out .= $start . '->key(' . $pairs[0] . ')->in = ' .
- $pairs[1] . ';' . "\n";
- }
- elseif($element instanceof \Hoa\Praspel\Model\Throwable) {
-
+ if (isset($constraints['key'])) {
+ foreach ($constraints['key'] as $pairs) {
+ $out .=
+ $start . '->key(' . $pairs[0] . ')->in = ' .
+ $pairs[1] . ';' . "\n";
+ }
+ }
+ } elseif ($element instanceof Praspel\Model\Throwable) {
$parent = '$' . $element->getParent()->getId();
$_variable = $element->getId();
$variable = '$' . $_variable;
- $out = "\n" .
- $variable . ' = ' . $parent .
- '->getClause(\'throwable\');' . "\n";
-
- foreach($element as $identifier) {
+ $out =
+ "\n" .
+ $variable . ' = ' . $parent .
+ '->getClause(\'throwable\');' . "\n";
+ foreach ($element as $identifier) {
$exception = $element[$identifier];
$start = $variable . '[\'' . $identifier . '\']';
- $out .= $start . ' = \'' . $exception->getInstanceName() .
- '\';' . "\n";
-
- if(false === $element->isDisjointed()) {
-
- if(null !== $with = $element->getWith()) {
+ $out .= $start . ' = \'' . $exception->getInstanceName() . '\';' . "\n";
+ if (false === $element->isDisjointed()) {
+ if (null !== $with = $element->getWith()) {
$temp = $_variable . '_' . $identifier . '_with';
- $out .= '$' . $temp . ' = ' .
- $variable . '->newWith();' . "\n";
+ $out .=
+ '$' . $temp . ' = ' .
+ $variable . '->newWith();' . "\n";
- foreach($with->getLocalVariables() as $var)
+ foreach ($with->getLocalVariables() as $var) {
$out .= $var->accept($this, $handle, $temp);
+ }
- foreach($with->getPredicates() as $predicate)
- $out .= '$' . $temp . '->predicate(\'' . $predicate .
- '\');' . "\n";
+ foreach ($with->getPredicates() as $predicate) {
+ $out .=
+ '$' . $temp . '->predicate(\'' . $predicate .
+ '\');' . "\n";
+ }
$out .= $start . '->setWith($' . $temp . ');' . "\n";
}
+ } else {
+ $out .=
+ $start . '->disjunctionWith(\'' .
+ $exception->getDisjunction() . '\');' . "\n";
}
- else
- $out .= $start . '->disjunctionWith(\'' .
- $exception->getDisjunction() . '\');' . "\n";
}
- }
- elseif($element instanceof \Hoa\Praspel\Model\DefaultBehavior) {
-
- $out = "\n" .
- '$' . $element->getId() . ' = $' .
- $element->getParent()->getId() .
- '->getClause(\'default\')' . "\n";
-
- foreach($element::getAllowedClauses() as $clause)
- if(true === $element->clauseExists($clause))
+ } elseif ($element instanceof Praspel\Model\DefaultBehavior) {
+ $out =
+ "\n" .
+ '$' . $element->getId() . ' = $' .
+ $element->getParent()->getId() .
+ '->getClause(\'default\')' . "\n";
+
+ foreach ($element::getAllowedClauses() as $clause) {
+ if (true === $element->clauseExists($clause)) {
$out .= $element->getClause($clause)->accept(
$this,
$handle,
$eldnah
);
- }
- elseif($element instanceof \Hoa\Praspel\Model\Behavior) {
-
- $out = "\n" .
- '$' . $element->getId() . ' = $' .
- $element->getParent()->getId() .
- '->getClause(\'behavior\')' .
- '->get(\'' . $element->getIdentifier() . '\');' . "\n";
-
- foreach($element::getAllowedClauses() as $clause)
- if(true === $element->clauseExists($clause))
+ }
+ }
+ } elseif ($element instanceof Praspel\Model\Behavior) {
+ $out =
+ "\n" .
+ '$' . $element->getId() . ' = $' .
+ $element->getParent()->getId() .
+ '->getClause(\'behavior\')' .
+ '->get(\'' . $element->getIdentifier() . '\');' . "\n";
+
+ foreach ($element::getAllowedClauses() as $clause) {
+ if (true === $element->clauseExists($clause)) {
$out .= $element->getClause($clause)->accept(
$this,
$handle,
$eldnah
);
- }
- elseif($element instanceof \Hoa\Praspel\Model\Description) {
-
+ }
+ }
+ } elseif ($element instanceof Praspel\Model\Description) {
$parent = '$' . $element->getParent()->getId();
$variable = '$' . $element->getId();
- $out = "\n" .
- $variable . ' = ' . $parent .
- '->getClause(\'description\');' . "\n";
-
- foreach($element as $example)
- $out .= $variable . '[] = \'' .
- preg_replace('#(?<!\\\)\'#', '\\\'', $example) .
- '\';' . "\n";
- }
- elseif($element instanceof \Hoa\Praspel\Model\Collection)
- foreach($element as $el)
+ $out =
+ "\n" .
+ $variable . ' = ' . $parent .
+ '->getClause(\'description\');' . "\n";
+
+ foreach ($element as $example) {
+ $out .=
+ $variable . '[] = \'' .
+ preg_replace('#(?<!\\\)\'#', '\\\'', $example) .
+ '\';' . "\n";
+ }
+ } elseif ($element instanceof Praspel\Model\Collection) {
+ foreach ($element as $el) {
$out .= $el->accept($this, $handle, $eldnah);
+ }
+ }
// Hoa\Realdom.
- elseif($element instanceof \Hoa\Realdom\Disjunction) {
-
+ elseif ($element instanceof Realdom\Disjunction) {
$realdoms = $element->getUnflattenedRealdoms();
- if(!empty($realdoms)) {
-
- $oout = array();
+ if (!empty($realdoms)) {
+ $oout = [];
- foreach($realdoms as $realdom) {
-
- if($realdom instanceof \Hoa\Realdom\IRealdom\Constant)
- $oout[] = 'const(' .
- $realdom->accept($this, $handle, $eldnah) .
- ')';
- else
+ foreach ($realdoms as $realdom) {
+ if ($realdom instanceof Realdom\IRealdom\Constant) {
+ $oout[] =
+ 'const(' .
+ $realdom->accept($this, $handle, $eldnah) .
+ ')';
+ } else {
$oout[] = $realdom->accept($this, $handle, $eldnah);
+ }
}
$out .= 'realdom()->' . implode('->or->', $oout);
}
- }
- elseif($element instanceof \Hoa\Realdom) {
-
- if($element instanceof \Hoa\Realdom\IRealdom\Constant) {
-
- if($element instanceof \Hoa\Realdom\_Array) {
-
- $oout = array();
-
- foreach($element['pairs'] as $pair) {
+ } elseif ($element instanceof Realdom) {
+ if ($element instanceof Realdom\IRealdom\Constant) {
+ if ($element instanceof Realdom\_Array) {
+ $oout = [];
+ foreach ($element['pairs'] as $pair) {
$_oout = null;
- foreach($pair as $_pair) {
-
- if(null !== $_oout)
+ foreach ($pair as $_pair) {
+ if (null !== $_oout) {
$_oout .= ', ';
+ }
$_oout .= $_pair->accept($this, $handle, $eldnah);
}
@@ -282,47 +276,47 @@ class Compiler implements \Hoa\Visitor\Visit {
}
$out .= 'array(' . implode(', ', $oout) . ')';
- }
- else
+ } else {
$out .= $element->getConstantRepresentation();
- }
- else {
-
- $oout = array();
+ }
+ } else {
+ $oout = [];
- foreach($element->getArguments() as $argument)
+ foreach ($element->getArguments() as $argument) {
$oout[] = $argument->accept($this, $handle, $eldnah);
+ }
- $out .= $element->getName() .
- '(' . implode(', ', $oout) . ')';
+ $out .=
+ $element->getName() .
+ '(' . implode(', ', $oout) . ')';
}
- }
- elseif($element instanceof \Hoa\Realdom\Crate\Constant) {
-
+ } elseif ($element instanceof Realdom\Crate\Constant) {
$holder = $element->getHolder();
$praspel = $element->getPraspelRepresentation();
- $out .= '$' . $element->getDeclaration()->getId() .
- '[\'' . $praspel() . '\']';
- }
- elseif($element instanceof \Hoa\Realdom\Crate\Variable) {
-
+ $out .=
+ '$' . $element->getDeclaration()->getId() .
+ '[\'' . $praspel() . '\']';
+ } elseif ($element instanceof Realdom\Crate\Variable) {
$holder = $element->getVariable();
- if($holder instanceof \Hoa\Praspel\Model\Variable\Implicit)
- $out .= 'variable($' . $holder->getClause()->getId() .
- '->getImplicitVariable(\'' . $holder->getName() .
- '\'))';
- else
- $out .= 'variable($' . $holder->getClause()->getId() .
- '->getVariable(\'' . $holder->getName() . '\', true))';
+ if ($holder instanceof Praspel\Model\Variable\Implicit) {
+ $out .=
+ 'variable($' . $holder->getClause()->getId() .
+ '->getImplicitVariable(\'' . $holder->getName() .
+ '\'))';
+ } else {
+ $out .=
+ 'variable($' . $holder->getClause()->getId() .
+ '->getVariable(\'' . $holder->getName() . '\', true))';
+ }
+ } else {
+ throw new Praspel\Exception\Compiler(
+ '%s is not yet implemented.',
+ 0,
+ get_class($element)
+ );
}
- else
- throw new \Hoa\Praspel\Exception\Compiler(
- '%s is not yet implemented.', 0, get_class($element));
-
return $out;
}
}
-
-}