-
Notifications
You must be signed in to change notification settings - Fork 0
/
readLine.pl
108 lines (75 loc) · 3.41 KB
/
readLine.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*************************************************************************
File: readLine.pl
Copyright (C) 2004,2005,2006 Patrick Blackburn & Johan Bos
This file is part of BB1, version 1.3 (November 2006).
BB1 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
BB1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BB1; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*************************************************************************/
:- module(readLine,[readLine/1]).
/*========================================================================
Output prompt, read from standard input and convert into list
========================================================================*/
readLine(WordList):-
nl, write('> '),
readWords(Words),
checkWords(Words,WordList).
/*========================================================================
Read in a sequence of characters, until a return is registered
========================================================================*/
readWords([Word|Rest]):-
get0(Char),
readWord(Char,Chars,State),
name(Word,Chars),
readRest(Rest,State).
readRest([],ended).
readRest(Rest,notended):-
readWords(Rest).
/*========================================================================
Read a word coded as Chars (a list of ascii values), starting
with with ascii value Char, and determine the State of input
(`ended' = end of line, `notended' = not end of line).
Blanks and full stops split words, a return ends a line.
========================================================================*/
readWord(32,[],notended):-!. %%% blank
readWord(46,[],notended):-!. %%% full stop
readWord(10,[],ended):-!. %%% return
readWord(Code,[Code|Rest],State):-
get0(Char),
readWord(Char,Rest,State).
/*========================================================================
Check if all words are unquoted atoms, if not convert them into atoms.
========================================================================*/
checkWords([],[]):- !.
checkWords([''|Rest1],Rest2):-
checkWords(Rest1,Rest2).
checkWords([Atom|Rest1],[Atom2|Rest2]):-
name(Atom,Word1),
convertWord(Word1,Word2),
name(Atom2,Word2),
checkWords(Rest1,Rest2).
/*========================================================================
Convert upper into lower case characters, and eliminate
non-alphabetic characters.
========================================================================*/
convertWord([],[]):- !.
convertWord([Capital|Rest1],[Small|Rest2]):-
Capital > 64, Capital < 91, !,
Small is Capital + 32,
convertWord(Rest1,Rest2).
convertWord([Number|Rest1],[Number|Rest2]):-
Number > 47, Number < 58, !,
convertWord(Rest1,Rest2).
convertWord([Weird|Rest1],Rest2):-
(Weird < 97; Weird > 122), !,
convertWord(Rest1,Rest2).
convertWord([Char|Rest1],[Char|Rest2]):-
convertWord(Rest1,Rest2).