Beispiel #1
0
function ast_has($ast, $key)
{
    if (is_object($ast)) {
        return property_exists($ast, $key);
    } else {
        if (is_array($ast)) {
            return array_key_exists($key, $ast);
        } else {
            grokit_logic_error('Called ' . __FUNCTION__ . ' on variable of type ' . gettype($ast) . ', expected array or object');
        }
    }
}
Beispiel #2
0
function getNoDefault(&$container, $key)
{
    if (is_array($container)) {
        if (array_key_exists($key, $container)) {
            return $container[$key];
        }
    } else {
        if (is_object($container)) {
            if (property_exists($container, $key)) {
                return $container->{$key};
            }
        } else {
            grokit_logic_error('Tried to use object of type ' . gettype($container) . ' as a container in ' . __FUNCTION__);
        }
    }
    grokit_logic_error('Tried to get non-existant property \'' . $key . '\' from container');
}
Beispiel #3
0
function parseExpression($ast)
{
    $type = ast_node_type($ast);
    switch ($type) {
        case NodeType::LITERAL:
            $ret = parseLiteral($ast);
            break;
        case NodeType::ATTRIBUTE:
            $ret = parseAttributeExpr($ast);
            break;
        case NodeType::FUNC:
            $ret = parseFunction($ast);
            break;
        case Nodetype::METHOD:
            $ret = parseMethod($ast);
            break;
        case NodeType::OPERATOR:
            $ret = parseOperator($ast);
            break;
        case NodeType::CASE_NODE:
            $ret = parseCase($ast);
            break;
        case NodeType::MATCH:
            $ret = parseMatch($ast);
            break;
        case NodeType::NUL:
            $ret = parseNull($ast);
            break;
        default:
            $source = ast_node_source($ast);
            grokit_logic_error('Attempted to evaluate node of type ' . $type . ' as an expression ' . $source);
    }
    return $ret;
}
Beispiel #4
0
 function parseProgramWaypoints($ast, $header)
 {
     $wps = ast_get($ast, NodeKey::WAYPOINTS);
     $edges = ast_get($ast, NodeKey::EDGES);
     $waypoints = [];
     $res = new GenerationInfo();
     foreach ($wps as $wpast) {
         // Get the waypoint's name from the node
         $wpname = ast_get($wpast, NodeKey::NAME);
         $type = ast_get($wpast, NodeKey::TYPE);
         //fwrite( STDERR, "====== Waypoint $wpname - $type ======\n");
         switch ($type) {
             case NodeType::PRINT_WP:
                 $gRes = parsePrintWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "print";
                 break;
             case NodeType::GI_WP:
                 $gRes = parseGIWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "gi";
                 break;
             case NodeType::SEL_WP:
                 $gRes = parseSelectionWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "selection";
                 break;
             case NodeType::JOIN_WP:
                 $gRes = parseJoinWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "join";
                 break;
             case NodeType::GLA_WP:
                 $gRes = parseGLAWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "gla";
                 break;
             case NodeType::GT_WP:
                 $gRes = parseGTWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = 'gt';
                 break;
             case NodeType::GIST_WP:
                 $gRes = parseGISTWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = 'gist';
                 break;
             case NodeType::SCAN_WP:
                 $gRes = parseScanWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "scan";
                 break;
             case NodeType::CLEANER_WP:
                 $gRes = parseCleanerWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "cleaner";
                 break;
             case NodeType::CACHE_WP:
                 $gRes = parseCacheWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "cache";
                 break;
             case NodeType::CLUSTER_WP:
                 $gRes = parseClusterWP($wpast, $wpname, $header);
                 $waypoints[$wpname] = "cluster";
                 break;
             default:
                 grokit_logic_error('Encountered unexpected node of type ' . $type . ' when a waypoint was expected');
         }
         $res->absorb($gRes);
     }
     return ["waypoints" => $waypoints, "edges" => $edges, "generated" => $res];
 }
 function hashComplex($val)
 {
     $hasher = hash_init('sha256');
     if (is_array($val)) {
         hash_update($hasher, '[');
         // Ensure array is sorted by keys
         ksort($val);
         foreach ($val as $name => $v) {
             hash_update($hasher, $name);
             hash_update($hasher, '=>');
             hash_update($hasher, hashComplex($v));
         }
         hash_update($hasher, ']');
     } else {
         if (is_gla($val)) {
             hash_update($hasher, 'gla');
             hash_update($hasher, $val->hash());
         } else {
             if (is_gf($val)) {
                 hash_update($hasher, 'gf');
                 hash_update($hasher, $val->hash());
             } else {
                 if (is_gt($val)) {
                     hash_update($hasher, 'gt');
                     hash_update($hasher, $val->hash());
                 } else {
                     if (is_gist($val)) {
                         hash_update($hasher, 'gist');
                         hash_update($hasher, $val->hash());
                     } else {
                         if (is_gi($val)) {
                             hash_update($hasher, 'gi');
                             hash_update($hasher, $val->hash());
                         } else {
                             if (is_datatype($val)) {
                                 hash_update($hasher, 'datatype');
                                 hash_update($hasher, $val->hash());
                             } else {
                                 if (is_functor($val)) {
                                     hash_update($hasher, 'functor');
                                     hash_update($hasher, $val->hash());
                                 } else {
                                     if (is_identifier($val)) {
                                         hash_update($hasher, 'identifier');
                                         hash_update($hasher, $val->hash());
                                     } else {
                                         if (is_attribute($val)) {
                                             hash_update($hasher, 'attribute');
                                             hash_update($hasher, strval($val));
                                         } else {
                                             if (is_int($val) || is_float($val) || is_string($val) || is_bool($val)) {
                                                 hash_update($hasher, gettype($val));
                                                 hash_update($hasher, $val);
                                             } else {
                                                 if (is_null($val)) {
                                                     hash_update($hasher, 'null');
                                                 } else {
                                                     $valType = is_object($val) ? get_class($val) : gettype($val);
                                                     grokit_logic_error('Unable to hash unknown type ' . $valType);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return hash_final($hasher);
 }
Beispiel #6
0
 public function absorbAttrList(array $list)
 {
     try {
         foreach ($list as $elem) {
             $this->absorbAttr($elem);
         }
     } catch (Exception $e) {
         grokit_logic_error("Failed to absorb info for attribute list: " . print_r($list, true), $e);
     }
 }