Example #1
0
    def build_IfExp(ctx, node):
        build_stmt(ctx, node.test)
        build_stmt(ctx, node.body)
        build_stmt(ctx, node.orelse)

        if is_taichi_class(node.test.ptr) or is_taichi_class(
                node.body.ptr) or is_taichi_class(node.orelse.ptr):
            node.ptr = ti_ops.select(node.test.ptr, node.body.ptr,
                                     node.orelse.ptr)
            return node.ptr

        is_static_if = (ASTTransformer.get_decorator(ctx,
                                                     node.test) == "static")

        if is_static_if:
            if node.test.ptr:
                node.ptr = build_stmt(ctx, node.body)
            else:
                node.ptr = build_stmt(ctx, node.orelse)
            return node.ptr

        val = impl.expr_init(None)

        impl.begin_frontend_if(node.test.ptr)
        _ti_core.begin_frontend_if_true()
        val.assign(node.body.ptr)
        _ti_core.pop_scope()
        _ti_core.begin_frontend_if_false()
        val.assign(node.orelse.ptr)
        _ti_core.pop_scope()

        node.ptr = val
        return node.ptr
Example #2
0
    def build_short_circuit_or(operands):
        if len(operands) == 1:
            return operands[0].ptr

        val = impl.expr_init(None)
        lhs = operands[0].ptr
        impl.begin_frontend_if(lhs)

        _ti_core.begin_frontend_if_true()
        val.assign(1)
        _ti_core.pop_scope()

        _ti_core.begin_frontend_if_false()
        rhs = ASTTransformer.build_short_circuit_or(operands[1:])
        val.assign(rhs)
        _ti_core.pop_scope()

        return val
Example #3
0
    def build_If(ctx, node):
        build_stmt(ctx, node.test)
        is_static_if = (ASTTransformer.get_decorator(ctx,
                                                     node.test) == "static")

        if is_static_if:
            if node.test.ptr:
                build_stmts(ctx, node.body)
            else:
                build_stmts(ctx, node.orelse)
            return node

        with ctx.non_static_scope_guard():
            impl.begin_frontend_if(node.test.ptr)
            _ti_core.begin_frontend_if_true()
            build_stmts(ctx, node.body)
            _ti_core.pop_scope()
            _ti_core.begin_frontend_if_false()
            build_stmts(ctx, node.orelse)
            _ti_core.pop_scope()
        return None
Example #4
0
    def build_While(ctx, node):
        if node.orelse:
            raise TaichiSyntaxError(
                "'else' clause for 'while' not supported in Taichi kernels")

        with ctx.loop_scope_guard():
            _ti_core.begin_frontend_while(expr.Expr(1).ptr)
            while_cond = build_stmt(ctx, node.test)
            impl.begin_frontend_if(while_cond)
            _ti_core.begin_frontend_if_true()
            _ti_core.pop_scope()
            _ti_core.begin_frontend_if_false()
            _ti_core.insert_break_stmt()
            _ti_core.pop_scope()
            build_stmts(ctx, node.body)
            _ti_core.pop_scope()
        return None