summaryrefslogtreecommitdiff
path: root/stdlib.pl
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 14:19:24 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 14:19:24 +0000
commitee65a81ee5b0112ba4480619ca672c569fb28b45 (patch)
tree974d03d92ef1b0d1f0badcf2986382236b5d905e /stdlib.pl
parent1c8789198373a52da9e80dc9b2b1ee2b67af61c4 (diff)
Add character input/output
Diffstat (limited to 'stdlib.pl')
-rw-r--r--stdlib.pl56
1 files changed, 52 insertions, 4 deletions
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