about summary refs log tree commit diff stats
path: root/modal/ocaml/app
diff options
context:
space:
mode:
Diffstat (limited to 'modal/ocaml/app')
-rw-r--r--modal/ocaml/app/dune5
-rw-r--r--modal/ocaml/app/main.ml34
2 files changed, 39 insertions, 0 deletions
diff --git a/modal/ocaml/app/dune b/modal/ocaml/app/dune
new file mode 100644
index 0000000..6666cbf
--- /dev/null
+++ b/modal/ocaml/app/dune
@@ -0,0 +1,5 @@
+(executable
+ (name main)
+ (public_name modal)
+ (libraries modal))
+
diff --git a/modal/ocaml/app/main.ml b/modal/ocaml/app/main.ml
new file mode 100644
index 0000000..63ecfb9
--- /dev/null
+++ b/modal/ocaml/app/main.ml
@@ -0,0 +1,34 @@
+module Ast = Modal.Ast
+module Parse = Modal.Parse
+module Eval = Modal.Eval
+module Program = Modal.Program
+
+let () =
+  let usage = "Usage: modal [-q] [-a] [-n] RULES.modal [INPUT]" in
+  if Array.length Sys.argv < 2 then (prerr_endline usage; exit 2);
+  let cfg = Eval.default_config () in
+  let rec parse_flags i =
+    if i < Array.length Sys.argv && String.length Sys.argv.(i) > 0 && Sys.argv.(i).[0] = '-' then (
+      (match Sys.argv.(i) with
+      | "-q" -> cfg.quiet <- true
+      | "-a" -> cfg.allow_access <- true
+      | "-n" -> cfg.cycles <- 0x7fffffff
+      | _ -> ());
+      parse_flags (i + 1))
+    else i
+  in
+  let i = parse_flags 1 in
+  if i >= Array.length Sys.argv then (prerr_endline usage; exit 2);
+  let rules_path = Sys.argv.(i) in
+  let input_arg = if i + 1 < Array.length Sys.argv then Some Sys.argv.(i + 1) else None in
+  let rules, file_input = Program.load_file rules_path in
+  let input =
+    match input_arg with
+    | Some s -> Parse.parse s
+    | None -> (
+        match file_input with Some n -> n | None -> Ast.List [])
+  in
+  let result = Eval.eval ~config:cfg rules input in
+  Format.printf "%a\n" Ast.pp result
+
+