mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
added loxstd with clock and exit
This commit is contained in:
parent
65a130deb8
commit
8f4b95a137
2 changed files with 29 additions and 0 deletions
1
lib/dune
1
lib/dune
|
|
@ -1,5 +1,6 @@
|
|||
(library
|
||||
(name Lox)
|
||||
(libraries unix)
|
||||
(preprocess
|
||||
(pps ppx_deriving.show)))
|
||||
|
||||
|
|
|
|||
28
lib/loxstd.ml
Normal file
28
lib/loxstd.ml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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
|
||||
| _ ->
|
||||
Printf.sprintf "Must call exit with integer, received %s instead" (show_lox_value n)
|
||||
|> Result.error)
|
||||
| _ -> assert false
|
||||
in
|
||||
{ name = "exit"; arity = 1; fn }
|
||||
|
||||
let init_std (env : environment) =
|
||||
let register_fn fn = Env.define_global env fn.name (Function (NativeFunction fn)) in
|
||||
let _ = List.map register_fn [ clock; exit ] in
|
||||
()
|
||||
Loading…
Add table
Add a link
Reference in a new issue