From c8ce785f49200ea434e7d2ea65b7eb55bf9f501e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 4 Jul 2021 14:24:08 -0700 Subject: [PATCH] macro: Move nesting_countdown into macro_expander. --- src/language/lexer/macro.c | 80 ++++++++++++++------------------------ 1 file changed, 30 insertions(+), 50 deletions(-) diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c index 0a5dfb86cc..82e71bb93e 100644 --- a/src/language/lexer/macro.c +++ b/src/language/lexer/macro.c @@ -872,6 +872,7 @@ struct macro_expander enum segmenter_mode segmenter_mode; struct stringi_map *vars; bool *expand; + int nesting_countdown; }; /* Each argument to a macro function is one of: @@ -892,15 +893,13 @@ struct parse_macro_function_ctx { const struct macro_token *input; size_t n_input; - int nesting_countdown; const struct macro_expander *me; const struct macro_expansion_stack *stack; bool *expand; }; static void -macro_expand (const struct macro_tokens *, int nesting_countdown, - const struct macro_expander *, +macro_expand (const struct macro_tokens *, const struct macro_expander *, const struct macro_expansion_stack *stack, bool *break_, struct macro_tokens *exp); @@ -972,7 +971,6 @@ parse_function_arg (struct parse_macro_function_ctx *ctx, struct parse_macro_function_ctx subctx = { .input = &ctx->input[i], .n_input = ctx->n_input - i, - .nesting_countdown = ctx->nesting_countdown, .me = ctx->me, .stack = ctx->stack, }; @@ -1247,7 +1245,7 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, macro_tokens_from_string__ (&mts, ss_cstr (args.strings[0]), ctx->me->segmenter_mode, ctx->stack); struct macro_tokens exp = { .n = 0 }; - macro_expand (&mts, ctx->nesting_countdown - 1, ctx->me, + macro_expand (&mts, ctx->me, &(struct macro_expansion_stack) { .name = "!EVAL", .next = ctx->stack, @@ -1272,7 +1270,6 @@ expand_macro_function (struct parse_macro_function_ctx *ctx, struct expr_context { - int nesting_countdown; const struct macro_expander *me; const struct macro_expansion_stack *stack; }; @@ -1316,7 +1313,6 @@ macro_evaluate_literal (const struct expr_context *ctx, struct parse_macro_function_ctx fctx = { .input = p, .n_input = end - p, - .nesting_countdown = ctx->nesting_countdown, .me = ctx->me, .stack = ctx->stack, }; @@ -1512,12 +1508,10 @@ macro_evaluate_or (const struct expr_context *ctx, static char * 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) { const struct expr_context ctx = { - .nesting_countdown = nesting_countdown, .me = me, .stack = stack, }; @@ -1526,13 +1520,11 @@ macro_evaluate_expression (const struct macro_token **tokens, size_t n_tokens, static bool 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, double *number) { - char *s = macro_evaluate_expression (tokens, n_tokens, nesting_countdown, - me, stack); + char *s = macro_evaluate_expression (tokens, n_tokens, me, stack); if (!s) return false; @@ -1579,7 +1571,7 @@ find_ifend_clause (const struct macro_token *p, const struct macro_token *end) static size_t macro_expand_if (const struct macro_token *tokens, size_t n_tokens, - int nesting_countdown, const struct macro_expander *me, + const struct macro_expander *me, const struct macro_expansion_stack *stack, bool *break_, struct macro_tokens *exp) { @@ -1590,8 +1582,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, return 0; p++; - char *result = macro_evaluate_expression (&p, end - p, nesting_countdown, - me, stack); + char *result = macro_evaluate_expression (&p, end - p, me, stack); if (!result) return 0; bool b = strcmp (result, "0"); @@ -1658,8 +1649,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, .mts = CONST_CAST (struct macro_token *, start), .n = n, }; - macro_expand (&mts, nesting_countdown, me, - &(struct macro_expansion_stack) { + macro_expand (&mts, me, &(struct macro_expansion_stack) { .name = "!IF", .next = stack, }, @@ -1670,7 +1660,7 @@ macro_expand_if (const struct macro_token *tokens, size_t n_tokens, 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_expander *me, const struct macro_expansion_stack *stack) { const struct macro_token *p = tokens; @@ -1706,8 +1696,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); + char *value = macro_evaluate_expression (&p, end - p, me, stack); if (!value) return 0; @@ -1740,7 +1729,7 @@ find_doend (const struct macro_expansion_stack *stack, 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_expander *me, const struct macro_expansion_stack *stack, struct macro_tokens *exp) { @@ -1775,8 +1764,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, && ss_equals_case (p->token.string, ss_cstr ("!IN"))) { p++; - char *list = macro_evaluate_expression (&p, end - p, nesting_countdown, - me, &next_stack); + char *list = macro_evaluate_expression (&p, end - p, me, &next_stack); if (!list) return 0; @@ -1811,8 +1799,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, ss_xstrdup (items.mts[i].representation)); bool break_ = false; - macro_expand (&inner, nesting_countdown, - me, &next_stack, &break_, exp); + macro_expand (&inner, me, &next_stack, &break_, exp); if (break_) break; } @@ -1822,8 +1809,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, &first)) + if (!macro_evaluate_number (&p, end - p, me, &next_stack, &first)) return 0; if (p >= end || p->token.type != T_MACRO_ID @@ -1836,8 +1822,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, p++; double last; - if (!macro_evaluate_number (&p, end - p, nesting_countdown, - me, &next_stack, &last)) + if (!macro_evaluate_number (&p, end - p, me, &next_stack, &last)) return 0; double by = 1.0; @@ -1845,8 +1830,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, && ss_equals_case (p->token.string, ss_cstr ("!BY"))) { p++; - if (!macro_evaluate_number (&p, end - p, nesting_countdown, - me, &next_stack, &by)) + if (!macro_evaluate_number (&p, end - p, me, &next_stack, &by)) return 0; if (by == 0.0) @@ -1887,8 +1871,7 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, xstrdup (index_s)); bool break_ = false; - macro_expand (&inner, nesting_countdown, - me, &next_stack, &break_, exp); + macro_expand (&inner, me, &next_stack, &break_, exp); if (break_) break; } @@ -1905,12 +1888,12 @@ macro_expand_do (const struct macro_token *tokens, size_t n_tokens, } static void -macro_expand (const struct macro_tokens *mts, int nesting_countdown, +macro_expand (const struct macro_tokens *mts, const struct macro_expander *me, const struct macro_expansion_stack *stack, bool *break_, struct macro_tokens *exp) { - if (nesting_countdown <= 0) + if (me->nesting_countdown <= 0) { macro_error (stack, NULL, _("Maximum nesting level %d exceeded. " "(Use SET MNEST to change the limit.)"), @@ -1941,9 +1924,9 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, .segmenter_mode = me->segmenter_mode, .expand = me->expand, .vars = &vars, + .nesting_countdown = me->nesting_countdown, }; - macro_expand (arg, nesting_countdown, &subme, - &(struct macro_expansion_stack) { + macro_expand (arg, &subme, &(struct macro_expansion_stack) { .name = param->name, .next = stack, }, break_, exp); @@ -1974,8 +1957,9 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, .segmenter_mode = me->segmenter_mode, .expand = me->expand, .vars = &vars, + .nesting_countdown = me->nesting_countdown, }; - macro_expand (arg, nesting_countdown, &subme, + macro_expand (arg, &subme, &(struct macro_expansion_stack) { .name = "!*", .next = stack, @@ -1990,8 +1974,7 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, continue; } - size_t n = macro_expand_if (&mts->mts[i], mts->n - i, - nesting_countdown, me, stack, + size_t n = macro_expand_if (&mts->mts[i], mts->n - i, me, stack, break_, exp); if (n > 0) { @@ -2035,9 +2018,10 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, .segmenter_mode = me->segmenter_mode, .expand = me->expand, .vars = &vars, + .nesting_countdown = me->nesting_countdown - 1, }; - macro_expand (&submc->macro->body, nesting_countdown - 1, - &subme, &(struct macro_expansion_stack) { + macro_expand (&submc->macro->body, &subme, + &(struct macro_expansion_stack) { .name = submc->macro->name, .file_name = submc->macro->file_name, .first_line = submc->macro->first_line, @@ -2072,7 +2056,6 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, struct parse_macro_function_ctx ctx = { .input = &mts->mts[i], .n_input = mts->n - i, - .nesting_countdown = nesting_countdown, .me = me, .stack = stack, }; @@ -2089,17 +2072,14 @@ macro_expand (const struct macro_tokens *mts, int nesting_countdown, continue; } - size_t n = macro_parse_let (&mts->mts[i], mts->n - i, - nesting_countdown, - me, stack); + size_t n = macro_parse_let (&mts->mts[i], mts->n - i, me, stack); if (n > 0) { i += n - 1; continue; } - n = macro_expand_do (&mts->mts[i], mts->n - i, nesting_countdown, me, - stack, exp); + n = macro_expand_do (&mts->mts[i], mts->n - i, me, stack, exp); if (n > 0) { i += n - 1; @@ -2130,6 +2110,7 @@ macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode, .segmenter_mode = segmenter_mode, .expand = &expand, .vars = &vars, + .nesting_countdown = settings_get_mnest (), }; struct macro_expansion_stack stack = { @@ -2138,8 +2119,7 @@ macro_call_expand (struct macro_call *mc, enum segmenter_mode segmenter_mode, .first_line = mc->macro->first_line, .last_line = mc->macro->last_line, }; - macro_expand (&mc->macro->body, settings_get_mnest (), - &me, &stack, NULL, exp); + macro_expand (&mc->macro->body, &me, &stack, NULL, exp); stringi_map_destroy (&vars); } -- 2.30.2