Skip to content

Commit

Permalink
Fix cyclic printing (just the printing aspect of this issue, not the …
Browse files Browse the repository at this point in the history
…residual), re issue #350
  • Loading branch information
infradig committed Sep 26, 2023
1 parent 50d93b4 commit 9440059
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9440059

Please sign in to comment.