feat: fin de séance

Co-authored-by: gdamms <gdamms@users.noreply.github.com>
This commit is contained in:
Laurent Fainsin 2021-12-08 12:02:30 +01:00
parent ca2978c95a
commit 96955b19ba
7 changed files with 119 additions and 24 deletions

View file

@ -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

95
src/passeCodeRatToTam.ml Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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")

View file

@ -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 |}]
*)