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 | NewType of typ
| Adresse of Tds.info_ast | Adresse of Tds.info_ast
| Tuple of expression list | Tuple of expression list
| Nothing
(* instructions existantes Rat *) (* instructions existantes Rat *)
(* = instruction de AstTds + informations associées aux identificateurs, mises à jour *) (* = 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{ main{
typedef Point = struct { int x int y int z }; typedef Point = struct { int x int y int z };
Point p = { 1 2 3 }; Point p = { 2 4 3 };
print (p.x); (p.y) = 1;
print (p.y);
} }

View file

@ -1,5 +1,6 @@
main{ main{
typedef Point = struct { struct{ int a int b } x int y int z }; typedef Point = struct { int y struct{ int a int b } x int z };
Point p = { {1 2} 3 4}; Point p = { 3 {2 1} 4 };
print ((p.x).b); print ((p.x).a);
} }

View file

@ -29,7 +29,47 @@ let rec getTailleAvant aff n =
in tai in tai
| _ -> failwith "TODO or not TODO" | _ -> failwith "TODO or not TODO"
end 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 = and getTailleAttribut aff n =
match aff with match aff with
@ -47,7 +87,45 @@ and getTailleAttribut aff n =
) l_typstr 0 ) l_typstr 0
| _ -> failwith "TODO' or not TODO'" | _ -> failwith "TODO' or not TODO'"
end 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 = and analyse_code_affectable a =
match a with match a with
@ -63,25 +141,10 @@ and analyse_code_affectable a =
| _ -> failwith "w-wtf bwo y-you okay -.-" | _ -> failwith "w-wtf bwo y-you okay -.-"
end end
| Attribut(a, i) -> | Attribut(_, _) ->
begin let base = getBase a in
let rec extraction_info aff = let reg = getReg a in
match aff with "LOAD (" ^ (string_of_int (getTaille (getType a))) ^ ") " ^ (string_of_int (base)) ^ "[" ^ reg ^ "]\n"
| 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
and analyse_code_expression e = and analyse_code_expression e =
match e with match e with
@ -143,6 +206,8 @@ and analyse_code_expression e =
| Tuple(l_expr) -> | Tuple(l_expr) ->
List.fold_right (fun e res -> analyse_code_expression e ^ res) 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 = and analyse_code_instruction i taille_return taille_args taille_var =
match i with match i with
| Declaration(i, e) -> | Declaration(i, e) ->
@ -174,11 +239,15 @@ and analyse_code_instruction i taille_return taille_args taille_var =
| Attribut(aff, info) -> | Attribut(aff, info) ->
begin begin
match (info_ast_to_info info) with match (info_ast_to_info (extraction_info aff)) with
| InfoVar(n, _, base, reg) -> | InfoVar(_, _, _, reg) ->
let t_avant = getTailleAvant aff n in begin
(analyse_code_expression e) ^ match (info_ast_to_info info) with
"STORE (" ^ (string_of_int (getTailleAttribut aff n)) ^ ") " ^ (string_of_int (base + t_avant)) ^ "[" ^ reg ^ "]\n" | InfoVar(n, _, _, _) ->
(analyse_code_expression e) ^
"STORE (" ^ (string_of_int (getTailleAttribut a n)) ^ ") " ^ (string_of_int (getBase a)) ^ "[" ^ reg ^ "]\n"
| _ -> failwith "toujours pas bonn"
end
| _ -> failwith "toujours pas bon" | _ -> failwith "toujours pas bon"
end end
end end
@ -296,9 +365,43 @@ and analyse_code_instruction i taille_return taille_args taille_var =
affichage base reg t affichage base reg t
| _ -> failwith "spa bon" | _ -> failwith "spa bon"
end 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 end
| _ -> failwith "spa cool ca" | _ -> failwith "spa cool ca"
in in

View file

@ -25,7 +25,14 @@ let rec recherche_type tds t =
| _ -> failwith "un truc louche ca" | _ -> failwith "un truc louche ca"
end end
end end
| _ -> t | _ ->
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 = let rec analyse_tds_affectable tds a modif =
@ -64,15 +71,18 @@ let rec analyse_tds_affectable tds a modif =
let info = extraction_info n_aff in let info = extraction_info n_aff in
match info_ast_to_info info with match info_ast_to_info info with
| InfoVar(_, t, _, _) -> | InfoVar(_, t, _, _) ->
match t with begin
| Struct(l_att) -> match t with
let t = List.fold_right (fun (t, n) res -> if str = n then t else res) l_att Undefined in | Struct(l_att) ->
if t != Undefined then let t = List.fold_right (fun (t, n) res -> if str = n then t else res) l_att Undefined in
AstTds.Attribut(n_aff, info_to_info_ast (InfoVar(str, t, 0, ""))) if t != Undefined then
else AstTds.Attribut(n_aff, info_to_info_ast (InfoVar(str, t, 0, "")))
raise (MauvaiseUtilisationIdentifiant str) else
| _ -> raise (MauvaiseUtilisationIdentifiant str) raise (MauvaiseUtilisationIdentifiant str)
| _ ->
let _ = print_endline (string_of_type t) in
raise (MauvaiseUtilisationIdentifiant str)
end
| _ -> raise (MauvaiseUtilisationIdentifiant str) | _ -> raise (MauvaiseUtilisationIdentifiant str)
(* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *) (* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *)
@ -187,34 +197,39 @@ let rec analyse_tds_instruction tds i =
raise (DoubleDeclaration n) raise (DoubleDeclaration n)
end end
| AstSyntax.Affectation (aff, e) -> | AstSyntax.Affectation (aff, e) ->
begin begin
let ne = analyse_tds_expression tds e in let ne = analyse_tds_expression tds e in
let n_a = analyse_tds_affectable tds aff false in let n_a = analyse_tds_affectable tds aff false in
match aff with match aff with
| Dref a -> | Dref a ->
Affectation (n_a, ne) Affectation (n_a, ne)
| Ident(n) -> | Ident(n) ->
begin begin
match chercherGlobalement tds n with match chercherGlobalement tds n with
| None -> | None ->
(* L'identifiant n'est pas trouvé dans la tds globale. *) (* L'identifiant n'est pas trouvé dans la tds globale. *)
raise (IdentifiantNonDeclare n) raise (IdentifiantNonDeclare n)
| Some info -> | Some info ->
(* L'identifiant est trouvé dans la tds globale, (* L'identifiant est trouvé dans la tds globale,
il a donc déjà été déclaré. L'information associée est récupérée. *) il a donc déjà été déclaré. L'information associée est récupérée. *)
begin begin
match info_ast_to_info info with match info_ast_to_info info with
| InfoVar _ -> | InfoVar _ ->
(* Renvoie de la nouvelle affectation où le nom a été remplacé par l'information (* Renvoie de la nouvelle affectation où le nom a été remplacé par l'information
et l'expression remplacée par l'expression issue de l'analyse *) et l'expression remplacée par l'expression issue de l'analyse *)
Affectation (n_a, ne) Affectation (n_a, ne)
| _ -> | _ ->
(* Modification d'une constante ou d'une fonction *) (* Modification d'une constante ou d'une fonction *)
raise (MauvaiseUtilisationIdentifiant n) raise (MauvaiseUtilisationIdentifiant n)
end end
end end
end | Attribut(aff2, n) ->
begin
(** RAF *)
Affectation(n_a, ne)
end
end
| AstSyntax.Constante (n,v) -> | AstSyntax.Constante (n,v) ->
begin begin
match chercherLocalement tds n with match chercherLocalement tds n with

View file

@ -148,7 +148,17 @@ let rec analyse_type_instruction opt i =
raise (TypeInattendu(nt, t)) raise (TypeInattendu(nt, t))
| _ -> failwith "wut" | _ -> failwith "wut"
end 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 end
| AstTds.Affichage e -> | AstTds.Affichage e ->

View file

@ -151,3 +151,7 @@ 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 } |}]
let%expect_test "Segment" =
runtam "../../fichiersRat/src-rat-tam-test/Segment.rat";
[%expect{| { { 2 1 } { 4 3 } } |}]