Well thats a late minute requirement that wasn’t specified
in your original post bullet list. Do you have more such surprises?
In the worst case this is simply a loop inside a loop. It is nowhere
forbidden to apply programming patterns more than once:
/* tail recursive */
loop :-
write('> '), flush_output,
loop2(X),
(X == end_of_file -> true;
write(X), nl,
loop).
/* tail recursive 2 */
loop2(R) :-
current_input(S),
read_line_to_string(S, X),
(sub_string(X, _, _, 0, '\\') ->
sub_string(X, 0, _, 1, Y),
loop2(L),
(L == end_of_file ->
throw(error(syntax_error(continuation_missing),_));
string_concat(Y, L, R));
R = X).
It is based on two data types, atom and string. Thats very
SWI-Prolog specific. So a test X == end_of_file
is not the
same as a test X == "end_of_file"
. Also a little SWI-Prolog
The sub_atom/5, atom_concat/3 and double_quotes flag are
from the ISO core standard, originally for atoms only. I
have used the new sub_string/5 and string_concat/3.
It seems to work as expected:
/* SWI-Prolog 9.3.24 */
?- prompt(_,'').
true.
?- loop.
> foo
foo
> foo\
bar
foobar
>
true.
Using strings, you wont polute the SWI-Prolog atom table.
But beware, read line history works worst with line
continuations. I haven’t seen many good solutions that
could recall a block instead only a line. Also extremly
annoying in many Prolog systems REPLs if you spread a
Prolog clause or query along multiple lines.