From c3c53017f9b8b95cd54b3b7d1c11b8bb6cd783b9 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Tue, 26 Nov 2019 19:38:43 -0700 Subject: [PATCH] add ambiguity check for period printing, remove extraneous space between last goal and period (#237) --- src/prolog/toplevel.pl | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/prolog/toplevel.pl b/src/prolog/toplevel.pl index d04d7b10..63433554 100644 --- a/src/prolog/toplevel.pl +++ b/src/prolog/toplevel.pl @@ -72,13 +72,46 @@ ; write_term(G, [quoted(true), variable_names(VarList)]) ). +'$write_last_goal'(G, VarList) :- + ( G = (Var = Value) -> + write(Var), + write(' = '), + ( '$needs_bracketing'(Value, (=)) -> + write('('), + write_term(Value, [quoted(true), variable_names(VarList)]), + write(')') + ; write_term(Value, [quoted(true), variable_names(VarList)]), + ( '$trailing_period_is_ambiguous'(Value) -> + write(' ') + ; true + ) + ) + ; G == [] -> + write('true') + ; write_term(G, [quoted(true), variable_names(VarList)]) + ). + '$write_eq'((G1, G2), VarList) :- !, '$write_goal'(G1, VarList), write(', '), '$write_eq'(G2, VarList). '$write_eq'(G, VarList) :- - '$write_goal'(G, VarList). + '$write_last_goal'(G, VarList). + +'$graphic_token_char'(C) :- + memberchk(C, ['#', '$', '&', '*', '+', '-', '.', ('/'), ':', + '<', '=', '>', '?', '@', '^', '~', ('\\')]). + +'$list_last_item'([C], C) :- !. +'$list_last_item'([_|Cs], D) :- + '$list_last_item'(Cs, D). + +'$trailing_period_is_ambiguous'(Value) :- + atom(Value), + atom_chars(Value, ValueChars), + '$list_last_item'(ValueChars, Char), + '$graphic_token_char'(Char). '$write_eqs_and_read_input'(B, VarList) :- sort(VarList, SortedVarList), @@ -89,7 +122,7 @@ write('true.'), nl ; thread_goals(Goals, ThreadedGoals, (',')), '$write_eq'(ThreadedGoals, VarList), - write(' .'), + write('.'), nl ) ; thread_goals(Goals, ThreadedGoals, (',')), -- 2.54.0