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{
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);
}

View file

@ -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"

View file

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