2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2021 Inria, contributor:
|
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>
|
2021-06-22 17:01:57 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
2021-06-22 17:01:57 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
2021-06-22 17:01:57 +03:00
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 18:59:49 +03:00
|
|
|
open Shared_ast
|
2021-06-22 17:01:57 +03:00
|
|
|
module A = Ast
|
|
|
|
module L = Lcalc.Ast
|
|
|
|
module D = Dcalc.Ast
|
|
|
|
|
2022-07-28 11:36:36 +03:00
|
|
|
type 'm ctxt = {
|
2023-02-10 20:56:40 +03:00
|
|
|
func_dict : ('m L.expr, A.FuncName.t) Var.Map.t;
|
2022-08-12 23:42:39 +03:00
|
|
|
decl_ctx : decl_ctx;
|
2023-02-10 20:56:40 +03:00
|
|
|
var_dict : ('m L.expr, A.VarName.t) Var.Map.t;
|
|
|
|
inside_definition_of : A.VarName.t option;
|
2022-03-21 16:58:54 +03:00
|
|
|
context_name : string;
|
2021-06-22 17:01:57 +03:00
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(* Expressions can spill out side effect, hence this function also returns a
|
|
|
|
list of statements to be prepended before the expression is evaluated *)
|
2022-08-25 20:46:13 +03:00
|
|
|
let rec translate_expr (ctxt : 'm ctxt) (expr : 'm L.expr) : A.block * A.expr =
|
2022-08-25 18:29:00 +03:00
|
|
|
match Marked.unmark expr with
|
2022-08-12 23:42:39 +03:00
|
|
|
| EVar v ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let local_var =
|
2022-07-28 11:36:36 +03:00
|
|
|
try A.EVar (Var.Map.find v ctxt.var_dict)
|
2022-11-17 19:13:35 +03:00
|
|
|
with Not_found -> (
|
|
|
|
try A.EFunc (Var.Map.find v ctxt.func_dict)
|
|
|
|
with Not_found ->
|
|
|
|
Errors.raise_spanned_error (Expr.pos expr)
|
|
|
|
"Var not found in lambda→scalc: %a@\nknown: @[<hov>%a@]@\n"
|
|
|
|
Print.var_debug v
|
|
|
|
(Format.pp_print_list ~pp_sep:Format.pp_print_space
|
|
|
|
(fun ppf (v, _) -> Print.var_debug ppf v))
|
|
|
|
(Var.Map.bindings ctxt.var_dict))
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-08-25 18:29:00 +03:00
|
|
|
[], (local_var, Expr.pos expr)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EStruct { fields; name } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let args_stmts, new_args =
|
2022-11-21 12:12:45 +03:00
|
|
|
StructField.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun _ arg (args_stmts, new_args) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
2022-05-04 16:48:03 +03:00
|
|
|
arg_stmts @ args_stmts, new_arg :: new_args)
|
2022-11-17 19:13:35 +03:00
|
|
|
fields ([], [])
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
|
|
|
let args_stmts = List.rev args_stmts in
|
2022-11-17 19:13:35 +03:00
|
|
|
args_stmts, (A.EStruct (new_args, name), Expr.pos expr)
|
|
|
|
| ETuple _ -> failwith "Tuples cannot be compiled to scalc"
|
|
|
|
| EStructAccess { e = e1; field; name } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
2022-11-17 19:13:35 +03:00
|
|
|
e1_stmts, (A.EStructFieldAccess (new_e1, field, name), Expr.pos expr)
|
|
|
|
| ETupleAccess _ -> failwith "Non-struct tuples cannot be compiled to scalc"
|
|
|
|
| EInj { e = e1; cons; name } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
2022-11-17 19:13:35 +03:00
|
|
|
e1_stmts, (A.EInj (new_e1, cons, name), Expr.pos expr)
|
|
|
|
| EApp { f; args } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let f_stmts, new_f = translate_expr ctxt f in
|
|
|
|
let args_stmts, new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun (args_stmts, new_args) arg ->
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
2022-05-04 16:48:03 +03:00
|
|
|
arg_stmts @ args_stmts, new_arg :: new_args)
|
2022-05-04 18:40:55 +03:00
|
|
|
([], []) args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
2022-08-25 18:29:00 +03:00
|
|
|
f_stmts @ args_stmts, (A.EApp (new_f, new_args), Expr.pos expr)
|
2022-08-12 23:42:39 +03:00
|
|
|
| EArray args ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let args_stmts, new_args =
|
|
|
|
List.fold_left
|
|
|
|
(fun (args_stmts, new_args) arg ->
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
2022-05-04 16:48:03 +03:00
|
|
|
arg_stmts @ args_stmts, new_arg :: new_args)
|
2022-05-04 18:40:55 +03:00
|
|
|
([], []) args
|
|
|
|
in
|
|
|
|
let new_args = List.rev new_args in
|
2022-08-25 18:29:00 +03:00
|
|
|
args_stmts, (A.EArray new_args, Expr.pos expr)
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
| EOp { op; _ } -> [], (A.EOp op, Expr.pos expr)
|
2022-08-25 18:29:00 +03:00
|
|
|
| ELit l -> [], (A.ELit l, Expr.pos expr)
|
2021-06-23 18:47:34 +03:00
|
|
|
| _ ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let tmp_var =
|
2023-02-10 20:56:40 +03:00
|
|
|
A.VarName.fresh
|
2022-05-04 18:40:55 +03:00
|
|
|
( (*This piece of logic is used to make the code more readable. TODO:
|
|
|
|
should be removed when
|
|
|
|
https://github.com/CatalaLang/catala/issues/240 is fixed. *)
|
|
|
|
(match ctxt.inside_definition_of with
|
|
|
|
| None -> ctxt.context_name
|
|
|
|
| Some v ->
|
2023-02-10 20:56:40 +03:00
|
|
|
let v = Marked.unmark (A.VarName.get_info v) in
|
2022-05-04 18:40:55 +03:00
|
|
|
let tmp_rex = Re.Pcre.regexp "^temp_" in
|
|
|
|
if Re.Pcre.pmatch ~rex:tmp_rex v then v else "temp_" ^ v),
|
2022-08-25 18:29:00 +03:00
|
|
|
Expr.pos expr )
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
inside_definition_of = Some tmp_var;
|
2023-02-10 20:56:40 +03:00
|
|
|
context_name = Marked.unmark (A.VarName.get_info tmp_var);
|
2022-05-04 18:40:55 +03:00
|
|
|
}
|
|
|
|
in
|
2022-08-25 18:29:00 +03:00
|
|
|
let tmp_stmts = translate_statements ctxt expr in
|
|
|
|
( ( A.SLocalDecl ((tmp_var, Expr.pos expr), (TAny, Expr.pos expr)),
|
|
|
|
Expr.pos expr )
|
2022-05-04 18:40:55 +03:00
|
|
|
:: tmp_stmts,
|
2022-08-25 18:29:00 +03:00
|
|
|
(A.EVar tmp_var, Expr.pos expr) )
|
2021-06-22 19:16:47 +03:00
|
|
|
|
2022-08-25 20:46:13 +03:00
|
|
|
and translate_statements (ctxt : 'm ctxt) (block_expr : 'm L.expr) : A.block =
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark block_expr with
|
2022-08-12 23:42:39 +03:00
|
|
|
| EAssert e ->
|
2022-05-04 18:40:55 +03:00
|
|
|
(* Assertions are always encapsulated in a unit-typed let binding *)
|
|
|
|
let e_stmts, new_e = translate_expr ctxt e in
|
2022-08-12 23:42:39 +03:00
|
|
|
e_stmts @ [A.SAssert (Marked.unmark new_e), Expr.pos block_expr]
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp { f = EAbs { binder; tys }, binder_mark; args } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
(* This defines multiple local variables at the time *)
|
2022-08-12 23:42:39 +03:00
|
|
|
let binder_pos = Expr.mark_pos binder_mark in
|
2022-05-04 18:40:55 +03:00
|
|
|
let vars, body = Bindlib.unmbind binder in
|
2022-11-17 19:13:35 +03:00
|
|
|
let vars_tau = List.map2 (fun x tau -> x, tau) (Array.to_list vars) tys in
|
2022-05-04 18:40:55 +03:00
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
var_dict =
|
|
|
|
List.fold_left
|
|
|
|
(fun var_dict (x, _) ->
|
2022-07-28 11:36:36 +03:00
|
|
|
Var.Map.add x
|
2023-02-10 20:56:40 +03:00
|
|
|
(A.VarName.fresh (Bindlib.name_of x, binder_pos))
|
2022-05-04 18:40:55 +03:00
|
|
|
var_dict)
|
|
|
|
ctxt.var_dict vars_tau;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
let local_decls =
|
|
|
|
List.map
|
|
|
|
(fun (x, tau) ->
|
2022-07-28 11:36:36 +03:00
|
|
|
( A.SLocalDecl ((Var.Map.find x ctxt.var_dict, binder_pos), tau),
|
2022-06-03 17:40:03 +03:00
|
|
|
binder_pos ))
|
2022-05-04 18:40:55 +03:00
|
|
|
vars_tau
|
|
|
|
in
|
|
|
|
let vars_args =
|
|
|
|
List.map2
|
|
|
|
(fun (x, tau) arg ->
|
2022-07-28 11:36:36 +03:00
|
|
|
(Var.Map.find x ctxt.var_dict, binder_pos), tau, arg)
|
2022-05-04 18:40:55 +03:00
|
|
|
vars_tau args
|
|
|
|
in
|
|
|
|
let def_blocks =
|
|
|
|
List.map
|
|
|
|
(fun (x, _tau, arg) ->
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
2022-05-30 12:20:48 +03:00
|
|
|
inside_definition_of = Some (Marked.unmark x);
|
|
|
|
context_name =
|
2023-02-10 20:56:40 +03:00
|
|
|
Marked.unmark (A.VarName.get_info (Marked.unmark x));
|
2022-05-04 18:40:55 +03:00
|
|
|
}
|
|
|
|
in
|
|
|
|
let arg_stmts, new_arg = translate_expr ctxt arg in
|
2022-05-04 16:48:03 +03:00
|
|
|
arg_stmts @ [A.SLocalDef (x, new_arg), binder_pos])
|
2022-05-04 18:40:55 +03:00
|
|
|
vars_args
|
|
|
|
in
|
|
|
|
let rest_of_block = translate_statements ctxt body in
|
|
|
|
local_decls @ List.flatten def_blocks @ rest_of_block
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; tys } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let vars, body = Bindlib.unmbind binder in
|
2022-08-12 23:42:39 +03:00
|
|
|
let binder_pos = Expr.pos block_expr in
|
2022-11-17 19:13:35 +03:00
|
|
|
let vars_tau = List.map2 (fun x tau -> x, tau) (Array.to_list vars) tys in
|
2022-05-04 18:40:55 +03:00
|
|
|
let closure_name =
|
|
|
|
match ctxt.inside_definition_of with
|
2023-02-10 20:56:40 +03:00
|
|
|
| None -> A.VarName.fresh (ctxt.context_name, Expr.pos block_expr)
|
2022-05-04 18:40:55 +03:00
|
|
|
| Some x -> x
|
|
|
|
in
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
ctxt with
|
|
|
|
var_dict =
|
|
|
|
List.fold_left
|
|
|
|
(fun var_dict (x, _) ->
|
2022-07-28 11:36:36 +03:00
|
|
|
Var.Map.add x
|
2023-02-10 20:56:40 +03:00
|
|
|
(A.VarName.fresh (Bindlib.name_of x, binder_pos))
|
2022-05-04 18:40:55 +03:00
|
|
|
var_dict)
|
|
|
|
ctxt.var_dict vars_tau;
|
|
|
|
inside_definition_of = None;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
let new_body = translate_statements ctxt body in
|
|
|
|
[
|
|
|
|
( A.SInnerFuncDef
|
|
|
|
( (closure_name, binder_pos),
|
|
|
|
{
|
|
|
|
func_params =
|
|
|
|
List.map
|
|
|
|
(fun (var, tau) ->
|
2022-07-28 11:36:36 +03:00
|
|
|
(Var.Map.find var ctxt.var_dict, binder_pos), tau)
|
2022-05-04 18:40:55 +03:00
|
|
|
vars_tau;
|
|
|
|
func_body = new_body;
|
|
|
|
} ),
|
|
|
|
binder_pos );
|
|
|
|
]
|
2022-11-17 19:13:35 +03:00
|
|
|
| EMatch { e = e1; cases; name } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let e1_stmts, new_e1 = translate_expr ctxt e1 in
|
2022-11-17 19:13:35 +03:00
|
|
|
let new_cases =
|
2022-11-21 12:12:45 +03:00
|
|
|
EnumConstructor.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun _ arg new_args ->
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark arg with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; _ } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let vars, body = Bindlib.unmbind binder in
|
|
|
|
assert (Array.length vars = 1);
|
|
|
|
let var = vars.(0) in
|
|
|
|
let scalc_var =
|
2023-02-10 20:56:40 +03:00
|
|
|
A.VarName.fresh (Bindlib.name_of var, Expr.pos arg)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
|
|
|
let ctxt =
|
2022-07-28 11:36:36 +03:00
|
|
|
{ ctxt with var_dict = Var.Map.add var scalc_var ctxt.var_dict }
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
|
|
|
let new_arg = translate_statements ctxt body in
|
|
|
|
(new_arg, scalc_var) :: new_args
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *))
|
2022-11-17 19:13:35 +03:00
|
|
|
cases []
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
let new_args = List.rev new_cases in
|
|
|
|
e1_stmts @ [A.SSwitch (new_e1, name, new_args), Expr.pos block_expr]
|
|
|
|
| EIfThenElse { cond; etrue; efalse } ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let cond_stmts, s_cond = translate_expr ctxt cond in
|
2022-11-17 19:13:35 +03:00
|
|
|
let s_e_true = translate_statements ctxt etrue in
|
|
|
|
let s_e_false = translate_statements ctxt efalse in
|
2022-08-16 11:04:01 +03:00
|
|
|
cond_stmts
|
|
|
|
@ [A.SIfThenElse (s_cond, s_e_true, s_e_false), Expr.pos block_expr]
|
2022-11-17 19:13:35 +03:00
|
|
|
| ECatch { body; exn; handler } ->
|
|
|
|
let s_e_try = translate_statements ctxt body in
|
|
|
|
let s_e_catch = translate_statements ctxt handler in
|
|
|
|
[A.STryExcept (s_e_try, exn, s_e_catch), Expr.pos block_expr]
|
2022-08-12 23:42:39 +03:00
|
|
|
| ERaise except ->
|
2022-07-25 12:00:18 +03:00
|
|
|
(* Before raising the exception, we still give a dummy definition to the
|
|
|
|
current variable so that tools like mypy don't complain. *)
|
|
|
|
(match ctxt.inside_definition_of with
|
|
|
|
| None -> []
|
|
|
|
| Some x ->
|
|
|
|
[
|
|
|
|
( A.SLocalDef
|
2022-08-16 11:04:01 +03:00
|
|
|
( (x, Expr.pos block_expr),
|
|
|
|
(Ast.EVar Ast.dead_value, Expr.pos block_expr) ),
|
2022-08-12 23:42:39 +03:00
|
|
|
Expr.pos block_expr );
|
2022-07-25 12:00:18 +03:00
|
|
|
])
|
2022-08-12 23:42:39 +03:00
|
|
|
@ [A.SRaise except, Expr.pos block_expr]
|
2022-02-01 17:41:53 +03:00
|
|
|
| _ -> (
|
2022-05-04 18:40:55 +03:00
|
|
|
let e_stmts, new_e = translate_expr ctxt block_expr in
|
|
|
|
e_stmts
|
|
|
|
@
|
|
|
|
match e_stmts with
|
|
|
|
| (A.SRaise _, _) :: _ ->
|
|
|
|
(* if the last statement raises an exception, then we don't need to return
|
|
|
|
or to define the current variable since this code will be
|
|
|
|
unreachable *)
|
|
|
|
[]
|
|
|
|
| _ ->
|
|
|
|
[
|
|
|
|
( (match ctxt.inside_definition_of with
|
2022-05-30 12:20:48 +03:00
|
|
|
| None -> A.SReturn (Marked.unmark new_e)
|
|
|
|
| Some x -> A.SLocalDef (Marked.same_mark_as x new_e, new_e)),
|
2022-08-12 23:42:39 +03:00
|
|
|
Expr.pos block_expr );
|
2022-05-04 18:40:55 +03:00
|
|
|
])
|
2021-06-22 17:01:57 +03:00
|
|
|
|
2022-04-26 17:06:36 +03:00
|
|
|
let rec translate_scope_body_expr
|
2022-08-12 23:42:39 +03:00
|
|
|
(scope_name : ScopeName.t)
|
|
|
|
(decl_ctx : decl_ctx)
|
2023-02-10 20:56:40 +03:00
|
|
|
(var_dict : ('m L.expr, A.VarName.t) Var.Map.t)
|
|
|
|
(func_dict : ('m L.expr, A.FuncName.t) Var.Map.t)
|
2022-08-25 20:46:13 +03:00
|
|
|
(scope_expr : 'm L.expr scope_body_expr) : A.block =
|
2022-04-26 17:06:36 +03:00
|
|
|
match scope_expr with
|
|
|
|
| Result e ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let block, new_e =
|
|
|
|
translate_expr
|
|
|
|
{
|
|
|
|
decl_ctx;
|
|
|
|
func_dict;
|
|
|
|
var_dict;
|
|
|
|
inside_definition_of = None;
|
2022-08-12 23:42:39 +03:00
|
|
|
context_name = Marked.unmark (ScopeName.get_info scope_name);
|
2022-05-04 18:40:55 +03:00
|
|
|
}
|
|
|
|
e
|
|
|
|
in
|
2022-05-30 12:20:48 +03:00
|
|
|
block @ [A.SReturn (Marked.unmark new_e), Marked.get_mark new_e]
|
2022-05-04 18:40:55 +03:00
|
|
|
| ScopeLet scope_let ->
|
|
|
|
let let_var, scope_let_next = Bindlib.unbind scope_let.scope_let_next in
|
|
|
|
let let_var_id =
|
2023-02-10 20:56:40 +03:00
|
|
|
A.VarName.fresh (Bindlib.name_of let_var, scope_let.scope_let_pos)
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-07-28 11:36:36 +03:00
|
|
|
let new_var_dict = Var.Map.add let_var let_var_id var_dict in
|
2022-05-04 18:40:55 +03:00
|
|
|
(match scope_let.scope_let_kind with
|
2022-08-12 23:42:39 +03:00
|
|
|
| Assertion ->
|
2022-05-04 18:40:55 +03:00
|
|
|
translate_statements
|
|
|
|
{
|
|
|
|
decl_ctx;
|
|
|
|
func_dict;
|
|
|
|
var_dict;
|
|
|
|
inside_definition_of = Some let_var_id;
|
2022-08-12 23:42:39 +03:00
|
|
|
context_name = Marked.unmark (ScopeName.get_info scope_name);
|
2022-05-04 18:40:55 +03:00
|
|
|
}
|
|
|
|
scope_let.scope_let_expr
|
|
|
|
| _ ->
|
|
|
|
let let_expr_stmts, new_let_expr =
|
2022-04-26 17:06:36 +03:00
|
|
|
translate_expr
|
|
|
|
{
|
|
|
|
decl_ctx;
|
|
|
|
func_dict;
|
|
|
|
var_dict;
|
2022-05-04 18:40:55 +03:00
|
|
|
inside_definition_of = Some let_var_id;
|
2022-08-12 23:42:39 +03:00
|
|
|
context_name = Marked.unmark (ScopeName.get_info scope_name);
|
2022-04-26 17:06:36 +03:00
|
|
|
}
|
2022-05-04 18:40:55 +03:00
|
|
|
scope_let.scope_let_expr
|
2021-06-22 17:01:57 +03:00
|
|
|
in
|
2022-05-04 18:40:55 +03:00
|
|
|
let_expr_stmts
|
|
|
|
@ [
|
|
|
|
( A.SLocalDecl
|
|
|
|
((let_var_id, scope_let.scope_let_pos), scope_let.scope_let_typ),
|
|
|
|
scope_let.scope_let_pos );
|
|
|
|
( A.SLocalDef ((let_var_id, scope_let.scope_let_pos), new_let_expr),
|
|
|
|
scope_let.scope_let_pos );
|
|
|
|
])
|
|
|
|
@ translate_scope_body_expr scope_name decl_ctx new_var_dict func_dict
|
|
|
|
scope_let_next
|
2021-06-22 17:01:57 +03:00
|
|
|
|
2022-06-23 15:06:11 +03:00
|
|
|
let translate_program (p : 'm L.program) : A.program =
|
2023-01-23 14:19:36 +03:00
|
|
|
let _, _, rglobals, rscopes =
|
|
|
|
Scope.fold_left
|
|
|
|
~f:(fun (func_dict, var_dict, globals, scopes) code_item var ->
|
|
|
|
match code_item with
|
|
|
|
| ScopeDef (name, body) ->
|
|
|
|
let scope_input_var, scope_body_expr =
|
|
|
|
Bindlib.unbind body.scope_body_expr
|
|
|
|
in
|
|
|
|
let input_pos = Marked.get_mark (ScopeName.get_info name) in
|
|
|
|
let scope_input_var_id =
|
2023-02-10 20:56:40 +03:00
|
|
|
A.VarName.fresh (Bindlib.name_of scope_input_var, input_pos)
|
2023-01-23 14:19:36 +03:00
|
|
|
in
|
|
|
|
let var_dict_local =
|
|
|
|
Var.Map.add scope_input_var scope_input_var_id var_dict
|
|
|
|
in
|
|
|
|
let new_scope_body =
|
|
|
|
translate_scope_body_expr name p.decl_ctx var_dict_local func_dict
|
|
|
|
scope_body_expr
|
|
|
|
in
|
2023-02-10 20:56:40 +03:00
|
|
|
let func_id = A.FuncName.fresh (Bindlib.name_of var, Pos.no_pos) in
|
2023-01-23 14:19:36 +03:00
|
|
|
( Var.Map.add var func_id func_dict,
|
|
|
|
var_dict,
|
|
|
|
globals,
|
|
|
|
{
|
|
|
|
Ast.scope_body_name = name;
|
|
|
|
Ast.scope_body_var = func_id;
|
|
|
|
scope_body_func =
|
|
|
|
{
|
|
|
|
A.func_params =
|
|
|
|
[
|
|
|
|
( (scope_input_var_id, input_pos),
|
|
|
|
(TStruct body.scope_body_input_struct, input_pos) );
|
|
|
|
];
|
|
|
|
A.func_body = new_scope_body;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
:: scopes )
|
2023-02-10 20:56:40 +03:00
|
|
|
| Topdef (name, _, (EAbs abs, _)) ->
|
|
|
|
(* Toplevel function def *)
|
|
|
|
let func_id = A.FuncName.fresh (Bindlib.name_of var, Pos.no_pos) in
|
|
|
|
let args_a, expr = Bindlib.unmbind abs.binder in
|
|
|
|
let args = Array.to_list args_a in
|
|
|
|
let args_id =
|
|
|
|
List.map2
|
|
|
|
(fun v ty ->
|
|
|
|
let pos = Marked.get_mark ty in
|
|
|
|
(A.VarName.fresh (Bindlib.name_of v, pos), pos), ty)
|
|
|
|
args abs.tys
|
|
|
|
in
|
|
|
|
let block, expr =
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
func_dict;
|
|
|
|
decl_ctx = p.decl_ctx;
|
|
|
|
var_dict =
|
|
|
|
List.fold_left2
|
|
|
|
(fun map arg ((id, _), _) -> Var.Map.add arg id map)
|
|
|
|
var_dict args args_id;
|
|
|
|
inside_definition_of = None;
|
|
|
|
context_name = Marked.unmark (TopdefName.get_info name);
|
|
|
|
}
|
|
|
|
in
|
|
|
|
translate_expr ctxt expr
|
|
|
|
in
|
|
|
|
let body_block =
|
|
|
|
block @ [A.SReturn (Marked.unmark expr), Marked.get_mark expr]
|
|
|
|
in
|
|
|
|
( Var.Map.add var func_id func_dict,
|
|
|
|
var_dict,
|
|
|
|
A.GlobalFunc
|
|
|
|
{
|
|
|
|
var = func_id;
|
|
|
|
func = { A.func_params = args_id; A.func_body = body_block };
|
|
|
|
}
|
|
|
|
:: globals,
|
|
|
|
scopes )
|
|
|
|
| Topdef (name, ty, expr) ->
|
|
|
|
(* Toplevel constant def *)
|
|
|
|
let var_id = A.VarName.fresh (Bindlib.name_of var, Pos.no_pos) in
|
|
|
|
let block, expr =
|
|
|
|
let ctxt =
|
|
|
|
{
|
|
|
|
func_dict;
|
|
|
|
decl_ctx = p.decl_ctx;
|
|
|
|
var_dict;
|
|
|
|
inside_definition_of = None;
|
|
|
|
context_name = Marked.unmark (TopdefName.get_info name);
|
|
|
|
}
|
|
|
|
in
|
|
|
|
translate_expr ctxt expr
|
|
|
|
in
|
|
|
|
(* If the evaluation of the toplevel expr requires preliminary
|
|
|
|
statements, we lift its computation into an auxiliary function *)
|
|
|
|
let globals =
|
|
|
|
match block with
|
|
|
|
| [] -> A.GlobalVar { var = var_id; expr } :: globals
|
|
|
|
| block ->
|
|
|
|
let pos = Marked.get_mark expr in
|
|
|
|
let func_id =
|
|
|
|
A.FuncName.fresh (Bindlib.name_of var ^ "_aux", pos)
|
|
|
|
in
|
|
|
|
(* The list is being built in reverse order *)
|
|
|
|
A.GlobalVar
|
|
|
|
{ var = var_id; expr = A.EApp ((EFunc func_id, pos), []), pos }
|
|
|
|
:: A.GlobalFunc
|
|
|
|
{
|
|
|
|
var = func_id;
|
|
|
|
func =
|
|
|
|
{
|
|
|
|
A.func_params = [];
|
|
|
|
A.func_body =
|
|
|
|
block
|
|
|
|
@ [
|
|
|
|
( A.SReturn (Marked.unmark expr),
|
|
|
|
Marked.get_mark expr );
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
:: globals
|
|
|
|
in
|
|
|
|
( func_dict,
|
|
|
|
(* No need to add func_id since the function will only be called
|
|
|
|
right here *)
|
|
|
|
Var.Map.add var var_id var_dict,
|
|
|
|
globals,
|
|
|
|
scopes ))
|
2023-01-23 14:19:36 +03:00
|
|
|
~init:
|
|
|
|
( (if !Cli.avoid_exceptions_flag then
|
|
|
|
Var.Map.singleton L.handle_default_opt A.handle_default_opt
|
|
|
|
else Var.Map.singleton L.handle_default A.handle_default),
|
|
|
|
Var.Map.empty,
|
|
|
|
[],
|
|
|
|
[] )
|
|
|
|
p.scopes
|
|
|
|
in
|
2021-06-22 17:01:57 +03:00
|
|
|
{
|
2022-08-12 23:42:39 +03:00
|
|
|
decl_ctx = p.decl_ctx;
|
2023-01-23 14:19:36 +03:00
|
|
|
globals = List.rev rglobals;
|
|
|
|
scopes = List.rev rscopes;
|
2021-06-22 17:01:57 +03:00
|
|
|
}
|