diff options
Diffstat (limited to 'bin/settings.ml')
-rw-r--r-- | bin/settings.ml | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/bin/settings.ml b/bin/settings.ml new file mode 100644 index 0000000..55699ee --- /dev/null +++ b/bin/settings.ml | |||
@@ -0,0 +1,120 @@ | |||
1 | open Core | ||
2 | |||
3 | type fuel_amount = [ `Limited | `Unlimited ] | ||
4 | type eval_strategy = [ `Shallow | `Deep ] | ||
5 | |||
6 | type options = { | ||
7 | eval_strategy : eval_strategy ref; | ||
8 | fuel_amount : fuel_amount ref; | ||
9 | imports_def_file : string option ref; | ||
10 | print_input : bool ref; | ||
11 | print_parsed : bool ref; | ||
12 | print_elaborated : bool ref; | ||
13 | print_nix_sexp : bool ref; | ||
14 | print_mininix_sexp : bool ref; | ||
15 | print_mininix_sexp_w_prelude : bool ref; | ||
16 | print_result_mininix_sexp : bool ref; | ||
17 | print_result_nix_sexp : bool ref; | ||
18 | } | ||
19 | |||
20 | let opts = | ||
21 | { | ||
22 | eval_strategy = ref `Deep; | ||
23 | fuel_amount = ref `Unlimited; | ||
24 | imports_def_file = ref None; | ||
25 | print_input = ref false; | ||
26 | print_parsed = ref false; | ||
27 | print_elaborated = ref false; | ||
28 | print_nix_sexp = ref false; | ||
29 | print_mininix_sexp = ref false; | ||
30 | print_mininix_sexp_w_prelude = ref false; | ||
31 | print_result_mininix_sexp = ref false; | ||
32 | print_result_nix_sexp = ref false; | ||
33 | } | ||
34 | |||
35 | type 'a setter = 'a -> unit | ||
36 | |||
37 | type setting = | ||
38 | | BoolSetting of bool ref | ||
39 | | EvalStrategySetting of eval_strategy ref | ||
40 | | FilenameOptionSetting of string option ref | ||
41 | | FuelAmountSetting of fuel_amount ref | ||
42 | |||
43 | let allowed_values s = | ||
44 | match s with | ||
45 | | BoolSetting _ -> [ "true"; "false" ] | ||
46 | | EvalStrategySetting _ -> [ "shallow"; "deep" ] | ||
47 | | FilenameOptionSetting _ -> [ "none"; "some " ] | ||
48 | | FuelAmountSetting _ -> [ "limited"; "unlimited" ] | ||
49 | |||
50 | let set_to s v = | ||
51 | match s with | ||
52 | | BoolSetting vref -> ( | ||
53 | match v with | ||
54 | | [ "true" ] -> | ||
55 | vref := true; | ||
56 | None | ||
57 | | [ "false" ] -> | ||
58 | vref := false; | ||
59 | None | ||
60 | | _ -> Some "expected one argument: 'true' or 'false'") | ||
61 | | EvalStrategySetting vref -> ( | ||
62 | match v with | ||
63 | | [ "shallow" ] -> | ||
64 | vref := `Shallow; | ||
65 | None | ||
66 | | [ "deep" ] -> | ||
67 | vref := `Deep; | ||
68 | None | ||
69 | | _ -> Some "expected one argument: 'shallow' or 'deep'") | ||
70 | | FilenameOptionSetting vref -> ( | ||
71 | match v with | ||
72 | | [ "none" ] -> | ||
73 | vref := None; | ||
74 | None | ||
75 | | [ "some"; filename ] -> | ||
76 | vref := Some (String.strip filename); | ||
77 | None | ||
78 | | _ -> Some "expected 'none' or 'some <filename>'") | ||
79 | | FuelAmountSetting vref -> ( | ||
80 | match v with | ||
81 | | [ "limited" ] -> | ||
82 | vref := `Limited; | ||
83 | None | ||
84 | | [ "unlimited" ] -> | ||
85 | vref := `Unlimited; | ||
86 | None | ||
87 | | _ -> Some "expected 'limited' or 'unlimited'") | ||
88 | |||
89 | let to_string s = | ||
90 | match s with | ||
91 | | BoolSetting vref -> Bool.to_string !vref | ||
92 | | EvalStrategySetting vref -> ( | ||
93 | match !vref with `Shallow -> "shallow" | `Deep -> "deep") | ||
94 | | FilenameOptionSetting vref -> ( | ||
95 | match !vref with None -> "none" | Some v -> "some " ^ v) | ||
96 | | FuelAmountSetting vref -> ( | ||
97 | match !vref with `Limited -> "limited" | `Unlimited -> "unlimited") | ||
98 | |||
99 | let settings = | ||
100 | Map.of_alist_exn | ||
101 | (module String) | ||
102 | [ | ||
103 | ("print_input", BoolSetting opts.print_input); | ||
104 | ("print_parsed", BoolSetting opts.print_parsed); | ||
105 | ("print_elaborated", BoolSetting opts.print_elaborated); | ||
106 | ("print_nix_sexp", BoolSetting opts.print_nix_sexp); | ||
107 | ("print_mininix_sexp", BoolSetting opts.print_mininix_sexp); | ||
108 | ( "print_mininix_sexp_w_prelude", | ||
109 | BoolSetting opts.print_mininix_sexp_w_prelude ); | ||
110 | ("print_result_mininix_sexp", BoolSetting opts.print_result_mininix_sexp); | ||
111 | ("print_result_nix_sexp", BoolSetting opts.print_result_nix_sexp); | ||
112 | ("eval_strategy", EvalStrategySetting opts.eval_strategy); | ||
113 | ("fuel_amount", FuelAmountSetting opts.fuel_amount); | ||
114 | ("imports_def_file", FilenameOptionSetting opts.imports_def_file); | ||
115 | ] | ||
116 | |||
117 | let print () = | ||
118 | printf "==> Settings:\n"; | ||
119 | Map.iteri settings ~f:(fun ~key:name ~data:setting -> | ||
120 | printf " %s: %s\n" name (to_string setting)) | ||