From 517d2e6e8641f589c8b71d646124ef9cbef8780a Mon Sep 17 00:00:00 2001 From: Guillotin Damien Date: Tue, 14 Dec 2021 10:02:57 +0100 Subject: [PATCH] feat: un peu d'affichage TODO: affichage d'un struct affectable & struct dans struct --- src/ast.ml | 2 +- src/fichiersRat/src-rat-tam-test/struct1.rat | 6 +- src/fichiersRat/src-rat-tam-test/struct2.rat | 5 ++ .../src-rat-tam-test/testprintPointeur.rat | 4 + .../src-rat-tam-test/testprintStruct.rat | 4 + src/passeCodeRatToTam.ml | 87 +++++++++++++------ src/passeTypeRat.ml | 14 ++- src/tds.ml | 1 + src/testTam.ml | 14 ++- src/type.ml | 2 + 10 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 src/fichiersRat/src-rat-tam-test/struct2.rat create mode 100644 src/fichiersRat/src-rat-tam-test/testprintPointeur.rat create mode 100644 src/fichiersRat/src-rat-tam-test/testprintStruct.rat diff --git a/src/ast.ml b/src/ast.ml index d69ffd3..53c6cff 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -189,7 +189,7 @@ type bloc = instruction list | AffichageRat of expression | AffichageBool of expression | AffichagePointeur of expression - | AffichageStruct of expressionn + | AffichageStruct of expression * typ | Conditionnelle of expression * bloc * bloc | TantQue of expression * bloc | Retour of expression diff --git a/src/fichiersRat/src-rat-tam-test/struct1.rat b/src/fichiersRat/src-rat-tam-test/struct1.rat index ee3d389..7bad746 100644 --- a/src/fichiersRat/src-rat-tam-test/struct1.rat +++ b/src/fichiersRat/src-rat-tam-test/struct1.rat @@ -1,5 +1,5 @@ main{ - typedef Point = struct { struct { int a int b } x int y int z }; - Point p = { {1 2} 3 4}; - print (p.x.a); + typedef Point = struct { int x int y int z }; + Point p = { 1 2 3 }; + print (p.x); } diff --git a/src/fichiersRat/src-rat-tam-test/struct2.rat b/src/fichiersRat/src-rat-tam-test/struct2.rat new file mode 100644 index 0000000..72ef434 --- /dev/null +++ b/src/fichiersRat/src-rat-tam-test/struct2.rat @@ -0,0 +1,5 @@ +main{ + typedef Point = struct { struct { int a int b } x int y int z }; + Point p = { {1 2} 3 4}; + print ((p.x).b); +} diff --git a/src/fichiersRat/src-rat-tam-test/testprintPointeur.rat b/src/fichiersRat/src-rat-tam-test/testprintPointeur.rat new file mode 100644 index 0000000..36e9484 --- /dev/null +++ b/src/fichiersRat/src-rat-tam-test/testprintPointeur.rat @@ -0,0 +1,4 @@ +main { + int * p = (new int); + print p; +} diff --git a/src/fichiersRat/src-rat-tam-test/testprintStruct.rat b/src/fichiersRat/src-rat-tam-test/testprintStruct.rat new file mode 100644 index 0000000..7c818bd --- /dev/null +++ b/src/fichiersRat/src-rat-tam-test/testprintStruct.rat @@ -0,0 +1,4 @@ +main { + struct{int x int y} p = {1 2}; + print p; +} diff --git a/src/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml index a1108b5..5bb0352 100644 --- a/src/passeCodeRatToTam.ml +++ b/src/passeCodeRatToTam.ml @@ -24,13 +24,15 @@ let rec getTailleAvant aff n = (* print_endline (string_of_bool trouv); print_endline (s ^ " " ^ n); print_endline (string_of_bool (s = n)); *) - if s = n or trouv then + if s = n || trouv then (res, true) else (res + (getTaille t), false) ) (0, false) l_typstr in tai + | _ -> failwith "TODO or not TODO" end + | Attribut(aff,info) -> failwith "TODO" and getTailleAttribut aff n = match aff with @@ -46,7 +48,9 @@ and getTailleAttribut aff n = else res ) l_typstr 0 + | _ -> failwith "TODO' or not TODO'" end + | Attribut(aff,info) -> failwith "TODO'" and analyse_code_affectable a = match a with @@ -68,13 +72,17 @@ and analyse_code_affectable a = match aff with | Dref(aff2, _) -> extraction_info aff2 | Ident(info) -> info - | Attribut(aff2, info) -> info + | Attribut(_, info) -> info in match (info_ast_to_info (extraction_info a)) with - | InfoVar(_, t, base, reg) -> - let InfoVar(n, _, _, _) = info_ast_to_info i in - let t_avant = getTailleAvant a n in - "LOAD (" ^ (string_of_int (getTailleAttribut a n)) ^ ") " ^ (string_of_int (base + t_avant)) ^ "[" ^ reg ^ "]\n" + | 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 @@ -82,9 +90,11 @@ and analyse_code_expression e = match e with | AppelFonction(i, le) -> begin - let InfoFun(nom,_,_) = info_ast_to_info i in + match info_ast_to_info i with + | InfoFun(nom,_,_) -> (List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^ "CALL (ST) " ^ nom ^ "\n" + | _ -> failwith "eeeeencore un pb" end | Booleen(b) -> @@ -128,18 +138,25 @@ and analyse_code_expression e = "SUBR MAlloc\n" | Adresse(i) -> - let InfoVar(_, _, base, reg) = info_ast_to_info i in - "LOADL " ^ (string_of_int base) ^ "\n" + begin + match info_ast_to_info i with + | InfoVar(_, _, base, _) -> "LOADL " ^ (string_of_int base) ^ "\n" + | _ -> failwith "on a un pb" + end | Tuple(l_expr) -> List.fold_right (fun e res -> analyse_code_expression e ^ res) l_expr "" and analyse_code_instruction i taille_return taille_args taille_var = match i with | Declaration(i, e) -> - let InfoVar(_, t, base, reg) = info_ast_to_info i in - "PUSH " ^ (string_of_int (getTaille t)) ^ "\n" ^ - analyse_code_expression e ^ - "STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" + begin + match info_ast_to_info i with + | InfoVar(_, t, base, reg) -> + "PUSH " ^ (string_of_int (getTaille t)) ^ "\n" ^ + analyse_code_expression e ^ + "STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" + | _ -> failwith "match non exautique" + end | Affectation(a, e) -> begin @@ -161,7 +178,7 @@ and analyse_code_instruction i taille_return taille_args taille_var = | Attribut(aff, info) -> begin match (info_ast_to_info info) with - | InfoVar(n, t, base, reg) -> + | 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" @@ -183,17 +200,38 @@ and analyse_code_instruction i taille_return taille_args taille_var = "SUBR BOut\n" | AffichagePointeur(e) -> + "LOADL '@'\n" ^ + "SUBR COut\n" ^ (analyse_code_expression e) ^ - "SUBR BOut\n" - - | AffichageStruct(le) -> - List.fold_right ( - fun e res -> - res ^ - (analyse_code_expression e) ^ - "SUBR BOut\n" - ) le "" + "SUBR IOut\n" + | AffichageStruct(e, t) -> + let l_e = + match e with + | Tuple(l_e) -> l_e + | Affectable(a) -> failwith "TODO encore" + | _ -> failwith "normal non" + in + let l_t = + match t with + | Struct(l_typstr) -> + let (l_t, _) = List.split l_typstr in + l_t + | _ -> failwith "un truc" + in + "LOADL '{'\n" ^ + "SUBR COut\n" ^ + (List.fold_right ( + fun (e1, t1) res -> + (match t1 with + | Bool -> (analyse_code_instruction (AffichageBool(e1)) "0" "0" "0") + | Int -> (analyse_code_instruction (AffichageInt(e1)) "0" "0" "0") + | Rat -> (analyse_code_instruction (AffichageRat(e1)) "0" "0" "0") + | Pointeur _ -> (analyse_code_instruction (AffichagePointeur(e1)) "0" "0" "0") + | Struct _ -> (analyse_code_instruction (AffichageStruct(e1, t1)) "0" "0" "0") + | _ -> failwith "jamais la tfacon") ^ "LOADL ' '\n" ^ "SUBR COut\n" ^ res) (List.combine l_e l_t) "") ^ + "LOADL '}'\n" ^ + "SUBR COut\n" | Conditionnelle(e, b1, b2) -> let etiq1 = getEtiquette () in @@ -222,7 +260,6 @@ and analyse_code_instruction i taille_return taille_args taille_var = "POP (" ^ taille_return ^ ") " ^ taille_var ^ "\n" ^ "RETURN (" ^ taille_return ^ ") " ^ taille_args ^ "\n" - | Empty -> "" and analyse_code_bloc bloc taille_return taille_args taille_var = @@ -232,7 +269,7 @@ and analyse_code_bloc bloc taille_return taille_args taille_var = ) bloc "" ^ "POP (0) " ^ taille_var ^ "\n" -and analyse_code_fonction (AstPlacement.Fonction(info, l_typinfo, bloc)) = +and analyse_code_fonction (AstPlacement.Fonction(info, _, bloc)) = match info_ast_to_info info with | InfoFun(name, t, l_t) -> let taille_var = string_of_int (List.fold_right (fun e res -> taille_variables_declarees e + res) bloc 0) in diff --git a/src/passeTypeRat.ml b/src/passeTypeRat.ml index b292517..0d6faed 100644 --- a/src/passeTypeRat.ml +++ b/src/passeTypeRat.ml @@ -26,6 +26,7 @@ let rec analyse_type_affectable a = | InfoVar(_, t, _, _) -> (AstType.Ident(info), t) | InfoConst _ -> (AstType.Ident(info), Int) | InfoFun _ -> failwith "c chelou frr" + | InfoType _ -> failwith "c koi le pb encore" end | AstTds.Attribut(aff, info) -> begin @@ -99,9 +100,13 @@ let rec analyse_type_expression e = (AstType.NewType(t), Pointeur(t)) | AstTds.Adresse(info) -> - let InfoVar(_, t, _, _) = info_ast_to_info info in - let _ = modifier_type_info (Pointeur(t)) info in - (AstType.Adresse(info), Pointeur(t)) + begin + match info_ast_to_info info with + | InfoVar(_, t, _, _) -> + let _ = modifier_type_info (Pointeur(t)) info in + (AstType.Adresse(info), Pointeur(t)) + | _ -> failwith "FAIII with" + end | AstTds.Tuple(l_expr) -> let n_l_expr, l_type = List.split (List.map analyse_type_expression l_expr) in @@ -143,6 +148,7 @@ let rec analyse_type_instruction opt i = raise (TypeInattendu(nt, t)) | _ -> failwith "wut" end + | AstTds.Attribut _ -> failwith "TODOOO" end | AstTds.Affichage e -> @@ -153,7 +159,7 @@ let rec analyse_type_instruction opt i = | Rat -> AstType.AffichageRat(ne) | Bool -> AstType.AffichageBool(ne) | Pointeur _ -> AstType.AffichagePointeur(ne) - | Struct _ -> AstType.AffichageStruct(ne) + | Struct _ -> AstType.AffichageStruct(ne, nt) | _ -> failwith "un truc chelou" end diff --git a/src/tds.ml b/src/tds.ml index 757744b..dd1f5de 100644 --- a/src/tds.ml +++ b/src/tds.ml @@ -289,6 +289,7 @@ let string_of_info info = | InfoVar (n,t,dep,base) -> "Variable "^n^" : "^(string_of_type t)^" "^(string_of_int dep)^"["^base^"]" | InfoFun (n,t,tp) -> "Fonction "^n^" : "^(List.fold_right (fun elt tq -> if tq = "" then (string_of_type elt) else (string_of_type elt)^" * "^tq) tp "" )^ " -> "^(string_of_type t) + | InfoType(n, t) -> "Type" ^ n ^ ": " ^ (string_of_type t) (* Affiche la tds locale *) let afficher_locale tds = diff --git a/src/testTam.ml b/src/testTam.ml index 3866f0a..982b885 100644 --- a/src/testTam.ml +++ b/src/testTam.ml @@ -138,4 +138,16 @@ let%expect_test "typedef2" = let%expect_test "struct1" = runtam "../../fichiersRat/src-rat-tam-test/struct1.rat"; - [%expect{| 123 |}] \ No newline at end of file + [%expect{| 1 |}] + +let%expect_test "struct2" = + runtam "../../fichiersRat/src-rat-tam-test/struct2.rat"; + [%expect{| 2 |}] + +let%expect_test "printPointeur" = + runtam "../../fichiersRat/src-rat-tam-test/testprintPointeur.rat"; + [%expect{| @1048575 |}] + +let%expect_test "printStruct" = + runtam "../../fichiersRat/src-rat-tam-test/testprintStruct.rat"; + [%expect{| {1 2 } |}] \ No newline at end of file diff --git a/src/type.ml b/src/type.ml index c7e7eeb..5efa21c 100644 --- a/src/type.ml +++ b/src/type.ml @@ -5,6 +5,7 @@ let rec string_of_type t = | Bool -> "Bool" | Int -> "Int" | Rat -> "Rat" + | TIdent s -> s | Pointeur(t_p) -> "*" ^ string_of_type t_p | Struct(l_typstr) -> "{" ^ (List.fold_right (fun (t, s) res -> string_of_type t ^ " : " ^ s ^ " " ^ res) l_typstr "}") | Undefined -> "Undefined" @@ -57,6 +58,7 @@ let rec getTaille t = | Int -> 1 | Bool -> 1 | Rat -> 2 + | TIdent _ -> failwith "les typedef n'ont pas de taille" | Pointeur(_) -> 1 | Struct(l_typestr) -> List.fold_right (fun (t, _) res -> (getTaille t) + res) l_typestr 0 | Undefined -> 0