chiark / gitweb /
Further macro expansion tests + fixes.
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 4 May 2008 11:38:17 +0000 (12:38 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 4 May 2008 11:38:17 +0000 (12:38 +0100)
lib/macros.c
lib/t-macros.c

index d5786a0dcd2e0aebd7b067a3fb2e0d39ffdd2811..98e1436fa779787ccab487b1de3d4f59bf353378 100644 (file)
@@ -557,13 +557,13 @@ int mx_expand_file(const char *path,
  * @param h Hash mapping argument names to argument values
  * @return Rewritten parse tree
  */
-static const struct mx_node *mx__rewrite(const struct mx_node *m,
+static const struct mx_node *mx__rewrite(const struct mx_node *definition,
                                          hash *h) {
-  const struct mx_node *head = 0, **tailp = &head, *argvalue, *mm;
+  const struct mx_node *head = 0, **tailp = &head, *argvalue, *m, *mm;
   struct mx_node *nm;
   int n;
   
-  for(; m; m = m->next) {
+  for(m = definition; m; m = m->next) {
     switch(m->type) {
     case MX_TEXT:
       nm = xmalloc(sizeof *nm);
@@ -596,6 +596,7 @@ static const struct mx_node *mx__rewrite(const struct mx_node *m,
          * values according to h. */
         nm = xmalloc(sizeof *nm);
         *nm = *m;
+        nm->args = xcalloc(nm->nargs, sizeof (struct mx_node *));
         for(n = 0; n < nm->nargs; ++n)
           nm->args[n] = mx__rewrite(m->args[n], h);
         nm->next = 0;
index c246ee5f27b6fb8075d4b55d85b3d77b3ea7b51e..79c430eeea0dfe4c0555196e6066ce93d5bb061c 100644 (file)
@@ -182,7 +182,7 @@ static void test_macros(void) {
   check_integer(mx_expandstr(m, &s, 0/*u*/, NAME), 0);          \
   if(s && strcmp(s, OUTPUT)) {                                  \
     fprintf(stderr, "%s:%d: test %s\n"                          \
-            "     INPUT: %s\n"                                  \
+            "     INPUT:\n%s\n"                                 \
             "  EXPECTED: '%s'\n"                                \
             "       GOT: '%s'\n",                               \
             __FILE__, __LINE__, NAME, INPUT, OUTPUT, s);        \
@@ -260,6 +260,21 @@ static void test_macros(void) {
   check_macro("macro1", "@define{m}{a b c}{@c@ @b@ @a@}@"
               "@m{1}{2}{3}",
               "3 2 1");
+  check_macro("macro2", "@m{b}{c}{a}",
+              "a c b");
+  check_macro("macro3", "@m{@eq{z}{z}}{p}{q}",
+              "q p true");
+  check_macro("macro4",
+              "@discard{\n"
+              "  @define{n}{a b c}\n"
+              "    {@if{@eq{@a@}{@b@}} {@c@} {no}}\n"
+              "}@"
+              "@n{x}{y}{z}",
+              "no");
+  check_macro("macro5",
+              "@n{x}{x}{z}",
+              "z");
+
 }
 
 TEST(macros);