Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add interactive.bool.

  • Loading branch information...
commit 03503f38cf660a8975d404c060cc68958d245a3b 1 parent aa78505
@smimram smimram authored
Showing with 20 additions and 0 deletions.
  1. +20 −0 src/lang/lang_builtins.ml
View
20 src/lang/lang_builtins.ml
@@ -2240,6 +2240,26 @@ let () =
Lang.val_fun [] ~ret_t:Lang.float_t (fun p _ -> Lang.float !v))
let () =
+ add_builtin "interactive.bool" ~cat:Interaction
+ ~descr:"Read a boolean from an interactive input."
+ ["",Lang.string_t,None,None; "",Lang.bool_t,None,None ]
+ (Lang.fun_t [] Lang.bool_t)
+ (fun p ->
+ let name = Lang.to_string (Lang.assoc "" 1 p) in
+ let v = Lang.to_bool (Lang.assoc "" 2 p) in
+ let v = ref v in
+ Var.add
+ name
+ Lang.float_t
+ ~get:(fun () -> Printf.sprintf "%B" !v)
+ ~set:(fun s -> v := s = "true")
+ ~validate:
+ (fun s ->
+ if s <> "true" && s <> "false" then
+ raise (Var.Invalid_value (s ^ " is not a boolean")));
+ Lang.val_fun [] ~ret_t:Lang.bool_t (fun p _ -> Lang.bool !v))
+
+let () =
add_builtin "print" ~cat:Interaction ~descr:"Print on standard output."
["newline",Lang.bool_t,Some (Lang.bool true),
Some "If true, a newline is added after displaying the value." ;
Please sign in to comment.
Something went wrong with that request. Please try again.