From c800a14262fc00ac6a9e6a214152fea182b06617 Mon Sep 17 00:00:00 2001 From: tali Date: Sat, 25 Nov 2023 15:59:51 -0500 Subject: [PATCH] use Fmt library in Syn --- lib/dune | 3 ++- lib/syn.ml | 43 +++++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/dune b/lib/dune index dd2f8c1..fc4888e 100644 --- a/lib/dune +++ b/lib/dune @@ -1,5 +1,6 @@ (library - (name spice)) + (name spice) + (libraries fmt)) (menhir (modules parser) diff --git a/lib/syn.ml b/lib/syn.ml index cbfba2c..5878076 100644 --- a/lib/syn.ml +++ b/lib/syn.ml @@ -66,31 +66,25 @@ let string_of_binop = function | Grt_eql -> ">=" | Lst_eql -> "<=" -let pf = Format.fprintf -let pp_str ppf str = pf ppf "%S" str - let pp_list pp_ele ppf list = - pf ppf "["; + Fmt.pf ppf "["; List.iteri (fun i ele -> - if i > 0 then pf ppf ","; + if i > 0 then Fmt.pf ppf ","; pp_ele ppf ele) list; - pf ppf "]" + Fmt.pf ppf "]" let rec pp_exp ppf = function - | Literal (Int n) -> pf ppf "{\"int\":%s}" (Int64.to_string n) - | Literal l -> pf ppf "%s" (string_of_literal l) - | Path (Var x) -> pf ppf "{\"var\":%S}" x - | Path (Ele (e, x)) -> pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e x - | Call (fn, args) -> pf ppf "{\"call\":%a}" (pp_list pp_exp) (Path fn :: args) + | Literal (Int n) -> Fmt.pf ppf "{\"int\":%s}" (Int64.to_string n) + | Literal l -> Fmt.pf ppf "%s" (string_of_literal l) + | Path (Var x) -> Fmt.pf ppf "{\"var\":%S}" x + | Path (Ele (e, x)) -> Fmt.pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e x + | Call (fn, args) -> Fmt.pf ppf "{\"call\":%a}" (pp_list pp_exp) (Path fn :: args) | If (ec, et, ee) -> - pf ppf "{\"if\":%a,\"then\":%a,\"else\":%a}" - pp_exp ec - pp_exp et - pp_exp ee + Fmt.pf ppf "{\"if\":%a,\"then\":%a,\"else\":%a}" pp_exp ec pp_exp et pp_exp ee | Binop (op, e1, e2) -> - pf + Fmt.pf ppf "{\"binop\":%S,\"lhs\":%a,\"rhs\":%a}" (string_of_binop op) @@ -99,22 +93,23 @@ let rec pp_exp ppf = function pp_exp e2 | Fun (params, body) -> - pf ppf "{\"fun\":%a,\"body\":%a}" (pp_list pp_str) params pp_exp body - | Obj body -> pf ppf "{\"obj\":%a}" (pp_list pp_item) body - | Scope body -> pf ppf "{\"scope\":%a}" (pp_list pp_item) body + Fmt.pf ppf "{\"fun\":%a,\"body\":%a}" (pp_list Fmt.string) params pp_exp body + | Obj body -> Fmt.pf ppf "{\"obj\":%a}" (pp_list pp_item) body + | Scope body -> Fmt.pf ppf "{\"scope\":%a}" (pp_list pp_item) body and pp_item ppf = function - | Item_exp e -> pf ppf "{\"exp\":%a}" pp_exp e - | Item_val (name, rhs) -> pf ppf "{\"val\":%S,\"rhs\":%a}" name pp_exp rhs + | Item_exp e -> Fmt.pf ppf "{\"exp\":%a}" pp_exp e + | Item_val (name, rhs) -> Fmt.pf ppf "{\"val\":%S,\"rhs\":%a}" name pp_exp rhs | Item_fun (name, params, body) -> - pf + Fmt.pf ppf "{\"fun\":%S,\"params\":%a,\"body\":%a}" name - (pp_list pp_str) + (pp_list Fmt.string) params pp_exp body - | Item_obj (name, body) -> pf ppf "{\"obj\":%S,\"body\":%a}" name (pp_list pp_item) body + | Item_obj (name, body) -> + Fmt.pf ppf "{\"obj\":%S,\"body\":%a}" name (pp_list pp_item) body let pp_modl ppf m = pp_list pp_item ppf m.items