Cleaning up

This commit is contained in:
Denis Merigoux 2023-08-05 16:23:29 +02:00
parent e2f8e56e7d
commit 695faebefb
5 changed files with 30 additions and 209 deletions

View File

@ -53,18 +53,19 @@ let format_op (fmt : Format.formatter) (op : operator Mark.pos) : unit =
| Minus_int | Minus_rat | Minus_mon | Minus_dur ->
Format.pp_print_string fmt "-"
(* Todo: use the names from [Operator.name] *)
| Not -> Format.pp_print_string fmt "not"
| Length -> Format.pp_print_string fmt "list_length"
| ToRat_int -> Format.pp_print_string fmt "decimal_of_integer"
| ToRat_mon -> Format.pp_print_string fmt "decimal_of_money"
| ToMoney_rat -> Format.pp_print_string fmt "money_of_decimal"
| GetDay -> Format.pp_print_string fmt "day_of_month_of_date"
| GetMonth -> Format.pp_print_string fmt "month_number_of_date"
| GetYear -> Format.pp_print_string fmt "year_of_date"
| FirstDayOfMonth -> Format.pp_print_string fmt "first_day_of_month"
| LastDayOfMonth -> Format.pp_print_string fmt "last_day_of_month"
| Round_mon -> Format.pp_print_string fmt "money_round"
| Round_rat -> Format.pp_print_string fmt "decimal_round"
| Not -> Format.pp_print_string fmt "!"
| Length -> Format.pp_print_string fmt "catala_list_length"
| ToRat_int -> Format.pp_print_string fmt "catala_decimal_from_integer"
| ToRat_mon -> Format.pp_print_string fmt "catala_decimal_from_money"
| ToMoney_rat -> Format.pp_print_string fmt "catala_money_from_decimal"
| GetDay -> Format.pp_print_string fmt "catala_day_of_month_of_date"
| GetMonth -> Format.pp_print_string fmt "catala_month_number_of_date"
| GetYear -> Format.pp_print_string fmt "catala_year_of_date"
| FirstDayOfMonth ->
Format.pp_print_string fmt "catala_date_first_day_of_month"
| LastDayOfMonth -> Format.pp_print_string fmt "catala_date_last_day_of_month"
| Round_mon -> Format.pp_print_string fmt "catala_money_round"
| Round_rat -> Format.pp_print_string fmt "catala_decimal_round"
| Add_int_int | Add_rat_rat | Add_mon_mon | Add_dat_dur _ | Add_dur_dur
| Concat ->
Format.pp_print_string fmt "+"
@ -75,8 +76,8 @@ let format_op (fmt : Format.formatter) (op : operator Mark.pos) : unit =
Format.pp_print_string fmt "*"
| Div_int_int | Div_rat_rat | Div_mon_mon | Div_mon_rat | Div_dur_dur ->
Format.pp_print_string fmt "/"
| And -> Format.pp_print_string fmt "and"
| Or -> Format.pp_print_string fmt "or"
| And -> Format.pp_print_string fmt "&&"
| Or -> Format.pp_print_string fmt "||"
| Eq -> Format.pp_print_string fmt "=="
| Xor -> Format.pp_print_string fmt "!="
| Lt_int_int | Lt_rat_rat | Lt_mon_mon | Lt_dat_dat | Lt_dur_dur ->
@ -89,13 +90,12 @@ let format_op (fmt : Format.formatter) (op : operator Mark.pos) : unit =
Format.pp_print_string fmt ">="
| Eq_int_int | Eq_rat_rat | Eq_mon_mon | Eq_dat_dat | Eq_dur_dur ->
Format.pp_print_string fmt "=="
| Map -> Format.pp_print_string fmt "list_map"
| Reduce -> Format.pp_print_string fmt "list_reduce"
| Filter -> Format.pp_print_string fmt "list_filter"
| Fold -> Format.pp_print_string fmt "list_fold_left"
| HandleDefault -> Format.pp_print_string fmt "handle_default"
| HandleDefaultOpt -> Format.pp_print_string fmt "handle_default_opt"
| FromClosureEnv | ToClosureEnv -> failwith "unimplemented"
| Map -> Format.pp_print_string fmt "catala_list_map"
| Reduce -> Format.pp_print_string fmt "catala_list_reduce"
| Filter -> Format.pp_print_string fmt "catala_list_filter"
| Fold -> Format.pp_print_string fmt "catala_list_fold_left"
| HandleDefault -> Format.pp_print_string fmt "catala_handle_default"
| HandleDefaultOpt | FromClosureEnv | ToClosureEnv -> failwith "unimplemented"
let format_string_list (fmt : Format.formatter) (uids : string list) : unit =
let sanitize_quotes = Re.compile (Re.char '"') in
@ -150,7 +150,7 @@ let rec format_typ (fmt : Format.formatter) (typ : typ) : unit =
let format_typ = format_typ in
match Mark.remove typ with
| TLit TUnit -> Format.fprintf fmt "\"catala_unit\""
| TLit TMoney -> Format.fprintf fmt "\"ctala_money\""
| TLit TMoney -> Format.fprintf fmt "\"catala_money\""
| TLit TInt -> Format.fprintf fmt "\"catala_integer\""
| TLit TRat -> Format.fprintf fmt "\"catala_decimal\""
| TLit TDate -> Format.fprintf fmt "\"catala_date\""
@ -377,7 +377,7 @@ let rec format_statement
| STryExcept (try_b, except, catch_b) ->
Format.fprintf fmt
(* TODO escape dummy__arg*)
"tryCatch@[<hov 2>(@[<hov 2>{@;\
"@[<hov 2>tryCatch(@[<hov 2>{@;\
%a@;\
}@],@;\
%a = function(dummy__arg) @[<hov 2>{@;\
@ -389,7 +389,8 @@ let rec format_statement
Format.fprintf fmt "@[<hov 2>stop(%a)@]" format_exception
(except, Mark.get s)
| SIfThenElse (cond, b1, b2) ->
Format.fprintf fmt "@[<hov 2>if %a:@\n%a@]@\n@[<hov 2>else:@\n%a@]"
Format.fprintf fmt
"@[<hov 2>if (%a) {@\n%a@]@\n@[<hov 2>} else {@\n%a@]@\n}"
(format_expression ctx) cond (format_block ctx) b1 (format_block ctx) b2
| SSwitch (e1, e_name, [(case_none, _); (case_some, case_some_var)])
when EnumName.equal e_name Expr.option_enum ->
@ -414,8 +415,8 @@ let rec format_statement
(EnumConstructor.Map.bindings (EnumName.Map.find e_name ctx.ctx_enums))
in
let tmp_var = VarName.fresh ("match_arg", Pos.no_pos) in
Format.fprintf fmt "%a <- %a@\n@[<hov 2>if %a@]@\n}" format_var tmp_var
(format_expression ctx) e1
Format.fprintf fmt "@[<hov 2>%a <- %a@]@\n@[<hov 2>if %a@]@\n}" format_var
tmp_var (format_expression ctx) e1
(Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@]@\n@[<hov 2>} else if ")
(fun fmt (case_block, payload_var, cons_name) ->
@ -429,10 +430,10 @@ let rec format_statement
| SAssert e1 ->
let pos = Mark.get s in
Format.fprintf fmt
"@[<hov 2>if (not (%a)) {@\n\
"@[<hov 2>if (!(%a)) {@\n\
stop(catala_assertion_failure(@[<hov 0>catala_position(@[<hov \
0>filename=\"%s\",@ start_line=%d,@ start_column=%d,@ end_line=%d,@ \
end_column=%d,@ law_headings=@[<hv>%a@])@])@]@]@\n\
end_column=%d,@ law_headings=@[<hv>%a@])@])@])@]@\n\
}"
(format_expression ctx)
(e1, Mark.get s)

View File

@ -1,4 +0,0 @@
source("test.r")
v <- Map(function(x) {
foo(catala_struct_FooIn(x_in = catala_integer_from_numeric(x)))
}, c(1:10000))

View File

@ -312,7 +312,7 @@ catala_assertion_failure <- function(pos) {
################ Defaults #################
handle_default <- function(pos, exceptions, just, cons) {
catala_handle_default <- function(pos, exceptions, just, cons) {
acc <- Reduce(function(acc, exception) {
new_val <- tryCatch(
exception(catala_unit(v = 0)),

View File

@ -1,29 +0,0 @@
# Salut
## Coucou
```catala
declaration structure S:
data a content integer
data b content boolean
data c content collection decimal
declaration enumeration E:
-- Case1 content S
-- Case2
declaration scope Foo:
input x content integer
internal y content E
output z content boolean
scope Foo:
definition y equals Case2
exception definition y under condition x = 1 consequence equals
Case1 content S { --a: 1 --b: true --c: [0.2;0.3]}
definition z equals match y with pattern
-- Case1: true
-- Case2: false
```

147
test.r
View File

@ -1,147 +0,0 @@
# This file has been generated by the Catala compiler, do not edit!
source("runtimes/r/runtime.R")
catala_struct_S <- setRefClass("catala_struct_S",
fields = list(
a = "catala_integer", b = "logical",
c = "list" # array("catala_decimal")
)
)
catala_struct_Foo <- setRefClass("catala_struct_Foo",
fields = list(z = "logical")
)
# Enum cases: "Case1" ("catala_struct_S"), "Case2" ("catala_unit")
catala_enum_E <- setRefClass("catala_enum_E",
fields = list(code = "character", value = "ANY")
)
catala_struct_FooIn <- setRefClass("catala_struct_FooIn",
fields = list(x_in = "catala_integer")
)
foo <- function(
foo_in # ("catala_struct_FooIn")
) {
x <- foo_in$x_in
tryCatch(
{
temp_y <- function(dummy_var # ("catala_unit")
) {
return(catala_enum_E(
code = "Case2",
value = catala_unit(v = 0)
))
}
temp_y_1 <- function(dummy_var # ("catala_unit")
) {
return(TRUE)
}
temp_y_2 <- function(dummy_var # ("catala_unit")
) {
temp_y_3 <- function(dummy_var # ("catala_unit")
) {
return(catala_enum_E(
code = "Case1",
value = catala_struct_S(
a = catala_integer_from_numeric(1),
b = TRUE, c = list(
catala_decimal_from_string("0.2"),
catala_decimal_from_string("0.3")
)
)
))
}
temp_y_4 <- function(dummy_var # ("catala_unit")
) {
return((x == catala_integer_from_numeric(1)))
}
return(handle_default(
catala_position(
filename = "",
start_line = 0, start_column = 1,
end_line = 0, end_column = 1,
law_headings = c()
), list(), temp_y_4,
temp_y_3
))
}
temp_y_5 <- handle_default(
catala_position(
filename = "",
start_line = 0, start_column = 1,
end_line = 0, end_column = 1,
law_headings = c()
), list(temp_y_2),
temp_y_1, temp_y
)
},
catala_empty_error = function(dummy__arg) {
temp_y_5 <- dead_value
stop(catala_no_value_provided_error(
catala_position(
filename = "test.catala_en",
start_line = 17,
start_column = 12,
end_line = 17,
end_column = 13,
law_headings = c(
"Coucou",
"Salut"
)
)
))
}
)
y <- temp_y_5
tryCatch(
{
temp_z <- function(dummy_var # ("catala_unit")
) {
match_arg <- y
if (match_arg$code == "Case1") {
dummy_var <- match_arg$value
return(TRUE)
} else if (match_arg$code == "Case2") {
dummy_var <- match_arg$value
return(FALSE)
}
}
temp_z_1 <- function(dummy_var # ("catala_unit")
) {
return(TRUE)
}
temp_z_2 <- handle_default(
catala_position(
filename = "",
start_line = 0, start_column = 1,
end_line = 0, end_column = 1,
law_headings = c()
), list(), temp_z_1,
temp_z
)
},
catala_empty_error = function(dummy__arg) {
temp_z_2 <- dead_value
stop(catala_no_value_provided_error(
catala_position(
filename = "test.catala_en",
start_line = 18,
start_column = 10,
end_line = 18,
end_column = 11,
law_headings = c(
"Coucou",
"Salut"
)
)
))
}
)
z <- temp_z_2
return(catala_struct_Foo(z = z))
}