Skip to content
Snippets Groups Projects
Commit 8e0823a8 authored by nilfit's avatar nilfit
Browse files

add translation of non-struct constructor expressions

parent c1344915
No related branches found
No related tags found
No related merge requests found
......@@ -48,6 +48,7 @@ module Rust = struct
| Evar of pvsymbol
| Efield of expr * ident
| Etup of expr list
| Eenum of rsymbol * expr list
| Estruct of rsymbol * rsymbol list * expr list (* Rs { r1: e1, r2: e2 } *)
| Eblock of expr list
| Ecall of rsymbol * expr list
......@@ -105,6 +106,7 @@ module Rust = struct
| Elet (p, e1, e2) -> Elet (p, clean_expr e1, clean_expr e2)
| Efield (e, id) -> Efield (clean_expr e, id)
| Etup el -> Etup (clean_list el)
| Eenum (rs, el) -> Eenum (rs, clean_list el)
| Estruct (rs, fl, el) -> Estruct (rs, fl, clean_list el)
| Ematch (e, bl) ->
let clean_bl = List.map (fun (p, e) -> (p, clean_expr e)) bl in
......@@ -269,9 +271,14 @@ module MLToRust = struct
| None, None, _ when is_rs_tuple rs ->
Rust.Etup (el)
| None, None, [e1] when isfield -> Rust.Efield (e1, rs.rs_name)
| None, None, _ when isconstructor () ->
| None, None, el when isconstructor () ->
(match get_record info rs with
| [] -> raise (TODO "Constructor (el)")
| [] ->
let maybe_box rs i e =
if Sbe.contains boxes.box_enum (rs.rs_name, i) then box_expr e
else e in
let el = List.mapi (maybe_box rs) el in
Rust.Eenum (rs, el)
| rsl ->
let maybe_box (rs: rsymbol) (e: Rust.expr) =
if Sid.contains boxes.box_fields rs.rs_name then box_expr e
......@@ -364,6 +371,7 @@ module MLToRust = struct
| Rust.Elet (_, e1, e2) -> map_and_union [e1;e2]
| Rust.Efield (e, _) -> discover_lts_tvs_expr e
| Rust.Etup el -> map_and_union el
| Rust.Eenum (_, el) -> map_and_union el
| Rust.Estruct (_, _, el) -> map_and_union el (* TODO check rsymbols*)
| Rust.Ematch (e, bl) ->
let bel = List.map (fun (_, e) -> e) bl in
......@@ -707,6 +715,7 @@ module Print = struct
(print_list comma (print_ty info)) argl
(print_ty info) res
(* TODO why not use rs.rs_name? *)
(* find ident of a struct by its rsymbol *)
let struct_id rs =
(match rs.rs_cty.cty_result.ity_node with
......@@ -755,6 +764,10 @@ module Print = struct
| Efield (e, id) ->
fprintf fmt "%a.%a" (print_expr info) e (print_lident info) id
| Etup el -> fprintf fmt "(%a)" (print_list comma (print_expr info)) el
| Eenum (rs, []) -> print_uident info fmt (struct_id rs)
| Eenum (rs, el) ->
fprintf fmt "%a (%a)" (print_uident info) (struct_id rs)
(print_list comma (print_expr info)) el
| Estruct (rs, fl, el) ->
fprintf fmt "@[<hov 2>%a { %a }@]" (print_uident info) (struct_id rs)
(print_list2 semi colon (print_rs info) (print_expr info)) (fl, el)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment