From d18de269421e9a3b5b1c254cd4e3927ddc012a97 Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Mon, 20 Oct 2025 07:08:47 -0400 Subject: [PATCH 1/3] Rename baseline for nested_fns test case --- tests/v4_tests/baselines/{nested_fns.sh.py => nested_fns.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/v4_tests/baselines/{nested_fns.sh.py => nested_fns.py} (100%) diff --git a/tests/v4_tests/baselines/nested_fns.sh.py b/tests/v4_tests/baselines/nested_fns.py similarity index 100% rename from tests/v4_tests/baselines/nested_fns.sh.py rename to tests/v4_tests/baselines/nested_fns.py From 4847adca5005fb117ec899f3f8fa6da265e5eeef Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Mon, 20 Oct 2025 07:13:30 -0400 Subject: [PATCH 2/3] Update baseline output for nested_fns test case --- tests/v4_tests/baselines/nested_fns.py | 31 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/v4_tests/baselines/nested_fns.py b/tests/v4_tests/baselines/nested_fns.py index 2ebd89a..5930931 100644 --- a/tests/v4_tests/baselines/nested_fns.py +++ b/tests/v4_tests/baselines/nested_fns.py @@ -1,13 +1,17 @@ #! /usr/bin/env python from __future__ import print_function class Bash2Py(object): + # __slots__ = ["val"] + # def __init__(self, value=''): self.val = value + # def setValue(self, value=None): self.val = value return value + def GetVariable(name, local=locals()): if name in local: return local[name] @@ -22,18 +26,27 @@ def Make(name, local=locals()): globals()[name] = ret return ret - Make("y").setValue("yyyyy") - print("in f2, y is",y.val) -) : +def f1 () : + global x + Make("x").setValue("xxxxx") print("in f1, x is",x.val) - def f2 () : - global x - global y - - Make("y").setValue("yyyyy") - print("in f2, y is",y.val) f2() +def f2 () : + global y + + Make("y").setValue("yyyyy") + print("in f2, y is",y.val) + + f3() + +def f3 () : + global z + + Make("z").setValue("zzzzz") + print("in f3, z is",z.val) + + f1() From a7ce954fca2c560ffbd2c8a8dc84866399891ca2 Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Mon, 20 Oct 2025 07:40:28 -0400 Subject: [PATCH 3/3] Restore printing function contents; use swap_burps --- burp.c | 10 ++++++++++ burp.h | 2 ++ fix_string.c | 15 +++------------ translate.c | 16 +++++++--------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/burp.c b/burp.c index 0232edd..9aa006d 100755 --- a/burp.c +++ b/burp.c @@ -51,6 +51,16 @@ char *strdup(const char *s) { } #endif +void swap_burps(burpT *oldP, burpT *newP) +{ + burpT temp; + + temp = *oldP; + *oldP = *newP; + *newP = temp; +} + + // Reset a burp without freeing its buffer, reducing explicit memory management. // When in doubt use burp_close() instead. void burp_reset(burpT *burpP) diff --git a/burp.h b/burp.h index ed86622..1109241 100755 --- a/burp.h +++ b/burp.h @@ -43,6 +43,8 @@ void burpn(burpT *burpP, const char *stringP, int lth); void burps(burpT *burpP, const char *stringP); void burp_esc_quote(burpT *burpP, int offset, int quote); void burp_rtrim(burpT *burpP); +void swap_burps(burpT *oldP, burpT *newP); + #define INDENT(X) X.m_indent++ #define OUTDENT(X) { assert(0 < X.m_indent); X.m_indent--; } diff --git a/fix_string.c b/fix_string.c index c1bea4e..6a020c3 100755 --- a/fix_string.c +++ b/fix_string.c @@ -27,15 +27,6 @@ extern void seen_global(const char *nameP, _BOOL local); char g_regmatch_var_name[] = "BASH_REMATCH"; -static void exchange_burp_buffers(burpT *oldP, burpT *newP) -{ - burpT temp; - - temp = *oldP; - *oldP = *newP; - *newP = temp; -} - // Finds and returns end of a quoted string (or NULL if improperly quoted) char * endQuotedString(char *stringP) { @@ -483,7 +474,7 @@ static void replaceSingleQuotes(void) } } burpc(&g_new, c); } - exchange_burp_buffers(&g_buffer, &g_new); + swap_burps(&g_buffer, &g_new); } // emitQuoted(): Quotes the supplied string @@ -1691,7 +1682,7 @@ static char * fix_string1(fix_typeE want, fix_typeE *gotP) // Everything has been written to g_new // Make it look as if it never left g_buffer - exchange_burp_buffers(&g_buffer, &g_new); + swap_burps(&g_buffer, &g_new); compactWhiteSpace(); rename_special(); @@ -1706,7 +1697,7 @@ static char * fix_string1(fix_typeE want, fix_typeE *gotP) got = FIX_EXPRESSION; g_new.m_lth = 0; burps(&g_new, translationP); - exchange_burp_buffers(&g_buffer, &g_new); + swap_burps(&g_buffer, &g_new); } } unmarkExpand(); diff --git a/translate.c b/translate.c index 1867dec..37f0862 100755 --- a/translate.c +++ b/translate.c @@ -2680,7 +2680,6 @@ static void reset_locals () static void print_function_def (FUNCTION_DEF *func) { - burpT temp; COMMAND *cmdcopy; REDIRECT *func_redirects; char *nameP = func->name->word; @@ -2692,9 +2691,7 @@ static void print_function_def (FUNCTION_DEF *func) func_redirects = NULL; burp(&g_output, "def %s (", nameP); - temp = save; - save = g_output; - g_output = temp; + swap_burps(&save, &g_output); g_output.m_lth = 0; burps(&g_output, " "); INDENT(g_output); @@ -2713,7 +2710,7 @@ static void print_function_def (FUNCTION_DEF *func) g_function_parms_count = 0; emit_command (cmdcopy->type == cm_group ? cmdcopy->value.Group->command - : cmdcopy); + : cmdcopy); // "g_output" now holds the function body remove_unwind_protect (); @@ -2729,7 +2726,7 @@ static void print_function_def (FUNCTION_DEF *func) // Insert global variable declarations if (g_variable_namesP) { for (current_varP = g_variable_namesP; current_varP; current_varP = next_varP) { - burp(&save, " global %s\n", current_varP->m_nameP); + burp(&save, " global %s\n", current_varP->m_nameP); // the indent here should match INDENT, dont assume its 4 next_varP = current_varP->m_nextP; free(current_varP->m_nameP); free(current_varP); @@ -2738,12 +2735,13 @@ static void print_function_def (FUNCTION_DEF *func) burpc(&save, '\n'); } + // "save" holds old backlog + function header "def foo(...):" + global declarations + // "g_output" holds the function body. We need swap-merge these. newline(""); OUTDENT(g_output); newline(""); - temp = save; - save = g_output; - g_output = temp; + burps(&save, g_output.m_P); + swap_burps(&save, &g_output); g_is_inside_function = FALSE;