codegen icon indicating copy to clipboard operation
codegen copied to clipboard

Operator precedence is not being handled

Open darkfoxprime opened this issue 10 years ago • 1 comments

Operator precedence is not being properly handled. A couple of examples:

>>> to_ast = lambda x:ast.parse(x, mode='eval')
>>> codegen.to_source(to_ast( '(1+2)*3' ))
'1 + 2 * 3'

1+2*3 = 7. (1+2)*3 = 9

>>> codegen.to_source(to_ast( '( "%s " % (sep,) ).join(foo)' ))
"'%s ' % (sep,).join(foo)"

This is evaluated as '%s ' % ((sep,).join(foo)) because . is higher precedence than %

darkfoxprime avatar Apr 22 '15 17:04 darkfoxprime

The version of this at https://github.com/berkerpeksag/astor handles this properly.

pmaupin avatar Jun 09 '15 04:06 pmaupin