I’m coming from the Ada world, where macros are far from being advertised,
so may be I’m biased being a bit afraid of macro. There are more check with
macdef than with #define, as I recall, but it seems macdef only wants
dynamic identifiers in the expression it introduces, so I’m talking here
about #define, as my question came in the static.
Is this kind of thing, which follows, rather OK or not, and also, is there
another way to do, without #define?
As they were many repetitions, I turned this:
dataprop UTF8_00 (i:int, c:int) =
| {b:hb00; i == 1} UTF8_00_1(i, b) of INPUT(i, b)
dataprop UTF8_C2 (i:int, c:int) =
| {b:hbC2; i == 1} UTF8_C2_1(i, b - 0xC0) of INPUT(i, b)
| {b:tb80; i == 2} UTF8_C2_2(i, added(c, b)) of (UTF8_C2(i-1, c), INPUT(i, b
))
| {b:tb80; i == 2} UTF8_C2_2(i, added(c, b)) of (UTF8_C2(i-1, c), INPUT(i, b
))
dataprop UTF8_E0 (i:int, c:int) =
| {b:hbE0; i == 1} UTF8_E0_1(i, b - 0xE0) of INPUT(i, b)
| {b:tbA0; i == 2} UTF8_E0_2(i, added(c, b)) of (UTF8_E0(i-1, c), INPUT(i, b
))
| {b:tb80; i == 3} UTF8_E0_3(i, added(c, b)) of (UTF8_E0(i-1, c), INPUT(i, b
))
dataprop UTF8_E1 (i:int, c:int) =
| {b:hbE1; i == 1} UTF8_E1_1(i, b - 0xE0) of INPUT(i, b)
| {b:tb80; i == 2} UTF8_E1_2(i, added(c, b)) of (UTF8_E1(i-1, c), INPUT(i, b
))
| {b:tb80; i == 3} UTF8_E1_3(i, added(c, b)) of (UTF8_E1(i-1, c), INPUT(i, b
))
/* more of the like */
Into this, using a #define:
#define NEXT(UTF8_XX) (UTF8_XX(i-1, c), INPUT(i, b))
dataprop UTF8_00 (i:int, c:int) =
| {b:hb00; i == 1} UTF8_00_1(i, b) of INPUT(i, b)
dataprop UTF8_C2 (i:int, c:int) =
| {b:hbC2; i == 1} UTF8_C2_1(i, b - 0xC0) of INPUT(i, b)
| {b:tb80; i == 2} UTF8_C2_2(i, added(c, b)) of NEXT(UTF8_C2)
| {b:tb80; i == 2} UTF8_C2_2(i, added(c, b)) of NEXT(UTF8_C2)
dataprop UTF8_E0 (i:int, c:int) =
| {b:hbE0; i == 1} UTF8_E0_1(i, b - 0xE0) of INPUT(i, b)
| {b:tbA0; i == 2} UTF8_E0_2(i, added(c, b)) of NEXT(UTF8_E0)
| {b:tb80; i == 3} UTF8_E0_3(i, added(c, b)) of NEXT(UTF8_E0)
dataprop UTF8_E1 (i:int, c:int) =
| {b:hbE1; i == 1} UTF8_E1_1(i, b - 0xE0) of INPUT(i, b)
| {b:tb80; i == 2} UTF8_E1_2(i, added(c, b)) of NEXT(UTF8_E1)
| {b:tb80; i == 3} UTF8_E1_3(i, added(c, b)) of NEXT(UTF8_E1)
/* more of the like */
#undef NEXT
Writing things this way helps readability and maintenance (as it is
terser), but is there a risk to face issues, in the large? If there are
people who use #defines a lot in ATS2, what’s your overall feelings and
opinions?
Or is there a better way to do? (for a previous case, I could use a
stadef
, but this won’t work here) As said above, I’m not used to
defines/macro and had bad experiences with C macros, thus my question for
comments, or better (if possible), another track.Le mardi 12 mai 2015 20:00:03 UTC+2, gmhwxi a écrit :
#define and macdef are largely undocumented.
#define is similar to its counterpart in C. At the beginning of the
following page, there
are some interesting cases of using #define:
http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=https://raw.githubusercontent.com/githwxi/ATS-Postiats/master/doc/EXAMPLE/ARITH/tally-of-powers.dats
The following old page contains some explanation on macdef:
http://ats-lang.sourceforge.net/htdocs-old/TUTORIAL/contents/macros.html