feat: debut des pointeurs

This commit is contained in:
Guillotin Damien 2021-12-11 12:30:54 +01:00
parent a1fa41107b
commit ba9e13e5b8
3 changed files with 24 additions and 6 deletions

View file

@ -5,6 +5,7 @@ module type Ast =
sig sig
type expression type expression
type instruction type instruction
type affectable
type fonction type fonction
type programme type programme
end end
@ -22,6 +23,12 @@ type unaire = Numerateur | Denominateur
(* Opérateurs binaires de Rat *) (* Opérateurs binaires de Rat *)
type binaire = Fraction | Plus | Mult | Equ | Inf type binaire = Fraction | Plus | Mult | Equ | Inf
type affectable =
(* Dé-référencement d'un affectable *)
| Dref of affectable
(* Identifiant *)
| Ident of string
(* Expressions de Rat *) (* Expressions de Rat *)
type expression = type expression =
(* Appel de fonction représenté par le nom de la fonction et la liste des paramètres réels *) (* Appel de fonction représenté par le nom de la fonction et la liste des paramètres réels *)
@ -36,14 +43,22 @@ type expression =
| Unaire of unaire * expression | Unaire of unaire * expression
(* Opération binaire représentée par l'opérateur, l'opérande gauche et l'opérande droite *) (* Opération binaire représentée par l'opérateur, l'opérande gauche et l'opérande droite *)
| Binaire of binaire * expression * expression | Binaire of binaire * expression * expression
(* Affectable représenté par son affectable *)
| Affectable of affectable
(* Null *)
| Null
(* Nouveau type *)
| NewType of typ
(* Adresse *)
| Adresse of string
(* Instructions de Rat *) (* Instructions de Rat *)
type bloc = instruction list type bloc = instruction list
and instruction = and instruction =
(* Déclaration de variable représentée par son type, son nom et l'expression d'initialisation *) (* Déclaration de variable représentée par son type, son nom et l'expression d'initialisation *)
| Declaration of typ * string * expression | Declaration of typ * string * expression
(* Affectation d'une variable représentée par son nom et la nouvelle valeur affectée *) (* Affectation d'un affectable à une expression *)
| Affectation of string * expression | Affectation of affectable * expression
(* Déclaration d'une constante représentée par son nom et sa valeur (entier) *) (* Déclaration d'une constante représentée par son nom et sa valeur (entier) *)
| Constante of string * int | Constante of string * int
(* Affichage d'une expression *) (* Affichage d'une expression *)

View file

@ -1,18 +1,20 @@
type typ = Bool | Int | Rat | Undefined type typ = Bool | Int | Rat | Pointeur of typ | Undefined
let string_of_type t = let rec string_of_type t =
match t with match t with
| Bool -> "Bool" | Bool -> "Bool"
| Int -> "Int" | Int -> "Int"
| Rat -> "Rat" | Rat -> "Rat"
| Pointeur(t_p) -> "Poi " ^ string_of_type t_p
| Undefined -> "Undefined" | Undefined -> "Undefined"
let est_compatible t1 t2 = let rec est_compatible t1 t2 =
match t1, t2 with match t1, t2 with
| Bool, Bool -> true | Bool, Bool -> true
| Int, Int -> true | Int, Int -> true
| Rat, Rat -> true | Rat, Rat -> true
| Pointeur(t_p1), Pointeur(t_p2) -> est_compatible t_p1 t_p2
| _ -> false | _ -> false
let%test _ = est_compatible Bool Bool let%test _ = est_compatible Bool Bool
@ -50,6 +52,7 @@ let getTaille t =
| Int -> 1 | Int -> 1
| Bool -> 1 | Bool -> 1
| Rat -> 2 | Rat -> 2
| Pointeur(_) -> 1
| Undefined -> 0 | Undefined -> 0
let%test _ = getTaille Int = 1 let%test _ = getTaille Int = 1

View file

@ -1,5 +1,5 @@
(* Types manipulés dans Rat *) (* Types manipulés dans Rat *)
type typ = Bool | Int | Rat | Undefined type typ = Bool | Int | Rat | Pointeur of typ | Undefined
(* string_of_type : typ -> string *) (* string_of_type : typ -> string *)
(* transforme un typ en chaîne de caractère *) (* transforme un typ en chaîne de caractère *)