Fix the Python printer

This commit is contained in:
Louis Gesbert 2024-07-08 14:44:55 +02:00
parent 97940c2cb6
commit 0b19130c1d
6 changed files with 283 additions and 249 deletions

View File

@ -33,9 +33,6 @@ module VarName =
end)
()
let dead_value = VarName.fresh ("dead_value", Pos.no_pos)
let handle_exceptions = FuncName.fresh ("handle_exceptions", Pos.no_pos)
type operator = Shared_ast.lcalc Shared_ast.operator
type expr = naked_expr Mark.pos

View File

@ -346,11 +346,9 @@ let rec format_expression ctx (fmt : Format.formatter) (e : expr) : unit =
args = [arg1];
} ->
Format.fprintf fmt "%a %a" format_op op (format_expression ctx) arg1
| EAppOp { op; args = [arg1] } ->
Format.fprintf fmt "%a(%a)" format_op op (format_expression ctx) arg1
| EApp { f = EFunc x, _; args = [(EArray el, _)] as args }
when Ast.FuncName.compare x Ast.handle_exceptions = 0 ->
Format.fprintf fmt "%a([%a], %a)@]" format_func_name x
| EAppOp { op = (HandleExceptions, _) as op; args = [(EArray el, _)] as args }
->
Format.fprintf fmt "@[<hv 4>%a(@,[%a],@ %a@;<0 -4>)@]" format_op op
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ ")
format_position)
@ -359,14 +357,16 @@ let rec format_expression ctx (fmt : Format.formatter) (e : expr) : unit =
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(format_expression ctx))
args
| EAppOp { op; args = [arg1] } ->
Format.fprintf fmt "%a(%a)" format_op op (format_expression ctx) arg1
| EApp { f; args } ->
Format.fprintf fmt "%a(@[<hov 0>%a)@]" (format_expression ctx) f
Format.fprintf fmt "%a(@[<hv 0>%a)@]" (format_expression ctx) f
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(format_expression ctx))
args
| EAppOp { op; args } ->
Format.fprintf fmt "%a(@[<hov 0>%a)@]" format_op op
Format.fprintf fmt "%a(@[<hv 0>%a)@]" format_op op
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(format_expression ctx))
@ -387,10 +387,10 @@ let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
=
match Mark.remove s with
| SInnerFuncDef { name; func = { func_params; func_body; _ } } ->
Format.fprintf fmt "@[<hov 4>def %a(%a):@\n%a@]" format_var
Format.fprintf fmt "@[<v 4>def %a(@[<hov>%a@]):@ %a@]" format_var
(Mark.remove name)
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(fun fmt (var, typ) ->
Format.fprintf fmt "%a:%a" format_var (Mark.remove var)
(format_typ ctx) typ))
@ -399,16 +399,16 @@ let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
assert false (* We don't need to declare variables in Python *)
| SLocalDef { name = v; expr = e; _ } | SLocalInit { name = v; expr = e; _ }
->
Format.fprintf fmt "@[<hov 4>%a = %a@]" format_var (Mark.remove v)
Format.fprintf fmt "@[<hv 4>%a = %a@]" format_var (Mark.remove v)
(format_expression ctx) e
| STryWEmpty { try_block = try_b; with_block = catch_b } ->
Format.fprintf fmt "@[<v 4>try:@,%a@]@\n@[<v 4>except Empty:@,%a@]"
Format.fprintf fmt "@[<v 4>try:@ %a@]@,@[<v 4>except Empty:@ %a@]"
(format_block ctx) try_b (format_block ctx) catch_b
| SRaiseEmpty -> Format.fprintf fmt "raise Empty"
| SFatalError err ->
Format.fprintf fmt "@[<hov 4>raise %a@]" format_error (err, Mark.get s)
| SIfThenElse { if_expr = cond; then_block = b1; else_block = b2 } ->
Format.fprintf fmt "@[<hov 4>if %a:@\n%a@]@\n@[<hov 4>else:@\n%a@]"
Format.fprintf fmt "@[<v 4>if %a:@ %a@]@,@[<v 4>else:@ %a@]"
(format_expression ctx) cond (format_block ctx) b1 (format_block ctx) b2
| SSwitch
{
@ -424,11 +424,11 @@ let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
when EnumName.equal e_name Expr.option_enum ->
(* We translate the option type with an overloading by Python's [None] *)
let tmp_var = VarName.fresh ("perhaps_none_arg", Pos.no_pos) in
Format.fprintf fmt "%a = %a@\n" format_var tmp_var (format_expression ctx)
e1;
Format.fprintf fmt "@[<v 4>if %a is None:@\n%a@]@\n" format_var tmp_var
Format.fprintf fmt "@[<hv 4>%a = %a@]@," format_var tmp_var
(format_expression ctx) e1;
Format.fprintf fmt "@[<v 4>if %a is None:@ %a@]@," format_var tmp_var
(format_block ctx) case_none;
Format.fprintf fmt "@[<v 4>else:@\n%a = %a@\n%a@]" format_var case_some_var
Format.fprintf fmt "@[<v 4>else:@ %a = %a@,%a@]" format_var case_some_var
format_var tmp_var (format_block ctx) case_some
| SSwitch { switch_expr = e1; enum_name = e_name; switch_cases = cases; _ } ->
let cons_map = EnumName.Map.find e_name ctx.decl_ctx.ctx_enums in
@ -455,10 +455,10 @@ let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
| SAssert e1 ->
let pos = Mark.get s in
Format.fprintf fmt
"@[<hov 4>if not (%a):@\n\
raise AssertionFailure(@[<hov 0>SourcePosition(@[<hov \
0>filename=\"%s\",@ start_line=%d,@ start_column=%d,@ end_line=%d,@ \
end_column=%d,@ law_headings=@[<hv>%a@])@])@]@]"
"@[<hv 4>if not (%a):@,\
raise AssertionFailure(@[<hov>SourcePosition(@[<hov 0>filename=\"%s\",@ \
start_line=%d,@ start_column=%d,@ end_line=%d,@ end_column=%d,@ \
law_headings=@[<hv>%a@])@])@]@]"
(format_expression ctx)
(e1, Mark.get s)
(Pos.get_file pos) (Pos.get_start_line pos) (Pos.get_start_column pos)
@ -467,12 +467,14 @@ let rec format_statement ctx (fmt : Format.formatter) (s : stmt Mark.pos) : unit
| SSpecialOp _ -> failwith "should not happen"
and format_block ctx (fmt : Format.formatter) (b : block) : unit =
Format.pp_open_vbox fmt 0;
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
~pp_sep:(fun fmt () -> Format.fprintf fmt "@,")
(format_statement ctx) fmt
(List.filter
(fun s -> match Mark.remove s with SLocalDecl _ -> false | _ -> true)
b)
b);
Format.pp_close_box fmt ()
let format_ctx
(type_ordering : Scopelang.Dependency.TVertex.t list)
@ -481,20 +483,20 @@ let format_ctx
let format_struct_decl fmt (struct_name, struct_fields) =
let fields = StructField.Map.bindings struct_fields in
Format.fprintf fmt
"class %a:@\n\
\ def __init__(self, %a) -> None:@\n\
%a@\n\
@\n\
\ def __eq__(self, other: object) -> bool:@\n\
\ if isinstance(other, %a):@\n\
\ return @[<hov>(%a)@]@\n\
\ else:@\n\
\ return False@\n\
@\n\
\ def __ne__(self, other: object) -> bool:@\n\
\ return not (self == other)@\n\
@\n\
\ def __str__(self) -> str:@\n\
"class %a:@,\
\ def __init__(self, %a) -> None:@,\
%a@,\
@,\
\ def __eq__(self, other: object) -> bool:@,\
\ if isinstance(other, %a):@,\
\ return @[<hov>(%a)@]@,\
\ else:@,\
\ return False@,\
@,\
\ def __ne__(self, other: object) -> bool:@,\
\ return not (self == other)@,\
@,\
\ def __str__(self) -> str:@,\
\ @[<hov 4>return \"%a(%a)\".format(%a)@]" (format_struct_name ctx)
struct_name
(Format.pp_print_list
@ -506,9 +508,7 @@ let format_ctx
(if StructField.Map.is_empty struct_fields then fun fmt _ ->
Format.fprintf fmt " pass"
else
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
(fun fmt (struct_field, _) ->
Format.pp_print_list (fun fmt (struct_field, _) ->
Format.fprintf fmt " self.%a = %a" format_struct_field_name
struct_field format_struct_field_name struct_field))
fields (format_struct_name ctx) struct_name
@ -536,32 +536,30 @@ let format_ctx
failwith "no constructors in the enum"
else
Format.fprintf fmt
"@[<hov 4>class %a_Code(Enum):@\n\
%a@]@\n\
@\n\
class %a:@\n\
\ def __init__(self, code: %a_Code, value: Any) -> None:@\n\
\ self.code = code@\n\
\ self.value = value@\n\
@\n\
@\n\
\ def __eq__(self, other: object) -> bool:@\n\
\ if isinstance(other, %a):@\n\
"@[<v 4>class %a_Code(Enum):@,\
%a@]@,\
@,\
class %a:@,\
\ def __init__(self, code: %a_Code, value: Any) -> None:@,\
\ self.code = code@,\
\ self.value = value@,\
@,\
@,\
\ def __eq__(self, other: object) -> bool:@,\
\ if isinstance(other, %a):@,\
\ return self.code == other.code and self.value == \
other.value@\n\
\ else:@\n\
\ return False@\n\
@\n\
@\n\
\ def __ne__(self, other: object) -> bool:@\n\
\ return not (self == other)@\n\
@\n\
\ def __str__(self) -> str:@\n\
other.value@,\
\ else:@,\
\ return False@,\
@,\
@,\
\ def __ne__(self, other: object) -> bool:@,\
\ return not (self == other)@,\
@,\
\ def __str__(self) -> str:@,\
\ @[<hov 4>return \"{}({})\".format(self.code, self.value)@]"
(format_enum_name ctx) enum_name
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n")
(fun fmt (i, enum_cons, _enum_cons_type) ->
(Format.pp_print_list (fun fmt (i, enum_cons, _enum_cons_type) ->
Format.fprintf fmt "%a = %d" format_enum_cons_name enum_cons i))
(List.mapi
(fun i (x, y) -> i, x, y)
@ -591,11 +589,11 @@ let format_ctx
match struct_or_enum with
| Scopelang.Dependency.TVertex.Struct s ->
if StructName.path s = [] then
Format.fprintf fmt "%a@\n@\n" format_struct_decl
Format.fprintf fmt "%a@,@," format_struct_decl
(s, StructName.Map.find s ctx.decl_ctx.ctx_structs)
| Scopelang.Dependency.TVertex.Enum e ->
if EnumName.path e = [] then
Format.fprintf fmt "%a@\n@\n" format_enum_decl
Format.fprintf fmt "%a@,@," format_enum_decl
(e, EnumName.Map.find e ctx.decl_ctx.ctx_enums))
(type_ordering @ scope_structs)
@ -611,14 +609,15 @@ let reserve_func_name = function
let format_code_item ctx fmt = function
| SVar { var; expr; typ = _ } ->
Format.fprintf fmt "@[<hv 4>%a = (@,%a@,@])@," format_var var
Format.fprintf fmt "@[<hv 4>%a = (@,%a@;<0 -4>)@]@," format_var var
(format_expression ctx) expr
| SFunc { var; func }
| SScope { scope_body_var = var; scope_body_func = func; _ } ->
let { Ast.func_params; Ast.func_body; _ } = func in
Format.fprintf fmt "@[<hv 4>def %a(%a):@\n%a@]@," format_func_name var
Format.fprintf fmt "@[<v 4>@[<hov 2>def %a(@,%a@;<0 -2>):@]@ %a@]@,"
format_func_name var
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ", ")
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(fun fmt (var, typ) ->
Format.fprintf fmt "%a:%a" format_var (Mark.remove var)
(format_typ ctx) typ))

View File

@ -318,36 +318,20 @@ let rec format_expression (ctx : decl_ctx) (fmt : Format.formatter) (e : expr) :
args = [arg1];
} ->
Format.fprintf fmt "%a %a" format_op op (format_expression ctx) arg1
| EAppOp { op; args = [arg1] } ->
Format.fprintf fmt "%a(%a)" format_op op (format_expression ctx) arg1
| EAppOp { op = HandleExceptions, _; _ } ->
Message.error ~internal:true
"R compilation does not currently support the avoiding of exceptions"
(* TODO: port the following to avoid-exceptions
* | EAppOp { op = (HandleDefault as op), _; args; _ } ->
* let pos = Mark.get e in
* Format.fprintf fmt
* "%a(@[<hov 0>catala_position(filename=\"%s\",@ start_line=%d,@ \
* start_column=%d,@ end_line=%d, end_column=%d,@ law_headings=%a), %a)@]"
* format_op (op, pos) (Pos.get_file pos) (Pos.get_start_line pos)
* (Pos.get_start_column pos) (Pos.get_end_line pos) (Pos.get_end_column pos)
* format_string_list (Pos.get_law_info pos)
* (Format.pp_print_list
* ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
* (format_expression ctx))
* args *)
| EApp { f = EFunc x, pos; args }
when Ast.FuncName.compare x Ast.handle_exceptions = 0 ->
| EAppOp { op = HandleExceptions, _ as op; args = [EArray el, _] as args } ->
Format.fprintf fmt
"%a(@[<hov 0>catala_position(filename=\"%s\",@ start_line=%d,@ \
start_column=%d,@ end_line=%d, end_column=%d,@ law_headings=%a), %a)@]"
format_func_name x (Pos.get_file pos) (Pos.get_start_line pos)
(Pos.get_start_column pos) (Pos.get_end_line pos) (Pos.get_end_column pos)
format_string_list (Pos.get_law_info pos)
"%a(%a, %a)@]"
format_op op
(Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ ")
format_position)
(List.map Mark.get el)
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
(format_expression ctx))
args
| EAppOp { op; args = [arg1] } ->
Format.fprintf fmt "%a(%a)" format_op op (format_expression ctx) arg1
| EApp { f; args } ->
Format.fprintf fmt "%a(@[<hov 0>%a)@]" (format_expression ctx) f
(Format.pp_print_list

View File

@ -91,7 +91,7 @@ class BIn:
def some_name(some_name_in:SomeNameIn):
i = some_name_in.i_in
perhaps_none_arg = handle_exceptions([])
perhaps_none_arg = handle_exceptions([], [])
if perhaps_none_arg is None:
if True:
temp_o = (i + integer_of_string("1"))
@ -100,7 +100,14 @@ def some_name(some_name_in:SomeNameIn):
else:
x = perhaps_none_arg
temp_o = x
perhaps_none_arg_1 = handle_exceptions([temp_o])
perhaps_none_arg_1 = handle_exceptions(
[SourcePosition(
filename="tests/backends/python_name_clash.catala_en",
start_line=10, start_column=23,
end_line=10, end_column=28, law_headings=[]
)],
[temp_o]
)
if perhaps_none_arg_1 is None:
if False:
temp_o_1 = None
@ -122,7 +129,7 @@ def some_name(some_name_in:SomeNameIn):
return SomeName(o = o)
def b(b_in:BIn):
perhaps_none_arg_3 = handle_exceptions([])
perhaps_none_arg_3 = handle_exceptions([], [])
if perhaps_none_arg_3 is None:
if True:
temp_result = integer_of_string("1")
@ -131,7 +138,14 @@ def b(b_in:BIn):
else:
x_2 = perhaps_none_arg_3
temp_result = x_2
perhaps_none_arg_4 = handle_exceptions([temp_result])
perhaps_none_arg_4 = handle_exceptions(
[SourcePosition(
filename="tests/backends/python_name_clash.catala_en",
start_line=16, start_column=33,
end_line=16, end_column=34, law_headings=[]
)],
[temp_result]
)
if perhaps_none_arg_4 is None:
if False:
temp_result_1 = None

View File

@ -107,164 +107,164 @@ $ catala test-scope S4
```catala-test-inline
$ catala scalc
let glob1_2 = 44.12
let glob1_1 = 44.12
let glob3_2 (x_3: money) = return to_rat x_3 + 10.
let glob3_1 (x_2: money) = return to_rat x_2 + 10.
let glob4_3 (x_4: money) (y_5: decimal) = return to_rat x_4 * y_5 + 10.
let glob4_2 (x_3: money) (y_4: decimal) = return to_rat x_3 * y_4 + 10.
let glob5_aux_4 =
decl x_7 : decimal;
x_7 = to_rat 2 * 3.;
decl y_8 : decimal;
y_8 = 1000.;
return x_7 * y_8
let glob5_aux_3 =
decl x_6 : decimal;
x_6 = to_rat 2 * 3.;
decl y_7 : decimal;
y_7 = 1000.;
return x_6 * y_7
let glob5_6 = glob5_aux_4 ()
let glob5_5 = glob5_aux_3 ()
let glob2_9 = A {"y": glob1_2 >= 30., "z": 123. * 17.}
let glob2_8 = A {"y": glob1_1 >= 30., "z": 123. * 17.}
let S2_5 (S2_in_10: S2_in) =
decl temp_a_12 : decimal;
let S2_4 (S2_in_9: S2_in) =
decl temp_a_11 : decimal;
decl temp_a_12 : option decimal;
decl temp_a_13 : option decimal;
decl temp_a_14 : option decimal;
switch handle_exceptions []:
| ENone __15
| ENone __14
if true:
temp_a_14 = ESome glob3_2 ¤44.00 + 100.
else:
temp_a_14 = ENone ()
| ESome x_16 →
temp_a_14 = ESome x_16;
switch handle_exceptions [temp_a_14]:
| ENone __17 →
if false:
temp_a_13 = ENone ()
temp_a_13 = ESome glob3_1 ¤44.00 + 100.
else:
temp_a_13 = ENone ()
| ESome x_18 →
temp_a_13 = ESome x_18;
switch temp_a_13:
| ENone __19 →
fatal NoValue
| ESome arg_20 →
temp_a_12 = arg_20;
decl a_11 : decimal;
a_11 = temp_a_12;
return S2 {"a": a_11}
let S3_6 (S3_in_21: S3_in) =
decl temp_a_23 : decimal;
decl temp_a_24 : option decimal;
decl temp_a_25 : option decimal;
switch handle_exceptions []:
| ENone __26 →
if true:
temp_a_25 = ESome 50. + glob4_3 ¤44.00 55.
else:
temp_a_25 = ENone ()
| ESome x_27 →
temp_a_25 = ESome x_27;
switch handle_exceptions [temp_a_25]:
| ENone __28 →
| ESome x_15 →
temp_a_13 = ESome x_15;
switch handle_exceptions [temp_a_13]:
| ENone __16 →
if false:
temp_a_24 = ENone ()
temp_a_12 = ENone ()
else:
temp_a_12 = ENone ()
| ESome x_17 →
temp_a_12 = ESome x_17;
switch temp_a_12:
| ENone __18 →
fatal NoValue
| ESome arg_19 →
temp_a_11 = arg_19;
decl a_10 : decimal;
a_10 = temp_a_11;
return S2 {"a": a_10}
let S3_5 (S3_in_20: S3_in) =
decl temp_a_22 : decimal;
decl temp_a_23 : option decimal;
decl temp_a_24 : option decimal;
switch handle_exceptions []:
| ENone __25 →
if true:
temp_a_24 = ESome 50. + glob4_2 ¤44.00 55.
else:
temp_a_24 = ENone ()
| ESome x_29 →
temp_a_24 = ESome x_29;
switch temp_a_24:
| ENone __30 →
fatal NoValue
| ESome arg_31 →
temp_a_23 = arg_31;
decl a_22 : decimal;
a_22 = temp_a_23;
return S3 {"a": a_22}
let S4_7 (S4_in_32: S4_in) =
decl temp_a_34 : decimal;
decl temp_a_35 : option decimal;
decl temp_a_36 : option decimal;
switch handle_exceptions []:
| ENone __37 →
if true:
temp_a_36 = ESome glob5_6 + 1.
else:
temp_a_36 = ENone ()
| ESome x_38 →
temp_a_36 = ESome x_38;
switch handle_exceptions [temp_a_36]:
| ENone __39 →
| ESome x_26 →
temp_a_24 = ESome x_26;
switch handle_exceptions [temp_a_24]:
| ENone __27 →
if false:
temp_a_35 = ENone ()
temp_a_23 = ENone ()
else:
temp_a_23 = ENone ()
| ESome x_28 →
temp_a_23 = ESome x_28;
switch temp_a_23:
| ENone __29 →
fatal NoValue
| ESome arg_30 →
temp_a_22 = arg_30;
decl a_21 : decimal;
a_21 = temp_a_22;
return S3 {"a": a_21}
let S4_6 (S4_in_31: S4_in) =
decl temp_a_33 : decimal;
decl temp_a_34 : option decimal;
decl temp_a_35 : option decimal;
switch handle_exceptions []:
| ENone __36 →
if true:
temp_a_35 = ESome glob5_5 + 1.
else:
temp_a_35 = ENone ()
| ESome x_40 →
temp_a_35 = ESome x_40;
switch temp_a_35:
| ENone __41 →
fatal NoValue
| ESome arg_42 →
temp_a_34 = arg_42;
decl a_33 : decimal;
a_33 = temp_a_34;
return S4 {"a": a_33}
let S_8 (S_in_43: S_in) =
decl temp_a_55 : decimal;
decl temp_a_56 : option decimal;
decl temp_a_57 : option decimal;
switch handle_exceptions []:
| ENone __58 →
if true:
temp_a_57 = ESome glob1_2 * glob1_2
else:
temp_a_57 = ENone ()
| ESome x_59 →
temp_a_57 = ESome x_59;
switch handle_exceptions [temp_a_57]:
| ENone __60 →
| ESome x_37 →
temp_a_35 = ESome x_37;
switch handle_exceptions [temp_a_35]:
| ENone __38 →
if false:
temp_a_56 = ENone ()
temp_a_34 = ENone ()
else:
temp_a_34 = ENone ()
| ESome x_39 →
temp_a_34 = ESome x_39;
switch temp_a_34:
| ENone __40 →
fatal NoValue
| ESome arg_41 →
temp_a_33 = arg_41;
decl a_32 : decimal;
a_32 = temp_a_33;
return S4 {"a": a_32}
let S_7 (S_in_42: S_in) =
decl temp_a_54 : decimal;
decl temp_a_55 : option decimal;
decl temp_a_56 : option decimal;
switch handle_exceptions []:
| ENone __57 →
if true:
temp_a_56 = ESome glob1_1 * glob1_1
else:
temp_a_56 = ENone ()
| ESome x_61 →
temp_a_56 = ESome x_61;
switch temp_a_56:
| ENone __62 →
fatal NoValue
| ESome arg_63 →
temp_a_55 = arg_63;
decl a_44 : decimal;
a_44 = temp_a_55;
decl temp_b_46 : A {y: bool; z: decimal};
decl temp_b_47 : option A {y: bool; z: decimal};
decl temp_b_48 : option A {y: bool; z: decimal};
switch handle_exceptions []:
| ENone __49 →
if true:
temp_b_48 = ESome glob2_9
else:
temp_b_48 = ENone ()
| ESome x_50 →
temp_b_48 = ESome x_50;
switch handle_exceptions [temp_b_48]:
| ENone __51 →
| ESome x_58 →
temp_a_56 = ESome x_58;
switch handle_exceptions [temp_a_56]:
| ENone __59 →
if false:
temp_b_47 = ENone ()
temp_a_55 = ENone ()
else:
temp_a_55 = ENone ()
| ESome x_60 →
temp_a_55 = ESome x_60;
switch temp_a_55:
| ENone __61 →
fatal NoValue
| ESome arg_62 →
temp_a_54 = arg_62;
decl a_43 : decimal;
a_43 = temp_a_54;
decl temp_b_45 : A {y: bool; z: decimal};
decl temp_b_46 : option A {y: bool; z: decimal};
decl temp_b_47 : option A {y: bool; z: decimal};
switch handle_exceptions []:
| ENone __48 →
if true:
temp_b_47 = ESome glob2_8
else:
temp_b_47 = ENone ()
| ESome x_52 →
temp_b_47 = ESome x_52;
switch temp_b_47:
| ENone __53 →
| ESome x_49 →
temp_b_47 = ESome x_49;
switch handle_exceptions [temp_b_47]:
| ENone __50 →
if false:
temp_b_46 = ENone ()
else:
temp_b_46 = ENone ()
| ESome x_51 →
temp_b_46 = ESome x_51;
switch temp_b_46:
| ENone __52 →
fatal NoValue
| ESome arg_54 →
temp_b_46 = arg_54;
decl b_45 : A {y: bool; z: decimal};
b_45 = temp_b_46;
return S {"a": a_44, "b": b_45}
| ESome arg_53
temp_b_45 = arg_53;
decl b_44 : A {y: bool; z: decimal};
b_44 = temp_b_45;
return S {"a": a_43, "b": b_44}
```
```catala-test-inline
@ -443,10 +443,10 @@ glob2 = (
decimal_of_string("30.")),
z = (decimal_of_string("123.") *
decimal_of_string("17.")))
)
)
def s2(s2_in:S2In):
perhaps_none_arg = handle_exceptions([])
perhaps_none_arg = handle_exceptions([], [])
if perhaps_none_arg is None:
if True:
temp_a = (glob3(money_of_cents_string("4400")) +
@ -456,7 +456,15 @@ def s2(s2_in:S2In):
else:
x_3 = perhaps_none_arg
temp_a = x_3
perhaps_none_arg_1 = handle_exceptions([temp_a])
perhaps_none_arg_1 = handle_exceptions(
[SourcePosition(
filename="tests/name_resolution/good/toplevel_defs.catala_en",
start_line=53, start_column=24,
end_line=53, end_column=43,
law_headings=["Test toplevel function defs"]
)],
[temp_a]
)
if perhaps_none_arg_1 is None:
if False:
temp_a_1 = None
@ -479,7 +487,7 @@ def s2(s2_in:S2In):
return S2(a = a)
def s3(s3_in:S3In):
perhaps_none_arg_3 = handle_exceptions([])
perhaps_none_arg_3 = handle_exceptions([], [])
if perhaps_none_arg_3 is None:
if True:
temp_a_3 = (decimal_of_string("50.") +
@ -490,7 +498,15 @@ def s3(s3_in:S3In):
else:
x_5 = perhaps_none_arg_3
temp_a_3 = x_5
perhaps_none_arg_4 = handle_exceptions([temp_a_3])
perhaps_none_arg_4 = handle_exceptions(
[SourcePosition(
filename="tests/name_resolution/good/toplevel_defs.catala_en",
start_line=74, start_column=24,
end_line=74, end_column=47,
law_headings=["Test function def with two args"]
)],
[temp_a_3]
)
if perhaps_none_arg_4 is None:
if False:
temp_a_4 = None
@ -513,7 +529,7 @@ def s3(s3_in:S3In):
return S3(a = a_1)
def s4(s4_in:S4In):
perhaps_none_arg_6 = handle_exceptions([])
perhaps_none_arg_6 = handle_exceptions([], [])
if perhaps_none_arg_6 is None:
if True:
temp_a_6 = (glob5 + decimal_of_string("1."))
@ -522,7 +538,15 @@ def s4(s4_in:S4In):
else:
x_7 = perhaps_none_arg_6
temp_a_6 = x_7
perhaps_none_arg_7 = handle_exceptions([temp_a_6])
perhaps_none_arg_7 = handle_exceptions(
[SourcePosition(
filename="tests/name_resolution/good/toplevel_defs.catala_en",
start_line=98, start_column=24,
end_line=98, end_column=34,
law_headings=["Test inline defs in toplevel defs"]
)],
[temp_a_6]
)
if perhaps_none_arg_7 is None:
if False:
temp_a_7 = None
@ -545,7 +569,7 @@ def s4(s4_in:S4In):
return S4(a = a_2)
def s(s_in:SIn):
perhaps_none_arg_9 = handle_exceptions([])
perhaps_none_arg_9 = handle_exceptions([], [])
if perhaps_none_arg_9 is None:
if True:
temp_a_9 = (glob1 * glob1)
@ -554,7 +578,15 @@ def s(s_in:SIn):
else:
x_9 = perhaps_none_arg_9
temp_a_9 = x_9
perhaps_none_arg_10 = handle_exceptions([temp_a_9])
perhaps_none_arg_10 = handle_exceptions(
[SourcePosition(
filename="tests/name_resolution/good/toplevel_defs.catala_en",
start_line=18, start_column=24,
end_line=18, end_column=37,
law_headings=["Test basic toplevel values defs"]
)],
[temp_a_9]
)
if perhaps_none_arg_10 is None:
if False:
temp_a_10 = None
@ -574,7 +606,7 @@ def s(s_in:SIn):
arg_3 = perhaps_none_arg_11
temp_a_11 = arg_3
a_3 = temp_a_11
perhaps_none_arg_12 = handle_exceptions([])
perhaps_none_arg_12 = handle_exceptions([], [])
if perhaps_none_arg_12 is None:
if True:
temp_b = glob2
@ -583,7 +615,15 @@ def s(s_in:SIn):
else:
x_11 = perhaps_none_arg_12
temp_b = x_11
perhaps_none_arg_13 = handle_exceptions([temp_b])
perhaps_none_arg_13 = handle_exceptions(
[SourcePosition(
filename="tests/name_resolution/good/toplevel_defs.catala_en",
start_line=19, start_column=24,
end_line=19, end_column=29,
law_headings=["Test basic toplevel values defs"]
)],
[temp_b]
)
if perhaps_none_arg_13 is None:
if False:
temp_b_1 = None

View File

@ -39,11 +39,11 @@ $ catala Scalc -s Foo2 -O -t
│ 5 │ output bar content integer
│ │ ‾‾‾
└─ Test
let Foo2_2 (Foo2_in_2: Foo2_in) =
decl temp_bar_4 : integer;
let Foo2_1 (Foo2_in_1: Foo2_in) =
decl temp_bar_3 : integer;
fatal NoValue;
decl bar_3 : integer;
bar_3 = temp_bar_4;
return Foo2 {"bar": bar_3}
decl bar_2 : integer;
bar_2 = temp_bar_3;
return Foo2 {"bar": bar_2}
```