home
/
code
/
tiger.ml.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix use type of last expression in a sequence
[tiger.ml.git]
/
compiler
/
src
/
lib
/
tiger
/
tiger_semant.ml
diff --git
a/compiler/src/lib/tiger/tiger_semant.ml
b/compiler/src/lib/tiger/tiger_semant.ml
index
a0fdf3d
..
70639e9
100644
(file)
--- a/
compiler/src/lib/tiger/tiger_semant.ml
+++ b/
compiler/src/lib/tiger/tiger_semant.ml
@@
-19,18
+19,19
@@
module Semant : sig
* Appel's
*)
val transExp : env:Env.t -> A.exp -> expty
* Appel's
*)
val transExp : env:Env.t -> A.exp -> expty
- val transVar : env:Env.t -> A.var -> expty
- val transDec : env:Env.t -> A.dec -> Env.t
- val transTy : env:Env.t -> A.ty -> Type.t (* needs only type env *)
+
+ (* transVar does not seem to be needed, as trvar handles all our cases.
+ * Am I wrong?
+ *
+ * val transVar : env:Env.t -> A.var -> expty
+ *
+ *)
end = struct
type expty =
{ exp : Translate.exp
; ty : Type.t
}
end = struct
type expty =
{ exp : Translate.exp
; ty : Type.t
}
- let unimplemented () =
- failwith "unimplemented"
-
let return ty = {exp = (); ty}
let return_unit = return Type.Unit
let return_nil = return Type.Nil
let return ty = {exp = (); ty}
let return_unit = return Type.Unit
let return_nil = return Type.Nil
@@
-99,12
+100,18
@@
end = struct
E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
);
return ty
E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
);
return ty
- | A.SeqExp exps ->
- (* Ignoring value because we only care if a type-checking exception
- * is raised in one of trexp calls: *)
- List.iter exps ~f:(fun (exp, _) -> ignore (trexp exp));
- (* FIXME: Return type of last expression, not unit. *)
+ | A.SeqExp [] ->
return_unit
return_unit
+ | A.SeqExp exps ->
+ let last xs =
+ xs
+ |> List.rev (* Yes, redundant, but clean-looking ;-P *)
+ |> List.hd (* Empty is matched in above SeqExp match case *)
+ in
+ exps
+ |> List.map ~f:(fun (exp, _) -> let {ty; _} = trexp exp in ty)
+ |> last
+ |> return
| A.AssignExp {var; exp; pos} ->
check_same (trvar var) (trexp exp) ~pos;
(* TODO: Add var->exp to val env? *)
| A.AssignExp {var; exp; pos} ->
check_same (trvar var) (trexp exp) ~pos;
(* TODO: Add var->exp to val env? *)
@@
-131,7
+138,8
@@
end = struct
check_int (trexp lo) ~pos;
check_int (trexp hi) ~pos;
(* Only care if a type-error is raised *)
check_int (trexp lo) ~pos;
check_int (trexp hi) ~pos;
(* Only care if a type-error is raised *)
- ignore (transExp ~env:(Env.set_typ env var Type.Int) body);
+ let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
+ ignore (transExp ~env body);
return_unit
| A.BreakExp _ ->
return_unit
return_unit
| A.BreakExp _ ->
return_unit
@@
-235,7
+243,7
@@
end = struct
)
in
trexp exp
)
in
trexp exp
- and transDec ~
env dec
=
+ and transDec ~
(env : Env.t) (dec : A.dec) : Env.t
=
(match dec with
| A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
let ty =
(match dec with
| A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
let ty =
@@
-279,7
+287,7
@@
end = struct
Env.set_val env name (Value.Fun {formals; result})
)
)
Env.set_val env name (Value.Fun {formals; result})
)
)
- and transTy ~
env typ
=
+ and transTy ~
(env : Env.t) (typ : A.ty) : Type.t
=
(match typ with
| A.NameTy {symbol=sym; pos} ->
env_get_typ ~sym ~env ~pos
(match typ with
| A.NameTy {symbol=sym; pos} ->
env_get_typ ~sym ~env ~pos
@@
-295,16
+303,6
@@
end = struct
let element_ty = env_get_typ ~sym ~env ~pos in
Type.new_array element_ty
)
let element_ty = env_get_typ ~sym ~env ~pos in
Type.new_array element_ty
)
-
- let transVar ~env:_ var =
- (match var with
- | A.SimpleVar {symbol=_; _} ->
- unimplemented ()
- | A.FieldVar {var=_; symbol=_; _} ->
- unimplemented ()
- | A.SubscriptVar {var=_; exp=_; _} ->
- unimplemented ()
- )
end
open Semant
end
open Semant
This page took
0.033885 seconds
and
4
git commands to generate.