summaryrefslogtreecommitdiff
path: root/src/PrintPure.ml
diff options
context:
space:
mode:
authorSon Ho2022-01-28 12:07:53 +0100
committerSon Ho2022-01-28 12:07:53 +0100
commitbb9d21e658630315a7e83bfbdfb7a1b53e3bcc1a (patch)
tree0a13b80013d64b7df469d7d5ef3528cfeb00cfec /src/PrintPure.ml
parenta96c9e10cec6b8af30dd1c70214ec9b6db66645f (diff)
Remove the Return and Fail variants from Pure.expression and add a
`monadic` boolean field to `Let`
Diffstat (limited to 'src/PrintPure.ml')
-rw-r--r--src/PrintPure.ml48
1 files changed, 26 insertions, 22 deletions
diff --git a/src/PrintPure.ml b/src/PrintPure.ml
index 98c832a1..77e01c65 100644
--- a/src/PrintPure.ml
+++ b/src/PrintPure.ml
@@ -127,23 +127,19 @@ let mk_ast_formatter (type_defs : T.type_def TypeDefId.Map.t)
fun_def_id_to_string;
}
-let type_id_to_string (fmt : type_formatter) (id : T.type_id) : string =
+let type_id_to_string (fmt : type_formatter) (id : type_id) : string =
match id with
- | T.AdtId id -> fmt.type_def_id_to_string id
- | T.Tuple -> ""
- | T.Assumed aty -> (
- match aty with
- | Box ->
- (* Boxes should have been eliminated *)
- failwith "Unreachable: boxes should have been eliminated")
+ | AdtId id -> fmt.type_def_id_to_string id
+ | Tuple -> ""
+ | Assumed aty -> ( match aty with Result -> "Result")
let rec ty_to_string (fmt : type_formatter) (ty : ty) : string =
match ty with
| Adt (id, tys) -> (
let tys = List.map (ty_to_string fmt) tys in
match id with
- | T.Tuple -> "(" ^ String.concat " * " tys ^ ")"
- | T.AdtId _ | T.Assumed _ ->
+ | Tuple -> "(" ^ String.concat " * " tys ^ ")"
+ | AdtId _ | Assumed _ ->
let tys = if tys = [] then "" else " " ^ String.concat " " tys in
type_id_to_string fmt id ^ tys)
| TypeVar tv -> fmt.type_var_id_to_string tv
@@ -226,10 +222,10 @@ let adt_g_value_to_string (fmt : value_formatter)
(field_values : 'v list) (ty : ty) : string =
let field_values = List.map value_to_string field_values in
match ty with
- | Adt (T.Tuple, _) ->
+ | Adt (Tuple, _) ->
(* Tuple *)
"(" ^ String.concat ", " field_values ^ ")"
- | Adt (T.AdtId def_id, _) ->
+ | Adt (AdtId def_id, _) ->
(* "Regular" ADT *)
let adt_ident =
match variant_id with
@@ -251,12 +247,19 @@ let adt_g_value_to_string (fmt : value_formatter)
let field_values = String.concat " " field_values in
adt_ident ^ " { " ^ field_values ^ " }"
else adt_ident
- | Adt (T.Assumed aty, _) -> (
+ | Adt (Assumed aty, _) -> (
(* Assumed type *)
match aty with
- | Box ->
- (* Box values should have been eliminated *)
- failwith "Unreachable")
+ | Result ->
+ let variant_id = Option.get variant_id in
+ if variant_id = result_return_id then
+ match field_values with
+ | [ v ] -> "@Result::Return " ^ v
+ | _ -> failwith "Result::Return takes exactly one value"
+ else if variant_id = result_fail_id then (
+ assert (field_values = []);
+ "@Result::Fail")
+ else failwith "Unreachable: improper variant id for result type")
| _ -> failwith "Inconsistent typed value"
let rec typed_lvalue_to_string (fmt : value_formatter) (v : typed_lvalue) :
@@ -356,11 +359,10 @@ let meta_to_string (fmt : ast_formatter) (meta : meta) : string =
let rec expression_to_string (fmt : ast_formatter) (indent : string)
(indent_incr : string) (e : expression) : string =
match e with
- | Return v -> "return " ^ typed_rvalue_to_string fmt v
- | Fail -> "fail"
| Value (v, _) -> typed_rvalue_to_string fmt v
| Call call -> call_to_string fmt indent indent_incr call
- | Let (lv, re, e) -> let_to_string fmt indent indent_incr lv re e
+ | Let (monadic, lv, re, e) ->
+ let_to_string fmt indent indent_incr monadic lv re e
| Switch (scrutinee, _, body) ->
switch_to_string fmt indent indent_incr scrutinee body
| Meta (meta, e) ->
@@ -382,13 +384,15 @@ and call_to_string (fmt : ast_formatter) (indent : string)
if all_args = [] then fun_id else fun_id ^ " " ^ String.concat " " all_args
and let_to_string (fmt : ast_formatter) (indent : string) (indent_incr : string)
- (lv : typed_lvalue) (re : expression) (e : expression) : string =
+ (monadic : bool) (lv : typed_lvalue) (re : expression) (e : expression) :
+ string =
let indent1 = indent ^ indent_incr in
let val_fmt = ast_to_value_formatter fmt in
let re = expression_to_string fmt indent1 indent_incr re in
let e = expression_to_string fmt indent indent_incr e in
let lv = typed_lvalue_to_string val_fmt lv in
- "let " ^ lv ^ " = " ^ re ^ " in\n" ^ indent ^ e
+ if monadic then lv ^ " <-- " ^ re ^ ";\n" ^ indent ^ e
+ else "let " ^ lv ^ " = " ^ re ^ " in\n" ^ indent ^ e
and switch_to_string (fmt : ast_formatter) (indent : string)
(indent_incr : string) (scrutinee : typed_rvalue) (body : switch_body) :
@@ -410,7 +414,7 @@ and switch_to_string (fmt : ast_formatter) (indent : string)
branches
in
let otherwise =
- indent ^ "| _ ->\n"
+ indent ^ "| _ ->\n" ^ indent1
^ expression_to_string fmt indent1 indent_incr otherwise
in
let all_branches = List.append branches [ otherwise ] in