From ee65a81ee5b0112ba4480619ca672c569fb28b45 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Fri, 16 Jul 2021 14:19:24 +0000 Subject: Add character input/output --- stdlib.pl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'stdlib.pl') diff --git a/stdlib.pl b/stdlib.pl index d31e3ab..0986781 100644 --- a/stdlib.pl +++ b/stdlib.pl @@ -217,10 +217,6 @@ write_canonical(Term) :- write_canonical(S, Term) :- write_term(S, Term, [quoted(true), ignore_ops(true)]). -nl :- - write_term(' -', []). - % Arithmetic comparisons defined in terms of >=. This is not the most effective way, % but it is fine for now. @@ -501,3 +497,55 @@ atom_concat(A1, A2, A3) :- atom_codes(A2, Codes2). atom_concat(A1, A2, A3) :- instantiation_error. + +% Character input/output + +get_char(Char) :- + current_input(S), + get_char(S, Char). + +get_code(Code) :- + current_input(S), + get_code(S, Code). + +get_code(S, Code) :- + get_char(S, Char), + ( Char = end_of_file + -> Code = -1 + ; char_code(Char, Code) + ). + +peek_char(Char) :- + current_input(S), + peek_char(S, Char). + +peek_code(Code) :- + current_input(S), + peek_code(S, Code). + +peek_code(S, Code) :- + peek_char(S, Char), + ( Char = end_of_file + -> Code = -1 + ; char_code(Char, Code) + ). + +put_char(Char) :- + current_output(S), + put_char(S, Char). + +put_code(Code) :- + current_output(S), + put_code(S, Code). + +put_code(S, Code) :- + char_code(Char, Code), + put_char(S, Char). + +nl :- + current_output(S), + nl(S). + +nl(S) :- + put_char(S, ' +'). % This should really be \n -- cgit v1.2.3