On Tue, May 13, 2025 at 11:22:29PM -0400, Nikolaos Chatzikonstantinou wrote: > > At any rate, I suspect you will find that you need to tweak m4p's eval > > to match: > > > > $ m4 > > eval(-1%3) > > -1 > > eval((0x7fffffff+2)%3) > > -1 > > $ m4p > > eval(-1%3) > > 2 > > eval((0x7fffffff+2)%3) > > 0 > > Those are useful examples. The +2 is accomplishing the same as writing > 0x80000001 in both cases, and again that's equal to (int)0x80000001L > assuming that reinterprets the bits in two's complement with 32bit > int. Where things really go south is in modular arithmetic, but not > because of the casts, but because Python's % behaves differently to > C's: in short it seems that C follows the dividers sign while Python > follows the divisors sign. I fixed that in the eval parser; let me > know if there's further examples where the results differ from C due > to casts, but I hope that settles it!
Python // rounds to -inf, not 0, which means you also need to fix: $ echo 'eval(-1/3)' | m4 0 $ echo 'eval(-1/3)' | m4p -1 > > P.S. The latest version v0.3.6 supports -D and $@ and $*, although I > have not implemented tail-call recursion yet. (To be frank, I'm not > sure how to do that, I'll have to look into it) Based on my initial testing, you aren't correctly rescanning macro output, and you're eating too much whitespace: $ m4 define(`foreach', `ifelse(`$2', `', `', `$1(`$2')`'$0(`$1', shift(shift($@)))')')dnl define(`echo', `$*.')dnl foreach(`echo', 1, 2, `3 4', (5, 6), `7, 8') 1.2.3 4.(5, 6).7, 8. echo((5, 6)) (5, 6). $ m4p define(`foreach', `ifelse(`$2', `', `', `$1(`$2')`'$0(`$1', shift(shift($@)))')')dnl define(`echo', `$*.')dnl foreach(`echo', 1, 2, `3 4', (5, 6), `7, 8') 1. echo((5, 6) (5,6). Naive tail-call recursion, where you do O(n^2) parsing by simply having every macro expansion result in more text to be rescanned, is better than nothing. Getting to O(n) tail-call recursion the way branch-1.6 is approaching is indeed much harder, because your parser has to track extra data about when it can share references to earlier text to bypass rescanning that text on its next encounter. -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org