Repository navigation
Conversation
RapydScript prints a unary operator directly against its operand, so
"- -x" came out as "--x": a pre-decrement that returns x-1 and changes
x, and for a literal ("- -3") a JavaScript syntax error that stops the
whole program from compiling. "+ +x" became "++x" the same way.
Before #170 the preprocessor rewrote every "-" as "-1*", which hid
this. Now print_unary_prefix adds a space when a sign operator is
followed by another sign operator (UglifyJS's rule for the same case).
Plain "-x" still compiles to "-x".
tests/test_compiler.py compiles and runs ten repeated-sign expressions
under node and compares them with CPython. Before the fix four fail
("- -3", "- - -x" syntax errors; "- -x" == 4, "+ +x" == 6, x changed);
after it all pass. Through GlowScriptOffline, a 122-row arithmetic
program matches CPython except for the 15 known pre-existing
differences, the #170 39-case program has 0 failures, and all 23
bundled demos run without errors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QNw6KLiif2r9zSoSQug4iL
|
On hold. #223 (#170) has a second, larger regression, and this fix doesn't cover it. Unary minus in front of a parenthesized base of
Cause: RapydScript's parser binds a unary operator tighter than glowscript.org has been rolled back to the version before #223 (20261101t092705). A parser fix, so that 🤖 Generated with Claude Code |
RapydScript gave unary operators the highest precedence, so "-x**2"
parsed as "(-x)**2", and the output stage turned that back into
"-Math.pow(x, 2)" only when the unary had no parentheses. Everything
else came out with the wrong sign:
-(x+1)**2 -> 36 (Python: -36)
-(x)**2 -> 25 (Python: -25)
- -x**2 -> -25 (Python: 25)
exp(-(x-3)**2/8) -> 1.65 (Python: 0.61)
The "-" -> "-1*" rewrite removed in #223 hid all of this, so #223
made it visible. maybe_unary now parses Python's "primary ** factor"
itself: after an atom, "**" takes the next unary expression as its
right operand. That binds ** tighter than a unary on its left and makes
it group right to left, which also fixes 2**3**2 (was 64, now 512) and
2**-1**2 (was 0.25, now 0.5) from #224.
tests/test_compiler.py: 21 power/unary expressions against CPython; 9
failed before the fix, all pass after. Through GlowScriptOffline: the
122-row arithmetic table now differs from CPython only in the 13 known
cases (% with negatives, round halves, int(bool), e), the #170 39-case
program has 0 failures, and all 23 demos run. **kwargs in calls and
definitions, comprehensions and conditionals with ** are unaffected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QNw6KLiif2r9zSoSQug4iL
|
Ready for review again. This now includes the second fix: Tested with rsWVPRunner v3.2.4-rc3 (RScompiler sha256
🤖 Generated with Claude Code |
Fixes two regressions from #223 (#170). Before #223, the preprocessor's
"-" → "-1*"rewrite hid two RapydScript bugs, and removing it exposed both. glowscript.org has been rolled back to the version before #223 (20261101t092705) until this is merged.1.
-(x+1)**2came out positiveRapydScript's parser gave unary operators higher precedence than
**, so-x**2parsed as(-x)**2. The output stage then turned that back into-Math.pow(x, 2), but only when the unary had no parentheses. Everything else came out with the wrong sign:-(x+1)**2(x = 5)-(x)**2-(2)**2- -x**2exp(-(x-3)**2/8)Fix (61d0edf):
maybe_unarynow parses Python'sprimary ** factoritself. After an atom,**takes the next unary expression as its right operand. That makes**bind tighter than a unary operator on its left, and makes it group right to left. That also fixes two items from #224:2**3**2(was 64, now 512) and2**-1**2(was 0.25, now 0.5). Reported upstream as kovidgoyal/rapydscript-ng#195; Kovid fixed it the same way in kovidgoyal/rapydscript-ng@50aa052 (Nov 3).2.
- -xcompiled to--xRapydScript printed a unary operator directly against its operand, so
- -xbecame--x. In JavaScript that's a pre-decrement: it gives the wrong value and changesx.- -3was a syntax error, so the program didn't compile at all.Fix (b185ec0):
print_unary_prefixnow adds a space when a sign operator follows another sign operator. Upstream fixed this the same day as kovidgoyal/rapydscript-ng#194 (ec30155).Testing
tests/test_compiler.pycompiles 31 expressions with the bundled compiler under node and compares each result with CPython: 10 repeated-sign cases and 21 power/unary cases. Before the fixes, 4 and 9 of them fail. After, all pass. It skips if node isn't installed, andpython -m pytest --noconftest tests/test_compiler.pyruns it without the Flask app.Through GlowScriptOffline:
The 13 rows still differing are the known
%, round-half, bool andeitems from Arithmetic that differs from Python: % with negatives, ** grouping, round halves, bool as int, e #224.**kwargsin calls and definitions, comprehensions, and conditionals containing**all behave as before.The same compiler is on Port glowscript compiler fixes: randint with negatives, 3/-3, and - -x rsWVPRunner#5, and the trinket trials will run it as rsWVPRunner v3.2.4-rc3.
🤖 Generated with Claude Code