def parenthesize(precedence, leaf, leaf_boxes, when_equal): from mathics.builtin import builtins_precedence while leaf.has_form("HoldForm", 1): leaf = leaf.leaves[0] if leaf.has_form(("Infix", "Prefix", "Postfix"), 3, None): leaf_prec = leaf.leaves[2].get_int_value() elif leaf.has_form("PrecedenceForm", 2): leaf_prec = leaf.leaves[1].get_int_value() else: leaf_prec = builtins_precedence.get(leaf.get_head_name()) if precedence is not None and leaf_prec is not None: if precedence > leaf_prec or (precedence == leaf_prec and when_equal): return Expression("RowBox", Expression("List", String("("), leaf_boxes, String(")"))) return leaf_boxes
def parenthesize(precedence, leaf, leaf_boxes, when_equal): from mathics.builtin import builtins_precedence while leaf.has_form('HoldForm', 1): leaf = leaf.leaves[0] if leaf.has_form(('Infix', 'Prefix', 'Postfix'), 3, None): leaf_prec = leaf.leaves[2].get_int_value() elif leaf.has_form('PrecedenceForm', 2): leaf_prec = leaf.leaves[1].get_int_value() else: leaf_prec = builtins_precedence.get(leaf.get_head_name()) if precedence is not None and leaf_prec is not None: if precedence > leaf_prec or (precedence == leaf_prec and when_equal): return Expression( 'RowBox', Expression('List', String("("), leaf_boxes, String(")"))) return leaf_boxes