From 96955b19badb4a7854a29dda115b3075510ed28f Mon Sep 17 00:00:00 2001 From: Laurent Fainsin Date: Wed, 8 Dec 2021 12:02:30 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20fin=20de=20s=C3=A9ance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: gdamms --- src/compilateur.ml | 7 ++- src/passeCodeRatToTam.ml | 95 ++++++++++++++++++++++++++++++++++++++++ src/passePlacementRat.ml | 11 ++--- src/passeTdsRat.ml | 15 ++++--- src/passeTypeRat.ml | 11 +++-- src/testPlacement.ml | 1 - src/testTam.ml | 3 -- 7 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 src/passeCodeRatToTam.ml diff --git a/src/compilateur.ml b/src/compilateur.ml index ebd2ce9..9ad1828 100644 --- a/src/compilateur.ml +++ b/src/compilateur.ml @@ -47,20 +47,19 @@ module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacement (* + passe de placement mémoire *) -open PasseTdsRat +(* open PasseTdsRat open PasseTypeRat open PassePlacementRat -module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeNop) +module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeNop) *) (* + passe de génération de code -> compilateur complet *) -(* open PasseTdsRat open PasseTypeRat open PassePlacementRat open PasseCodeRatToTam module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam) -*) + open Lexing diff --git a/src/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml new file mode 100644 index 0000000..b03c571 --- /dev/null +++ b/src/passeCodeRatToTam.ml @@ -0,0 +1,95 @@ +(* Module de la passe de typage *) +module PasseCodeRatToTam : Passe.Passe with type t1 = Ast.AstPlacement.programme and type t2 = string = +struct + + open Tds + open Ast + open AstPlacement + open Type + + type t1 = Ast.AstPlacement.programme + type t2 = string + +let rec analyse_code_expression e = + match e with + | AstPlacement.AppelFonction(InfoFun(nom,_,_), le) -> + begin + (List.fold_right (fun e res_q -> res ^ analyse_code_expression e) le "") ^ + "CALL (ST)" ^ + nom ^ + "\n" + end + + | AstPlacement.Ident(InfoIdent(_,t,base,reg)) -> + "LOAD (" ^ + (getTaille t) ^ + ") " ^ + (string_of_int base) ^ + "[" ^ + reg ^ + "]\n" + + | AstPlacement.Booleen(b) -> + if b then + "LOADL 1\n" + else + "LOADL 0\n" + + | AstPlacement.Entier(n) -> + "LOADL" ^ + (string_of_int n) ^ + "\n" + + | AstPlacement.Unaire(u, e) -> + (analyse_code_expression e) ^ + begin + match u with + | Numerateur -> "LOAD (1) -2[ST]\n" + | Denominateur -> "LOAD (1) -1[ST]\n" + end + + | AstPlacement.Binaire(b, e1, e2) -> + (analyse_code_expression e1) ^ + (analyse_code_expression e2) ^ + begin + match b with + | Fraction -> "" + | PlusInt -> "SUBR IAdd\n" + | MultInt -> "SUBR IMul\n" + | EquInt -> "SUBR IEq\n" + | Inf -> "SUBR ILss\n" + | PlusRat -> "CALL (ST) RAdd\n" + | MultRat -> "CALL (ST) RMul\n" + | EquBool -> "SUBR IEq\n" + end + + +and analyse_code_instruction i typefun = + match i with + | AstPlacement.Declaration -> + + | AstPlacement.Affectation -> + + | AstPlacement.AffichageInt -> + + | AstPlacement.AffichageRat -> + + | AstPlacement.AffichageBool -> + + | AstPlacement.Conditionnelle -> + + | AstPlacement.TantQue -> + + | AstPlacement.Retour -> + +and analyse_code_bloc base reg bloc = failwith "TODO" + +and analyse_code_fonction (AstPlacement.Fonction(info, l_typinfo, bloc)) = failwith "TODO" + +let analyser (AstPlacement.Programme(fonctions, prog)) = + let code = getEntete in + let code = code ^ List.fold_right (fun e res_q -> (analyse_code_fonction e) ^ res_q) fonctions "" in + let code = code ^ "main\n" in + let code = code ^ analyse_code_bloc prog in + code +end diff --git a/src/passePlacementRat.ml b/src/passePlacementRat.ml index eff5c18..adc1219 100644 --- a/src/passePlacementRat.ml +++ b/src/passePlacementRat.ml @@ -36,7 +36,8 @@ let rec analyse_placement_instruction i base reg = and analyse_placement_bloc base reg bloc = match bloc with - | [] -> base + | [] -> + base | t::q -> let taille = analyse_placement_instruction t base reg in analyse_placement_bloc (base + taille) reg q @@ -51,11 +52,11 @@ and analyse_placement_fonction (AstType.Fonction(info, l_typinfo, bloc)) = | _ -> failwith "pas normal" end ) l_typinfo 0 in - AstPlacement.Fonction(info, l_typinfo, bloc) + let _ = analyse_placement_bloc 3 "LB" bloc in + Fonction(info, l_typinfo, bloc) -let analyser (AstType.Programme(fonctions, prog)) = (* failwith "TODO" *) - let n_fonctions = List.map (fun e -> analyse_placement_fonction e ) fonctions in +let analyser (AstType.Programme(fonctions, prog)) = + let n_fonctions = List.map analyse_placement_fonction fonctions in let _ = analyse_placement_bloc 0 "SB" prog in Programme(n_fonctions, prog) - end diff --git a/src/passeTdsRat.ml b/src/passeTdsRat.ml index 015f274..b237493 100644 --- a/src/passeTdsRat.ml +++ b/src/passeTdsRat.ml @@ -212,26 +212,27 @@ let analyse_tds_fonction maintds (AstSyntax.Fonction(t, str, l_typstr, bloc)) = fun (t, nom) -> match chercherLocalement tds_bloc nom with | None -> - ajouter tds_bloc nom (info_to_info_ast (Tds.InfoVar(nom, t, 0, ""))); - (t, info_to_info_ast (Tds.InfoVar(nom, t, 0, ""))) + let i_ast_var = info_to_info_ast (Tds.InfoVar(nom, t, 0, "")) in + ajouter tds_bloc nom i_ast_var; + (t, i_ast_var) (* Si un argument est en double, on lève une exception *) | Some _ -> raise (DoubleDeclaration nom) ) 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 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 + let _ = ajouter tds_bloc str (info_to_info_ast info_fun) in (* On génère le nouveau bloc avec la tds locale *) let new_bloc = analyse_tds_bloc tds_bloc bloc 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 AstTds fonction *) - AstTds.Fonction(t, (info_to_info_ast info), nl_typinfo, new_bloc) + AstTds.Fonction(t, (info_to_info_ast info_fun), nl_typinfo, new_bloc) end end diff --git a/src/passeTypeRat.ml b/src/passeTypeRat.ml index 27b8570..3addae1 100644 --- a/src/passeTypeRat.ml +++ b/src/passeTypeRat.ml @@ -173,10 +173,13 @@ 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) + begin + let n_l_info = List.map (fun (ti, i) -> modifier_type_info ti i; (ti, i)) l_typinfo in + let tp, ip = List.split n_l_info in + let _ = modifier_type_fonction_info t tp info in + let nb = analyse_type_bloc opt bloc in + AstType.Fonction(info, ip, nb) + end else raise (TypeInattendu(t, t_ret)) | None -> failwith "Il faut un return dans une fonction" diff --git a/src/testPlacement.ml b/src/testPlacement.ml index 571ee12..e0dab53 100644 --- a/src/testPlacement.ml +++ b/src/testPlacement.ml @@ -218,4 +218,3 @@ let%test "test12_f_r" = let%test "test12_f_i" = test "../../fichiersRat/src-rat-placement-test/test12.rat" "f" ("i",1) (-1, "LB") - diff --git a/src/testTam.ml b/src/testTam.ml index 6987720..a80fe51 100644 --- a/src/testTam.ml +++ b/src/testTam.ml @@ -1,4 +1,3 @@ -(* open Compilateur (* Changer le chemin d'accès du jar. *) @@ -109,5 +108,3 @@ let%expect_test "factfuns" = let%expect_test "factrec" = runtam "../../fichiersRat/src-rat-tam-test/factrec.rat"; [%expect{| 120 |}] - -*)