This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterAst.ml
114 lines (94 loc) · 4.49 KB
/
printerAst.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
open Ast
open Type
(* Interface d'affichage des arbres abstraits *)
module type PrinterAst =
sig
module A:Ast
(* string_of_expression : expression -> string *)
(* transforme une expression en chaîne de caractère *)
val string_of_expression : A.expression -> string
(* string_of_instruction : instruction -> string *)
(* transforme une instruction en chaîne de caractère *)
val string_of_instruction : A.instruction -> string
(* string_of_fonction : fonction -> string *)
(* transforme une fonction en chaîne de caractère *)
val string_of_fonction : A.fonction -> string
(* string_of_ast : ast -> string *)
(* transforme un ast en chaîne de caractère *)
val string_of_programme : A.programme -> string
(* print_ast : ast -> unit *)
(* affiche un ast *)
val print_programme : A.programme -> unit
end
(*Module d'affiche des AST issus de la phase d'analyse syntaxique *)
module PrinterAstSyntax : PrinterAst with module A = AstSyntax =
struct
module A = AstSyntax
open A
(* Conversion des opérateurs unaires *)
let string_of_unaire op =
match op with
| Numerateur -> "num "
| Denominateur -> "denom "
(* Conversion des opérateurs binaires *)
let string_of_binaire b =
match b with
| Fraction -> "/ " (* not used *)
| Plus -> "+ "
| Mult -> "* "
| Equ -> "= "
| Inf -> "< "
let rec string_of_affectable aff =
match aff with
| Ident n -> n
| Deref a -> "*"^(string_of_affectable a)
| Acces (a, c) -> (string_of_affectable a)^"."^c
(* 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 ""))^") "
| Affectable (aff) -> (string_of_affectable aff)^" "
| 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)^" "
| Binaire (b,e1,e2) ->
begin
match b with
| Fraction -> "["^(string_of_expression e1)^"/"^(string_of_expression e2)^"] "
| _ -> (string_of_expression e1)^(string_of_binaire b)^(string_of_expression e2)^" "
end
| Null -> "null "
| Adresse (n) -> "&"^n^" "
| New (t) -> "new "^(string_of_type t)^" "
| StructExpr le -> "{"^((List.fold_right (fun i tq -> (string_of_expression i)^tq) le ""))^"} "
(* Conversion des instructions *)
let rec string_of_instruction i =
match i with
| Declaration (t, n, e) -> "Declaration : "^(string_of_type t)^" "^n^" = "^(string_of_expression e)^"\n"
| Affectation (n,e) -> "Affectation : "^(string_of_affectable n)^" = "^(string_of_expression e)^"\n"
| Constante (n,i) -> "Constante : "^n^" = "^(string_of_int i)^"\n"
| Affichage e -> "Affichage : "^(string_of_expression e)^"\n"
| Conditionnelle (c,t,e) -> "Conditionnelle : IF "^(string_of_expression c)^"\n"^
"THEN \n"^((List.fold_right (fun i tq -> (string_of_instruction i)^tq) t ""))^
"ELSE \n"^((List.fold_right (fun i tq -> (string_of_instruction i)^tq) e ""))^"\n"
| TantQue (c,b) -> "TantQue : TQ "^(string_of_expression c)^"\n"^
"FAIRE \n"^((List.fold_right (fun i tq -> (string_of_instruction i)^tq) b ""))^"\n"
| Retour (e) -> "Retour : RETURN "^(string_of_expression e)^"\n"
| TypedefLocal (n,t) -> "TypedefLocal : " ^ n ^ " = " ^ (string_of_type t) ^ "\n"
| AddAff (a, e) -> "AddAff : " ^ (string_of_affectable a) ^ " += " ^ (string_of_expression e) ^ "\n"
(* Conversion des fonctions *)
let string_of_fonction (Fonction(t,n,lp,li)) = (string_of_type t)^" "^n^" ("^((List.fold_right (fun (t,n) tq -> (string_of_type t)^" "^n^" "^tq) lp ""))^") = \n"^
((List.fold_right (fun i tq -> (string_of_instruction i)^tq) li ""))^"\n"
(* Conversion des typedefs *)
let string_of_typedef (TypedefGlobal(n, t)) = "typedef " ^ n ^ " = " ^ (string_of_type t) ^ "\n"
(* Conversion d'un programme Rat *)
let string_of_programme (Programme (typedefs, fonctions, instruction)) =
(List.fold_right (fun td tq -> (string_of_typedef td)^tq) typedefs "")^
(List.fold_right (fun f tq -> (string_of_fonction f)^tq) fonctions "")^
(List.fold_right (fun i tq -> (string_of_instruction i)^tq) instruction "")
(* Affichage d'un programme Rat *)
let print_programme programme =
print_string "AST : \n";
print_string (string_of_programme programme);
flush_all ()
end