On Wed, May 14, 2025 at 10:33:32AM -0400, Nikolaos Chatzikonstantinou wrote:
> > Or maybe it is nested shift that is broken:
> > $ m4p
> > shift(1, 2, 3)
> > 2,3
> > shift(shift(1, 2, 3))
> >
> > $
> >
> > the second one should have output 3, not empty.  Maybe that means your
> > output of $@ is wrong (conceptually, "1, 2, 3" passed to $* should
> > result in "1,2,3"; and passed to $@ should result in "`1',`2',`3'",
> > and NOT "``1,2,3''").
> 
> Thank you!
> 
> I think I fixed this bug in 0.5.4 just now. There were a couple of
> things I had gotten wrong, perhaps the most stark was that I was
> running the parser on secondary parentheses, giving incorrect results
> e.g. for len((2, 3)) (should be 6).

Closer, but you still need to strip a layer of quotes inside unquoted
parens:

$ m4
define(a,A)define(`echo',`$1')dnl
echo((a,b))
(A,b)
echo(`(a,b)')
(A,b)
echo((`a',`b'))
(A,b)
echo((``a'',``b''))
(a,b)
$ m4p
define(a,A)define(`echo',`$1')dnl
echo((a,b))
(A,b)
echo(`(a,b)')
(A,b)
echo((`a',`b'))
(a,b)
echo((``a'',``b''))
(`a',`b')


At any rate, I can now program m4 iterations - you've made some fast
progress at getting the implementation working, even if not efficient.
Of course, it is also a demonstration of how much slower your
implementation is at the moment:

$ cat for.txt
define(`forloop', `ifelse($1,1000,,`,$0(incr($1))')')
define(`n', `$#')
n(forloop(0))
define(`foreach', `ifelse($#,1,+1,`+1$0(shift($@))')')
eval(foreach(forloop(0)))

On my laptop, I get approximately:
m4 1.4.19:   100ms
m4 1.4.20:    60ms    # Faster, because I improved eval
branch-1.6:   10ms    # Faster, because I improved shift($@) iteration
m4p:        9631ms

More importantly, if I double the number in forloop from 1000 to 2000,
I see:
m4 1.4.19:    270ms    # More than double - shift($@) is still O(n^2)
m4 1.4.20:    240ms    # More than double - shift($@) is still O(n^2)
branch-1.6:    10ms    # In the noise - overhead of startup dominates the O(n) 
scaling
m4p:        37765ms    # Yep, proof that O(n^2) scaling is at work

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


Reply via email to