Improve 'switch' translation so that 'case' and 'default' can be used together
authorNick Downing <nick@ndcode.org>
Fri, 18 Jan 2019 02:00:17 +0000 (13:00 +1100)
committerNick Downing <nick@ndcode.org>
Fri, 18 Jan 2019 02:00:17 +0000 (13:00 +1100)
ast.py

diff --git a/ast.py b/ast.py
index 62fb950..7a362bb 100644 (file)
--- 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