[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
allow nameless m4_for iteration
From: |
Eric Blake |
Subject: |
allow nameless m4_for iteration |
Date: |
Sun, 30 Sep 2007 06:49:22 -0600 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070728 Thunderbird/2.0.0.6 Mnenhy/0.7.5.666 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Borrowing an idea from the m4 manual:
m4_for(,1,3,,-) should result in ---, not an error.
2007-09-30 Eric Blake <address@hidden>
Allow nameless iteration.
* lib/m4sugar/m4sugar.m4 (m4_for, _m4_for): Access variable
indirectly.
* tests/m4sugar.at (myvar): Test this.
- --
Don't work too hard, make some time for fun as well!
Eric Blake address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFG/5tR84KuGfSFAYARAgXGAKDZRwOVBJJgLZh6/LL/ywRBZ87KHACgiiXR
upmX02Yz8YYltEpYV/WQQ5E=
=XTaz
-----END PGP SIGNATURE-----
>From 615300a1d5e4a7bb89df2d40b8acf2ede38142d0 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Sun, 30 Sep 2007 06:27:46 -0600
Subject: [PATCH] Allow nameless iteration.
* lib/m4sugar/m4sugar.m4 (m4_for, _m4_for): Access variable
indirectly.
* tests/m4sugar.at (myvar): Test this.
Signed-off-by: Eric Blake <address@hidden>
---
ChangeLog | 7 +++++++
lib/m4sugar/m4sugar.m4 | 17 +++++++++++------
tests/m4sugar.at | 4 ++++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 69d2053..cfe81ea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-09-30 Eric Blake <address@hidden>
+
+ Allow nameless iteration.
+ * lib/m4sugar/m4sugar.m4 (m4_for, _m4_for): Access variable
+ indirectly.
+ * tests/m4sugar.at (myvar): Test this.
+
2007-09-29 Eric Blake <address@hidden>
Speed optimization: avoid m4 regex when other algorithms work.
diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4
index 8fea736..bd3fb17 100644
--- a/lib/m4sugar/m4sugar.m4
+++ b/lib/m4sugar/m4sugar.m4
@@ -580,16 +580,21 @@ m4_builtin([undefine], $@)])
# --------------------------------------------------------
# Expand EXPRESSION defining VARIABLE to FROM, FROM + 1, ..., TO.
# Both limits are included, and bounds are checked for consistency.
+# The algorithm is robust to indirect VARIABLE names.
m4_define([m4_for],
[m4_pushdef([$1], m4_eval([$2]))dnl
-m4_if(m4_eval(([$3]) > $1), 1,
+m4_if(m4_eval(([$3]) > m4_defn([$1])), 1,
[m4_pushdef([_m4_step], m4_eval(m4_default([$4], 1)))dnl
m4_assert(_m4_step > 0)dnl
-_m4_for([$1], m4_eval((([$3]) - $1) / _m4_step * _m4_step + $1), _m4_step,
[$5])],
- m4_eval(([$3]) < $1), 1,
+_m4_for([$1], m4_eval((([$3]) - m4_defn([$1]))
+ / _m4_step * _m4_step + m4_defn([$1])),
+ _m4_step, [$5])],
+ m4_eval(([$3]) < m4_defn([$1])), 1,
[m4_pushdef([_m4_step], m4_eval(m4_default([$4], -1)))dnl
m4_assert(_m4_step < 0)dnl
-_m4_for([$1], m4_eval(($1 - ([$3])) / -(_m4_step) * _m4_step + $1), _m4_step,
[$5])],
+_m4_for([$1], m4_eval((m4_defn([$1]) - ([$3]))
+ / -(_m4_step) * _m4_step + m4_defn([$1])),
+ _m4_step, [$5])],
[m4_pushdef(_m4_step,[])dnl
$5])[]dnl
m4_popdef([_m4_step])dnl
@@ -601,8 +606,8 @@ m4_popdef([$1])])
# Core of the loop, no consistency checks, all arguments are plain numbers.
m4_define([_m4_for],
[$4[]dnl
-m4_if($1, [$2], [],
- [m4_define([$1], m4_eval($1+[$3]))_m4_for([$1], [$2], [$3], [$4])])])
+m4_if(m4_defn([$1]), [$2], [],
+ [m4_define([$1], m4_eval(m4_defn([$1])+[$3]))$0($@)])])
# Implementing `foreach' loops in m4 is much more tricky than it may
diff --git a/tests/m4sugar.at b/tests/m4sugar.at
index 6f3da58..5aff0c9 100644
--- a/tests/m4sugar.at
+++ b/tests/m4sugar.at
@@ -281,6 +281,9 @@ m4_for([myvar], -2+8, 3-5, , [ myvar])
m4_for([myvar], 8, 16, 3 * 2, [ myvar])
m4_for([myvar], 8, 16, -3 * -2, [ myvar])
m4_for([myvar], [2<<2], [2<<3], [-3 * (-2)], [ myvar])
+dnl Make sure we can do nameless iteration
+m4_for(, 1, 10, , -)
+dnl foreach tests
m4_foreach([myvar], [[a], [b, c], [d], [e
],[f]], [ myvar|])
m4_foreach_w([myvar], [a b c, d,e f
@@ -313,6 +316,7 @@ myvar
8 14
8 14
8 14
+----------
a| b, c| d| e
| f|
a| b| c,| d,e| f| g|
--
1.5.3.2
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- allow nameless m4_for iteration,
Eric Blake <=