Ejemplo n.º 1
0
 /**
  * Visit an element.
  *
  * @param   \Hoa\Visitor\Element  $element    Element to visit.
  * @param   mixed                 &$handle    Handle (reference).
  * @param   mixed                 $eldnah     Handle (not reference).
  * @return  mixed
  */
 public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
 {
     $out = null;
     // Hoa\Praspel.
     if ($element instanceof HoaPraspel\Model\Specification) {
         $oout = [];
         foreach ($element::getAllowedClauses() as $clause) {
             if (true === $element->clauseExists($clause)) {
                 $oout[] = $element->getClause($clause)->accept($this, $handle, $eldnah);
             }
         }
         $out = implode("\n", $oout);
     } elseif ($element instanceof HoaPraspel\Model\Is) {
         $out = '@is ' . $element->getPropertyName() . ';';
     } elseif ($element instanceof HoaPraspel\Model\Declaration) {
         $clause = $element->getName();
         $out = '@' . $clause;
         $oout = [];
         foreach ($element->getLocalVariables() as $name => $var) {
             $oout[] = ' ' . $var->accept($this, $handle, $eldnah);
         }
         foreach ($element->getPredicates() as $predicate) {
             $oout[] = ' \\pred(\'' . $predicate . '\')';
         }
         $out .= implode(' and', $oout) . ';';
     } elseif ($element instanceof HoaPraspel\Model\Variable) {
         $name = $element->getName();
         if (true === $element->isLocal()) {
             $out = 'let ';
         }
         $out .= $name;
         if (null === ($alias = $element->getAlias())) {
             $out .= ': ' . $element->getDomains()->accept($this, $handle, $eldnah);
         } else {
             $out .= ' domainof ' . $alias;
         }
         $constraints = $element->getConstraints();
         if (isset($constraints['is'])) {
             $out .= ' and ' . $name . ' is ' . implode(', ', $constraints['is']);
         }
         if (isset($constraints['contains'])) {
             foreach ($constraints['contains'] as $contains) {
                 $out .= ' and ' . $name . ' contains ' . $contains->accept($this, $handle, $eldnah);
             }
         }
         if (isset($constraints['key'])) {
             foreach ($constraints['key'] as $pairs) {
                 $out .= ' and ' . $name . '[' . $pairs[0]->accept($this, $handle, $eldnah) . ']: ' . $pairs[1]->accept($this, $handle, $eldnah);
             }
         }
     } elseif ($element instanceof HoaPraspel\Model\Throwable) {
         $oout = [];
         foreach ($element as $identifier) {
             $exception = $element[$identifier];
             if (true === $exception->isDisjointed()) {
                 continue;
             }
             $line = ' ' . $exception->getInstanceName() . ' ' . $identifier;
             foreach ((array) $exception->getDisjunction() as $_identifier) {
                 $_exception = $element[$_identifier];
                 $line .= ' or ' . $_exception->getInstanceName() . ' ' . $_identifier;
             }
             if (null !== ($with = $exception->getWith())) {
                 $line .= ' with ';
                 $liine = [];
                 foreach ($with as $var) {
                     $liine[] = $var->accept($this, $handle, $eldnah);
                 }
                 foreach ($with->getPredicates() as $predicate) {
                     $liine[] = '\\pred(\'' . $predicate . '\')';
                 }
                 $line .= implode(' and ', $liine);
             }
             $oout[] = $line;
         }
         $out = '@throwable' . implode(' or', $oout) . ';';
     } elseif ($element instanceof HoaPraspel\Model\DefaultBehavior) {
         $out = '@default {' . "\n";
         $oout = [];
         foreach ($element::getAllowedClauses() as $clause) {
             if (true === $element->clauseExists($clause)) {
                 $oout[] = '    ' . str_replace("\n", "\n" . '    ', $element->getClause($clause)->accept($this, $handle, $eldnah));
             }
         }
         $out .= implode("\n", $oout) . "\n" . '}';
     } elseif ($element instanceof HoaPraspel\Model\Behavior) {
         $out = '@behavior ' . $element->getIdentifier() . ' {' . "\n";
         $oout = [];
         foreach ($element::getAllowedClauses() as $clause) {
             if (true === $element->clauseExists($clause)) {
                 $oout[] = '    ' . str_replace("\n", "\n" . '    ', $element->getClause($clause)->accept($this, $handle, $eldnah));
             }
         }
         $out .= implode("\n", $oout) . "\n" . '}';
     } elseif ($element instanceof HoaPraspel\Model\Description) {
         $oout = [];
         foreach ($element as $example) {
             $oout[] = '@description \'' . preg_replace('#(?<!\\\\)\'#', '\\\'', $example) . '\';';
         }
         $out = implode("\n", $oout);
     } elseif ($element instanceof HoaPraspel\Model\Collection) {
         foreach ($element as $el) {
             $out .= $el->accept($this, $handle, $eldnah);
         }
     } elseif ($element instanceof Realdom\Disjunction) {
         $realdoms = $element->getUnflattenedRealdoms();
         if (!empty($realdoms)) {
             $oout = [];
             foreach ($realdoms as $realdom) {
                 $oout[] = $realdom->accept($this, $handle, $eldnah);
             }
             $out .= implode(' or ', $oout);
         }
     } elseif ($element instanceof Realdom) {
         if ($element instanceof Realdom\IRealdom\Constant) {
             $out .= $element->getConstantRepresentation();
         } else {
             $oout = [];
             foreach ($element->getArguments() as $argument) {
                 $oout[] = $argument->accept($this, $handle, $eldnah);
             }
             $out .= $element->getName() . '(' . implode(', ', $oout) . ')';
         }
     } elseif ($element instanceof Realdom\Crate\Constant) {
         $praspel = $element->getPraspelRepresentation();
         $out .= $praspel();
     } elseif ($element instanceof Realdom\Crate\Variable) {
         $out .= $element->getVariable()->getName();
     } else {
         throw new HoaPraspel\Exception\Compiler('%s is not yet implemented.', 0, get_class($element));
     }
     return $out;
 }
Ejemplo n.º 2
0
 /**
  * Visit an element.
  *
  * @param   \Hoa\Visitor\Element  $element    Element to visit.
  * @param   mixed                 &$handle    Handle (reference).
  * @param   mixed                 $eldnah     Handle (not reference).
  * @return  mixed
  */
 public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
 {
     $out = null;
     if ($element instanceof Ruler\Model) {
         $out .= $element->getExpression()->accept($this, $handle, $eldnah);
     } elseif ($element instanceof Ruler\Model\Operator) {
         $name = $element->getName();
         $arguments = [];
         foreach ($element->getArguments() as $argument) {
             $arguments[] = $argument->accept($this, $handle, $eldnah);
         }
         if (true === $element->isFunction()) {
             $out .= $name . '(' . implode(', ', $arguments) . ')';
         } else {
             if (!isset($arguments[1])) {
                 $_out = $name . ' ' . $arguments[0];
             } else {
                 $_out = $arguments[0] . ' ' . $name . ' ' . $arguments[1];
             }
             if (false === Ruler\Model\Operator::isToken($name)) {
                 $_out = '(' . $_out . ')';
             }
             $out .= $_out;
         }
     } elseif ($element instanceof Ruler\Model\Bag\Scalar) {
         $value = $element->getValue();
         if (true === $value) {
             $out .= 'true';
         } elseif (false === $value) {
             $out .= 'false';
         } elseif (null === $value) {
             $out .= 'null';
         } elseif (is_numeric($value)) {
             $out .= (string) $value;
         } else {
             $out .= '\'' . str_replace('\\', '\\\'', $value) . '\'';
         }
     } elseif ($element instanceof Ruler\Model\Bag\RulerArray) {
         $values = [];
         foreach ($element->getArray() as $value) {
             $values[] = $value->accept($this, $handle, $eldnah);
         }
         $out .= '[' . implode(', ', $values) . ']';
     } elseif ($element instanceof Ruler\Model\Bag\Context) {
         $out .= $element->getId();
         foreach ($element->getDimensions() as $dimension) {
             $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
             switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
                 case Ruler\Model\Bag\Context::ARRAY_ACCESS:
                     $out .= '[' . $value->accept($this, $handle, $eldnah) . ']';
                     break;
                 case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS:
                     $out .= '.' . $value;
                     break;
                 case Ruler\Model\Bag\Context::METHOD_ACCESS:
                     $out .= '.' . $value->accept($this, $handle, $eldnah);
                     break;
             }
         }
     }
     return $out;
 }
Ejemplo n.º 3
0
 /**
  * Visit an element.
  *
  * @param   \Hoa\Visitor\Element  $element    Element to visit.
  * @param   mixed                 &$handle    Handle (reference).
  * @param   mixed                 $eldnah     Handle (not reference).
  * @return  mixed
  */
 public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
 {
     $out = null;
     $_ = str_repeat('    ', $this->_indentation);
     if ($element instanceof Ruler\Model) {
         $this->_indentation = 1;
         $out = '$model = new \\Hoa\\Ruler\\Model();' . "\n" . '$model->expression =' . "\n" . $element->getExpression()->accept($this, $handle, $eldnah) . ';';
     } elseif ($element instanceof Ruler\Model\Operator) {
         $out = $_ . '$model->';
         $name = $element->getName();
         $_handle = [];
         if (false === $element->isFunction()) {
             if (true === Core\Consistency::isIdentifier($name)) {
                 $out .= $name;
             } else {
                 $out .= '{\'' . $name . '\'}';
             }
             $out .= '(' . "\n";
         } else {
             $out .= 'func(' . "\n" . $_ . '    ';
             $_handle[] = '\'' . $name . '\'';
         }
         ++$this->_indentation;
         foreach ($element->getArguments() as $argument) {
             $_handle[] = $argument->accept($this, $handle, $eldnah);
         }
         --$this->_indentation;
         $out .= implode(',' . "\n", $_handle) . "\n" . $_ . ')';
     } elseif ($element instanceof Ruler\Model\Bag\Scalar) {
         $value = $element->getValue();
         $out = $_;
         if (true === $value) {
             $out .= 'true';
         } elseif (false === $value) {
             $out .= 'false';
         } elseif (null === $value) {
             $out .= 'null';
         } elseif (is_numeric($value)) {
             $out .= (string) $value;
         } else {
             $out .= '\'' . str_replace('\\', '\\\'', $value) . '\'';
         }
     } elseif ($element instanceof Ruler\Model\Bag\RulerArray) {
         $values = [];
         ++$this->_indentation;
         foreach ($element->getArray() as $value) {
             $values[] = $value->accept($this, $handle, $eldnah);
         }
         --$this->_indentation;
         $out = $_ . '[' . "\n" . implode(',' . "\n", $values) . "\n" . $_ . ']';
     } elseif ($element instanceof Ruler\Model\Bag\Context) {
         $out = $_ . '$model->variable(\'' . $element->getId() . '\')';
         $this->_indentation += 2;
         foreach ($element->getDimensions() as $dimension) {
             $value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
             $out .= "\n" . $_ . '    ->';
             switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
                 case Ruler\Model\Bag\Context::ARRAY_ACCESS:
                     $out .= 'index(' . "\n" . $value->accept($this, $handle, $eldnah) . "\n" . $_ . '    )';
                     break;
                 case Ruler\Model\Bag\Context::ATTRIBUTE_ACCESS:
                     $out .= 'attribute(\'' . $value . '\')';
                     break;
                 case Ruler\Model\Bag\Context::METHOD_ACCESS:
                     $out .= 'call(' . "\n" . $value->accept($this, $handle, $eldnah) . "\n" . $_ . '    )';
                     break;
             }
         }
         $this->_indentation -= 2;
     }
     return $out;
 }
Ejemplo n.º 4
0
 /**
  * Visit an element.
  *
  * @param   \Hoa\Visitor\Element  $element    Element to visit.
  * @param   mixed                 &$handle    Handle (reference).
  * @param   mixed                 $eldnah     Handle (not reference).
  * @return  mixed
  */
 public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
 {
     $out = null;
     // Hoa\Praspel.
     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)) {
                 $out .= $element->getClause($clause)->accept($this, $handle, $eldnah);
             }
         }
     } elseif ($element instanceof Praspel\Model\Is) {
         $variable = '$' . $element->getParent()->getId();
         $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";
         foreach ($element->getLocalVariables() as $var) {
             $out .= $var->accept($this, $handle, $eldnah);
         }
         foreach ($element->getPredicates() as $predicate) {
             $out .= $variable . '->predicate(\'' . $predicate . '\');' . "\n";
         }
     } elseif ($element instanceof Praspel\Model\Variable) {
         $variable = '$' . ($eldnah ?: $element->getClause()->getId());
         $name = $element->getName();
         $start = $variable . '[\'' . $name . '\']';
         if (true === $element->isLocal()) {
             $out .= $variable . '->let[\'' . $name . '\']';
         } else {
             $out .= $start;
         }
         if (null !== ($alias = $element->getAlias())) {
             $out .= '->domainof(\'' . $alias . '\');' . "\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['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 Praspel\Model\Throwable) {
         $parent = '$' . $element->getParent()->getId();
         $_variable = $element->getId();
         $variable = '$' . $_variable;
         $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())) {
                     $temp = $_variable . '_' . $identifier . '_with';
                     $out .= '$' . $temp . ' = ' . $variable . '->newWith();' . "\n";
                     foreach ($with->getLocalVariables() as $var) {
                         $out .= $var->accept($this, $handle, $temp);
                     }
                     foreach ($with->getPredicates() as $predicate) {
                         $out .= '$' . $temp . '->predicate(\'' . $predicate . '\');' . "\n";
                     }
                     $out .= $start . '->setWith($' . $temp . ');' . "\n";
                 }
             } else {
                 $out .= $start . '->disjunctionWith(\'' . $exception->getDisjunction() . '\');' . "\n";
             }
         }
     } 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 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 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 Praspel\Model\Collection) {
         foreach ($element as $el) {
             $out .= $el->accept($this, $handle, $eldnah);
         }
     } elseif ($element instanceof Realdom\Disjunction) {
         $realdoms = $element->getUnflattenedRealdoms();
         if (!empty($realdoms)) {
             $oout = [];
             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 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) {
                             $_oout .= ', ';
                         }
                         $_oout .= $_pair->accept($this, $handle, $eldnah);
                     }
                     $oout[] = 'array(' . $_oout . ')';
                 }
                 $out .= 'array(' . implode(', ', $oout) . ')';
             } else {
                 $out .= $element->getConstantRepresentation();
             }
         } else {
             $oout = [];
             foreach ($element->getArguments() as $argument) {
                 $oout[] = $argument->accept($this, $handle, $eldnah);
             }
             $out .= $element->getName() . '(' . implode(', ', $oout) . ')';
         }
     } elseif ($element instanceof Realdom\Crate\Constant) {
         $holder = $element->getHolder();
         $praspel = $element->getPraspelRepresentation();
         $out .= '$' . $element->getDeclaration()->getId() . '[\'' . $praspel() . '\']';
     } elseif ($element instanceof Realdom\Crate\Variable) {
         $holder = $element->getVariable();
         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));
     }
     return $out;
 }