Update paren module to handle ternary conditional operator

This commit is contained in:
Pragmatic Software 2013-12-23 08:48:24 +00:00
parent 8adc766dc4
commit 0f2616a552
2 changed files with 9 additions and 3 deletions

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 476,
BUILD_DATE => "2013-12-01",
BUILD_REVISION => 477,
BUILD_DATE => "2013-12-23",
};
1;

View File

@ -51,6 +51,12 @@ class CGenerator(c_generator.CGenerator):
else:
return n.op + operand
def visit_TernaryOp(self, n):
return '({0}) ? ({1}) : ({2})'.format(
self.visit(n.cond),
self.visit(n.iftrue),
self.visit(n.iffalse))
def visit_Assignment(self, n):
return '%s %s %s' % (self.visit(n.lvalue), n.op, self._parenthesize_unless_simple(n.rvalue))
@ -68,7 +74,7 @@ def parenthesize(source):
if __name__ == "__main__":
if len(sys.argv) > 2:
parenthesize(' '.join(sys.argv[2:]))
parenthesize(' '.join(sys.argv[2:]).rstrip(';'))
elif len(sys.argv) == 2:
print(sys.argv[1] + ': ' + "Usage: paren <expression>")
else: