Example #1
0
function ast_node_data($ast)
{
    grokit_logic_assert(ast_has($ast, NodeKey::NODE_DATA), 'Attempted to get data of non-node value.');
    return ast_get($ast, NodeKey::NODE_DATA);
}
Example #2
0
 function parseAttributeMap($map, &$info)
 {
     grokit_logic_assert(is_array($map) || is_object($map), 'Passed non-list value of type ' . gettype($map) . ' to ' . __FUNCTION__);
     // Transform it into an array if it isn't already
     $ret = [];
     foreach ($map as $attName => $queries) {
         $ret[$attName] = $queries;
         $att = lookupAttribute($attName);
         // Get information about attribute
         $info->absorbAttr($att);
     }
     return $ret;
 }
Example #3
0
function parseExpressionList($ast)
{
    $list = [];
    grokit_logic_assert(is_array($ast), 'Attempted to parse value of type ' . gettype($ast) . ' as an array.');
    foreach ($ast as $expr) {
        $list[] = parseExpression($expr);
    }
    return $list;
}
Example #4
0
 public static function SplitNamespace($name)
 {
     grokit_logic_assert(self::IsNamespaced($name), 'Attempting to split non-namespaced name ' . $name . ' by namespace.');
     return $parts = explode(NS_SEP, $name, 2);
 }
Example #5
0
 public static function Pop()
 {
     //fwrite(STDERR, 'Pop' . PHP_EOL);
     grokit_logic_assert(count(self::$stack) > 0, 'Too many pops on LibraryManager');
     $inst = array_pop(self::$stack);
     self::$instance = $inst;
 }
Example #6
0
 public static function setAttributeType($name, $type_ast)
 {
     grokit_logic_assert(self::attributeExists($name), 'No attribute named ' . $name . ' exists');
     //fwrite(STDERR, "Setting type of attribute $name to " . print_r($type_ast, true) . PHP_EOL);
     self::$att_map[$name]->setType($type_ast);
 }
Example #7
0
 public function makeConstant()
 {
     grokit_logic_assert($this->is_const, 'Attempting to add non-constant value (' . $this->value . ') to constants');
     $newName = generate_name('ct_');
     $ctVal = 'const ' . $this->type . ' ' . $newName . ' = ' . $this->value . ';';
     // Add all preprocessing statements to the list of constants.
     $this->constants = array_merge($this->constants, $this->preprocess);
     $this->preprocess = [];
     // Add the constant statement to the list and then set the value to the
     // attribute name.
     $this->constants[] = $ctVal;
     $this->value = $newName;
 }
Example #8
0
 public function lookupMethod($name, array $args)
 {
     grokit_assert(array_key_exists($name, $this->methods), 'No method registered with the name ' . $this->value() . '->' . $name);
     $candidates =& $this->methods[$name];
     // Matches is a mapping from the score to the candidate MethodInfo
     // We keep track of all possible matches in case we have multiple possibilities,
     // and we may in the future be able to print nice error messages.
     $matches = [];
     $nMatches = 0;
     $fStr = $this->value() . '->' . $name . '(' . implode(', ', $args) . ')';
     //fwrite(STDERR, 'Looking up ' . $fStr . PHP_EOL);
     foreach ($candidates as $cand) {
         $rating = $cand->compatibility($args);
         //fwrite(STDERR, 'Candidate: ' . $cand . ' Rating: ' . $rating . PHP_EOL );
         if ($rating >= 0) {
             if (!array_key_exists($rating, $matches)) {
                 $matches[$rating] = [];
             }
             $matches[$rating][] = $cand;
             $nMatches += 1;
         }
     }
     grokit_logic_assert(!array_key_exists(0, $matches) || count($matches[0]) == 1, 'Got more than one exact match for method ' . $fStr);
     //fwrite(STDERR, 'Matches: ' . PHP_EOL);
     //fwrite(STDERR, print_r($matches, true) . PHP_EOL);
     //fwrite(STDERR, 'Defined functions: ' . print_r($this->functionCache, true) . PHP_EOL );
     //fwrite(STDERR, 'Registered Functions: ' . print_r($this->registeredFunctions, true) . PHP_EOL );
     // If we have an exact match, use that.
     if (array_key_exists(0, $matches) && count($matches[0]) == 1) {
         $match = $matches[0][0];
     } else {
         if ($nMatches == 1) {
             // If there were no exact matches, but there was only one match, use that.
             $match = array_pop($matches);
             $match = array_pop($match);
         } else {
             if ($nMatches == 0) {
                 grokit_error('Failed to lookup method ' . $fStr . ', no possible matches.');
             } else {
                 // There were multiple possible matches.
                 // Aggregate the strings representing the possible matches and
                 // then put out an error.
                 $matchz = [];
                 foreach ($matches as $matchList) {
                     foreach ($matchList as $match) {
                         $matchz[] = $match;
                     }
                 }
                 $matchStr = implode(PHP_EOL, $matchz);
                 grokit_error('Failed to lookup method ' . $fStr . ', multiple possible' . ' matches:' . PHP_EOL . $matchStr);
             }
         }
     }
     return $match;
 }