feat: vive les pointeuUuUur

This commit is contained in:
Guillotin Damien 2021-12-11 19:27:53 +01:00
parent 33ccff335a
commit 5e17ce496a
15 changed files with 106 additions and 47 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
_build/ _build/
srcTAM/ srcTAM/
out.tam

View file

@ -144,7 +144,7 @@ type binaire = Fraction | PlusInt | PlusRat | MultInt | MultRat | EquInt | EquBo
type affectable = type affectable =
(* Dé-référencement d'un affectable *) (* Dé-référencement d'un affectable *)
| Dref of affectable | Dref of affectable * typ
(* Identifiant *) (* Identifiant *)
| Ident of Tds.info_ast | Ident of Tds.info_ast
@ -200,6 +200,8 @@ end
module AstPlacement = module AstPlacement =
struct struct
type affectable = AstType.affectable
(* Expressions existantes dans notre langage *) (* Expressions existantes dans notre langage *)
(* = expression de AstType *) (* = expression de AstType *)
type expression = AstType.expression type expression = AstType.expression

View file

@ -33,7 +33,7 @@ end
(* Compilateur créant l'AST *) (* Compilateur créant l'AST *)
module CompilateurRat = Compilateur (PasseTdsNop) (PasseNop) (PasseNop) (PasseCodeNopNop) (* module CompilateurRat = Compilateur (PasseTdsNop) (PasseNop) (PasseNop) (PasseCodeNopNop) *)
(* + passe de résolution des identifiants *) (* + passe de résolution des identifiants *)
@ -54,11 +54,11 @@ module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacement
(* + passe de génération de code -> compilateur complet *) (* + passe de génération de code -> compilateur complet *)
(* open PasseTdsRat open PasseTdsRat
open PasseTypeRat open PasseTypeRat
open PassePlacementRat open PassePlacementRat
open PasseCodeRatToTam open PasseCodeRatToTam
module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam) *) module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam)

View file

@ -0,0 +1,7 @@
main{
int * px = (new int);
int x = 3;
px = &x;
int y = (*px);
print y;
}

View file

@ -0,0 +1,3 @@
test{
int x = (new int);
}

View file

@ -0,0 +1,3 @@
test{
int * x = 2;
}

View file

@ -12,18 +12,26 @@ struct
type t1 = Ast.AstPlacement.programme type t1 = Ast.AstPlacement.programme
type t2 = string type t2 = string
let rec analyse_code_affectable a =
match a with
| Dref(aff, t) ->
analyse_code_affectable aff ^
"LOADI (" ^ (string_of_int (getTaille t)) ^ ")\n"
| Ident(i) ->
match info_ast_to_info i with
| InfoVar(_, t, base, reg) -> "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
| InfoConst(_,v) -> "LOADL " ^ (string_of_int v) ^ "\n"
| _ -> failwith "w-wtf bwo y-you okay -.-"
let rec analyse_code_expression e = let rec analyse_code_expression e =
match e with match e with
| AppelFonction(i, le) -> | AppelFonction(i, le) ->
begin begin
let InfoFun(nom,_,_) = info_ast_to_info i in let InfoFun(nom,_,_) = info_ast_to_info i in
(List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^ (List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^
"CALL (ST) " ^ nom ^ "\n" "CALL (ST) " ^ nom ^ "\n"
end end
(* | Ident(i) ->
let InfoVar(_,t,base,reg) = info_ast_to_info i in
"LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" *)
| Booleen(b) -> | Booleen(b) ->
if b then if b then
@ -56,7 +64,18 @@ let rec analyse_code_expression e =
| MultRat -> "CALL (ST) RMul\n" | MultRat -> "CALL (ST) RMul\n"
| EquBool -> "SUBR IEq\n" | EquBool -> "SUBR IEq\n"
end end
| Affectable(a) -> analyse_code_affectable a
| Null -> "SUBR MVoid\n"
| NewType(t) ->
"LOADL " ^ (string_of_int (getTaille t)) ^ "\n" ^
"SUBR MAlloc\n"
| Adresse(i) ->
let InfoVar(_, _, base, reg) = info_ast_to_info i in
"LOADL " ^ (string_of_int base) ^ "\n"
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
@ -66,10 +85,24 @@ and analyse_code_instruction i taille_return taille_args taille_var =
analyse_code_expression e ^ analyse_code_expression e ^
"STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" "STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
| Affectation(i, e) -> | Affectation(a, e) ->
let InfoVar(_, t, base, reg) = (info_ast_to_info i) in begin
analyse_code_expression e ^ match a with
"STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" | Ident(i) ->
begin
match (info_ast_to_info i) with
| InfoVar (_, t, base, reg) ->
(analyse_code_expression e) ^
"STORE (" ^ string_of_int (getTaille t) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
| _ -> failwith ("Erreur interne : symbole non reconnnu")
end
| Dref(a, t) ->
(analyse_code_expression e) ^
(analyse_code_affectable a) ^
"STOREI (" ^ string_of_int (getTaille t) ^ ")\n"
end
| AffichageInt(e) -> | AffichageInt(e) ->
(analyse_code_expression e) ^ (analyse_code_expression e) ^

View file

@ -11,6 +11,14 @@ struct
type t1 = Ast.AstType.programme type t1 = Ast.AstType.programme
type t2 = Ast.AstPlacement.programme type t2 = Ast.AstPlacement.programme
(* let rec analyse_placement_affectable a =
begin
match a with
| AstType.Dref(aff) ->
| AstType.Ident(i) ->
end *)
let rec analyse_placement_instruction i base reg = let rec analyse_placement_instruction i base reg =
match i with match i with
| AstType.Declaration(info, _) -> | AstType.Declaration(info, _) ->

View file

@ -26,17 +26,16 @@ let rec analyse_tds_affectable tds a modif =
begin begin
match (info_ast_to_info info) with match (info_ast_to_info info) with
| InfoVar _ -> AstTds.Ident(info) | InfoVar _ -> AstTds.Ident(info)
| InfoConst _ -> | InfoConst(_) ->
begin begin
if modif then if modif then
failwith "bah non en fait" failwith "bah non en fait"
else else
AstTds.Ident(info) AstTds.Ident(info)
end end
| InfoFun _ -> failwith "t chelou bro" | InfoFun _ -> raise (MauvaiseUtilisationIdentifiant(n))
end end
end end
(* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *) (* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *)
(* Paramètre tds : la table des symboles courante *) (* Paramètre tds : la table des symboles courante *)
@ -130,7 +129,7 @@ let rec analyse_tds_instruction tds i =
| 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 a true 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)

View file

@ -17,7 +17,7 @@ let rec analyse_type_affectable a =
| AstTds.Dref(a) -> | AstTds.Dref(a) ->
begin begin
match (analyse_type_affectable a) with match (analyse_type_affectable a) with
| (na, Pointeur t) -> (AstType.Dref(na), t) | (na, Pointeur t) -> (AstType.Dref(na, t), t)
| _ -> failwith "jsp koi mettre ici" | _ -> failwith "jsp koi mettre ici"
end end
| AstTds.Ident(info) -> | AstTds.Ident(info) ->
@ -27,24 +27,6 @@ let rec analyse_type_affectable a =
| InfoConst _ -> (AstType.Ident(info), Int) | InfoConst _ -> (AstType.Ident(info), Int)
| InfoFun _ -> failwith "c chelou frr" | InfoFun _ -> failwith "c chelou frr"
end end
(*
let rec analyse_type_affectable (aff:AstTds.affectable) : (affectable*typ) =
match aff with
| AstTds.Ident info ->
begin
match info_ast_to_info info with
| InfoVar (_,t,_,_) -> (Ident info, t)
| InfoConst _ -> (Ident info, Int)
| _ -> failwith ("Erreur interne : symbole non reconnnu")
end
| AstTds.Valeur aff ->
begin
match analyse_type_affectable aff with
| (naff, Pointeur t) -> (Valeur(naff, t),t)
| _ -> raise PasUnPointeur
end
*)
(* analyse_tds_expression : AstTds.expression -> (AstType.expression, type) *) (* analyse_tds_expression : AstTds.expression -> (AstType.expression, type) *)
@ -105,11 +87,13 @@ let rec analyse_type_expression e =
| AstTds.Null -> (AstType.Null, Pointeur(Undefined)) | AstTds.Null -> (AstType.Null, Pointeur(Undefined))
| AstTds.NewType(t) -> (AstType.NewType(t), Pointeur(t)) | AstTds.NewType(t) ->
(AstType.NewType(t), Pointeur(t))
| AstTds.Adresse(info) -> | AstTds.Adresse(info) ->
let _ = modifier_type_info Int info in let InfoVar(_, t, _, _) = info_ast_to_info info in
(AstType.Adresse(info), Int) let _ = modifier_type_info (Pointeur(t)) info in
(AstType.Adresse(info), Pointeur(t))
(* analyse_tds_instruction : AstTds.instruction -> tds -> AstType.instruction *) (* analyse_tds_instruction : AstTds.instruction -> tds -> AstType.instruction *)
(* Paramètre tds : la table des symboles courante *) (* Paramètre tds : la table des symboles courante *)
@ -121,13 +105,14 @@ let rec analyse_type_instruction opt i =
match i with match i with
| AstTds.Declaration (t, info, e) -> | AstTds.Declaration (t, info, e) ->
let (ne, nt) = analyse_type_expression e in let (ne, nt) = analyse_type_expression e in
if (est_compatible t nt) then if (est_compatible nt t) then
let _ = modifier_type_info t info in let _ = modifier_type_info t info in
AstType.Declaration(info, ne) AstType.Declaration(info, ne)
else else
let _ = print_endline (string_of_type nt) in
let _ = print_endline (string_of_type t) in
raise (TypeInattendu(nt, t)) raise (TypeInattendu(nt, t))
| AstTds.Affectation (aff, e) -> | AstTds.Affectation (aff, e) ->
let (ne, nt) = analyse_type_expression e in let (ne, nt) = analyse_type_expression e in
begin begin

View file

@ -108,3 +108,7 @@ let%expect_test "factfuns" =
let%expect_test "factrec" = let%expect_test "factrec" =
runtam "../../fichiersRat/src-rat-tam-test/factrec.rat"; runtam "../../fichiersRat/src-rat-tam-test/factrec.rat";
[%expect{| 120 |}] [%expect{| 120 |}]
let%expect_test "pointeur1" =
runtam "../../fichiersRat/src-rat-tam-test/pointeur1.rat";
[%expect{| 3 |}]

View file

@ -419,6 +419,20 @@ let _ = compiler "../../fichiersRat/src-rat-tam-test/factiter.rat" in ()
let%test_unit "code_complique" = let%test_unit "code_complique" =
let _ = compiler "../../fichiersRat/src-rat-tam-test/complique.rat" in () let _ = compiler "../../fichiersRat/src-rat-tam-test/complique.rat" in ()
let%test_unit ""=
try
let _ = compiler "../../fichiersRat/src-rat-type-test/testIdentPointeur1.rat"
in raise ErreurNonDetectee
with
| TypeInattendu(Pointeur(Int),Int) -> ()
let%test_unit ""=
try
let _ = compiler "../../fichiersRat/src-rat-type-test/testIdentPointeur2.rat"
in raise ErreurNonDetectee
with
| TypeInattendu(Int,Pointeur(Int)) -> ()
(* ------------------------------ *) (* ------------------------------ *)
(* Avec fonction *) (* Avec fonction *)
(* ------------------------------ *) (* ------------------------------ *)

View file

@ -5,7 +5,7 @@ let rec string_of_type t =
| Bool -> "Bool" | Bool -> "Bool"
| Int -> "Int" | Int -> "Int"
| Rat -> "Rat" | Rat -> "Rat"
| Pointeur(t_p) -> "Poi " ^ string_of_type t_p | Pointeur(t_p) -> "*" ^ string_of_type t_p
| Undefined -> "Undefined" | Undefined -> "Undefined"