feat: tout est bon TODO: des ptits tests en plus c'est tjrs mieux

This commit is contained in:
Guillotin Damien 2021-12-30 20:52:46 +01:00
parent cb4b00f4ad
commit 13ed03e015
8 changed files with 224 additions and 73 deletions

View file

@ -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 *)

View file

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

View file

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

View file

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

View file

@ -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
@ -143,6 +206,8 @@ and analyse_code_expression e =
| 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
| Declaration(i, e) ->
@ -173,12 +238,16 @@ and analyse_code_instruction i taille_return taille_args taille_var =
"STOREI (" ^ string_of_int (getTaille t) ^ ")\n"
| Attribut(aff, info) ->
begin
match (info_ast_to_info (extraction_info aff)) with
| InfoVar(_, _, _, reg) ->
begin
match (info_ast_to_info info) with
| InfoVar(n, _, base, reg) ->
let t_avant = getTailleAvant aff n in
| InfoVar(n, _, _, _) ->
(analyse_code_expression e) ^
"STORE (" ^ (string_of_int (getTailleAttribut aff n)) ^ ") " ^ (string_of_int (base + t_avant)) ^ "[" ^ reg ^ "]\n"
"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

View file

@ -25,7 +25,14 @@ let rec recherche_type tds t =
| _ -> failwith "un truc louche ca"
end
end
| _ ->
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,6 +71,7 @@ let rec analyse_tds_affectable tds a modif =
let info = extraction_info n_aff in
match info_ast_to_info info with
| InfoVar(_, t, _, _) ->
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
@ -71,8 +79,10 @@ let rec analyse_tds_affectable tds a modif =
AstTds.Attribut(n_aff, info_to_info_ast (InfoVar(str, t, 0, "")))
else
raise (MauvaiseUtilisationIdentifiant str)
| _ -> 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 *)
@ -214,6 +224,11 @@ let rec analyse_tds_instruction tds i =
raise (MauvaiseUtilisationIdentifiant n)
end
end
| Attribut(aff2, n) ->
begin
(** RAF *)
Affectation(n_a, ne)
end
end
| AstSyntax.Constante (n,v) ->
begin

View file

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

View file

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