evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.04k stars 120 forks source link

Line break characters aren't compatible with pure Python #818

Closed kg583 closed 7 months ago

kg583 commented 7 months ago

Coconut seems to consider quite a few characters that aren't \n\r line breaks, meaning they can't appear inside regular strings. This prevents pure Python code containing such characters from being valid Coconut.

print("")                 # CoconutSyntaxError: linebreak in non-multi-line string
print("\x0b\x0c\x1c\x1d\x1e")    # This is a workaround

This has a further consequence which breaks the function of some programs: such "line breaks" automatically become \n\r inside multiline strings.

print(*map(ord,""""""))             # 10 10 10 10 10
print(*map(ord,"\x0b\x0c\x1c\x1d\x1e"))    # 11 12 28 29 30

It's worth addressing that this was all discovered by Coconut's recent (read: yesterday) addition to code.golf. Many Python competitors (myself included) ported over Python solutions as a starting point and found them breaking due to parser errors.

Of course, golfed code is often objectively disgusting, and it is no surprise that it puts strain on the parser. Other parser incompatibilities have been discovered, and while they do strictly break "all valid Python is valid Coconut", they vary in reasonability. For example, the added imaginary literal syntax leads to

0in[1,2,3]    # CoconutParseError: parsing failed

but this will become a SyntaxError in a future Python version (currently a SyntaxWarning), so there's probably little cause to fix it.

I am happy to open issues for other discovered parsing problems, though I also do not want to load the issue list with trifles. This is a great project and one that is already very well-liked for golfing, even with these hiccups.

evhub commented 7 months ago

This is great; thanks for raising this! Very happy to get these sorts of nitpicky issues where there are slight differences between Python and Coconut; these are bugs and definitely should be fixed.

evhub commented 7 months ago

Fixed as of coconut-develop>=3.0.4-post_dev14 (pip install -U coconut-develop>=3.0.4-post_dev14 to get the fix).

(including the 0in[1,2,3] thing)