Example #1
0
function parseFunction($ast)
{
    assert_ast_type($ast, NodeType::FUNC);
    $data = ast_node_data($ast);
    $name = parseIdentifier(ast_get($data, NodeKey::NAME));
    $args = parseExpressionList(ast_get($data, NodeKey::ARGS));
    $targs = parseTemplateArgs(ast_get($data, NodeKey::TARGS));
    $targs = is_null($targs) ? [] : $targs;
    $source = ast_node_source($ast);
    $arg_types = array_map(function ($item) {
        return $item->type();
    }, $args);
    try {
        $func = lookupFunction($name, $arg_types, $targs);
    } catch (Exception $e) {
        grokit_error('Failed to find function ' . $name . ' called from ' . $source, $e);
    }
    $info = $func->apply($args, $source);
    // If the function is deterministic and all of the expressions were constant,
    // just turn this expression into a constant.
    if ($info->is_const()) {
        $info->makeConstant();
    }
    return $info;
}
Example #2
0
 function convertExpression(grokit\ExpressionInfo &$expression, $type, $source = null)
 {
     if ($source === null) {
         $source = $expression->source();
     }
     grokit_logic_assert(canConvert($expression->type(), $type), 'Unable to convert expression from type ' . $expression->type() . ' to type ' . $type);
     if ($expression->type() == $type) {
         return $expression;
     }
     $func = lookupFunction(strval($type), [$expression->type()], []);
     //fwrite(STDERR, "Convert: source(" . $expression->type() . ") dest(" . $type . ")" . PHP_EOL);
     //fwrite(STDERR, print_r($func, true));
     $info = $func->apply([$expression], $source);
     if ($info->is_const()) {
         $info->makeConstant();
     }
     return $info;
 }