Commits

Eli Bendersky committed 20f6a91

Issue #79: fix C generation of switch/case AST nodes

  • Participants
  • Parent commits ae50806

Comments (0)

Files changed (3)

   - pycparser now carries its PLY dependency along. The pycparser/ply directory
     contains the source of PLY for the currently supported version. This makes
     distribution and testing easier.
+  - Issue #79: fix generation of new switch/case AST nodes.
   - Issue #83: fix parsing and C generation to distinguish between initializer
     lists in declarations and initializing variables with parenthesized
     comma-separated expressions.

pycparser/c_generator.py

 
     def visit_Case(self, n):
         s = 'case ' + self.visit(n.expr) + ':\n'
-        s += self._generate_stmt(n.stmt, add_indent=True)
+        for stmt in n.stmts:
+            s += self._generate_stmt(stmt, add_indent=True)
         return s
 
     def visit_Default(self, n):
-        return 'default:\n' + self._generate_stmt(n.stmt, add_indent=True)
+        s = 'default:\n'
+        for stmt in n.stmts:
+            s += self._generate_stmt(stmt, add_indent=True)
+        return s
 
     def visit_Label(self, n):
         return n.name + ':\n' + self._generate_stmt(n.stmt)

tests/test_c_generator.py

             }
         ''')
 
+    def test_switchcase(self):
+        self._assert_ctoc_correct(r'''
+        int main() {
+            switch (myvar) {
+            case 10:
+            {
+                k = 10;
+                p = k + 1;
+                break;
+            }
+            case 20:
+            case 30:
+                return 20;
+            default:
+                break;
+            }
+        }
+        ''')
+
     def test_issue36(self):
         self._assert_ctoc_correct(r'''
             int main() {