Skip to content

Commit 8e56b53

Browse files
refactor: introduces a struct for mixin data
1 parent 884d377 commit 8e56b53

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/mo_frontend/scope.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ type lib_env = T.typ T.Env.t
1414
type typ_env = T.con T.Env.t
1515
type con_env = T.ConSet.t
1616
type fld_src_env = Field_sources.srcs_map
17-
18-
type mixin_env = (S.import list * S.pat * S.dec_field list * T.typ) T.Env.t
19-
and obj_env = scope T.Env.t (* internal object scopes *)
17+
type mixin_env = mixin_data T.Env.t
18+
and mixin_data = {
19+
imports : S.import list;
20+
arg : S.pat;
21+
decs : S.dec_field list;
22+
typ : T.typ;
23+
}
24+
type obj_env = scope T.Env.t (* internal object scopes *)
2025
and scope =
2126
{ val_env : val_env;
2227
lib_env : lib_env;

src/mo_frontend/scope.mli

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ type lib_env = Mo_types.Type.typ Env.t
66
type typ_env = con Env.t
77
type con_env = ConSet.t
88
type fld_src_env = Mo_types.Field_sources.srcs_map
9-
10-
(* TODO: make this a record *)
11-
type mixin_env = (Mo_def.Syntax.import list * Mo_def.Syntax.pat * Mo_def.Syntax.dec_field list * typ) Env.t
12-
and obj_env = scope Env.t (* internal object scopes *)
9+
type mixin_env = mixin_data Env.t
10+
and mixin_data = {
11+
imports : Mo_def.Syntax.import list;
12+
arg : Mo_def.Syntax.pat;
13+
decs : Mo_def.Syntax.dec_field list;
14+
typ : typ;
15+
}
16+
type obj_env = scope Env.t (* internal object scopes *)
1317
and scope =
1418
{ val_env : val_env;
1519
lib_env : lib_env;
@@ -26,4 +30,4 @@ val adjoin : scope -> scope -> scope
2630

2731
val adjoin_val_env : scope -> val_env -> scope
2832
val lib : string -> typ -> scope
29-
val mixin : string -> Mo_def.Syntax.import list * Mo_def.Syntax.pat * Mo_def.Syntax.dec_field list * typ -> scope
33+
val mixin : string -> mixin_data -> scope

src/mo_frontend/typing.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,7 +4070,7 @@ and infer_dec env dec : T.typ =
40704070
error env dec.at "M0227" "mixins can only be included in an actor context";
40714071
match T.Env.find_opt i.it env.mixins with
40724072
| None -> error env i.at "M0226" "unknown mixin %s" i.it
4073-
| Some (_, pat, _, _) -> check_exp env pat.note arg
4073+
| Some mix -> check_exp env mix.Scope.arg.note arg
40744074
end;
40754075
T.unit
40764076
| ExpD exp -> infer_exp env exp
@@ -4263,9 +4263,9 @@ and gather_dec env scope dec : Scope.t =
42634263
}
42644264
| LetD (pat, exp, _) -> (match is_mixin_import env exp.it with
42654265
| None -> gather_pat env scope pat
4266-
| Some (imports, args, t, decs) ->
4266+
| Some mix ->
42674267
match pat.it with
4268-
| VarP id -> Scope.adjoin scope (Scope.mixin id.it (imports, args, t, decs))
4268+
| VarP id -> Scope.adjoin scope (Scope.mixin id.it mix)
42694269
| _ -> error env pat.at "M0229" "mixins may only be imported by binding to a name"
42704270
)
42714271
| VarD (id, _) -> Scope.adjoin_val_env scope (gather_id env scope.Scope.val_env id Scope.Declaration)
@@ -4307,9 +4307,9 @@ and gather_dec env scope dec : Scope.t =
43074307
| IncludeD(i, _, _) -> begin
43084308
match T.Env.find_opt i.it env.mixins with
43094309
| None -> error env i.at "M0226" "unknown mixin %s" i.it
4310-
| Some(imports, pat, decs, t) ->
4310+
| Some mix ->
43114311
let open Scope in
4312-
let (_, fields) = T.as_obj t in
4312+
let (_, fields) = T.as_obj mix.typ in
43134313
let add_field acc = function
43144314
| T.{ lab; typ = T.Typ t; _ } ->
43154315
if T.Env.mem lab acc.typ_env then error_duplicate env "type " { it = lab; at = i.at; note = () };
@@ -4373,12 +4373,13 @@ and infer_dec_typdecs env dec : Scope.t =
43734373
| IncludeD (i, _, n) -> begin
43744374
match T.Env.find_opt i.it env.mixins with
43754375
| None -> error env i.at "M0226" "unknown mixin %s" i.it
4376-
| Some(imports, pat, decs, t) ->
4377-
n := Some({ imports; pat; decs });
4378-
let (_, fields) = T.as_obj t in
4376+
| Some mix ->
4377+
let open Scope in
4378+
n := Some({ imports = mix.imports; pat = mix.arg; decs = mix.decs });
4379+
let (_, fields) = T.as_obj mix.typ in
43794380
let scope = scope_of_object env fields in
43804381
(* Mark all included idents as used to avoid spurious warnings *)
4381-
T.Env.iter (fun i _ -> use_identifier env i) scope.Scope.val_env;
4382+
T.Env.iter (fun i _ -> use_identifier env i) scope.val_env;
43824383
scope
43834384
end
43844385
(* TODO: generalize beyond let <id> = <obje> *)
@@ -4401,9 +4402,9 @@ and infer_dec_typdecs env dec : Scope.t =
44014402
(* TODO: generalize beyond let <id> = <valpath> *)
44024403
| LetD ({it = VarP id; _}, exp, _) ->
44034404
begin match is_mixin_import env exp.it with
4404-
| Some (imports, args, t, decs) ->
4405+
| Some mix ->
44054406
(* Format.printf "Adding mixin %s at %a\n" id.it display_typ t; *)
4406-
Scope.mixin id.it (imports, args, t, decs)
4407+
Scope.mixin id.it mix
44074408
| None ->
44084409
(match infer_val_path env exp with
44094410
| None -> Scope.empty
@@ -4648,8 +4649,8 @@ let check_lib scope pkg_opt lib : Scope.t Diag.result =
46484649
("system", obj Module [id.it, install_typ (List.map (close cs) ts1) class_typ])
46494650
]) in
46504651
Scope.lib lib.note.filename typ
4651-
| MixinU (pat, decs) ->
4652-
Scope.mixin lib.note.filename (imports, pat, decs, typ)
4652+
| MixinU (arg, decs) ->
4653+
Scope.mixin lib.note.filename Scope.{ imports; arg; decs; typ }
46534654
| ActorU _ ->
46544655
error env cub.at "M0144" "bad import: expected a module or actor class but found an actor"
46554656
| ProgU _ ->

0 commit comments

Comments
 (0)