<commit_before>"""
Code generator for C constructs.
"""

from ctree.codegen import CodeGenVisitor
from ctree.c.nodes import Op
from ctree.c.types import Ptr, get_ctree_type
from ctree.precedence import UnaryOp, BinaryOp, TernaryOp, Cast
from ctree.precedence import get_precedence, is_left_associative


class CCodeGen(CodeGenVisitor):
    """
    Manages generation of C code.
    """

    def _requires_parentheses(self, node):
        """
        Return True if the current precedence is less than the
        parent precedence.  If the precedences are equal, check whether the
        node's orientation to the parent matches associativity.  If it doesn't,
        enclose with parentheses.
        """
        parent = getattr(node, 'parent', None)
        if isinstance(node, (UnaryOp, BinaryOp, TernaryOp)) and\
                isinstance(parent, (UnaryOp, BinaryOp, TernaryOp, Cast)):
            prec = get_precedence(node)
            parent_prec = get_precedence(parent)
            is_not_last_child = isinstance(parent, UnaryOp) or\
                                isinstance(parent, Cast) or\
                                (isinstance(parent, BinaryOp) and node is parent.left) or\
                                (isinstance(parent, TernaryOp) and node is not parent.elze)
            assoc_left = is_left_associative(parent)
            if (prec < parent_prec) or \
                    (prec == parent_prec and (assoc_left is not is_not_last_child)):
                return True
        return False

    # -------------------------------------------------------------------------
    # visitor methods

    def visit_FunctionDecl(self, node):
        params = ", ".join(map(str, node.params))
        s = ""
        if node.kernel:
            s += "__kernel "
        if node.static:
            s += "static "
        if node.inline:
            s += "inline "
        s += "%s %s(%s)" % (node.return_type, node.name, params)
        if node.defn:
            s += " %s" % self._genblock(node.defn)
        return s

    def visit_UnaryOp(self, node):
        if isinstance(node.op, (Op.PostInc, Op.PostDec)):
            s = "%s%s" % (node.arg, node.op)
        else:
            s = "%s %s" % (node.op, node.arg)
        return self._parentheses(node) % s

    def visit_BinaryOp(self, node):
        if isinstance(node.op, Op.ArrayRef):
            s = "%s[%s]" % (node.left, node.right)
        else:
            s = "%s %s %s" % (node.left, node.op, node.right)
        return self._parentheses(node) % s

    def visit_AugAssign(self, node):
        return "%s %s= %s" % (node.target, node.op, node.value)

    def visit_TernaryOp(self, node):
        s = "%s ? %s : %s" % (node.cond, node.then, node.elze)
        return self._parentheses(node) % s

    def visit_Cast(self, node):
        s = "(%s) %s" % (node.type, node.value)
        return self._parentheses(node) % s

    def visit_Constant(self, node):
        if isinstance(node.value, str):
            return "'%s'" % node.value[0]
        else:
            return str(node.value)<commit_msg>Reverting changes to Postinc/Postdec codegen<commit_after>"""
Code generator for C constructs.
"""

from ctree.codegen import CodeGenVisitor
from ctree.c.nodes import Op
from ctree.c.types import Ptr, get_ctree_type
from ctree.precedence import UnaryOp, BinaryOp, TernaryOp, Cast
from ctree.precedence import get_precedence, is_left_associative


class CCodeGen(CodeGenVisitor):
    """
    Manages generation of C code.
    """

    def _requires_parentheses(self, node):
        """
        Return True if the current precedence is less than the
        parent precedence.  If the precedences are equal, check whether the
        node's orientation to the parent matches associativity.  If it doesn't,
        enclose with parentheses.
        """
        parent = getattr(node, 'parent', None)
        if isinstance(node, (UnaryOp, BinaryOp, TernaryOp)) and\
                isinstance(parent, (UnaryOp, BinaryOp, TernaryOp, Cast)):
            prec = get_precedence(node)
            parent_prec = get_precedence(parent)
            is_not_last_child = isinstance(parent, UnaryOp) or\
                                isinstance(parent, Cast) or\
                                (isinstance(parent, BinaryOp) and node is parent.left) or\
                                (isinstance(parent, TernaryOp) and node is not parent.elze)
            assoc_left = is_left_associative(parent)
            if (prec < parent_prec) or \
                    (prec == parent_prec and (assoc_left is not is_not_last_child)):
                return True
        return False

    # -------------------------------------------------------------------------
    # visitor methods

    def visit_FunctionDecl(self, node):
        params = ", ".join(map(str, node.params))
        s = ""
        if node.kernel:
            s += "__kernel "
        if node.static:
            s += "static "
        if node.inline:
            s += "inline "
        s += "%s %s(%s)" % (node.return_type, node.name, params)
        if node.defn:
            s += " %s" % self._genblock(node.defn)
        return s

    def visit_UnaryOp(self, node):
        if isinstance(node.op, (Op.PostInc, Op.PostDec)):
            s = "%s %s" % (node.arg, node.op)
        else:
            s = "%s %s" % (node.op, node.arg)
        return self._parentheses(node) % s

    def visit_BinaryOp(self, node):
        if isinstance(node.op, Op.ArrayRef):
            s = "%s[%s]" % (node.left, node.right)
        else:
            s = "%s %s %s" % (node.left, node.op, node.right)
        return self._parentheses(node) % s

    def visit_AugAssign(self, node):
        return "%s %s= %s" % (node.target, node.op, node.value)

    def visit_TernaryOp(self, node):
        s = "%s ? %s : %s" % (node.cond, node.then, node.elze)
        return self._parentheses(node) % s

    def visit_Cast(self, node):
        s = "(%s) %s" % (node.type, node.value)
        return self._parentheses(node) % s

    def visit_Constant(self, node):
        if isinstance(node.value, str):
            return "'%s'" % node.value[0]
        else:
            return str(node.value)
{'fraction_non_alphanumeric': 0.13646640826873385, 'fraction_numerical': 0.00032299741602067185, 'mean_word_length': 1.8925735637552545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2802769', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1836, 'n_words': 508}