forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsb_spec_set.ml
80 lines (69 loc) · 2.64 KB
/
bsb_spec_set.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
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
[@@@warning "+9"]
(* TODO: sync up with {!Js_packages_info.module_system} *)
type format = Ext_module_system.t = NodeJS | Es6 | Es6_global
type spec = { format : format; in_source : bool; suffix : Ext_js_suffix.t }
type t = spec list
let cmp (s1 : spec) ({ format; in_source; suffix } : spec) =
let v = compare s1.format format in
if v <> 0 then v
else
let v = compare s1.in_source in_source in
if v <> 0 then v else compare s1.suffix suffix
let empty = []
let rec insert lst piviot =
match lst with
| [] -> [ piviot ]
| x :: xs ->
let v = cmp piviot x in
if v = 0 then lst
else if v < 0 then piviot :: lst
else x :: insert xs piviot
let add spec specs =
match specs with
| [] -> [ spec ]
| [ a ] ->
let v = cmp spec a in
if v < 0 then spec :: specs else if v = 0 then specs else [ a; spec ]
| [ a; b ] ->
let v = cmp spec a in
if v < 0 then spec :: specs
else if v = 0 then specs
else
let v1 = cmp spec b in
if v < 0 then [ a; spec; b ]
else if v1 = 0 then specs
else [ a; b; spec ]
| _ :: _ :: _ :: _ ->
(* unlikely to happen *)
insert specs spec
let singleton x = [ x ]
let rec fold f t acc = match t with [] -> acc | x :: xs -> fold f xs (f x acc)
let rec iter f t =
match t with
| [] -> ()
| x :: xs ->
f x;
iter f xs