From 641fbddf309f4df442e5323f77fab3e6c12903b0 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Fri, 18 Jan 2019 13:00:17 +1100 Subject: [PATCH] Improve 'switch' translation so that 'case' and 'default' can be used together --- ast.py | 85 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/ast.py b/ast.py index 62fb950..7a362bb 100644 --- a/ast.py +++ b/ast.py @@ -4541,47 +4541,64 @@ class AST(element.Element): context.indent += ' ' if_text = 'if' for i in self[1][0]: - if isinstance(i, AST.StatementCase): - cond_expr = AST.ExpressionEqual( - children = [ - self[0], - i[0] - ], - binary_operator = ' == ', - precedence = 8 - ) - j = i[1] - while isinstance(j, AST.StatementCase): - cond_expr = AST.ExpressionLogicalOr( + if ( + isinstance(i, AST.StatementCase) or + isinstance(i, AST.StatementDefault) + ): + if isinstance(i, AST.StatementCase): + cond_expr = AST.ExpressionEqual( children = [ - cond_expr, - AST.ExpressionEqual( + self[0], + i[0] + ], + binary_operator = ' == ', + precedence = 8 + ) + j = i[1] + else: + cond_expr = None + j = i[0] + if ( + isinstance(j, AST.StatementCase) or + isinstance(j, AST.StatementDefault) + ): + if isinstance(j, AST.StatementCase): + if cond_expr is not None: + cond_expr = AST.ExpressionLogicalOr( children = [ - self[0], - j[0] + cond_expr, + AST.ExpressionEqual( + children = [ + self[0], + j[0] + ], + binary_operator = ' == ', + precedence = 8 + ) ], - binary_operator = ' == ', - precedence = 8 + binary_operator = ' or ', + precedence = 3 ) - ], - binary_operator = ' or ', - precedence = 3 + j = j[1] + else: + cond_expr = None + j = j[0] + if cond_expr is not None: + assert if_text is not None + context.lines.append( + '{0:s}{1:s} {2:s}:\n'.format( + indent_save, + if_text, + cond_expr.translate_expression(context, 0) + ) ) - j = j[1] - context.lines.append( - '{0:s}{1:s} {2:s}:\n'.format( - indent_save, - if_text, - cond_expr.translate_expression(context, 0) + if_text = 'elif' + else: + context.lines.append( + '{0:s}else:\n'.format(indent_save) ) - ) - if_text = 'elif' + if_text = None j.translate_declaration_or_statement(context) - elif isinstance(i, AST.StatementDefault): - context.lines.append( - '{0:s}else:\n'.format(indent_save) - ) - self[0].translate_declaration_or_statement(context) else: i.translate_declaration_or_statement(context) context.indent = indent_save -- 2.34.1