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
(name spice))
(name spice)
(libraries fmt))
(menhir
(modules parser)

View File

@ -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