From 9440059412fcf3b9ceef4ad128f784c60e6b2c72 Mon Sep 17 00:00:00 2001 From: Andrew Davison Date: Wed, 27 Sep 2023 09:32:42 +1000 Subject: [PATCH] Fix cyclic printing (just the printing aspect of this issue, not the residual), re issue #350 --- src/print.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/print.c b/src/print.c index 83b65ae9f..42604a30e 100644 --- a/src/print.c +++ b/src/print.c @@ -1068,6 +1068,7 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int if (is_postfix(c)) { cell *lhs = c + 1; + cell *save_lhs = lhs; pl_idx lhs_ctx = c_ctx; slot *e = NULL; @@ -1089,6 +1090,12 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int if ((c->val_off == g_plus_s) && search_op(q->st.m, C_STR(q, lhs), NULL, true) && lhs->arity) space = true; if (isalpha(*src)) space = true; + if ((lhs == save_c) && (lhs_ctx == save_c_ctx)) { + SB_sprintf(q->sb, "%s", !is_ref(save_lhs) ? C_STR(q, save_lhs) : "_"); + q->last_thing = WAS_OTHER; + return true; + } + if ((q->last_thing != WAS_SPACE) && space) { SB_sprintf(q->sb, "%s", " "); q->last_thing = WAS_SPACE; @@ -1110,6 +1117,7 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int } cell *rhs = c + 1; + cell *save_rhs = rhs; pl_idx rhs_ctx = c_ctx; slot *e = NULL; uint32_t save_vgen = 0; @@ -1150,6 +1158,12 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int SB_strcatn(q->sb, src, srclen); if (quote) { SB_sprintf(q->sb, "%s", quote?"' ":""); } + if ((rhs == save_c) && (rhs_ctx == save_c_ctx)) { + SB_sprintf(q->sb, "%s", !is_ref(save_rhs) ? C_STR(q, save_rhs) : "_"); + q->last_thing = WAS_OTHER; + return true; + } + if (space || parens) { SB_sprintf(q->sb, "%s", " "); q->last_thing = WAS_SPACE; @@ -1162,6 +1176,7 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int return true; } + q->last_thing = WAS_OTHER; if (parens) { SB_sprintf(q->sb, "%s", "("); q->last_thing = WAS_OTHER; } q->parens = parens; print_term_to_buf_(q, rhs, rhs_ctx, running, 0, 0, depth+1); @@ -1173,8 +1188,10 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int // Infix.. cell *lhs = c + 1; + cell *save_lhs = lhs; pl_idx lhs_ctx = c_ctx; cell *rhs = lhs + lhs->nbr_cells; + cell *save_rhs = rhs; pl_idx rhs_ctx = c_ctx; slot *e = NULL; uint32_t save_vgen = 0; @@ -1207,6 +1224,9 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int if (q->last_thing != WAS_SPACE) SB_sprintf(q->sb, "%s", " "); SB_sprintf(q->sb, "%s", "..."); q->last_thing = WAS_SYMBOL; + } else if ((lhs == save_c) && (lhs_ctx == save_c_ctx)) { + SB_sprintf(q->sb, "%s", !is_ref(save_lhs) ? C_STR(q, save_lhs) : "_"); + q->last_thing = WAS_OTHER; } else { if (lhs_parens) { SB_sprintf(q->sb, "%s", "("); } q->parens = lhs_parens; @@ -1306,6 +1326,9 @@ static bool print_term_to_buf_(query *q, cell *c, pl_idx c_ctx, int running, int if (q->last_thing != WAS_SPACE) SB_sprintf(q->sb, "%s", " "); SB_sprintf(q->sb, "%s", "..."); q->last_thing = WAS_SYMBOL; + } else if ((rhs == save_c) && (rhs_ctx == save_c_ctx)) { + SB_sprintf(q->sb, "%s", !is_ref(save_rhs) ? C_STR(q, save_rhs) : "_"); + q->last_thing = WAS_OTHER; } else { if (rhs_parens) { SB_sprintf(q->sb, "%s", "("); q->last_thing = WAS_OTHER; } q->parens = rhs_parens || space;