Skip to content

Commit

Permalink
[mycpp] Add parens around expressions.
Browse files Browse the repository at this point in the history
Without this, we'll have operator precedence bugs.

test/spec-cpp: 856 passing under osh_eval.cc!  Up from 851, so it fixed
5 bugs.
  • Loading branch information
Andy Chu committed Jul 15, 2020
1 parent a5eb988 commit d8db720
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mycpp/cppgen_pass.py
Expand Up @@ -759,9 +759,16 @@ def visit_op_expr(self, o: 'mypy.nodes.OpExpr') -> T:
self.write(')')
return

# These parens are sometimes extra, but sometimes required. Example:
#
# if ((a and (false or true))) { # right
# vs.
# if (a and false or true)) { # wrong
self.write('(')
self.accept(o.left)
self.write(' %s ', c_op)
self.accept(o.right)
self.write(')')

def visit_comparison_expr(self, o: 'mypy.nodes.ComparisonExpr') -> T:
# Make sure it's binary
Expand Down
8 changes: 8 additions & 0 deletions mycpp/examples/conditional.py
Expand Up @@ -46,6 +46,14 @@ def run_tests():
log("x = %d", x)


# Expressions where parens are needed
a = False
if a and (False or True):
print('yes')
else:
print('no')


def run_benchmarks():
# type: () -> None
pass
Expand Down

0 comments on commit d8db720

Please sign in to comment.