From cb4b00f4ad72b4c35e0e10c3e5139991924eab41 Mon Sep 17 00:00:00 2001 From: Guillotin Damien Date: Thu, 16 Dec 2021 16:58:34 +0100 Subject: [PATCH] feat: le print struct fonctionne a 100% --- src/fichiersRat/src-rat-tam-test/struct2.rat | 2 +- src/passeCodeRatToTam.ml | 121 +++++++++++++++---- src/testTam.ml | 2 +- 3 files changed, 100 insertions(+), 25 deletions(-) diff --git a/src/fichiersRat/src-rat-tam-test/struct2.rat b/src/fichiersRat/src-rat-tam-test/struct2.rat index 72ef434..e66a22b 100644 --- a/src/fichiersRat/src-rat-tam-test/struct2.rat +++ b/src/fichiersRat/src-rat-tam-test/struct2.rat @@ -1,5 +1,5 @@ main{ - typedef Point = struct { struct { int a int b } x int y int z }; + 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/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml index 5bb0352..b7a25b4 100644 --- a/src/passeCodeRatToTam.ml +++ b/src/passeCodeRatToTam.ml @@ -21,9 +21,6 @@ let rec getTailleAvant aff n = | InfoVar(_, Struct(l_typstr), _, _) -> let tai, _ = List.fold_left ( fun (res, trouv) (t, s) -> - (* print_endline (string_of_bool trouv); - print_endline (s ^ " " ^ n); - print_endline (string_of_bool (s = n)); *) if s = n || trouv then (res, true) else @@ -206,30 +203,108 @@ and analyse_code_instruction i taille_return taille_args taille_var = "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 + + let rec affichage base reg t0 = + match t0 with | Struct(l_typstr) -> - let (l_t, _) = List.split l_typstr in - l_t - | _ -> failwith "un truc" + begin + let l_t, _ = List.split l_typstr in + let res, _ = List.fold_left ( + fun (res, taille) t -> + match t with + | Bool -> ( + res ^ + "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int taille) ^ "[" ^ reg ^ "]\n" ^ + "SUBR BOut\n" ^ + "LOADL ' '\n" ^ + "SUBR COut\n" + ,taille + (getTaille t)) + | Int -> ( + res ^ + "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int taille) ^ "[" ^ reg ^ "]\n" ^ + "SUBR IOut\n" ^ + "LOADL ' '\n" ^ + "SUBR COut\n" + ,taille + (getTaille t)) + | Rat -> ( + res ^ + "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int taille) ^ "[" ^ reg ^ "]\n" ^ + "CALL (ST) ROut\n" ^ + "LOADL ' '\n" ^ + "SUBR COut\n" + ,taille + (getTaille t)) + | Pointeur(_) -> ( + res ^ + "LOADL '@'\n" ^ + "SUBR COut\n" ^ + "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int taille) ^ "[" ^ reg ^ "]\n" ^ + "SUBR IOut\n" ^ + "LOADL ' '\n" ^ + "SUBR COut\n" + ,taille + (getTaille t)) + | Struct(_) -> ( + res ^ + "LOADL '{'\n" ^ + "SUBR COut\n" ^ + (affichage taille reg t) ^ + "LOADL '}'\n" ^ + "SUBR COut\n" ^ + "LOADL ' '\n" ^ + "SUBR COut\n" + ,taille + (getTaille t)) + | _ -> failwith "peut pas print des non printable, ofc" + ) ("LOADL ' '\nSUBR COut\n", base) l_t in + res + end + | _ -> failwith "toujours pas bon ca" in + + let rec affichage_expression e t = + let l_typstr = + match t with + | Struct(l_typstr) -> l_typstr + | _ -> failwith "wowowowow" + in + let l_t, l_s = List.split l_typstr in + match e with + | Tuple(l_e) -> + List.fold_right ( + fun (e, t) res -> + let i = + match t with + | Bool -> AffichageBool(e) + | Int -> AffichageInt(e) + | Rat -> AffichageRat(e) + | Pointeur(_) -> AffichagePointeur(e) + | Struct(_) -> AffichageStruct(e, t) + | _ -> failwith "peut pas print des non printable, duh" + in + "LOADL ' '\n" ^ + "SUBR COut\n" ^ + (analyse_code_instruction i "0" "0" "0") ^ + res + ) (List.combine l_e l_t) "LOADL ' '\nSUBR COut\n" + + | Affectable(a) -> + begin + match a with + | Dref(a,t) -> failwith "ok1" + | Ident(i) -> + begin + match info_ast_to_info i with + | InfoVar(_, t, base, reg) -> + affichage base reg t + | _ -> failwith "spa bon" + end + | Attribut(a,i) -> failwith "ok3" + end + + | _ -> failwith "spa cool ca" + 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) "") ^ + (affichage_expression e t) ^ "LOADL '}'\n" ^ "SUBR COut\n" diff --git a/src/testTam.ml b/src/testTam.ml index 982b885..08e15fd 100644 --- a/src/testTam.ml +++ b/src/testTam.ml @@ -150,4 +150,4 @@ 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 } |}] \ No newline at end of file