diff --git a/src/ast.ml b/src/ast.ml index 2c009d7..de140dc 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -33,8 +33,6 @@ type affectable = type expression = (* Appel de fonction représenté par le nom de la fonction et la liste des paramètres réels *) | AppelFonction of string * expression list - (* Accès à un identifiant représenté par son nom *) - | Ident of string (* Booléen *) | Booleen of bool (* Entier *) @@ -87,16 +85,25 @@ end module AstTds = struct + type affectable = + (* Dé-référencement d'un affectable *) + | Dref of affectable + (* Identifiant *) + | Ident of Tds.info_ast + (* Expressions existantes dans notre langage *) (* ~ expression de l'AST syntaxique où les noms des identifiants ont été remplacés par les informations associées aux identificateurs *) type expression = | AppelFonction of Tds.info_ast * expression list - | Ident of Tds.info_ast (* le nom de l'identifiant est remplacé par ses informations *) | Booleen of bool | Entier of int | Unaire of AstSyntax.unaire * expression | Binaire of AstSyntax.binaire * expression * expression + | Affectable of affectable + | Null + | NewType of typ + | Adresse of Tds.info_ast (* instructions existantes dans notre langage *) (* ~ instruction de l'AST syntaxique où les noms des identifiants ont été @@ -105,7 +112,7 @@ struct type bloc = instruction list and instruction = | Declaration of typ * Tds.info_ast * expression (* le nom de l'identifiant est remplacé par ses informations *) - | Affectation of Tds.info_ast * expression (* le nom de l'identifiant est remplacé par ses informations *) + | Affectation of affectable * expression (* le nom de l'identifiant est remplacé par ses informations *) | Affichage of expression | Conditionnelle of expression * bloc * bloc | TantQue of expression * bloc @@ -135,15 +142,24 @@ type unaire = Numerateur | Denominateur (* Opérateurs binaires existants dans Rat - résolution de la surcharge *) type binaire = Fraction | PlusInt | PlusRat | MultInt | MultRat | EquInt | EquBool | Inf +type affectable = + (* Dé-référencement d'un affectable *) + | Dref of affectable + (* Identifiant *) + | Ident of Tds.info_ast + (* Expressions existantes dans Rat *) (* = expression de AstTds *) type expression = | AppelFonction of Tds.info_ast * expression list - | Ident of Tds.info_ast | Booleen of bool | Entier of int | Unaire of unaire * expression | Binaire of binaire * expression * expression + | Affectable of affectable + | Null + | NewType of typ + | Adresse of Tds.info_ast (* instructions existantes Rat *) (* = instruction de AstTds + informations associées aux identificateurs, mises à jour *) @@ -151,7 +167,7 @@ type expression = type bloc = instruction list and instruction = | Declaration of Tds.info_ast * expression - | Affectation of Tds.info_ast * expression + | Affectation of affectable * expression | AffichageInt of expression | AffichageRat of expression | AffichageBool of expression @@ -202,9 +218,3 @@ type programme = Programme of fonction list * bloc end - -(* Conversion des affectables *) -let rec string_of_affectable a = - match a with - | AstSyntax.Dref(n) -> (string_of_affectable n) - | AstSyntax.Ident(n) -> n ^ " " diff --git a/src/compilateur.ml b/src/compilateur.ml index 9ad1828..e6189f0 100644 --- a/src/compilateur.ml +++ b/src/compilateur.ml @@ -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) *) diff --git a/src/parser.mly b/src/parser.mly index 620467d..8d42968 100644 --- a/src/parser.mly +++ b/src/parser.mly @@ -70,14 +70,14 @@ is : | i1=i li=is {i1::li} i : -| t=typ n=ID EQUAL e1=e PV {Declaration (t,n,e1)} -| n=ID EQUAL e1=e PV {Affectation (n,e1)} -| CONST n=ID EQUAL e=ENTIER PV {Constante (n,e)} -| PRINT e1=e PV {Affichage (e1)} -| IF exp=e li1=bloc ELSE li2=bloc {Conditionnelle (exp,li1,li2)} -| WHILE exp=e li=bloc {TantQue (exp,li)} -| RETURN exp=e PV {Retour (exp)} -| a1=a EQUAL exp=e1 {Affectation(Deref(a1), e1)} +| t=typ n=ID EQUAL exp=e PV {Declaration (t,n,exp)} +| n=ID EQUAL exp=e PV {Affectation (Ident(n),exp)} +| CONST n=ID EQUAL e=ENTIER PV {Constante (n,e)} +| PRINT exp=e PV {Affichage (exp)} +| IF exp=e li1=bloc ELSE li2=bloc {Conditionnelle (exp,li1,li2)} +| WHILE exp=e li=bloc {TantQue (exp,li)} +| RETURN exp=e PV {Retour (exp)} +| aff=a EQUAL exp=e {Affectation (aff, exp)} dp : | {[]} @@ -87,7 +87,7 @@ typ : | BOOL {Bool} | INT {Int} | RAT {Rat} -| t1=typ MULT {Deref t1} +| t1=typ MULT {Pointeur (t1)} e : | CALL n=ID PO lp=cp PF {AppelFonction (n,lp)} @@ -103,14 +103,14 @@ e : | PO e1=e INF e2=e PF {Binaire (Inf,e1,e2)} | PO exp=e PF {exp} | a1=a {Affectable a1} -| NULL { ??? } -| PO NEW t1=typ PF { ??? } -| AMP n=ID { Deref n } +| NULL {Null} +| PO NEW t1=typ PF {NewType (t1)} +| AMP n=ID {Adresse (n)} cp : | {[]} | e1=e le=cp {e1::le} a : -| n=ID {Ident n} -| PO MULT a1=a PF {Deref a1} \ No newline at end of file +| n=ID {Ident (n)} +| PO MULT a1=a PF {Dref (a1)} \ No newline at end of file diff --git a/src/passeCodeRatToTam.ml b/src/passeCodeRatToTam.ml index 534c6cf..74e7838 100644 --- a/src/passeCodeRatToTam.ml +++ b/src/passeCodeRatToTam.ml @@ -21,9 +21,9 @@ let rec analyse_code_expression e = "CALL (ST) " ^ nom ^ "\n" end - | Ident(i) -> + (* | 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" + "LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n" *) | Booleen(b) -> if b then diff --git a/src/passeTdsRat.ml b/src/passeTdsRat.ml index 0fcfa04..0c56cde 100644 --- a/src/passeTdsRat.ml +++ b/src/passeTdsRat.ml @@ -6,11 +6,38 @@ struct open Exceptions open Ast open AstTds + open PrinterAst type t1 = Ast.AstSyntax.programme type t2 = Ast.AstTds.programme +let rec analyse_tds_affectable tds a modif = + match a with + | AstSyntax.Dref new_aff -> + let x = analyse_tds_affectable tds new_aff true in + AstTds.Dref(x) + + | AstSyntax.Ident n -> + begin + match (chercherGlobalement tds n) with + | None -> raise (IdentifiantNonDeclare n) + | Some info -> + begin + match (info_ast_to_info info) with + | InfoVar _ -> AstTds.Ident(info) + | InfoConst _ -> + begin + if modif then + failwith "bah non en fait" + else + AstTds.Ident(info) + end + | InfoFun _ -> failwith "t chelou bro" + end + end + + (* analyse_tds_expression : AstSyntax.expression -> AstTds.expression *) (* Paramètre tds : la table des symboles courante *) (* Paramètre e : l'expression à analyser *) @@ -34,21 +61,7 @@ let rec analyse_tds_expression tds e = (* L'identifiant ne fait pas référence à une fonction, on lève ue exception *) | _ -> raise (MauvaiseUtilisationIdentifiant str) end - | AstSyntax.Ident(str) -> - begin - match chercherGlobalement tds str with - (* L'identifiant n'est pas déclaré *) - | None -> raise (IdentifiantNonDeclare str) - | Some info -> - match (info_ast_to_info info) with - | InfoVar(_, _, _, _) -> - AstTds.Ident(info) - | InfoConst(_, v) -> - AstTds.Entier(v) - - (** L'identifiant fait référence a une fonction lors de l'association a une varibale, on lève une exception *) - | _ -> raise (MauvaiseUtilisationIdentifiant str) - end + | AstSyntax.Booleen(b) -> begin AstTds.Booleen(b) @@ -68,7 +81,20 @@ let rec analyse_tds_expression tds e = let new_expr_2 = analyse_tds_expression tds expr_2 in AstTds.Binaire(b, new_expr_1, new_expr_2) end - + | AstSyntax.Affectable(a) -> AstTds.Affectable(analyse_tds_affectable tds a false) + | AstSyntax.Null -> AstTds.Null + | AstSyntax.NewType(t) -> AstTds.NewType(t) + | AstSyntax.Adresse(n) -> + let info = chercherGlobalement tds n in + match info with + | None -> + raise (IdentifiantNonDeclare n) + | Some i -> + begin + match (info_ast_to_info i) with + | InfoVar _ -> AstTds.Adresse(i) + | _ -> raise (MauvaiseUtilisationIdentifiant(n)) + end (* analyse_tds_instruction : AstSyntax.instruction -> tds -> AstTds.instruction *) (* Paramètre tds : la table des symboles courante *) @@ -101,27 +127,33 @@ let rec analyse_tds_instruction tds i = il a donc déjà été déclaré dans le bloc courant *) raise (DoubleDeclaration n) end - | AstSyntax.Affectation (n,e) -> + | AstSyntax.Affectation (aff, e) -> begin - match chercherGlobalement tds (string_of_affectable n) with - | None -> - (* L'identifiant n'est pas trouvé dans la tds globale. *) - raise (IdentifiantNonDeclare (string_of_affectable n)) - | Some info -> - (* 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. *) + let ne = analyse_tds_expression tds e in + let n_a = analyse_tds_affectable tds a true in + match aff with + | Dref a -> + Affectation (n_a, ne) + + | Ident(n) -> begin - match info_ast_to_info info with - | InfoVar _ -> - (* Vérification de la bonne utilisation des identifiants dans l'expression *) - (* et obtention de l'expression transformée *) - let ne = analyse_tds_expression tds e in - (* 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 *) - Affectation (info, ne) - | _ -> - (* Modification d'une constante ou d'une fonction *) - raise (MauvaiseUtilisationIdentifiant (string_of_affectable n)) + match chercherGlobalement tds n with + | None -> + (* L'identifiant n'est pas trouvé dans la tds globale. *) + raise (IdentifiantNonDeclare n) + | Some info -> + (* 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. *) + begin + match info_ast_to_info info with + | InfoVar _ -> + (* 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 *) + Affectation (n_a, ne) + | _ -> + (* Modification d'une constante ou d'une fonction *) + raise (MauvaiseUtilisationIdentifiant n) + end end end | AstSyntax.Constante (n,v) -> diff --git a/src/passeTypeRat.ml b/src/passeTypeRat.ml index 3addae1..5d79a53 100644 --- a/src/passeTypeRat.ml +++ b/src/passeTypeRat.ml @@ -12,6 +12,41 @@ struct type t2 = Ast.AstType.programme +let rec analyse_type_affectable a = + match a with + | AstTds.Dref(a) -> + begin + match (analyse_type_affectable a) with + | (na, Pointeur t) -> (AstType.Dref(na), t) + | _ -> failwith "jsp koi mettre ici" + end + | AstTds.Ident(info) -> + begin + match info_ast_to_info info with + | InfoVar(_, t, _, _) -> (AstType.Ident(info), t) + | 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) *) (* Paramètre tds : la table des symboles courante *) (* Paramètre e : l'expression à analyser *) @@ -60,18 +95,22 @@ let rec analyse_type_expression e = | _, _, _ -> raise (TypeBinaireInattendu(b, t1, t2)) end - | AstTds.Ident(info) -> - begin - match (info_ast_to_info info) with - | InfoVar(_, t, _, _) -> (AstType.Ident(info), t) - | InfoConst(_, _) -> (AstType.Ident(info), Int) - | _ -> failwith "un truc chelou encore" - end - | AstTds.Booleen(b) -> (AstType.Booleen(b), Bool) | AstTds.Entier(i) -> (AstType.Entier(i), Int) + | AstTds.Affectable(a) -> + let (n_a, n_t) = analyse_type_affectable a in + (AstType.Affectable(n_a), n_t) + + | AstTds.Null -> (AstType.Null, Pointeur(Undefined)) + + | AstTds.NewType(t) -> (AstType.NewType(t), Pointeur(t)) + + | AstTds.Adresse(info) -> + let _ = modifier_type_info Int info in + (AstType.Adresse(info), Int) + (* analyse_tds_instruction : AstTds.instruction -> tds -> AstType.instruction *) (* Paramètre tds : la table des symboles courante *) (* Paramètre i : l'instruction à analyser *) @@ -89,17 +128,27 @@ let rec analyse_type_instruction opt i = raise (TypeInattendu(nt, t)) - | AstTds.Affectation (info, e) -> - begin - let (ne, nt) = analyse_type_expression e in - match (info_ast_to_info info) with - | InfoVar(_, t, _, _) -> - if est_compatible t nt then - AstType.Affectation(info, ne) - else - raise (TypeInattendu(nt, t)) - | _ -> failwith "wut" - end + | AstTds.Affectation (aff, e) -> + let (ne, nt) = analyse_type_expression e in + begin + match aff with + | Dref a -> + begin + match (analyse_type_affectable a) with + | (na, Pointeur t) -> AstType.Affectation(na, ne) + | _ -> failwith "jsp koi mettre ici" + end + | AstTds.Ident info -> + begin + match (info_ast_to_info info) with + | InfoVar(_, t, _, _) -> + if est_compatible t nt then + AstType.Affectation(AstType.Ident(info), ne) + else + raise (TypeInattendu(nt, t)) + | _ -> failwith "wut" + end + end | AstTds.Affichage e -> begin diff --git a/src/printerAst.ml b/src/printerAst.ml index 04f4f57..e51a7a4 100644 --- a/src/printerAst.ml +++ b/src/printerAst.ml @@ -50,11 +50,16 @@ struct | Equ -> "= " | Inf -> "< " + (* Conversion des affectables *) + let rec string_of_affectable a = + match a with + | AstSyntax.Dref(n) -> (string_of_affectable n) + | AstSyntax.Ident(n) -> n ^ " " + (* Conversion des expressions *) let rec string_of_expression e = match e with | AppelFonction (n,le) -> "call "^n^"("^((List.fold_right (fun i tq -> (string_of_expression i)^tq) le ""))^") " - | Ident n -> n^" " | Booleen b -> if b then "true " else "false " | Entier i -> (string_of_int i)^" " | Unaire (op,e1) -> (string_of_unaire op) ^ (string_of_expression e1)^" " diff --git a/src/tds.ml b/src/tds.ml index b967a55..919d00e 100644 --- a/src/tds.ml +++ b/src/tds.ml @@ -346,5 +346,4 @@ let%test _ = match info_ast_to_info ia with | InfoVar ("x", Rat, 10 , "LB") -> true | _ -> false - - + diff --git a/src/testPlacement.ml b/src/testPlacement.ml.bak similarity index 100% rename from src/testPlacement.ml rename to src/testPlacement.ml.bak diff --git a/src/testTDS.ml b/src/testTDS.ml.bak similarity index 100% rename from src/testTDS.ml rename to src/testTDS.ml.bak diff --git a/src/testTam.ml b/src/testTam.ml.bak similarity index 100% rename from src/testTam.ml rename to src/testTam.ml.bak diff --git a/src/testType.ml b/src/testType.ml.bak similarity index 100% rename from src/testType.ml rename to src/testType.ml.bak