Move 'expand' into macro_expander.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 4 Jul 2021 20:35:55 +0000 (13:35 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 4 Jul 2021 20:35:55 +0000 (13:35 -0700)
src/language/lexer/macro.c

index ea3a7042487057fef1b22905ca7be03982e5a930..b7962cd35b0b54e6634029c6bbb682e8371f7ffc 100644 (file)
@@ -870,6 +870,7 @@ struct macro_expander
     const struct macro *macro;
     struct macro_tokens **args;
     enum segmenter_mode segmenter_mode;
+    bool *expand;
   };
 
 /* Each argument to a macro function is one of:
@@ -901,8 +902,7 @@ static void
 macro_expand (const struct macro_tokens *, int nesting_countdown,
               const struct macro_expander *, struct string_map *vars,
               const struct macro_expansion_stack *stack,
-              bool *expand, bool *break_,
-              struct macro_tokens *exp);
+              bool *break_, struct macro_tokens *exp);
 
 static bool
 expand_macro_function (struct parse_macro_function_ctx *ctx,
@@ -979,7 +979,6 @@ parse_function_arg (struct parse_macro_function_ctx *ctx,
         .me = ctx->me,
         .stack = ctx->stack,
         .vars = ctx->vars,
-        .expand = ctx->expand,
       };
       size_t subinput_consumed;
       if (expand_macro_function (&subctx, farg, &subinput_consumed))
@@ -1256,7 +1255,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx,
                     &(struct macro_expansion_stack) {
                       .name = "!EVAL",
                       .next = ctx->stack,
-                    }, ctx->expand, NULL, &exp);
+                    }, NULL, &exp);
       macro_tokens_to_representation (&exp, output, NULL, NULL);
       macro_tokens_uninit (&exp);
       macro_tokens_uninit (&mts);
@@ -1281,7 +1280,6 @@ struct expr_context
     const struct macro_expander *me;
     const struct macro_expansion_stack *stack;
     struct string_map *vars;
-    bool *expand;
   };
 
 static char *macro_evaluate_or (const struct expr_context *ctx,
@@ -1327,7 +1325,6 @@ macro_evaluate_literal (const struct expr_context *ctx,
     .me = ctx->me,
     .stack = ctx->stack,
     .vars = ctx->vars,
-    .expand = ctx->expand,
   };
   struct string function_output = DS_EMPTY_INITIALIZER;
   size_t function_consumed = parse_function_arg (&fctx, 0, &function_output);
@@ -1524,14 +1521,13 @@ macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens,
                            int nesting_countdown,
                            const struct macro_expander *me,
                            const struct macro_expansion_stack *stack,
-                           struct string_map *vars, bool *expand)
+                           struct string_map *vars)
 {
   const struct expr_context ctx = {
     .nesting_countdown = nesting_countdown,
     .me = me,
     .stack = stack,
     .vars = vars,
-    .expand = expand,
   };
   return macro_evaluate_or (&ctx, tokens, *tokens + n_tokens);
 }
@@ -1541,11 +1537,10 @@ macro_evaluate_number (const struct macro_token **tokens, size_t n_tokens,
                        int nesting_countdown,
                        const struct macro_expander *me,
                        const struct macro_expansion_stack *stack,
-                       struct string_map *vars,
-                       bool *expand, double *number)
+                       struct string_map *vars, double *number)
 {
   char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown,
-                                       me, stack, vars, expand);
+                                       me, stack, vars);
   if (!s)
     return false;
 
@@ -1595,7 +1590,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
                  int nesting_countdown, const struct macro_expander *me,
                  const struct macro_expansion_stack *stack,
                  struct string_map *vars,
-                 bool *expand, bool *break_, struct macro_tokens *exp)
+                 bool *break_, struct macro_tokens *exp)
 {
   const struct macro_token *p = tokens;
   const struct macro_token *end = tokens + n_tokens;
@@ -1605,7 +1600,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
 
   p++;
   char *result = macro_evaluate_expression (&p, end - p, nesting_countdown,
-                                            me, stack, vars, expand);
+                                            me, stack, vars);
   if (!result)
     return 0;
   bool b = strcmp (result, "0");
@@ -1677,7 +1672,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens,
                       .name = "!IF",
                       .next = stack,
                     },
-                    expand, break_, exp);
+                    break_, exp);
     }
   return (end_if + 1) - tokens;
 }
@@ -1686,7 +1681,7 @@ static size_t
 macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
                  int nesting_countdown, const struct macro_expander *me,
                  const struct macro_expansion_stack *stack,
-                 struct string_map *vars, bool *expand)
+                 struct string_map *vars)
 {
   const struct macro_token *p = tokens;
   const struct macro_token *end = tokens + n_tokens;
@@ -1722,7 +1717,7 @@ macro_parse_let (const struct macro_token *tokens, size_t n_tokens,
   p++;
 
   char *value = macro_evaluate_expression (&p, end - p, nesting_countdown,
-                                           me, stack, vars, expand);
+                                           me, stack, vars);
   if (!value)
     return 0;
 
@@ -1757,8 +1752,7 @@ static size_t
 macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
                  int nesting_countdown, const struct macro_expander *me,
                  const struct macro_expansion_stack *stack,
-                 struct string_map *vars,
-                 bool *expand, struct macro_tokens *exp)
+                 struct string_map *vars, struct macro_tokens *exp)
 {
   const struct macro_token *p = tokens;
   const struct macro_token *end = tokens + n_tokens;
@@ -1792,8 +1786,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
     {
       p++;
       char *list = macro_evaluate_expression (&p, end - p, nesting_countdown,
-                                              me, &next_stack, vars,
-                                              expand);
+                                              me, &next_stack, vars);
       if (!list)
         return 0;
 
@@ -1829,7 +1822,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
 
           bool break_ = false;
           macro_expand (&inner, nesting_countdown,
-                        me, vars, &next_stack, expand, &break_, exp);
+                        me, vars, &next_stack, &break_, exp);
           if (break_)
             break;
         }
@@ -1840,7 +1833,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
       p++;
       double first;
       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
-                                  me, &next_stack, vars, expand, &first))
+                                  me, &next_stack, vars, &first))
         return 0;
 
       if (p >= end || p->token.type != T_MACRO_ID
@@ -1854,7 +1847,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
 
       double last;
       if (!macro_evaluate_number (&p, end - p, nesting_countdown,
-                                  me, &next_stack, vars, expand, &last))
+                                  me, &next_stack, vars, &last))
         return 0;
 
       double by = 1.0;
@@ -1863,7 +1856,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
         {
           p++;
           if (!macro_evaluate_number (&p, end - p, nesting_countdown,
-                                      me, &next_stack, vars, expand, &by))
+                                      me, &next_stack, vars, &by))
             return 0;
 
           if (by == 0.0)
@@ -1905,7 +1898,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens,
 
               bool break_ = false;
               macro_expand (&inner, nesting_countdown,
-                            me, vars, &next_stack, expand, &break_, exp);
+                            me, vars, &next_stack, &break_, exp);
               if (break_)
                 break;
             }
@@ -1925,7 +1918,7 @@ static void
 macro_expand (const struct macro_tokens *mts, int nesting_countdown,
               const struct macro_expander *me, struct string_map *vars,
               const struct macro_expansion_stack *stack,
-              bool *expand, bool *break_, struct macro_tokens *exp)
+              bool *break_, struct macro_tokens *exp)
 {
   if (nesting_countdown <= 0)
     {
@@ -1952,19 +1945,20 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
           if (param)
             {
               const struct macro_tokens *arg = me->args[param - me->macro->params];
-              if (*expand && param->expand_arg)
+              if (*me->expand && param->expand_arg)
                 {
                   struct macro_expander subme = {
                     .macros = me->macros,
                     .macro = NULL,
                     .args = NULL,
                     .segmenter_mode = me->segmenter_mode,
+                    .expand = me->expand,
                   };
                   macro_expand (arg, nesting_countdown, &subme, NULL,
                                 &(struct macro_expansion_stack) {
                                   .name = param->name,
                                   .next = stack,
-                                }, expand, break_, exp);
+                                }, break_, exp);
                 }
               else
                 for (size_t i = 0; i < arg->n; i++)
@@ -1981,19 +1975,20 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
                     break;
 
                   const struct macro_tokens *arg = me->args[j];
-                  if (*expand && param->expand_arg)
+                  if (*me->expand && param->expand_arg)
                     {
                       struct macro_expander subme = {
                         .macros = me->macros,
                         .macro = NULL,
                         .args = NULL,
                         .segmenter_mode = me->segmenter_mode,
+                        .expand = me->expand,
                       };
                       macro_expand (arg, nesting_countdown, &subme, NULL,
                                     &(struct macro_expansion_stack) {
                                       .name = "!*",
                                       .next = stack,
-                                    }, expand, break_, exp);
+                                    }, break_, exp);
                     }
                   else
                     for (size_t k = 0; k < arg->n; k++)
@@ -2005,7 +2000,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
 
           size_t n = macro_expand_if (&mts->mts[i], mts->n - i,
                                       nesting_countdown, me, stack,
-                                      vars, expand, break_, exp);
+                                      vars, break_, exp);
           if (n > 0)
             {
               i += n - 1;
@@ -2025,7 +2020,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
             }
         }
 
-      if (*expand)
+      if (*me->expand)
         {
           struct macro_call *submc;
           int retval = macro_call_create (me->macros, token, &submc);
@@ -2044,6 +2039,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
                 .macro = submc->macro,
                 .args = submc->args,
                 .segmenter_mode = me->segmenter_mode,
+                .expand = me->expand,
               };
               macro_expand (&submc->macro->body, nesting_countdown - 1,
                             &subme, NULL, &(struct macro_expansion_stack) {
@@ -2052,7 +2048,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
                               .first_line = submc->macro->first_line,
                               .last_line = submc->macro->last_line,
                               .next = stack,
-                            }, expand, break_, exp);
+                            }, break_, exp);
               macro_call_destroy (submc);
               continue;
             }
@@ -2084,7 +2080,6 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
         .me = me,
         .stack = stack,
         .vars = vars,
-        .expand = expand,
       };
       struct string function_output = DS_EMPTY_INITIALIZER;
       size_t function_consumed;
@@ -2101,7 +2096,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
 
       size_t n = macro_parse_let (&mts->mts[i], mts->n - i,
                                   nesting_countdown,
-                                  me, stack, vars, expand);
+                                  me, stack, vars);
       if (n > 0)
         {
           i += n - 1;
@@ -2109,7 +2104,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
         }
 
       n = macro_expand_do (&mts->mts[i], mts->n - i, nesting_countdown, me,
-                           stack, vars, expand, exp);
+                           stack, vars, exp);
       if (n > 0)
         {
           i += n - 1;
@@ -2117,9 +2112,9 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown,
         }
 
       if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
-        *expand = true;
+        *me->expand = true;
       else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
-        *expand = false;
+        *me->expand = false;
       else
         macro_tokens_add (exp, mt);
     }
@@ -2133,14 +2128,15 @@ macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
 {
   assert (mc->state == MC_FINISHED);
 
+  bool expand = true;
   struct macro_expander me = {
     .macros = mc->macros,
     .macro = mc->macro,
     .args = mc->args,
     .segmenter_mode = segmenter_mode,
+    .expand = &expand,
   };
 
-  bool expand = true;
   struct macro_expansion_stack stack = {
     .name = mc->macro->name,
     .file_name = mc->macro->file_name,
@@ -2148,6 +2144,6 @@ macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode,
     .last_line = mc->macro->last_line,
   };
   macro_expand (&mc->macro->body, settings_get_mnest (),
-                &me, NULL, &stack, &expand, NULL, exp);
+                &me, NULL, &stack, NULL, exp);
 }