use Fmt library in Syn

This commit is contained in:
tali 2023-11-25 15:59:51 -05:00
parent dc3a0368b3
commit c800a14262
2 changed files with 21 additions and 25 deletions

View File

@ -1,5 +1,6 @@
(library (library
(name spice)) (name spice)
(libraries fmt))
(menhir (menhir
(modules parser) (modules parser)

View File

@ -66,31 +66,25 @@ let string_of_binop = function
| Grt_eql -> ">=" | Grt_eql -> ">="
| Lst_eql -> "<=" | Lst_eql -> "<="
let pf = Format.fprintf
let pp_str ppf str = pf ppf "%S" str
let pp_list pp_ele ppf list = let pp_list pp_ele ppf list =
pf ppf "["; Fmt.pf ppf "[";
List.iteri List.iteri
(fun i ele -> (fun i ele ->
if i > 0 then pf ppf ","; if i > 0 then Fmt.pf ppf ",";
pp_ele ppf ele) pp_ele ppf ele)
list; list;
pf ppf "]" Fmt.pf ppf "]"
let rec pp_exp ppf = function let rec pp_exp ppf = function
| Literal (Int n) -> pf ppf "{\"int\":%s}" (Int64.to_string n) | Literal (Int n) -> Fmt.pf ppf "{\"int\":%s}" (Int64.to_string n)
| Literal l -> pf ppf "%s" (string_of_literal l) | Literal l -> Fmt.pf ppf "%s" (string_of_literal l)
| Path (Var x) -> pf ppf "{\"var\":%S}" x | Path (Var x) -> Fmt.pf ppf "{\"var\":%S}" x
| Path (Ele (e, x)) -> pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e x | Path (Ele (e, x)) -> Fmt.pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e x
| Call (fn, args) -> pf ppf "{\"call\":%a}" (pp_list pp_exp) (Path fn :: args) | Call (fn, args) -> Fmt.pf ppf "{\"call\":%a}" (pp_list pp_exp) (Path fn :: args)
| If (ec, et, ee) -> | If (ec, et, ee) ->
pf ppf "{\"if\":%a,\"then\":%a,\"else\":%a}" Fmt.pf ppf "{\"if\":%a,\"then\":%a,\"else\":%a}" pp_exp ec pp_exp et pp_exp ee
pp_exp ec
pp_exp et
pp_exp ee
| Binop (op, e1, e2) -> | Binop (op, e1, e2) ->
pf Fmt.pf
ppf ppf
"{\"binop\":%S,\"lhs\":%a,\"rhs\":%a}" "{\"binop\":%S,\"lhs\":%a,\"rhs\":%a}"
(string_of_binop op) (string_of_binop op)
@ -99,22 +93,23 @@ let rec pp_exp ppf = function
pp_exp pp_exp
e2 e2
| Fun (params, body) -> | Fun (params, body) ->
pf ppf "{\"fun\":%a,\"body\":%a}" (pp_list pp_str) params pp_exp body Fmt.pf ppf "{\"fun\":%a,\"body\":%a}" (pp_list Fmt.string) params pp_exp body
| Obj body -> pf ppf "{\"obj\":%a}" (pp_list pp_item) body | Obj body -> Fmt.pf ppf "{\"obj\":%a}" (pp_list pp_item) body
| Scope body -> pf ppf "{\"scope\":%a}" (pp_list pp_item) body | Scope body -> Fmt.pf ppf "{\"scope\":%a}" (pp_list pp_item) body
and pp_item ppf = function and pp_item ppf = function
| Item_exp e -> pf ppf "{\"exp\":%a}" pp_exp e | Item_exp e -> Fmt.pf ppf "{\"exp\":%a}" pp_exp e
| Item_val (name, rhs) -> pf ppf "{\"val\":%S,\"rhs\":%a}" name pp_exp rhs | Item_val (name, rhs) -> Fmt.pf ppf "{\"val\":%S,\"rhs\":%a}" name pp_exp rhs
| Item_fun (name, params, body) -> | Item_fun (name, params, body) ->
pf Fmt.pf
ppf ppf
"{\"fun\":%S,\"params\":%a,\"body\":%a}" "{\"fun\":%S,\"params\":%a,\"body\":%a}"
name name
(pp_list pp_str) (pp_list Fmt.string)
params params
pp_exp pp_exp
body 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 let pp_modl ppf m = pp_list pp_item ppf m.items