From ba61dfd69504ec6263a9dee9931d93adeb6f3142 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 7 Jul 2025 21:52:08 +0200 Subject: Initialize repository --- lib/mininix/mininix2nix.ml | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/mininix/mininix2nix.ml (limited to 'lib/mininix/mininix2nix.ml') diff --git a/lib/mininix/mininix2nix.ml b/lib/mininix/mininix2nix.ml new file mode 100644 index 0000000..efbc42a --- /dev/null +++ b/lib/mininix/mininix2nix.ml @@ -0,0 +1,54 @@ +open Conv +open Core + +(* [or] is not a 'strong' keyword. That means that 'it depends' whether it is + identified as such. In the context of the left-hand side of an attribute, it + is not recognized as such. *) +let strong_keywords = + [ "with"; "rec"; "let"; "in"; "inherit"; "if"; "then"; "else"; "assert" ] + +let id_re = Str.regexp {|^[A-Za-z_]+[A-Za-z0-9'_-]*$|} + +let is_simple_id s = + Str.string_match id_re s 0 + && not (List.exists strong_keywords ~f:(String.( = ) s)) + +let thunk_map_to_map tm = + Extraction.thunk_map_fold + (fun k t -> Map.add_exn ~key:(String.of_char_list k) ~data:t) + (Map.empty (module String)) + tm + +let from_lit l = + match l with + | Extraction.LitString s -> Nix.Ast.Val (Nix.Ast.Str (str s, [])) + | Extraction.LitNull -> Nix.Ast.Id "null" + | Extraction.LitBool b -> Nix.Ast.Id (if b then "true" else "false") + | Extraction.LitNum x -> + Nix.Ast.Val + (match x with + | Extraction.NInt x -> Nix.Ast.Int (x |> Extraction.string_of_Z |> str) + | Extraction.NFloat x -> + Nix.Ast.Float (Printf.sprintf "%g" (float_from_flocq x))) + +let rec from_val = function + | Extraction.VClo _ | Extraction.VCloMatch _ -> Nix.Ast.Id "" + | Extraction.VLit l -> from_lit l + | Extraction.VAttr bs -> + let bs = + thunk_map_to_map bs + |> Map.to_alist ~key_order:`Increasing + |> List.map ~f:(fun (x, t) -> + let lhs = + if is_simple_id x then Nix.Ast.Id x + else Nix.Ast.Val (Nix.Ast.Str (x, [])) + in + Nix.Ast.AttrPath ([ lhs ], from_thunk t)) + in + Nix.Ast.Val (Nix.Ast.AttSet (Nix.Ast.Nonrec, bs)) + | Extraction.VList ts -> Nix.Ast.Val (Nix.Ast.List List.(ts >>| from_thunk)) + +and from_thunk = function + | Extraction.Thunk (_, ELit l) -> from_lit l + | Extraction.Thunk _ | Extraction.Indirect _ -> Nix.Ast.Id "" + | Extraction.Forced v -> from_val v -- cgit v1.2.3