-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.m
74 lines (63 loc) · 1.82 KB
/
sample.m
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
%-----------------------------------------------------------------------------%
:- module sample.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module bool.
:- import_module list.
:- import_module scanner.
:- import_module tdop.
main(!IO) :-
io.command_line_arguments(Args, !IO),
( Args = ["--echo" | _] ->
loop(yes, !IO)
;
loop(no, !IO)
).
:- pred loop(bool::in, io::di, io::uo) is det.
loop(Echo, !IO) :-
io.write_string("> ", !IO),
io.flush_output(!IO),
io.read_line(ReadRes, !IO),
(
ReadRes = ok(Cs),
( scan(Cs, Tokens) ->
(
Echo = yes,
foldl(io.write_char, Cs, !IO)
;
Echo = no
),
( parse_expr(E, Tokens, TokensRem) ->
io.write(E, !IO),
io.nl(!IO),
(
TokensRem = []
;
TokensRem = [_ | _],
io.write_string("remaining tokens: ", !IO),
io.write(TokensRem, !IO),
io.nl(!IO)
)
;
write_string("parse failed\n", !IO)
)
;
io.write_string("scan failed\n", !IO)
),
io.nl(!IO),
loop(Echo, !IO)
;
ReadRes = eof,
io.nl(!IO)
;
ReadRes = error(Error),
io.write_string("error: ", !IO),
io.write_string(io.error_message(Error), !IO),
io.nl(!IO)
).
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et