コード例 #1
0
    def build_Call(ctx, node):
        if ASTTransformer.get_decorator(ctx, node) == 'static':
            with ctx.static_scope_guard():
                build_stmt(ctx, node.func)
                build_stmts(ctx, node.args)
                build_stmts(ctx, node.keywords)
        else:
            build_stmt(ctx, node.func)
            build_stmts(ctx, node.args)
            build_stmts(ctx, node.keywords)

        args = []
        for arg in node.args:
            if isinstance(arg, ast.Starred):
                for i in arg.ptr:
                    args.append(i)
            else:
                args.append(arg.ptr)
        keywords = dict(ChainMap(*[keyword.ptr for keyword in node.keywords]))
        func = node.func.ptr

        if isinstance(node.func, ast.Attribute) and isinstance(
                node.func.value.ptr, str) and node.func.attr == 'format':
            args.insert(0, node.func.value.ptr)
            node.ptr = impl.ti_format(*args, **keywords)
            return node.ptr

        if ASTTransformer.build_call_if_is_builtin(ctx, node, args, keywords):
            return node.ptr

        node.ptr = func(*args, **keywords)
        ASTTransformer.warn_if_is_external_func(ctx, node)

        return node.ptr
コード例 #2
0
ファイル: ast_transformer.py プロジェクト: Leonz5288/taichi
    def build_Call(ctx, node):
        is_in_static_scope_prev = ctx.is_in_static_scope
        if ASTTransformer.get_decorator(ctx, node) == 'static':
            ctx.is_in_static_scope = True

        build_stmt(ctx, node.func)
        build_stmts(ctx, node.args)
        build_stmts(ctx, node.keywords)

        ctx.is_in_static_scope = is_in_static_scope_prev

        args = []
        for arg in node.args:
            if isinstance(arg, ast.Starred):
                args += arg.ptr
            else:
                args.append(arg.ptr)
        keywords = dict(ChainMap(*[keyword.ptr for keyword in node.keywords]))

        if isinstance(node.func, ast.Attribute):
            attr_name = node.func.attr
            if attr_name == 'format' and isinstance(node.func.value.ptr, str):
                args.insert(0, node.func.value.ptr)
                node.ptr = impl.ti_format(*args, **keywords)
            else:
                node.ptr = node.func.ptr(*args, **keywords)
        elif isinstance(node.func, ast.Name):
            func_name = node.func.id
            if func_name == 'print':
                node.ptr = impl.ti_print(*args, **keywords)
            elif func_name == 'min':
                node.ptr = ti_ops.ti_min(*args, **keywords)
            elif func_name == 'max':
                node.ptr = ti_ops.ti_max(*args, **keywords)
            elif func_name == 'int':
                node.ptr = impl.ti_int(*args, **keywords)
            elif func_name == 'float':
                node.ptr = impl.ti_float(*args, **keywords)
            elif func_name == 'any':
                node.ptr = ti_ops.ti_any(*args, **keywords)
            elif func_name == 'all':
                node.ptr = ti_ops.ti_all(*args, **keywords)
            else:
                node.ptr = node.func.ptr(*args, **keywords)
        else:
            node.ptr = node.func.ptr(*args, **keywords)

        return node.ptr
コード例 #3
0
ファイル: ast_transformer.py プロジェクト: Leonz5288/taichi
    def build_JoinedStr(ctx, node):
        str_spec = ''
        args = []
        for sub_node in node.values:
            if isinstance(sub_node, ast.FormattedValue):
                str_spec += '{}'
                args.append(build_stmt(ctx, sub_node.value))
            elif isinstance(sub_node, ast.Constant):
                str_spec += sub_node.value
            elif isinstance(sub_node, ast.Str):
                str_spec += sub_node.s
            else:
                raise TaichiSyntaxError("Invalid value for fstring.")

        args.insert(0, str_spec)
        node.ptr = impl.ti_format(*args)
        return node.ptr