feat: vive les pointeuUuUur
This commit is contained in:
parent
33ccff335a
commit
5e17ce496a
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
_build/
|
||||
srcTAM/
|
||||
srcTAM/
|
||||
out.tam
|
|
@ -144,7 +144,7 @@ type binaire = Fraction | PlusInt | PlusRat | MultInt | MultRat | EquInt | EquBo
|
|||
|
||||
type affectable =
|
||||
(* Dé-référencement d'un affectable *)
|
||||
| Dref of affectable
|
||||
| Dref of affectable * typ
|
||||
(* Identifiant *)
|
||||
| Ident of Tds.info_ast
|
||||
|
||||
|
@ -200,6 +200,8 @@ end
|
|||
module AstPlacement =
|
||||
struct
|
||||
|
||||
type affectable = AstType.affectable
|
||||
|
||||
(* Expressions existantes dans notre langage *)
|
||||
(* = expression de AstType *)
|
||||
type expression = AstType.expression
|
||||
|
|
|
@ -33,7 +33,7 @@ end
|
|||
|
||||
|
||||
(* 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 *)
|
||||
|
@ -54,11 +54,11 @@ module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacement
|
|||
|
||||
|
||||
(* + passe de génération de code -> compilateur complet *)
|
||||
(* open PasseTdsRat
|
||||
open PasseTdsRat
|
||||
open PasseTypeRat
|
||||
open PassePlacementRat
|
||||
open PasseCodeRatToTam
|
||||
module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam) *)
|
||||
module CompilateurRat = Compilateur (PasseTdsRat) (PasseTypeRat) (PassePlacementRat) (PasseCodeRatToTam)
|
||||
|
||||
|
||||
|
||||
|
|
7
src/fichiersRat/src-rat-tam-test/pointeur1.rat
Normal file
7
src/fichiersRat/src-rat-tam-test/pointeur1.rat
Normal file
|
@ -0,0 +1,7 @@
|
|||
main{
|
||||
int * px = (new int);
|
||||
int x = 3;
|
||||
px = &x;
|
||||
int y = (*px);
|
||||
print y;
|
||||
}
|
3
src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat
Normal file
3
src/fichiersRat/src-rat-type-test/testIdentPointeur1.rat
Normal file
|
@ -0,0 +1,3 @@
|
|||
test{
|
||||
int x = (new int);
|
||||
}
|
3
src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat
Normal file
3
src/fichiersRat/src-rat-type-test/testIdentPointeur2.rat
Normal file
|
@ -0,0 +1,3 @@
|
|||
test{
|
||||
int * x = 2;
|
||||
}
|
|
@ -12,18 +12,26 @@ struct
|
|||
type t1 = Ast.AstPlacement.programme
|
||||
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 =
|
||||
match e with
|
||||
| AppelFonction(i, le) ->
|
||||
begin
|
||||
let InfoFun(nom,_,_) = info_ast_to_info i in
|
||||
(List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^
|
||||
"CALL (ST) " ^ nom ^ "\n"
|
||||
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" *)
|
||||
begin
|
||||
let InfoFun(nom,_,_) = info_ast_to_info i in
|
||||
(List.fold_right (fun e res -> analyse_code_expression e ^ res) le "") ^
|
||||
"CALL (ST) " ^ nom ^ "\n"
|
||||
end
|
||||
|
||||
| Booleen(b) ->
|
||||
if b then
|
||||
|
@ -56,7 +64,18 @@ let rec analyse_code_expression e =
|
|||
| MultRat -> "CALL (ST) RMul\n"
|
||||
| EquBool -> "SUBR IEq\n"
|
||||
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 =
|
||||
match i with
|
||||
|
@ -66,10 +85,24 @@ and analyse_code_instruction i taille_return taille_args taille_var =
|
|||
analyse_code_expression e ^
|
||||
"STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
|
||||
|
||||
| Affectation(i, e) ->
|
||||
let InfoVar(_, t, base, reg) = (info_ast_to_info i) in
|
||||
analyse_code_expression e ^
|
||||
"STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
|
||||
| Affectation(a, e) ->
|
||||
begin
|
||||
match a with
|
||||
| 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) ->
|
||||
(analyse_code_expression e) ^
|
||||
|
|
|
@ -11,6 +11,14 @@ struct
|
|||
type t1 = Ast.AstType.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 =
|
||||
match i with
|
||||
| AstType.Declaration(info, _) ->
|
||||
|
|
|
@ -26,17 +26,16 @@ let rec analyse_tds_affectable tds a modif =
|
|||
begin
|
||||
match (info_ast_to_info info) with
|
||||
| InfoVar _ -> AstTds.Ident(info)
|
||||
| InfoConst _ ->
|
||||
| InfoConst(_) ->
|
||||
begin
|
||||
if modif then
|
||||
failwith "bah non en fait"
|
||||
else
|
||||
AstTds.Ident(info)
|
||||
end
|
||||
| InfoFun _ -> failwith "t chelou bro"
|
||||
| InfoFun _ -> raise (MauvaiseUtilisationIdentifiant(n))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
(* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *)
|
||||
(* Paramètre tds : la table des symboles courante *)
|
||||
|
@ -130,7 +129,7 @@ let rec analyse_tds_instruction tds i =
|
|||
| AstSyntax.Affectation (aff, e) ->
|
||||
begin
|
||||
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
|
||||
| Dref a ->
|
||||
Affectation (n_a, ne)
|
||||
|
|
|
@ -17,7 +17,7 @@ let rec analyse_type_affectable a =
|
|||
| AstTds.Dref(a) ->
|
||||
begin
|
||||
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"
|
||||
end
|
||||
| AstTds.Ident(info) ->
|
||||
|
@ -27,24 +27,6 @@ let rec analyse_type_affectable a =
|
|||
| InfoConst _ -> (AstType.Ident(info), Int)
|
||||
| InfoFun _ -> failwith "c chelou frr"
|
||||
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) *)
|
||||
|
@ -105,11 +87,13 @@ let rec analyse_type_expression e =
|
|||
|
||||
| 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) ->
|
||||
let _ = modifier_type_info Int info in
|
||||
(AstType.Adresse(info), Int)
|
||||
let InfoVar(_, t, _, _) = info_ast_to_info info in
|
||||
let _ = modifier_type_info (Pointeur(t)) info in
|
||||
(AstType.Adresse(info), Pointeur(t))
|
||||
|
||||
(* analyse_tds_instruction : AstTds.instruction -> tds -> AstType.instruction *)
|
||||
(* Paramètre tds : la table des symboles courante *)
|
||||
|
@ -121,13 +105,14 @@ let rec analyse_type_instruction opt i =
|
|||
match i with
|
||||
| AstTds.Declaration (t, info, e) ->
|
||||
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
|
||||
AstType.Declaration(info, ne)
|
||||
else
|
||||
let _ = print_endline (string_of_type nt) in
|
||||
let _ = print_endline (string_of_type t) in
|
||||
raise (TypeInattendu(nt, t))
|
||||
|
||||
|
||||
| AstTds.Affectation (aff, e) ->
|
||||
let (ne, nt) = analyse_type_expression e in
|
||||
begin
|
||||
|
|
|
@ -108,3 +108,7 @@ let%expect_test "factfuns" =
|
|||
let%expect_test "factrec" =
|
||||
runtam "../../fichiersRat/src-rat-tam-test/factrec.rat";
|
||||
[%expect{| 120 |}]
|
||||
|
||||
let%expect_test "pointeur1" =
|
||||
runtam "../../fichiersRat/src-rat-tam-test/pointeur1.rat";
|
||||
[%expect{| 3 |}]
|
|
@ -419,6 +419,20 @@ let _ = compiler "../../fichiersRat/src-rat-tam-test/factiter.rat" in ()
|
|||
let%test_unit "code_complique" =
|
||||
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 *)
|
||||
(* ------------------------------ *)
|
|
@ -5,7 +5,7 @@ let rec string_of_type t =
|
|||
| Bool -> "Bool"
|
||||
| Int -> "Int"
|
||||
| Rat -> "Rat"
|
||||
| Pointeur(t_p) -> "Poi " ^ string_of_type t_p
|
||||
| Pointeur(t_p) -> "*" ^ string_of_type t_p
|
||||
| Undefined -> "Undefined"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue