From 5e17ce496afa509bdf673ab8ccfaff96a47c57cb Mon Sep 17 00:00:00 2001 From: Guillotin Damien Date: Sat, 11 Dec 2021 19:27:53 +0100 Subject: [PATCH] feat: vive les pointeuUuUur --- .gitignore | 3 +- src/ast.ml | 4 +- src/compilateur.ml | 6 +- .../src-rat-tam-test/pointeur1.rat | 7 +++ .../src-rat-type-test/testIdentPointeur1.rat | 3 + .../src-rat-type-test/testIdentPointeur2.rat | 3 + src/passeCodeRatToTam.ml | 59 +++++++++++++++---- src/passePlacementRat.ml | 8 +++ src/passeTdsRat.ml | 7 +-- src/passeTypeRat.ml | 33 +++-------- ...{testPlacement.ml.bak => testPlacement.ml} | 0 src/{testTDS.ml.bak => testTDS.ml} | 0 src/{testTam.ml.bak => testTam.ml} | 4 ++ src/{testType.ml.bak => testType.ml} | 14 +++++ src/type.ml | 2 +- 15 files changed, 106 insertions(+), 47 deletions(-) create mode 100644 src/fichiersRat/src-rat-tam-test/pointeur1.rat create mode 100644 src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat create mode 100644 src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat rename src/{testPlacement.ml.bak => testPlacement.ml} (100%) rename src/{testTDS.ml.bak => testTDS.ml} (100%) rename src/{testTam.ml.bak => testTam.ml} (96%) rename src/{testType.ml.bak => testType.ml} (97%) diff --git a/.gitignore b/.gitignore index e3b3875..5009f83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ _build/ -srcTAM/ \ No newline at end of file +srcTAM/ +out.tam \ No newline at end of file diff --git a/src/ast.ml b/src/ast.ml index de140dc..eb1b7a0 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -144,7 +144,7 @@ type binaire = Fraction | PlusInt | PlusRat | MultInt | MultRat | EquInt | EquBo type affectable = (* Dé-référencement d'un affectable *) - | Dref of affectable + | Dref of affectable * typ (* Identifiant *) | Ident of Tds.info_ast @@ -200,6 +200,8 @@ end module AstPlacement = struct +type affectable = AstType.affectable + (* Expressions existantes dans notre langage *) (* = expression de AstType *) type expression = AstType.expression diff --git a/src/compilateur.ml b/src/compilateur.ml index e6189f0..9ad1828 100644 --- a/src/compilateur.ml +++ b/src/compilateur.ml @@ -33,7 +33,7 @@ end (* Compilateur créant l'AST *) -module CompilateurRat = Compilateur (PasseTdsNop) (PasseNop) (PasseNop) (PasseCodeNopNop) +(* module CompilateurRat = Compilateur (PasseTdsNop) (PasseNop) (PasseNop) (PasseCodeNopNop) *) (* + passe de résolution des identifiants *) @@ -54,11 +54,11 @@ module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacement (* + passe de génération de code -> compilateur complet *) -(* open PasseTdsRat +open PasseTdsRat open PasseTypeRat open PassePlacementRat open PasseCodeRatToTam -module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam) *) +module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam) diff --git a/src/fichiersRat/src-rat-tam-test/pointeur1.rat b/src/fichiersRat/src-rat-tam-test/pointeur1.rat new file mode 100644 index 0000000..9b24a86 --- /dev/null +++ b/src/fichiersRat/src-rat-tam-test/pointeur1.rat @@ -0,0 +1,7 @@ +main{ + int * px = (new int); + int x = 3; + px = &x; + int y = (*px); + print y; +} diff --git a/src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat b/src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat new file mode 100644 index 0000000..20f7bbf --- /dev/null +++ b/src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat @@ -0,0 +1,3 @@ +test{ + int x = (new int); +} \ No newline at end of file diff --git a/src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat b/src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat new file mode 100644 index 0000000..d647b94 --- /dev/null +++ b/src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat @@ -0,0 +1,3 @@ +test{ + int * x = 2; +} \ No newline at end of file diff --git a/src/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml index 74e7838..74724d4 100644 --- a/src/passeCodeRatToTam.ml +++ b/src/passeCodeRatToTam.ml @@ -12,18 +12,26 @@ struct type t1 = Ast.AstPlacement.programme type t2 = string +let rec analyse_code_affectable a = + match a with + | Dref(aff, t) -> + analyse_code_affectable aff ^ + "LOADI (" ^ (string_of_int (getTaille t)) ^ ")\n" + + | Ident(i) -> + match info_ast_to_info i with + | InfoVar(_, t, base, reg) -> "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" + | InfoConst(_,v) -> "LOADL " ^ (string_of_int v) ^ "\n" + | _ -> failwith "w-wtf bwo y-you okay -.-" + let rec analyse_code_expression e = match e with | AppelFonction(i, le) -> - begin - let InfoFun(nom,_,_) = info_ast_to_info i in - (List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^ - "CALL (ST) " ^ nom ^ "\n" - end - - (* | Ident(i) -> - let InfoVar(_,t,base,reg) = info_ast_to_info i in - "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" *) + begin + let InfoFun(nom,_,_) = info_ast_to_info i in + (List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^ + "CALL (ST) " ^ nom ^ "\n" + end | Booleen(b) -> if b then @@ -56,7 +64,18 @@ let rec analyse_code_expression e = | MultRat -> "CALL (ST) RMul\n" | EquBool -> "SUBR IEq\n" end + + | Affectable(a) -> analyse_code_affectable a + + | Null -> "SUBR MVoid\n" + + | NewType(t) -> + "LOADL " ^ (string_of_int (getTaille t)) ^ "\n" ^ + "SUBR MAlloc\n" + | Adresse(i) -> + let InfoVar(_, _, base, reg) = info_ast_to_info i in + "LOADL " ^ (string_of_int base) ^ "\n" and analyse_code_instruction i taille_return taille_args taille_var = match i with @@ -66,10 +85,24 @@ and analyse_code_instruction i taille_return taille_args taille_var = analyse_code_expression e ^ "STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" - | Affectation(i, e) -> - let InfoVar(_, t, base, reg) = (info_ast_to_info i) in - analyse_code_expression e ^ - "STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" + | Affectation(a, e) -> + begin + match a with + | Ident(i) -> + begin + match (info_ast_to_info i) with + | InfoVar (_, t, base, reg) -> + (analyse_code_expression e) ^ + "STORE (" ^ string_of_int (getTaille t) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" + | _ -> failwith ("Erreur interne : symbole non reconnnu") + end + + | Dref(a, t) -> + (analyse_code_expression e) ^ + (analyse_code_affectable a) ^ + "STOREI (" ^ string_of_int (getTaille t) ^ ")\n" + end + | AffichageInt(e) -> (analyse_code_expression e) ^ diff --git a/src/passePlacementRat.ml b/src/passePlacementRat.ml index adc1219..1171cc3 100644 --- a/src/passePlacementRat.ml +++ b/src/passePlacementRat.ml @@ -11,6 +11,14 @@ struct type t1 = Ast.AstType.programme type t2 = Ast.AstPlacement.programme + +(* let rec analyse_placement_affectable a = + begin + match a with + | AstType.Dref(aff) -> + | AstType.Ident(i) -> + end *) + let rec analyse_placement_instruction i base reg = match i with | AstType.Declaration(info, _) -> diff --git a/src/passeTdsRat.ml b/src/passeTdsRat.ml index 0c56cde..c974ab0 100644 --- a/src/passeTdsRat.ml +++ b/src/passeTdsRat.ml @@ -26,17 +26,16 @@ let rec analyse_tds_affectable tds a modif = begin match (info_ast_to_info info) with | InfoVar _ -> AstTds.Ident(info) - | InfoConst _ -> + | InfoConst(_) -> begin if modif then failwith "bah non en fait" else AstTds.Ident(info) end - | InfoFun _ -> failwith "t chelou bro" + | InfoFun _ -> raise (MauvaiseUtilisationIdentifiant(n)) end end - (* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *) (* Paramètre tds : la table des symboles courante *) @@ -130,7 +129,7 @@ let rec analyse_tds_instruction tds i = | AstSyntax.Affectation (aff, e) -> begin let ne = analyse_tds_expression tds e in - let n_a = analyse_tds_affectable tds a true in + let n_a = analyse_tds_affectable tds aff false in match aff with | Dref a -> Affectation (n_a, ne) diff --git a/src/passeTypeRat.ml b/src/passeTypeRat.ml index 5d79a53..a2e3a24 100644 --- a/src/passeTypeRat.ml +++ b/src/passeTypeRat.ml @@ -17,7 +17,7 @@ let rec analyse_type_affectable a = | AstTds.Dref(a) -> begin match (analyse_type_affectable a) with - | (na, Pointeur t) -> (AstType.Dref(na), t) + | (na, Pointeur t) -> (AstType.Dref(na, t), t) | _ -> failwith "jsp koi mettre ici" end | AstTds.Ident(info) -> @@ -27,24 +27,6 @@ let rec analyse_type_affectable a = | InfoConst _ -> (AstType.Ident(info), Int) | InfoFun _ -> failwith "c chelou frr" end - -(* -let rec analyse_type_affectable (aff:AstTds.affectable) : (affectable*typ) = - match aff with - | AstTds.Ident info -> - begin - match info_ast_to_info info with - | InfoVar (_,t,_,_) -> (Ident info, t) - | InfoConst _ -> (Ident info, Int) - | _ -> failwith ("Erreur interne : symbole non reconnnu") - end - | AstTds.Valeur aff -> - begin - match analyse_type_affectable aff with - | (naff, Pointeur t) -> (Valeur(naff, t),t) - | _ -> raise PasUnPointeur - end -*) (* analyse_tds_expression : AstTds.expression -> (AstType.expression, type) *) @@ -105,11 +87,13 @@ let rec analyse_type_expression e = | AstTds.Null -> (AstType.Null, Pointeur(Undefined)) - | AstTds.NewType(t) -> (AstType.NewType(t), Pointeur(t)) + | AstTds.NewType(t) -> + (AstType.NewType(t), Pointeur(t)) | AstTds.Adresse(info) -> - let _ = modifier_type_info Int info in - (AstType.Adresse(info), Int) + let InfoVar(_, t, _, _) = info_ast_to_info info in + let _ = modifier_type_info (Pointeur(t)) info in + (AstType.Adresse(info), Pointeur(t)) (* analyse_tds_instruction : AstTds.instruction -> tds -> AstType.instruction *) (* Paramètre tds : la table des symboles courante *) @@ -121,13 +105,14 @@ let rec analyse_type_instruction opt i = match i with | AstTds.Declaration (t, info, e) -> let (ne, nt) = analyse_type_expression e in - if (est_compatible t nt) then + if (est_compatible nt t) then let _ = modifier_type_info t info in AstType.Declaration(info, ne) else + let _ = print_endline (string_of_type nt) in + let _ = print_endline (string_of_type t) in raise (TypeInattendu(nt, t)) - | AstTds.Affectation (aff, e) -> let (ne, nt) = analyse_type_expression e in begin diff --git a/src/testPlacement.ml.bak b/src/testPlacement.ml similarity index 100% rename from src/testPlacement.ml.bak rename to src/testPlacement.ml diff --git a/src/testTDS.ml.bak b/src/testTDS.ml similarity index 100% rename from src/testTDS.ml.bak rename to src/testTDS.ml diff --git a/src/testTam.ml.bak b/src/testTam.ml similarity index 96% rename from src/testTam.ml.bak rename to src/testTam.ml index a80fe51..d183461 100644 --- a/src/testTam.ml.bak +++ b/src/testTam.ml @@ -108,3 +108,7 @@ let%expect_test "factfuns" = let%expect_test "factrec" = runtam "../../fichiersRat/src-rat-tam-test/factrec.rat"; [%expect{| 120 |}] + +let%expect_test "pointeur1" = + runtam "../../fichiersRat/src-rat-tam-test/pointeur1.rat"; + [%expect{| 3 |}] \ No newline at end of file diff --git a/src/testType.ml.bak b/src/testType.ml similarity index 97% rename from src/testType.ml.bak rename to src/testType.ml index d47bbe0..7d3e6f7 100644 --- a/src/testType.ml.bak +++ b/src/testType.ml @@ -419,6 +419,20 @@ 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 () +let%test_unit ""= + try + let _ = compiler "../../fichiersRat/src-rat-type-test/testIdentPointeur1.rat" + in raise ErreurNonDetectee + with + | TypeInattendu(Pointeur(Int),Int) -> () + +let%test_unit ""= + try + let _ = compiler "../../fichiersRat/src-rat-type-test/testIdentPointeur2.rat" + in raise ErreurNonDetectee + with + | TypeInattendu(Int,Pointeur(Int)) -> () + (* ------------------------------ *) (* Avec fonction *) (* ------------------------------ *) diff --git a/src/type.ml b/src/type.ml index ac01184..95190a2 100644 --- a/src/type.ml +++ b/src/type.ml @@ -5,7 +5,7 @@ let rec string_of_type t = | Bool -> "Bool" | Int -> "Int" | Rat -> "Rat" - | Pointeur(t_p) -> "Poi " ^ string_of_type t_p + | Pointeur(t_p) -> "*" ^ string_of_type t_p | Undefined -> "Undefined"