| A.WhileExp {test; body; pos} ->
(* test : must be int, because we have no bool *)
check_int (trexp test) ~pos;
- ignore (trexp body); (* Only care if a type-error is raised *)
+ let (loop, env) = Env.loop_begin env in
+ (* Only care if an error is raised *)
+ ignore (transExp ~env body);
+ ignore (Env.loop_end env loop);
return_unit
| A.ForExp {var; lo; hi; body; pos; escape=_} ->
check_int (trexp lo) ~pos;
check_int (trexp hi) ~pos;
- (* Only care if a type-error is raised *)
+ let (loop, env) = Env.loop_begin env in
let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
+ (* Only care if an error is raised *)
ignore (transExp ~env body);
+ ignore (Env.loop_end env loop);
return_unit
- | A.BreakExp _ ->
+ | A.BreakExp pos ->
+ (match Env.loop_current env with
+ | Some _ -> ()
+ | None -> E.raise (E.Break_outside_loop pos)
+ );
return_unit
| A.LetExp {decs; body; pos=_} ->
(* (1) decs augment env *)
Env.set_typ env name (Type.Name (name, ref None))
)
in
- List.iter typedecs ~f:(fun (A.TypeDec {name; ty=ty_exp; pos}) ->
- let ty = transTy ~env ty_exp in
- (match env_get_typ ~sym:name ~env ~pos with
+ List.iter typedecs ~f:(fun (A.TypeDec {name=ty_name; ty=ty_exp; pos}) ->
+ let ty = transTy ~env ~ty_name ~ty_exp in
+ (match env_get_typ ~sym:ty_name ~env ~pos with
| Type.Name (_, ty_opt_ref) ->
ty_opt_ref := Some ty
| Type.Unit
);
env_with_fun_heads_only
)
- and transTy ~(env : Env.t) (ty_exp : A.ty) : Type.t =
+ and transTy ~(env : Env.t) ~ty_name ~(ty_exp : A.ty) : Type.t =
(match ty_exp with
| A.NameTy {symbol=sym; pos} ->
env_get_typ ~sym ~env ~pos
(name, ty)
)
in
- Type.new_record fields
+ Type.new_record ~name:ty_name ~fields
| A.ArrayTy {symbol=sym; pos} ->
let element_ty = env_get_typ ~sym ~env ~pos in
- Type.new_array element_ty
+ Type.new_array ~name:ty_name ~ty:element_ty
)
end