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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
open Fmlib
open Alba_core
module Command_parser =
Parser_lang.Make (Parser_lang.Command)
module Located = Character_parser.Located
module Make (Io: Io.SIG) =
struct
module Cli_state =
struct
type entry = {
input: string;
context_loaded: Context.t;
context: Context.t;
loaded_file: string option
}
type t = entry option
let init: t =
let context = Standard_context.make () in
Some {
input = "";
context_loaded = context;
context;
loaded_file = None;
}
let clear: t -> t =
Option.map
(fun entry -> {entry with context = entry.context_loaded})
let exit: t = None
let prompt (state: t): string option =
Option.map
(fun entry ->
if entry.input = "" then
"> "
else
"| ")
state
let add (line: string) (state: t): t =
Option.map
(fun entry ->
{entry with
input =
if entry.input = "" then
line
else
entry.input ^ "\n" ^ line
}
)
state
let put_context (context: Context.t) (state: t): t =
Option.map
(fun entry ->
{entry with context; input = ""})
state
let put_loaded (name: string) (context: Context.t): t -> t =
Option.map
(fun _ ->
{
input = "";
context;
context_loaded = context;
loaded_file = Some name;
}
)
let clear_input (state: t): t =
Option.map
(fun entry ->
{entry with input = ""})
state
let entry (state: t): entry =
match state with
| None ->
assert false
| Some entry ->
entry
let input (state: t): string =
(entry state).input
let context (state: t): Context.t =
(entry state).context
end
module Pretty =
struct
module Out =
Fmlib.Io.Output (Io)
include
Pretty_printer.Pretty (Out)
let run (pr: t): unit Io.t =
Out.run
Io.File.stdout
(run 0 80 80 pr)
end
let parse (input: string): Command_parser.parser =
let len = String.length input
in
let module P = Command_parser in
let rec parse i p =
let more = P.needs_more p
in
if i < len && more then
parse (i + 1) (P.put_character p input.[i])
else if more then
P.put_end p
else
p
in
parse 0 P.(make command)
let build_and_compute
(input: string)
(context: Context.t)
(expression: Ast.Expression.t)
(compute: bool)
: Pretty.t
=
match
Build_expression.build expression context
with
| Error problem ->
let module Builder_print = Build_problem.Print (Pretty) in
Builder_print.print_with_source input problem
| Ok (term, typ) ->
let term =
let term =
if compute then
Context.compute term context
else
term
in
match term with
| Term.Typed (term, _) ->
term
| _ ->
term
in
let open Pretty in
let module P = Context.Pretty (Pretty)
in
cut
<+>
P.print Term.(Typed (term, typ)) context
<+>
cut
let add_definition
(def: Ast.Expression.definition)
(state: Cli_state.t)
: Cli_state.t Io.t
=
let open Io in
match
Builder.add_definition def (Cli_state.context state)
with
| Ok context ->
return (Cli_state.put_context context state)
| Error problem ->
let module Builder_print = Build_problem.Print (Pretty) in
Pretty.run
(Builder_print.print_with_source
(Cli_state.input state)
problem)
>>= fun _ ->
return (Cli_state.clear_input state)
let process_input (state: Cli_state.t): Cli_state.t Io.t =
let continue_after action =
Io.(
action
>>= fun _ ->
return (Cli_state.clear_input state))
in
let input = Cli_state.input state
in
let p = parse input
in
assert (Command_parser.has_ended p);
match Command_parser.result p with
| Some cmd ->
assert (Command_parser.has_succeeded p);
(
match cmd with
| Parser_lang.Command.Do_nothing ->
Io.return (Cli_state.clear_input state)
| Parser_lang.Command.Exit ->
Io.return Cli_state.exit
| Parser_lang.Command.Evaluate expression ->
continue_after
(Pretty.run
(build_and_compute
input
(Cli_state.context state)
expression
true))
| Parser_lang.Command.Type_check expression ->
continue_after
(Pretty.run
(build_and_compute
input
(Cli_state.context state)
expression
false))
| Parser_lang.Command.Clear ->
Io.return (Cli_state.clear state)
| Parser_lang.Command.Load file ->
let module Compile = Module.Make (Io) in
Io.(
Compile.compile file >>= function
| None ->
Io.return (Cli_state.clear_input state)
| Some context ->
Io.return
(Cli_state.put_loaded
(Located.value file)
context
state)
)
| Parser_lang.Command.Reload ->
assert false
| Parser_lang.Command.Define def ->
add_definition def state
)
| None ->
let module Printer =
Command_parser.Error_printer (Pretty)
in
continue_after
(Pretty.run
(Printer.print_with_source input p))
let next (state: Cli_state.t) (line: string): Cli_state.t Io.t =
assert (Cli_state.prompt state <> None);
let state = Cli_state.add line state
in
let is_last (line: string): bool =
let len = String.length line in
len = 0
|| line.[len - 1] <> ' '
in
if is_last line then
process_input state
else
Io.return state
let stop (state: Cli_state.t): Cli_state.t Io.t =
Io.return state
let run_cli _: unit Io.t =
Io.(
cli_loop
Cli_state.init
Cli_state.prompt
next
stop
>>= fun _ ->
return ()
)
module Evaluate_stdin =
struct
module Expression_parser =
Parser_lang.Make (Ast.Expression)
module Writable =
struct
type t = {
can_end: bool;
input: string;
parser: Expression_parser.parser;
}
let init (): t =
{
can_end =
false;
input =
"";
parser =
Expression_parser.(make (expression ()));
}
let needs_more (w: t): bool =
Expression_parser.needs_more w.parser
||
not w.can_end
let put_character (w: t) (c: char): t =
let open Expression_parser in
{
can_end =
c = '\n';
input =
w.input ^ Common.String.one c;
parser =
if needs_more w.parser then
put_character w.parser c
else
w.parser;
}
let put_end (w: t): t =
let open Expression_parser in
{ w with
parser =
if needs_more w.parser then
put_end w.parser
else
w.parser;
}
let result (w: t): string * Expression_parser.parser =
w.input,
w.parser
end
let run _: unit Io.t =
let module R = Io.File.Read (Writable) in
let module Error = Fmlib.Io.Error in
Io.(
R.read File.stdin (Writable.init ())
>>= fun io_res ->
match io_res with
| Error (_, error) ->
Pretty.(run
(string (Error.message error) <+> cut)
)
| Ok w ->
let input, p = Writable.result w in
let open Expression_parser
in
assert (has_ended p);
match result p with
| None ->
let module Printer =
Error_printer (Pretty)
in
Pretty.run
(Printer.print_with_source input p)
| Some expression ->
Pretty.run
(build_and_compute
input
(Standard_context.make ())
expression
false)
)
end
let run_eval _: unit Io.t =
Evaluate_stdin.run ()
end