diff options
author | eli.bendersky <devnull@localhost> | 2011-03-04 09:51:23 +0200 |
---|---|---|
committer | eli.bendersky <devnull@localhost> | 2011-03-04 09:51:23 +0200 |
commit | fc96e5e750fe78919d937f2189c5f49b1692c394 (patch) | |
tree | bfe67fbf949834a691dae3ce653e00b38343cb13 /examples | |
parent | ae36e9629694dd6dc12b2f03f4856f9cd90896f6 (diff) | |
download | pycparser-fc96e5e750fe78919d937f2189c5f49b1692c394.tar.gz |
fixing issue 23: coords of casts
Diffstat (limited to 'examples')
-rw-r--r-- | examples/c-to-c.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/examples/c-to-c.py b/examples/c-to-c.py index 7201909..f79d204 100644 --- a/examples/c-to-c.py +++ b/examples/c-to-c.py @@ -141,6 +141,12 @@ class CGenerator(object): if n.expr: s += ' ' + self.visit(n.expr) return s + ';' + def visit_Break(self, n): + return 'break;' + + def visit_Continue(self, n): + return 'continue;' + def visit_For(self, n): s = 'for (' if n.init: s += self.visit(n.init) @@ -152,6 +158,13 @@ class CGenerator(object): s += self._generate_stmt(n.stmt, add_indent=True) return s + def visit_While(self, n): + s = 'while (' + if n.cond: s += self.visit(n.cond) + s += ')\n' + s += self._generate_stmt(n.stmt, add_indent=True) + return s + def _generate_stmt(self, n, add_indent=False): """ Generation from a statement node. This method exists as a wrapper for individual visit_* methods to handle different treatment of @@ -159,7 +172,7 @@ class CGenerator(object): """ typ = type(n) if add_indent: self.indent_level += 2 - s = self._make_indent() + indent = self._make_indent() if add_indent: self.indent_level -= 2 if typ in ( c_ast.Decl, c_ast.Assignment, c_ast.Cast, c_ast.UnaryOp, @@ -167,7 +180,7 @@ class CGenerator(object): # These can also appear in an expression context so no semicolon # is added to them automatically # - return s + self.visit(n) + ';\n' + return indent + self.visit(n) + ';\n' elif typ in (c_ast.Compound,): # No extra indentation required before the opening brace of a # compound - because it consists of multiple lines it has to @@ -175,7 +188,7 @@ class CGenerator(object): # return self.visit(n) else: - return s + self.visit(n) + '\n' + return indent + self.visit(n) + '\n' def _generate_decl(self, n): """ Generation from a Decl node. @@ -244,8 +257,10 @@ static unsigned int hash_func(const char* str, unsigned int table_size) a++; ++a; - for (hash_value = 0; *str != 0; ++str) - {hash_value = (a*hash_value + *str) % table_size;} + while (hash_value == 0) { + hash_value = (a*hash_value + *str) % table_size; + break; + } return hash_value; } @@ -264,3 +279,5 @@ static unsigned int hash_func(const char* str, unsigned int table_size) # assignments... - where to parenthesize? maybe just in BinaryOp? # Other precedence-important operators (such as cast) need parens as well +# ZZZ: turn self.indent_level += 2 ... -= 2 into a context manager! + |