summaryrefslogtreecommitdiff
path: root/read.prolog
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-03-18 13:08:01 +1100
committerJed Barber <jjbarber@y7mail.com>2014-03-18 13:08:01 +1100
commit52d43529322de3896143b640415ea9ef8998712a (patch)
tree19b571a2797da621daf13824dcddf55c0608c3ba /read.prolog
parent6f066415dc9b296cafa3f214400b2ea1625f5312 (diff)
Changed file extensions to ensure correct language detection on github
Diffstat (limited to 'read.prolog')
-rw-r--r--read.prolog56
1 files changed, 56 insertions, 0 deletions
diff --git a/read.prolog b/read.prolog
new file mode 100644
index 0000000..63d3ae7
--- /dev/null
+++ b/read.prolog
@@ -0,0 +1,56 @@
+
+:- module(read, [prompt/1, readList/1]).
+
+
+% functions for obtaining a line of input text and parsing
+% it into a list of words
+
+% prompt provides the user with a '>' prompt as well
+% readList merely obtains and parses the input
+
+
+prompt(L) :-
+ write('> '),
+ readList(L).
+
+
+readList(L) :-
+ readLine(X),
+ wordList(L,X,[]), !.
+
+
+readLine(L) :-
+ get_char(C),
+ readLine_tail(C,L).
+
+
+readLine_tail('\n',[]) :- !.
+readLine_tail(C,[C|X]) :-
+ get_char(C2),
+ readLine_tail(C2,X).
+
+
+wordList(X) --> whitespace, wordList(X).
+wordList([X]) --> word(X).
+wordList([X]) --> word(X), whitespace.
+wordList([X|Y]) --> word(X), whitespace, wordList(Y).
+
+
+word(W) --> charList(X), {atom_chars(W,X)}.
+
+
+charList([X|Xs]) --> char(X), charList(Xs).
+charList([X]) --> char(X).
+
+
+whitespace --> whsp, whitespace.
+whitespace --> whsp.
+
+
+whsp --> oneOf(_,[' ', '\r', '\n', '\t']).
+char(X) --> noneOf(X,[' ', '\r', '\n', '\t']).
+
+
+oneOf(X,L) --> [X], {member(X,L)}.
+noneOf(X,L) --> [X], {not(member(X,L))}.
+