feat: le print struct fonctionne a 100%

This commit is contained in:
Guillotin Damien 2021-12-16 16:58:34 +01:00
parent 517d2e6e86
commit cb4b00f4ad
3 changed files with 100 additions and 25 deletions

View file

@ -1,5 +1,5 @@
main{ 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}; Point p = { {1 2} 3 4};
print ((p.x).b); print ((p.x).b);
} }

View file

@ -21,9 +21,6 @@ let rec getTailleAvant aff n =
| InfoVar(_, Struct(l_typstr), _, _) -> | InfoVar(_, Struct(l_typstr), _, _) ->
let tai, _ = List.fold_left ( let tai, _ = List.fold_left (
fun (res, trouv) (t, s) -> 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 if s = n || trouv then
(res, true) (res, true)
else else
@ -206,30 +203,108 @@ and analyse_code_instruction i taille_return taille_args taille_var =
"SUBR IOut\n" "SUBR IOut\n"
| AffichageStruct(e, t) -> | AffichageStruct(e, t) ->
let l_e =
match e with let rec affichage base reg t0 =
| Tuple(l_e) -> l_e match t0 with
| Affectable(a) -> failwith "TODO encore"
| _ -> failwith "normal non"
in
let l_t =
match t with
| Struct(l_typstr) -> | Struct(l_typstr) ->
let (l_t, _) = List.split l_typstr in begin
l_t let l_t, _ = List.split l_typstr in
| _ -> failwith "un truc" let res, _ = List.fold_left (
in 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" ^ "LOADL '{'\n" ^
"SUBR COut\n" ^ "SUBR COut\n" ^
(List.fold_right ( (affichage taille reg t) ^
fun (e1, t1) res -> "LOADL '}'\n" ^
(match t1 with "SUBR COut\n" ^
| Bool -> (analyse_code_instruction (AffichageBool(e1)) "0" "0" "0") "LOADL ' '\n" ^
| Int -> (analyse_code_instruction (AffichageInt(e1)) "0" "0" "0") "SUBR COut\n"
| Rat -> (analyse_code_instruction (AffichageRat(e1)) "0" "0" "0") ,taille + (getTaille t))
| Pointeur _ -> (analyse_code_instruction (AffichagePointeur(e1)) "0" "0" "0") | _ -> failwith "peut pas print des non printable, ofc"
| Struct _ -> (analyse_code_instruction (AffichageStruct(e1, t1)) "0" "0" "0") ) ("LOADL ' '\nSUBR COut\n", base) l_t in
| _ -> failwith "jamais la tfacon") ^ "LOADL ' '\n" ^ "SUBR COut\n" ^ res) (List.combine l_e l_t) "") ^ 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" ^
(affichage_expression e t) ^
"LOADL '}'\n" ^ "LOADL '}'\n" ^
"SUBR COut\n" "SUBR COut\n"

View file

@ -150,4 +150,4 @@ let%expect_test "printPointeur" =
let%expect_test "printStruct" = let%expect_test "printStruct" =
runtam "../../fichiersRat/src-rat-tam-test/testprintStruct.rat"; runtam "../../fichiersRat/src-rat-tam-test/testprintStruct.rat";
[%expect{| {1 2 } |}] [%expect{| { 1 2 } |}]