feat: ✨ fin TP type + début placement mémoire
This commit is contained in:
parent
415a0249a1
commit
bbb7239e52
51
src/passePlacementRat.ml
Normal file
51
src/passePlacementRat.ml
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
(* Module de la passe de typage *)
|
||||||
|
module PassePlacementRat : Passe.Passe with type t1 = Ast.AstType.programme and type t2 = Ast.AstType.programme =
|
||||||
|
struct
|
||||||
|
|
||||||
|
open Tds
|
||||||
|
open Exceptions
|
||||||
|
open Ast
|
||||||
|
open AstType
|
||||||
|
open Type
|
||||||
|
|
||||||
|
type t1 = Ast.AstType.programme
|
||||||
|
type t2 = Ast.AstType.programme
|
||||||
|
|
||||||
|
let rec analyse_placement_instruction i base reg =
|
||||||
|
match i with
|
||||||
|
| AstType.Declaration(info, e) ->
|
||||||
|
begin
|
||||||
|
match info_ast_to_info info with
|
||||||
|
| InfoVar(_, t, _, _) ->
|
||||||
|
let taille = getTaille t in
|
||||||
|
modifier_adresse_info base reg info;
|
||||||
|
taille
|
||||||
|
| _ -> failwith "wtf frère"
|
||||||
|
end
|
||||||
|
|
||||||
|
| AstType.Conditionnelle(e, b1, b2) ->
|
||||||
|
let _ = analyse_placement_bloc base reg b1 in
|
||||||
|
let _ = analyse_placement_bloc base reg b2 in
|
||||||
|
0
|
||||||
|
|
||||||
|
| AstType.TantQue(e, b) ->
|
||||||
|
let _ = analyse_placement_bloc base reg b in
|
||||||
|
0
|
||||||
|
|
||||||
|
| _ -> 0
|
||||||
|
|
||||||
|
and analyse_placement_bloc base reg bloc =
|
||||||
|
match bloc with
|
||||||
|
| [] -> base
|
||||||
|
| t::q ->
|
||||||
|
let taille = analyse_placement_instruction t base reg in
|
||||||
|
analyse_placement_bloc (base + taille) reg q
|
||||||
|
|
||||||
|
and analyse_placement_fonction a b c = failwith "TODO"
|
||||||
|
|
||||||
|
let analyser (AstType.Programme(fonctions, prog)) = failwith "TODO"
|
||||||
|
(* let nf = List.map (analyse_type_bloc Some(...)) fonctions in *)
|
||||||
|
(* let nb = analyse_placement_bloc None prog in *)
|
||||||
|
(* Programme([], nb) *)
|
||||||
|
|
||||||
|
end
|
|
@ -6,6 +6,7 @@ struct
|
||||||
open Exceptions
|
open Exceptions
|
||||||
open Ast
|
open Ast
|
||||||
open AstType
|
open AstType
|
||||||
|
open Type
|
||||||
|
|
||||||
type t1 = Ast.AstTds.programme
|
type t1 = Ast.AstTds.programme
|
||||||
type t2 = Ast.AstType.programme
|
type t2 = Ast.AstType.programme
|
||||||
|
@ -26,29 +27,31 @@ let rec analyse_type_expression e =
|
||||||
if (est_compatible_list l_type_fun l_type) then
|
if (est_compatible_list l_type_fun l_type) then
|
||||||
(AstType.AppelFonction(info, n_l_expr), t)
|
(AstType.AppelFonction(info, n_l_expr), t)
|
||||||
else
|
else
|
||||||
raise TypesParametresInattendus(l_type_fun, l_type)
|
raise (TypesParametresInattendus(l_type_fun, l_type))
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.Unaire(u, expr) ->
|
| AstTds.Unaire(u, expr) ->
|
||||||
begin
|
begin
|
||||||
match (analyse_type_expression expr) with
|
match (analyse_type_expression expr) with
|
||||||
| n_expr, Rat ->
|
| n_expr, Rat ->
|
||||||
|
begin
|
||||||
match u with
|
match u with
|
||||||
| AstSyntax.Numerateur -> (AstType.Unaire(Numerateur, n_expr), Int)
|
| AstSyntax.Numerateur -> (AstType.Unaire(Numerateur, n_expr), Int)
|
||||||
| AstSyntax.Denominateur -> (AstType.Unaire(Denominateur, n_expr), Int)
|
| AstSyntax.Denominateur -> (AstType.Unaire(Denominateur, n_expr), Int)
|
||||||
| _, t -> raise (TypeInattendu(Rat, t))
|
end
|
||||||
|
| _, t -> raise (TypeInattendu(t, Rat))
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.Binaire(b, expr_1, expr_2) ->
|
| AstTds.Binaire(b, expr_1, expr_2) ->
|
||||||
begin
|
begin
|
||||||
let (n_expr_1, t1) = analyse_type_expresseion expr_1 in
|
let (n_expr_1, t1) = analyse_type_expression expr_1 in
|
||||||
let (n_expr_2, t2) = analyse_type_expresseion expr_2 in
|
let (n_expr_2, t2) = analyse_type_expression expr_2 in
|
||||||
match (b, t1, t2) with
|
match (b, t1, t2) with
|
||||||
| Fraction, Int, Int -> (AstType.Binaire(Fraction, n_expr_1, n_expr_2), Rat)
|
| Fraction, Int, Int -> (AstType.Binaire(Fraction, n_expr_1, n_expr_2), Rat)
|
||||||
| Plus, Int, Int -> (AstType.Binaire(PlusInt, n_expr_1, n_expr_2), Int)
|
| Plus, Int, Int -> (AstType.Binaire(PlusInt, n_expr_1, n_expr_2), Int)
|
||||||
| Mult, Int, Int -> (AstType.Binaire(MultInt, n_expr_1, n_expr_2), Int)
|
| Mult, Int, Int -> (AstType.Binaire(MultInt, n_expr_1, n_expr_2), Int)
|
||||||
| Equ, Int ,Int -> (AstType.Binaire(EquInt, n_expr_1, n_expr_2), Int)
|
| Equ, Int ,Int -> (AstType.Binaire(EquInt, n_expr_1, n_expr_2), Bool)
|
||||||
| Inf, Int, Int -> (AstType.Binaire(Inf, n_expr_1, n_expr_2), Int)
|
| Inf, Int, Int -> (AstType.Binaire(Inf, n_expr_1, n_expr_2), Bool)
|
||||||
| Plus, Rat, Rat -> (AstType.Binaire(PlusRat, n_expr_1, n_expr_2), Rat)
|
| Plus, Rat, Rat -> (AstType.Binaire(PlusRat, n_expr_1, n_expr_2), Rat)
|
||||||
| Mult, Rat, Rat -> (AstType.Binaire(MultRat, n_expr_1, n_expr_2), Rat)
|
| Mult, Rat, Rat -> (AstType.Binaire(MultRat, n_expr_1, n_expr_2), Rat)
|
||||||
| Equ, Bool ,Bool -> (AstType.Binaire(EquBool, n_expr_1, n_expr_2), Bool)
|
| Equ, Bool ,Bool -> (AstType.Binaire(EquBool, n_expr_1, n_expr_2), Bool)
|
||||||
|
@ -75,63 +78,71 @@ en une instruction de type AstType.instruction *)
|
||||||
let rec analyse_type_instruction opt i =
|
let rec analyse_type_instruction opt i =
|
||||||
match i with
|
match i with
|
||||||
| AstTds.Declaration (t, info, e) ->
|
| AstTds.Declaration (t, info, e) ->
|
||||||
begin
|
let (ne, nt) = analyse_type_expression e in
|
||||||
let (ne, nt) = analyse_type_expr e in
|
|
||||||
if (est_compatible t nt) then
|
if (est_compatible t nt) then
|
||||||
modifier_type_info t info;
|
let _ = modifier_type_info t info in
|
||||||
AstType.Declaration(info, ne)
|
AstType.Declaration(info, ne)
|
||||||
else
|
else
|
||||||
raise TypeInattendu(t, nt)
|
raise (TypeInattendu(nt, t))
|
||||||
end
|
|
||||||
|
|
||||||
| AstTds.Affectation (info, e) ->
|
| AstTds.Affectation (info, e) ->
|
||||||
begin
|
begin
|
||||||
let (ne, nt) = analyse_type_expr e in
|
let (ne, nt) = analyse_type_expression e in
|
||||||
match (info_ast_to_info info) with
|
match (info_ast_to_info info) with
|
||||||
| InfoVar(_, t, _, _) ->
|
| InfoVar(_, t, _, _) ->
|
||||||
if est_compatible t nt then
|
if est_compatible t nt then
|
||||||
AstType.Affectation(info, ne)
|
AstType.Affectation(info, ne)
|
||||||
else
|
else
|
||||||
raise TypeInattendu(t, nt)
|
raise (TypeInattendu(nt, t))
|
||||||
| _ -> failwith "wut"
|
| _ -> failwith "wut"
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.Affichage e ->
|
| AstTds.Affichage e ->
|
||||||
begin
|
begin
|
||||||
let (ne, nt) = analyse_type_expr e in
|
let (ne, nt) = analyse_type_expression e in
|
||||||
match nt with
|
match nt with
|
||||||
| Int -> AstType.AffciaheInt(ne)
|
| Int -> AstType.AffichageInt(ne)
|
||||||
| Rat -> AstType.AffciaheRat(ne)
|
| Rat -> AstType.AffichageRat(ne)
|
||||||
| Bool -> AstType.AffichagBool(ne)
|
| Bool -> AstType.AffichageBool(ne)
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.Conditionnelle (e, b1, b2) ->
|
| AstTds.Conditionnelle (e, b1, b2) ->
|
||||||
begin
|
begin
|
||||||
let (ne, nt) = analyse_type_expr e in
|
let (ne, nt) = analyse_type_expression e in
|
||||||
let nb1 = analyse_type_bloc opt b1 in
|
let nb1 = analyse_type_bloc opt b1 in
|
||||||
let nb2 = analyse_type_bloc opt b2 in
|
let nb2 = analyse_type_bloc opt b2 in
|
||||||
|
if (est_compatible Bool nt) then
|
||||||
AstType.Conditionnelle(ne, nb1, nb2)
|
AstType.Conditionnelle(ne, nb1, nb2)
|
||||||
|
else
|
||||||
|
raise (TypeInattendu(nt, Bool))
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.TantQue (e, b) ->
|
| AstTds.TantQue (e, b) ->
|
||||||
begin
|
begin
|
||||||
let (ne, nt) = analyse_type_expr e in
|
let (ne, nt) = analyse_type_expression e in
|
||||||
let nb = analyse_type_bloc b in
|
if (est_compatible nt Bool) then
|
||||||
AstType.Conditionnelle(ne, nb)
|
let nb = analyse_type_bloc opt b in
|
||||||
|
AstType.TantQue(ne, nb)
|
||||||
|
else
|
||||||
|
raise (TypeInattendu(nt, Bool))
|
||||||
end
|
end
|
||||||
|
|
||||||
| AstTds.Retour (e) ->
|
| AstTds.Retour (e) ->
|
||||||
begin
|
begin
|
||||||
match opt with
|
match opt with
|
||||||
| Some(t) ->
|
| Some(t) ->
|
||||||
let (ne, nt) = analyse_type_expr e in
|
let (ne, nt) = analyse_type_expression e in
|
||||||
if est_compatible t nt then
|
if est_compatible t nt then
|
||||||
AstType.Retour(ne)
|
AstType.Retour(ne)
|
||||||
else
|
else
|
||||||
raise TypeInattendu(t, nt)
|
raise (TypeInattendu(t, nt))
|
||||||
| None -> failwith "Pas de return dans le main"
|
| None -> failwith "Pas de return dans le main"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
| AstTds.Empty -> AstType.Empty
|
||||||
|
|
||||||
|
|
||||||
(* analyse_type_bloc : AstTds.bloc -> AstType.bloc *)
|
(* analyse_type_bloc : AstTds.bloc -> AstType.bloc *)
|
||||||
(* Paramètre tds : la table des symboles courante *)
|
(* Paramètre tds : la table des symboles courante *)
|
||||||
(* Paramètre li : liste d'instructions à analyser *)
|
(* Paramètre li : liste d'instructions à analyser *)
|
||||||
|
@ -154,49 +165,18 @@ en une fonction de type AstType.fonction *)
|
||||||
AstTds.fonction[typ * string * (typ * string) list * bloc] ->
|
AstTds.fonction[typ * string * (typ * string) list * bloc] ->
|
||||||
AstType.fonction[typ * Tds.info_ast * (typ * Tds.info_ast ) list * bloc]
|
AstType.fonction[typ * Tds.info_ast * (typ * Tds.info_ast ) list * bloc]
|
||||||
*)
|
*)
|
||||||
let analyse_type_fonction opt (AstTds.Fonction(t, str, l_typstr, bloc)) = failwith "TODO"
|
let analyse_type_fonction opt (AstTds.Fonction(t, info, l_typinfo, bloc)) =
|
||||||
(* begin
|
match opt with
|
||||||
match chercherLocalement maintds str with
|
| Some t_ret ->
|
||||||
| Some _ -> raise (DoubleDeclaration str)
|
if est_compatible t_ret t then
|
||||||
| None ->
|
let _ = modifier_type_info t info in
|
||||||
begin
|
let n_l_info = List.map (fun (ti, i) -> modifier_type_info ti i; i) l_typinfo in
|
||||||
(* Info de l'identifiant de la fonction *)
|
let nb = analyse_type_bloc opt bloc in
|
||||||
let info = Tds.InfoVar(str, Undefined, 0, "") in
|
AstType.Fonction(info, n_l_info, nb)
|
||||||
|
else
|
||||||
|
raise (TypeInattendu(t, t_ret))
|
||||||
|
| None -> failwith "Il faut un return dans une fonction"
|
||||||
|
|
||||||
(* Copie de la tds globale dans la tds locale au bloc *)
|
|
||||||
let tds_bloc = creerTDSFille maintds in
|
|
||||||
|
|
||||||
(* Ajouter les arguments de la fonction dans la tds locale *)
|
|
||||||
let _ = (List.map (
|
|
||||||
fun (_, nom) ->
|
|
||||||
match chercherLocalement tds_bloc nom with
|
|
||||||
| None -> ajouter tds_bloc nom (info_to_info_ast (Tds.InfoVar(nom, Undefined, 0, "")))
|
|
||||||
(* Si un argument est en double, on lève une exception *)
|
|
||||||
| Some _ -> raise (DoubleDeclaration nom)
|
|
||||||
) l_typstr) in
|
|
||||||
|
|
||||||
(* On ajoute a la tds locale la fonction pour qu'il puisse y avoir des appels récursifs *)
|
|
||||||
let _ = ajouter tds_bloc str (info_to_info_ast (Tds.InfoFun(str, t, (List.map (fun (t, _) -> t) l_typstr)))) in
|
|
||||||
|
|
||||||
(* On génère le nouveau bloc avec la tds locale *)
|
|
||||||
let new_bloc = analyse_type_bloc tds_bloc bloc in
|
|
||||||
|
|
||||||
(* On transforme les (type * str) en (typ * info) *)
|
|
||||||
let nl_typinfo = List.map (
|
|
||||||
fun (t, str2) ->
|
|
||||||
( t, info_to_info_ast (Tds.InfoVar(str2, Undefined, 0, "")) )
|
|
||||||
) l_typstr in
|
|
||||||
|
|
||||||
(* On crée l'info de la fonction *)
|
|
||||||
let info_fun = InfoFun(str, t, (List.map (fun (t, _) -> t) l_typstr)) in
|
|
||||||
|
|
||||||
(* On ajoute la fonction a la tds globale *)
|
|
||||||
ajouter maintds str (info_to_info_ast info_fun);
|
|
||||||
|
|
||||||
(* On retourne la AstType fonction *)
|
|
||||||
AstType.Fonction(t, (info_to_info_ast info), nl_typinfo, new_bloc)
|
|
||||||
end
|
|
||||||
end *)
|
|
||||||
|
|
||||||
(* analyser : AstTds.ast -> AstType.ast *)
|
(* analyser : AstTds.ast -> AstType.ast *)
|
||||||
(* Paramètre : le programme à analyser *)
|
(* Paramètre : le programme à analyser *)
|
||||||
|
@ -204,7 +184,8 @@ let analyse_type_fonction opt (AstTds.Fonction(t, str, l_typstr, bloc)) = failw
|
||||||
en un programme de type AstType.ast *)
|
en un programme de type AstType.ast *)
|
||||||
(* Erreur si mauvaise utilisation des identifiants *)
|
(* Erreur si mauvaise utilisation des identifiants *)
|
||||||
let analyser (AstTds.Programme(fonctions, prog)) =
|
let analyser (AstTds.Programme(fonctions, prog)) =
|
||||||
(* let nf = List.map (analyse_type_fonction Some(...)) fonctions in *)
|
(* let nf = List.map (fun (AstTds.Fonction(t, info, l_typinfo, bloc)) ->
|
||||||
|
analyse_type_fonction (Some t) (AstTds.Fonction(t, info, l_typinfo, bloc))) fonctions in *)
|
||||||
let nb = analyse_type_bloc None prog in
|
let nb = analyse_type_bloc None prog in
|
||||||
Programme([], nb)
|
Programme([], nb)
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,12 @@ exception ErreurNonDetectee
|
||||||
(* Sans fonction *)
|
(* Sans fonction *)
|
||||||
(* ------------------------------ *)
|
(* ------------------------------ *)
|
||||||
|
|
||||||
|
|
||||||
let%test_unit "testDeclaration1"=
|
let%test_unit "testDeclaration1"=
|
||||||
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration1.rat" in ()
|
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration1.rat" in ()
|
||||||
(*
|
|
||||||
|
|
||||||
let%test_unit "testDeclaration2"=
|
let%test_unit "testDeclaration2"=
|
||||||
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration2.rat" in ()
|
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration2.rat" in ()
|
||||||
|
|
||||||
let%test_unit "testDeclaration3"=
|
let%test_unit "testDeclaration3"=
|
||||||
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration3.rat" in ()
|
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration3.rat" in ()
|
||||||
|
@ -419,13 +419,12 @@ let _ = compiler "../../fichiersRat/src-rat-tam-test/factiter.rat" in ()
|
||||||
let%test_unit "code_complique" =
|
let%test_unit "code_complique" =
|
||||||
let _ = compiler "../../fichiersRat/src-rat-tam-test/complique.rat" in ()
|
let _ = compiler "../../fichiersRat/src-rat-tam-test/complique.rat" in ()
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
(* ------------------------------ *)
|
(* ------------------------------ *)
|
||||||
(* Avec fonction *)
|
(* Avec fonction *)
|
||||||
(* ------------------------------ *)
|
(* ------------------------------ *)
|
||||||
|
|
||||||
(*
|
(*
|
||||||
|
|
||||||
let%test_unit "test2"=
|
let%test_unit "test2"=
|
||||||
let _ = compiler "../../fichiersRat/src-rat-type-test/test2.rat" in ()
|
let _ = compiler "../../fichiersRat/src-rat-type-test/test2.rat" in ()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue