2024-08-28 17:19:34 +02:00
|
|
|
open Environment
|
|
|
|
|
open Value
|
|
|
|
|
|
|
|
|
|
let clock : native_function =
|
|
|
|
|
let fn = function
|
|
|
|
|
| [] ->
|
|
|
|
|
let time = Unix.gettimeofday () in
|
|
|
|
|
Number time |> Result.ok
|
|
|
|
|
| _ -> assert false
|
|
|
|
|
in
|
|
|
|
|
{ name = "clock"; arity = 0; fn }
|
|
|
|
|
|
|
|
|
|
let exit : native_function =
|
|
|
|
|
let fn = function
|
|
|
|
|
| [ n ] -> (
|
|
|
|
|
match n with
|
|
|
|
|
| Number n when Float.is_integer n -> Int.of_float n |> exit
|
|
|
|
|
| _ ->
|
2024-08-28 23:36:56 +02:00
|
|
|
Printf.sprintf "Must call exit with integer, received %s instead"
|
|
|
|
|
(string_of_lox_value n)
|
2024-08-28 17:19:34 +02:00
|
|
|
|> Result.error)
|
|
|
|
|
| _ -> assert false
|
|
|
|
|
in
|
|
|
|
|
{ name = "exit"; arity = 1; fn }
|
|
|
|
|
|
|
|
|
|
let init_std (env : environment) =
|
2024-08-28 22:55:15 +02:00
|
|
|
let register_fn fn = Env.define_global env fn.name (NativeFunction fn) in
|
2024-08-28 17:19:34 +02:00
|
|
|
let _ = List.map register_fn [ clock; exit ] in
|
|
|
|
|
()
|