standalone exapunks vm in ocaml
1(* This Source Code Form is subject to the terms of the Mozilla Public
2 License, v. 2.0. If a copy of the MPL was not distributed with this
3 file, You can obtain one at https://mozilla.org/MPL/2.0/. *)
4
5open Common
6
7type t = Common.file
8
9let file_gensym = ref 0
10
11let show file =
12 Format.sprintf "{name: %s, contents: \"%s\"}" file.name
13 (file.contents |> Dynarray.to_list |> List.map Value.show |> String.concat ", ")
14
15let pp ppf file =
16 Format.fprintf ppf "%s" (show file);
17 ()
18
19let create ?(contents = []) name = { name; contents = Dynarray.of_list contents }
20
21let equal (f1 : t) (f2 : t) : bool =
22 f1.name = f2.name && Dynarray.equal Value.equal f1.contents f2.contents
23
24let contents_equal (f1 : t) (f2 : t) : bool =
25 Dynarray.equal Value.equal f1.contents f2.contents
26
27let fill file strs = Dynarray.append_list file.contents strs
28
29let clear file = Dynarray.clear file.contents
30
31let length file = Dynarray.length file.contents
32
33let void file idx =
34 let len = length file in
35 if idx < 0 then ()
36 else if idx >= len then ()
37 else
38 let contents = file.contents in
39 let new_contents = Dynarray.make (len - 1) (Value.int 0) in
40 let left_len = idx in
41 let right_len = len - left_len - 1 in
42 if left_len > 0 then
43 Dynarray.blit ~src:contents ~src_pos:0 ~dst:new_contents ~dst_pos:0 ~len:left_len;
44 if right_len > 0 then
45 Dynarray.blit ~src:contents ~src_pos:(idx + 1) ~dst:new_contents ~dst_pos:left_len
46 ~len:right_len;
47 file.contents <- new_contents;
48 ()
49
50let next_name () =
51 let id = string_of_int !file_gensym in
52 file_gensym := !file_gensym + 1;
53 id