feat: fin TP type + début placement mémoire

This commit is contained in:
Laurent Fainsin 2021-12-01 15:55:42 +01:00
parent 415a0249a1
commit bbb7239e52
3 changed files with 126 additions and 95 deletions

51
src/passePlacementRat.ml Normal file
View 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

View file

@ -6,6 +6,7 @@ struct
open Exceptions
open Ast
open AstType
open Type
type t1 = Ast.AstTds.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
(AstType.AppelFonction(info, n_l_expr), t)
else
raise TypesParametresInattendus(l_type_fun, l_type)
raise (TypesParametresInattendus(l_type_fun, l_type))
end
| AstTds.Unaire(u, expr) ->
begin
match (analyse_type_expression expr) with
| n_expr, Rat ->
match u with
| AstSyntax.Numerateur -> (AstType.Unaire(Numerateur, n_expr), Int)
| AstSyntax.Denominateur -> (AstType.Unaire(Denominateur, n_expr), Int)
| _, t -> raise (TypeInattendu(Rat, t))
begin
match u with
| AstSyntax.Numerateur -> (AstType.Unaire(Numerateur, n_expr), Int)
| AstSyntax.Denominateur -> (AstType.Unaire(Denominateur, n_expr), Int)
end
| _, t -> raise (TypeInattendu(t, Rat))
end
| AstTds.Binaire(b, expr_1, expr_2) ->
begin
let (n_expr_1, t1) = analyse_type_expresseion expr_1 in
let (n_expr_2, t2) = analyse_type_expresseion expr_2 in
let (n_expr_1, t1) = analyse_type_expression expr_1 in
let (n_expr_2, t2) = analyse_type_expression expr_2 in
match (b, t1, t2) with
| 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)
| 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)
| Inf, Int, Int -> (AstType.Binaire(Inf, 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), Bool)
| 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)
| Equ, Bool ,Bool -> (AstType.Binaire(EquBool, n_expr_1, n_expr_2), Bool)
@ -75,62 +78,70 @@ en une instruction de type AstType.instruction *)
let rec analyse_type_instruction opt i =
match i with
| AstTds.Declaration (t, info, e) ->
begin
let (ne, nt) = analyse_type_expr e in
if (est_compatible t nt) then
modifier_type_info t info;
AstType.Declaration(info, ne)
else
raise TypeInattendu(t, nt)
end
| AstTds.Affectation (info, e) ->
begin
let (ne, nt) = analyse_type_expr e in
match (info_ast_to_info info) with
| InfoVar(_, t, _, _) ->
if est_compatible t nt then
AstType.Affectation(info, ne)
else
raise TypeInattendu(t, nt)
| _ -> failwith "wut"
end
let (ne, nt) = analyse_type_expression e in
if (est_compatible t nt) then
let _ = modifier_type_info t info in
AstType.Declaration(info, ne)
else
raise (TypeInattendu(nt, t))
| AstTds.Affectation (info, e) ->
begin
let (ne, nt) = analyse_type_expression e in
match (info_ast_to_info info) with
| InfoVar(_, t, _, _) ->
if est_compatible t nt then
AstType.Affectation(info, ne)
else
raise (TypeInattendu(nt, t))
| _ -> failwith "wut"
end
| AstTds.Affichage e ->
begin
let (ne, nt) = analyse_type_expr e in
let (ne, nt) = analyse_type_expression e in
match nt with
| Int -> AstType.AffciaheInt(ne)
| Rat -> AstType.AffciaheRat(ne)
| Bool -> AstType.AffichagBool(ne)
| Int -> AstType.AffichageInt(ne)
| Rat -> AstType.AffichageRat(ne)
| Bool -> AstType.AffichageBool(ne)
end
| AstTds.Conditionnelle (e, b1, b2) ->
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 nb2 = analyse_type_bloc opt b2 in
AstType.Conditionnelle(ne, nb1, nb2)
if (est_compatible Bool nt) then
AstType.Conditionnelle(ne, nb1, nb2)
else
raise (TypeInattendu(nt, Bool))
end
| AstTds.TantQue (e, b) ->
| AstTds.TantQue (e, b) ->
begin
let (ne, nt) = analyse_type_expr e in
let nb = analyse_type_bloc b in
AstType.Conditionnelle(ne, nb)
let (ne, nt) = analyse_type_expression e in
if (est_compatible nt Bool) then
let nb = analyse_type_bloc opt b in
AstType.TantQue(ne, nb)
else
raise (TypeInattendu(nt, Bool))
end
| AstTds.Retour (e) ->
| AstTds.Retour (e) ->
begin
match opt with
| Some(t) ->
let (ne, nt) = analyse_type_expr e in
if est_compatible t nt then
let (ne, nt) = analyse_type_expression e in
if est_compatible t nt then
AstType.Retour(ne)
else
raise TypeInattendu(t, nt)
else
raise (TypeInattendu(t, nt))
| None -> failwith "Pas de return dans le main"
end
| AstTds.Empty -> AstType.Empty
(* analyse_type_bloc : AstTds.bloc -> AstType.bloc *)
(* Paramètre tds : la table des symboles courante *)
@ -154,49 +165,18 @@ en une fonction de type AstType.fonction *)
AstTds.fonction[typ * string * (typ * string) 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"
(* begin
match chercherLocalement maintds str with
| Some _ -> raise (DoubleDeclaration str)
| None ->
begin
(* Info de l'identifiant de la fonction *)
let info = Tds.InfoVar(str, Undefined, 0, "") in
let analyse_type_fonction opt (AstTds.Fonction(t, info, l_typinfo, bloc)) =
match opt with
| Some t_ret ->
if est_compatible t_ret t then
let _ = modifier_type_info t info in
let n_l_info = List.map (fun (ti, i) -> modifier_type_info ti i; i) l_typinfo in
let nb = analyse_type_bloc opt bloc 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 *)
(* 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 *)
(* Erreur si mauvaise utilisation des identifiants *)
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
Programme([], nb)

View file

@ -7,12 +7,12 @@ exception ErreurNonDetectee
(* Sans fonction *)
(* ------------------------------ *)
let%test_unit "testDeclaration1"=
let _ = compiler "../../fichiersRat/src-rat-type-test/testDeclaration1.rat" in ()
(*
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 _ = 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 _ = compiler "../../fichiersRat/src-rat-tam-test/complique.rat" in ()
*)
(* ------------------------------ *)
(* Avec fonction *)
(* ------------------------------ *)
(*
let%test_unit "test2"=
let _ = compiler "../../fichiersRat/src-rat-type-test/test2.rat" in ()
@ -559,4 +558,4 @@ let%test_unit "code_testfun6" =
let%test_unit "code_testfuns" =
let _ = compiler "../../fichiersRat/src-rat-tam-test/testfuns.rat" in ()
*)
*)