From 13ed03e0157e514c5549b2b061f5cde576355e83 Mon Sep 17 00:00:00 2001 From: Guillotin Damien Date: Thu, 30 Dec 2021 20:52:46 +0100 Subject: [PATCH] feat: tout est bon TODO: des ptits tests en plus c'est tjrs mieux --- src/ast.ml | 1 + src/fichiersRat/src-rat-tam-test/Segment.rat | 16 ++ src/fichiersRat/src-rat-tam-test/struct1.rat | 5 +- src/fichiersRat/src-rat-tam-test/struct2.rat | 7 +- src/passeCodeRatToTam.ml | 159 +++++++++++++++---- src/passeTdsRat.ml | 91 ++++++----- src/passeTypeRat.ml | 12 +- src/testTam.ml | 6 +- 8 files changed, 224 insertions(+), 73 deletions(-) create mode 100644 src/fichiersRat/src-rat-tam-test/Segment.rat diff --git a/src/ast.ml b/src/ast.ml index 53c6cff..785dc6b 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -177,6 +177,7 @@ type expression = | NewType of typ | Adresse of Tds.info_ast | Tuple of expression list + | Nothing (* instructions existantes Rat *) (* = instruction de AstTds + informations associées aux identificateurs, mises à jour *) diff --git a/src/fichiersRat/src-rat-tam-test/Segment.rat b/src/fichiersRat/src-rat-tam-test/Segment.rat new file mode 100644 index 0000000..f5e050c --- /dev/null +++ b/src/fichiersRat/src-rat-tam-test/Segment.rat @@ -0,0 +1,16 @@ +typedef Point = struct { int x int y }; +typedef Segment = struct { Point a Point b }; + +Segment slide(Segment s Point d) { + ((s.a).x) += (d.x); + ((s.a).y) += (d.y); + ((s.b).x) += (d.x); + ((s.b).y) += (d.y); + return s; +} + +main { + Segment s = {{1 2}{3 4}}; + Point p = {1 -1}; + print call slide(s p); +} \ No newline at end of file diff --git a/src/fichiersRat/src-rat-tam-test/struct1.rat b/src/fichiersRat/src-rat-tam-test/struct1.rat index 7bad746..10cba60 100644 --- a/src/fichiersRat/src-rat-tam-test/struct1.rat +++ b/src/fichiersRat/src-rat-tam-test/struct1.rat @@ -1,5 +1,6 @@ main{ typedef Point = struct { int x int y int z }; - Point p = { 1 2 3 }; - print (p.x); + Point p = { 2 4 3 }; + (p.y) = 1; + print (p.y); } diff --git a/src/fichiersRat/src-rat-tam-test/struct2.rat b/src/fichiersRat/src-rat-tam-test/struct2.rat index e66a22b..4e2418b 100644 --- a/src/fichiersRat/src-rat-tam-test/struct2.rat +++ b/src/fichiersRat/src-rat-tam-test/struct2.rat @@ -1,5 +1,6 @@ main{ - typedef Point = struct { struct{ int a int b } x int y int z }; - Point p = { {1 2} 3 4}; - print ((p.x).b); + typedef Point = struct { int y struct{ int a int b } x int z }; + Point p = { 3 {2 1} 4 }; + print ((p.x).a); + } diff --git a/src/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml index b7a25b4..622f9ee 100644 --- a/src/passeCodeRatToTam.ml +++ b/src/passeCodeRatToTam.ml @@ -29,7 +29,47 @@ let rec getTailleAvant aff n = in tai | _ -> failwith "TODO or not TODO" end - | Attribut(aff,info) -> failwith "TODO" + | Attribut(aff2, info) -> (** (A.c).x + avec A = (1 2 (3 4) 5) = (a b c d) + avec c = (x y) + ici A ~ aff2 | c ~ info | x ~ n *) + begin + match info_ast_to_info info with + | InfoVar(n2, _, _, _) -> + let t2 = getType aff2 in + let aff_gen = Ident (info_to_info_ast (InfoVar("", t2, 0, ""))) in + getTailleAvant aff_gen n + | _ -> failwith "adaidadj" + end + +and getType aff = + match aff with + | Dref(aff2, _) -> getType aff2 + | Ident(info) -> + begin + match info_ast_to_info info with + | InfoVar(_, t, _, _) -> t + | _ -> failwith "prooblemo" + end + | Attribut(aff2, info) -> + begin + match info_ast_to_info info with + | InfoVar(n, _, _, _) -> + begin + match getType aff2 with + | Struct(l_typstr) -> + let t2 = List.fold_left ( + fun res (t, s) -> + if s = n then + t + else + res + ) Undefined l_typstr + in t2 + | _ -> failwith "pas ok" + end + | _ -> failwith "sproblematik" + end and getTailleAttribut aff n = match aff with @@ -47,7 +87,45 @@ and getTailleAttribut aff n = ) l_typstr 0 | _ -> failwith "TODO' or not TODO'" end - | Attribut(aff,info) -> failwith "TODO'" + | Attribut(_, _) -> getTaille (getType aff) + +and getBase aff = + match aff with + | Dref (a,_) -> getBase a + | Ident(i) -> + begin + match info_ast_to_info i with + | InfoVar(_, _, base, _) -> base + | _ -> failwith "spa bon" + end + | Attribut(a,i) -> + begin + match info_ast_to_info i with + | InfoVar (n,_,_,_) -> + let ib = getBase a in + let ita = getTailleAvant aff n in + let res = ib + ita in + res + | _ -> failwith "v vhjbhki" + end + +and getReg aff = + match aff with + | Dref (a,_) -> getReg a + | Ident(i) -> + begin + match info_ast_to_info i with + | InfoVar(_, _, _, reg) -> reg + | _ -> failwith "spa bon" + end + | Attribut(a,_) -> getReg a + +and extraction_info aff = + match aff with + | Dref(aff2, _) -> extraction_info aff2 + | Ident(info) -> info + | Attribut(aff2, _) -> extraction_info aff2 + and analyse_code_affectable a = match a with @@ -63,25 +141,10 @@ and analyse_code_affectable a = | _ -> failwith "w-wtf bwo y-you okay -.-" end - | Attribut(a, i) -> - begin - let rec extraction_info aff = - match aff with - | Dref(aff2, _) -> extraction_info aff2 - | Ident(info) -> info - | Attribut(_, info) -> info - in - match (info_ast_to_info (extraction_info a)) with - | InfoVar(_, _, base, reg) -> - begin - match info_ast_to_info i with - | InfoVar(n, _, _, _) -> - let t_avant = getTailleAvant a n in - "LOAD (" ^ (string_of_int (getTailleAttribut a n)) ^ ") " ^ (string_of_int (base + t_avant)) ^ "[" ^ reg ^ "]\n" - | _ -> failwith "g pu d'ID" - end - | _ -> failwith "toujours pas bon" - end + | Attribut(_, _) -> + let base = getBase a in + let reg = getReg a in + "LOAD (" ^ (string_of_int (getTaille (getType a))) ^ ") " ^ (string_of_int (base)) ^ "[" ^ reg ^ "]\n" and analyse_code_expression e = match e with @@ -142,6 +205,8 @@ and analyse_code_expression e = end | Tuple(l_expr) -> List.fold_right (fun e res -> analyse_code_expression e ^ res) l_expr "" + + | Nothing -> "" and analyse_code_instruction i taille_return taille_args taille_var = match i with @@ -174,11 +239,15 @@ and analyse_code_instruction i taille_return taille_args taille_var = | Attribut(aff, info) -> begin - match (info_ast_to_info info) with - | InfoVar(n, _, base, reg) -> - let t_avant = getTailleAvant aff n in - (analyse_code_expression e) ^ - "STORE (" ^ (string_of_int (getTailleAttribut aff n)) ^ ") " ^ (string_of_int (base + t_avant)) ^ "[" ^ reg ^ "]\n" + match (info_ast_to_info (extraction_info aff)) with + | InfoVar(_, _, _, reg) -> + begin + match (info_ast_to_info info) with + | InfoVar(n, _, _, _) -> + (analyse_code_expression e) ^ + "STORE (" ^ (string_of_int (getTailleAttribut a n)) ^ ") " ^ (string_of_int (getBase a)) ^ "[" ^ reg ^ "]\n" + | _ -> failwith "toujours pas bonn" + end | _ -> failwith "toujours pas bon" end end @@ -296,9 +365,43 @@ and analyse_code_instruction i taille_return taille_args taille_var = affichage base reg t | _ -> failwith "spa bon" end - | Attribut(a,i) -> failwith "ok3" + | Attribut(a2,i) -> + match info_ast_to_info i with + | InfoVar(n, t, _, _) -> + let base = getBase a in + let reg = getReg a in + affichage base reg t + | _ -> failwith "zifj" + end + | AppelFonction(i, le) -> + begin + match info_ast_to_info i with + | InfoFun(nom, Struct(l_typstr), _) -> + let t = Struct(l_typstr) in + let base = -getTaille t in + let reg = "ST" in + (List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^ + "CALL (ST) " ^ nom ^ "\n" ^ + (affichage base reg t) ^ + "POP (0) " ^ (string_of_int (getTaille t)) ^ "\n" + (* List.fold_right ( + fun (t, _) res -> + let i = + match t with + | Bool -> AffichageBool(Nothing) + | Int -> AffichageInt(Nothing) + | Rat -> AffichageRat(Nothing) + | Pointeur(_) -> AffichagePointeur(Nothing) + | Struct(l_ts) -> AffichageStruct(Tuple(List.map (fun _ -> Nothing) l_ts), t) + | _ -> failwith "peut pas print des non printable, duuuh" + in + "LOADL ' '\n" ^ + "SUBR COut\n" ^ + (analyse_code_instruction i "0" "0" "0") ^ + res + ) l_typstr "LOADL ' '\nSUBR COut\n" *) + | _ -> failwith "bizreement pas ok" end - | _ -> failwith "spa cool ca" in diff --git a/src/passeTdsRat.ml b/src/passeTdsRat.ml index 11e2da1..e6b2c8e 100644 --- a/src/passeTdsRat.ml +++ b/src/passeTdsRat.ml @@ -25,7 +25,14 @@ let rec recherche_type tds t = | _ -> failwith "un truc louche ca" end end - | _ -> t + | _ -> + begin + match t with + | Struct (l_typstr) -> + let n_l_typstr = (List.map (fun (t, n) -> (recherche_type tds t, n)) l_typstr) in + Struct n_l_typstr + | _ -> t + end let rec analyse_tds_affectable tds a modif = @@ -64,15 +71,18 @@ let rec analyse_tds_affectable tds a modif = let info = extraction_info n_aff in match info_ast_to_info info with | InfoVar(_, t, _, _) -> - match t with - | Struct(l_att) -> - let t = List.fold_right (fun (t, n) res -> if str = n then t else res) l_att Undefined in - if t != Undefined then - AstTds.Attribut(n_aff, info_to_info_ast (InfoVar(str, t, 0, ""))) - else - raise (MauvaiseUtilisationIdentifiant str) - | _ -> raise (MauvaiseUtilisationIdentifiant str) - + begin + match t with + | Struct(l_att) -> + let t = List.fold_right (fun (t, n) res -> if str = n then t else res) l_att Undefined in + if t != Undefined then + AstTds.Attribut(n_aff, info_to_info_ast (InfoVar(str, t, 0, ""))) + else + raise (MauvaiseUtilisationIdentifiant str) + | _ -> + let _ = print_endline (string_of_type t) in + raise (MauvaiseUtilisationIdentifiant str) + end | _ -> raise (MauvaiseUtilisationIdentifiant str) (* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *) @@ -187,34 +197,39 @@ let rec analyse_tds_instruction tds i = raise (DoubleDeclaration n) end | AstSyntax.Affectation (aff, e) -> - begin - let ne = analyse_tds_expression tds e in - let n_a = analyse_tds_affectable tds aff false in - match aff with - | Dref a -> - Affectation (n_a, ne) - - | Ident(n) -> - begin - match chercherGlobalement tds n with - | None -> - (* L'identifiant n'est pas trouvé dans la tds globale. *) - raise (IdentifiantNonDeclare n) - | Some info -> - (* L'identifiant est trouvé dans la tds globale, - il a donc déjà été déclaré. L'information associée est récupérée. *) - begin - match info_ast_to_info info with - | InfoVar _ -> - (* Renvoie de la nouvelle affectation où le nom a été remplacé par l'information - et l'expression remplacée par l'expression issue de l'analyse *) - Affectation (n_a, ne) - | _ -> - (* Modification d'une constante ou d'une fonction *) - raise (MauvaiseUtilisationIdentifiant n) - end - end - end + begin + let ne = analyse_tds_expression tds e in + let n_a = analyse_tds_affectable tds aff false in + match aff with + | Dref a -> + Affectation (n_a, ne) + + | Ident(n) -> + begin + match chercherGlobalement tds n with + | None -> + (* L'identifiant n'est pas trouvé dans la tds globale. *) + raise (IdentifiantNonDeclare n) + | Some info -> + (* L'identifiant est trouvé dans la tds globale, + il a donc déjà été déclaré. L'information associée est récupérée. *) + begin + match info_ast_to_info info with + | InfoVar _ -> + (* Renvoie de la nouvelle affectation où le nom a été remplacé par l'information + et l'expression remplacée par l'expression issue de l'analyse *) + Affectation (n_a, ne) + | _ -> + (* Modification d'une constante ou d'une fonction *) + raise (MauvaiseUtilisationIdentifiant n) + end + end + | Attribut(aff2, n) -> + begin + (** RAF *) + Affectation(n_a, ne) + end + end | AstSyntax.Constante (n,v) -> begin match chercherLocalement tds n with diff --git a/src/passeTypeRat.ml b/src/passeTypeRat.ml index 0d6faed..0e4b64a 100644 --- a/src/passeTypeRat.ml +++ b/src/passeTypeRat.ml @@ -148,7 +148,17 @@ let rec analyse_type_instruction opt i = raise (TypeInattendu(nt, t)) | _ -> failwith "wut" end - | AstTds.Attribut _ -> failwith "TODOOO" + | AstTds.Attribut(aff2, info) -> + begin + match info_ast_to_info info with + | InfoVar(_, t, _, _) -> + if est_compatible t nt then + let na, _ = analyse_type_affectable aff2 in + AstType.Affectation(Attribut(na, info), ne) + else + raise (TypeInattendu(nt, t)) + | _ -> failwith "pas bon du tout" + end end | AstTds.Affichage e -> diff --git a/src/testTam.ml b/src/testTam.ml index 08e15fd..2939676 100644 --- a/src/testTam.ml +++ b/src/testTam.ml @@ -150,4 +150,8 @@ let%expect_test "printPointeur" = let%expect_test "printStruct" = runtam "../../fichiersRat/src-rat-tam-test/testprintStruct.rat"; - [%expect{| { 1 2 } |}] \ No newline at end of file + [%expect{| { 1 2 } |}] + +let%expect_test "Segment" = + runtam "../../fichiersRat/src-rat-tam-test/Segment.rat"; + [%expect{| { { 2 1 } { 4 3 } } |}] \ No newline at end of file