package alba

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file module.ml

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
open Fmlib
open Common
open Alba_core


module Parser = Parser_lang.Make (Unit)
module Located = Character_parser.Located



module Compiler =
struct
    type lines =
        string Sequence.t

    type error =
        | Parse_error
        | Build_error of Build_problem.t

    type value =
        Term.t * Term.typ * Context.t

    type t = {
        lines: lines;
        line:  string;
        terminate_line: bool; (* error occurred, but line not yet completed. *)
        parser: Parser.parser;
        context: Context.t;
        values: value list;
        error:  error option;
    }


    let make_from_context (context: Context.t): t =
        {
            lines = Sequence.empty;

            line  = "";

            terminate_line = false;

            parser = Parser.(make (source_file true));

            context;

            values = [];

            error = None;
        }


    let make _: t =
        make_from_context (Standard_context.make ())


    let context (c: t): Context.t =
        c.context


    let needs_more (compiler: t): bool =
        (
            compiler.error = None
            &&
            Parser.needs_more compiler.parser
        )
        ||
        compiler.terminate_line


    let has_succeeded (compiler: t): bool =
        Parser.has_succeeded compiler.parser
        &&
        compiler.error = None



    let make_semantic
        (parser: Parser.parser) (compiler: t)
        : error option * value list * Context.t
        =
        let open Parser_lang in
        let open Source_file in
        match top (Parser.state parser) with
        | Expression (eval_flag, expression) ->
        (
            match Build_expression.build expression compiler.context with
            | Error problem ->
                Some (Build_error problem),
                compiler.values,
                compiler.context

            | Ok (term, typ) ->
                let term, typ =
                    match term with
                    | Term.Typed (term, typ) ->
                        term, typ
                    | _ ->
                        term, typ
                in
                let term =
                    if eval_flag then
                        Context.compute term compiler.context
                    else
                        term
                in
                None,
                (term, typ, compiler.context) :: compiler.values,
                compiler.context
        )
        | Entry entry ->
        (
            match Builder.add_entry entry compiler.context with
            | Ok context ->
                None, compiler.values, context

            | Error problem ->
                Some (Build_error problem),
                compiler.values,
                compiler.context
        )


    let add_character (c: char) (compiler: t) : string * lines =
        if c = '\n' then
            "",
            Sequence.push compiler.line compiler.lines
        else
            compiler.line ^ String.one c,
            compiler.lines


    let make_step (c: char) (parser: Parser.parser) (compiler: t): t =
        (* The parser has already made its step, now we do the semantics. *)
        let failed = Parser.has_failed parser
        in
        let error, values, context =
            if failed then
                Some Parse_error, compiler.values, compiler.context
            else
                let src0 = Parser.state compiler.parser
                and src1 = Parser.state parser
                in
                if
                    Parser_lang.Source_file.(count src0 < count src1)
                then
                    make_semantic parser compiler
                else
                    None, compiler.values, compiler.context
        and line, lines =
            add_character c compiler
        in
        {
            line;

            lines;

            terminate_line = failed (*&& c <> '\n'*);

            parser;

            context;

            error;

            values;
        }


    let put_character (compiler: t) (c: char): t =
        assert (needs_more compiler);
        if compiler.terminate_line then
            let line, lines = add_character c compiler in
            { compiler with
                line;

                lines;

                terminate_line =
                    c <> '\n';
            }
        else
            let parser =
                Parser.put_character compiler.parser c
            in
            make_step c parser compiler



    let put_end (compiler: t): t =
        assert (needs_more compiler);
        if compiler.terminate_line then
            let line, lines = add_character '\n' compiler in
            { compiler with
                line;
                lines;
                terminate_line = false;
            }
        else
            let parser = Parser.put_end compiler.parser in
            make_step '\n' parser compiler



    module Print (Pretty: Pretty_printer.SIG) =
    struct
        let print_error (compiler: t): Pretty.t =
            let lines =
                Sequence.push compiler.line compiler.lines
            in
            match compiler.error with
            | None ->
                assert false (* Illegal call! *)
            | Some Parse_error ->
                let module Parser_print =
                    Parser.Error_printer (Pretty)
                in
                Parser_print.print_with_source_lines
                    lines
                    compiler.parser
            | Some (Build_error problem) ->
                let module Builder_print =
                    Build_problem.Print (Pretty)
                in
                Builder_print.print_with_source_lines
                    lines
                    problem

        let print_values (compiler: t): Pretty.t =
            let module Context_print = Context.Pretty (Pretty) in
            Pretty.(chain
                (List.rev_map
                    (fun (term, typ, context) ->
                        Context_print.print
                            Term.(Typed (term, typ))
                            context
                        <+>
                        cut)
                    compiler.values)
            )
    end
end





module Make (Io: Io.SIG) =
struct
    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 (* Pretty *)



    let compile (file: string Located.t): Context.t option Io.t =
        let name = Located.value file
        in
        let open Io in
        File.In.open_ name
        >>= function
        | Error error ->
            Pretty.(run
                (
                    string "Cannot open file '" <+> string name <+> char '\''
                    <+> cut <+> cut
                    <+> string (Fmlib.Io.Error.message error)
                    <+> cut <+> cut
                )
            )
            >>= fun _ ->
            return None

        | Ok fd ->
            let module Reader = File.Read (Compiler) in
            Reader.read fd (Compiler.make ())
            >>= fun io_result ->
            File.In.close fd
            >>= fun _ ->
            match io_result with
            | Error (_, error) ->
                let module Io_error = Fmlib.Io.Error in
                    Pretty.(run (string (Io_error.message error) <+> cut))
                    >>= fun _ ->
                    return None

            | Ok compiler ->
                let module Print = Compiler.Print (Pretty) in
                if Compiler.has_succeeded compiler then
                    return (Some (Compiler.context compiler))
                else
                    Pretty.run (Print.print_error compiler)
                    >>= fun _ -> return None


    let run _: unit Io.t =
        let module Reader = Io.File.Read (Compiler) in
        let open Io in
        Reader.read File.stdin (Compiler.make ())
        >>= fun io_result ->
        match io_result with
        | Error (_, error) ->
            let module Io_error = Fmlib.Io.Error in
            Io.(
                Pretty.(run (string (Io_error.message error) <+> cut))
                >>= fun _ ->
                exit 1)
        | Ok compiler ->
            let open Io in
            let module Print = Compiler.Print (Pretty) in
            Pretty.run (Print.print_values compiler)
            >>= fun _ ->
            if Compiler.has_succeeded compiler then
                return ()
            else
                Pretty.run (Print.print_error compiler)
end
OCaml

Innovation. Community. Security.