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
open Ast

let register env =
  (*
  --# Get current working directory
  --#
  --# Returns the absolute path of the current working directory.
  --#
  --# @name getwd
  --# @return :: String The current working directory.
  --# @example
  --#   getwd()
  --# @family core
  --# @export
  *)
  let env = Env.add "getwd"
    (make_builtin_named ~name:"getwd" 0 (fun _args _env ->
      try VString (Sys.getcwd ())
      with Sys_error msg ->
        Error.runtime_error (Printf.sprintf "getwd: %s" msg)
    ))
    env
  in
  (*
  --# Check if file exists
  --#
  --# Checks if a regular file exists at the given path.
  --# Returns false if the path is a directory or does not exist.
  --#
  --# @name file_exists
  --# @param path :: String The path to check.
  --# @return :: Bool True if it's a regular file.
  --# @example
  --#   file_exists("data.csv")
  --# @family core
  --# @export
  *)
  let env = Env.add "file_exists"
    (make_builtin_named ~name:"file_exists" 1 (fun args _env ->
      match args with
      | [(_, VString path)] | [(_, VSymbol path)] ->
          let exists =
            match Unix.stat path with
            | exception Unix.Unix_error _ -> false
            | { Unix.st_kind = Unix.S_REG; _ } -> true
            | _ -> false
          in
          VBool exists
      | [(_, other)] ->
          Error.type_error (Printf.sprintf "Function `file_exists` expects a String, got %s." (Utils.type_name other))
      | _ -> Error.arity_error_named "file_exists" 1 (List.length args)
    ))
    env
  in
  (*
  --# Check if directory exists
  --#
  --# Checks if a directory exists at the given path.
  --#
  --# @name dir_exists
  --# @param path :: String The path to check.
  --# @return :: Bool True if it's a directory.
  --# @example
  --#   dir_exists("tests")
  --# @family core
  --# @export
  *)
  let env = Env.add "dir_exists"
    (make_builtin_named ~name:"dir_exists" 1 (fun args _env ->
      match args with
      | [(_, VString path)] | [(_, VSymbol path)] ->
          let exists =
            match Unix.stat path with
            | exception Unix.Unix_error _ -> false
            | { Unix.st_kind = Unix.S_DIR; _ } -> true
            | _ -> false
          in
          VBool exists
      | [(_, other)] ->
          Error.type_error (Printf.sprintf "Function `dir_exists` expects a String, got %s." (Utils.type_name other))
      | _ -> Error.arity_error_named "dir_exists" 1 (List.length args)
    ))
    env
  in
  (*
  --# Read file contents
  --#
  --# Reads the entire content of a file into a string.
  --#
  --# @name read_file
  --# @param path :: String The path to the file.
  --# @return :: String The file content.
  --# @example
  --#   read_file("config.json")
  --# @family core
  --# @export
  *)
  let env = Env.add "read_file"
    (make_builtin_named ~name:"read_file" 1 (fun args _env ->
      match args with
      | [(_, VString path)] | [(_, VSymbol path)] ->
          (try
            let ic = open_in path in
            Fun.protect ~finally:(fun () -> close_in_noerr ic) (fun () ->
              let n = in_channel_length ic in
              let buf = Bytes.create n in
              really_input ic buf 0 n;
              VString (Bytes.to_string buf)
            )
          with
          | Sys_error msg ->
            Error.make_error FileError (Printf.sprintf "read_file: %s" msg)
          | exn ->
            Error.make_error FileError (Printf.sprintf "read_file: %s" (Printexc.to_string exn)))
      | [(_, other)] ->
          Error.type_error (Printf.sprintf "Function `read_file` expects a String, got %s." (Utils.type_name other))
      | _ -> Error.arity_error_named "read_file" 1 (List.length args)
    ))
    env
  in
  (*
  --# List files in directory
  --#
  --# Returns a list of files and directories in the specified path.
  --# Supports an optional regex pattern for filtering.
  --#
  --# @name list_files
  --# @param path :: String [Optional] The directory to list. Defaults to ".".
  --# @param pattern :: String [Optional] Regex pattern to filter results.
  --# @return :: List[String] List of filenames.
  --# @example
  --#   list_files(".", pattern = "\\.t$")
  --# @family core
  --# @export
  *)
  let env = Env.add "list_files"
    (make_builtin_named ~name:"list_files" ~variadic:true 0 (fun args _env ->
      let positional = List.filter (fun (name, _) -> name = None) args in
      let n_positional = List.length positional in
      if n_positional > 1 then
        Error.arity_error_named "list_files" 1 n_positional
      else
      let path_result = match positional with
        | [(_, VString s)] | [(_, VSymbol s)] -> Ok s
        | [(_, other)] ->
            Error (Printf.sprintf "Function `list_files` expects a String path, got %s." (Utils.type_name other))
        | _ -> Ok "."
      in
      match path_result with
      | Error msg -> Error.type_error msg
      | Ok path ->
          let pattern_result = match List.assoc_opt (Some "pattern") args with
            | Some (VString s) | Some (VSymbol s) ->
                (match Str.regexp s with
                 | exception Failure msg ->
                     Error (Printf.sprintf "Invalid pattern for `list_files`: %s" msg)
                 | re -> Ok (Some re))
            | None -> Ok None
            | Some other ->
                Error (Printf.sprintf "Argument `pattern` of `list_files` must be a String, got %s." (Utils.type_name other))
          in
          (match pattern_result with
           | Error msg -> Error.type_error msg
           | Ok compiled_pattern ->
               (try
                 let entries = Sys.readdir path in
                 let filenames = Array.to_list entries in
                 let filtered = match compiled_pattern with
                   | None -> filenames
                   | Some re ->
                       List.filter (fun name ->
                         match Str.search_forward re name 0 with
                         | exception Not_found -> false
                         | _ -> true
                       ) filenames
                 in
                 let sorted = List.sort String.compare filtered in
                 VList (List.map (fun name -> (None, VString name)) sorted)
               with Sys_error msg ->
                 Error.make_error FileError (Printf.sprintf "list_files: %s" msg)))
    ))
    env
  in
  (*
  --# Get environment variable
  --#
  --# Retrieves the value of an environment variable.
  --#
  --# @name env
  --# @param name :: String The name of the environment variable.
  --# @return :: String | NA The value of the variable, or null if not set.
  --# @example
  --#   env("HOME")
  --# @family core
  --# @export
  *)
  let env = Env.add "env"
    (make_builtin_named ~name:"env" 1 (fun args _env ->
      match args with
      | [(_, VString name)] | [(_, VSymbol name)] ->
          (match Sys.getenv_opt name with
           | Some value -> VString value
           | None -> (VNA NAGeneric))
      | [(_, other)] ->
          Error.type_error (Printf.sprintf "Function `env` expects a String, got %s." (Utils.type_name other))
      | _ -> Error.arity_error_named "env" 1 (List.length args)
    ))
    env
  in
  env