-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbre.ml
550 lines (457 loc) · 14.7 KB
/
arbre.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
let oc = open_out "prog.c"
let indent = ref 0
let indent_func = ref 0
let includeprintf = ref false
let nameFunc = ref ""
let typeFunc = ref ""
let hash_table = Hashtbl.create 100;;
let hash_table_fonc = Hashtbl.create 100;;
let incr v = v:=!v+1;;
let decr v = v:=!v-1;;
type t_decl = {
_name : string;
_type : string;
};;
type t_terminal =
| Ident of string
| Integer of string
| Double of string
| String of string
| True
| False
| As
| To
| Step
| Empty
;;
type t_operateur =
| Lesser
| Greater
| Equal
| Notequal
| Lessequal
| Greaterequal
| Empty
;;
type t_condition=
| Conditionnelle of t_terminal * t_operateur * t_terminal
;;
type t_math =
| Minus
| Plus
| Mul
| Div
;;
type t_type =
| Tint of string
| Tdouble of string
| Tstring of string
| Empty
;;
type t_instr =
| SCom of string
| MCom of string
| DimMult of string list * t_type
| DimAffect of string * t_type * string
| Decl of string * t_type
| Affect of string * string
| Print of t_terminal
| Func of string
| If of t_condition
| ElseIf of t_condition
| Then
| Else
| EndIf
| While of t_condition
| Wend
| Do
| Loop
| DoWhile of t_condition
| For of t_terminal * t_terminal * t_type * t_terminal * t_terminal * t_terminal * t_terminal * t_math * t_terminal
| Next
| Empty
;;
type t_fonc =
| Sub of string
| EndSub
| Function of string * string
| Return of t_terminal
| EndFunc
| SCom_fonc of string
| MCom_fonc of string
| DimMult_fonc of string list * t_type
| DimAffect_fonc of string * t_type * string
| Decl_fonc of string * t_type
| Declf of string * t_type
| DeclE
| Affect_fonc of string * string
| Print_fonc of t_terminal
| Func_fonc of string
| If_fonc of t_condition
| ElseIf_fonc of t_condition
| Then_fonc
| Else_fonc
| EndIf_fonc
| While_fonc of t_condition
| Wend_fonc
| Do_fonc
| Loop_fonc
| DoWhile_fonc of t_condition
| For_fonc of t_terminal * t_terminal * t_type * t_terminal * t_terminal * t_terminal * t_terminal * t_math* t_terminal
| Next_fonc
| Empty
;;
type t_import=
| Include of string
;;
type t_prog = {
mutable struct_instr : t_instr list;
mutable struct_fonc : t_fonc list;
mutable struct_import: t_import list;
};;
let set_prog_list a b c = {struct_instr = a ; struct_fonc = b; struct_import = c};;
let indentation = function () -> for i=0 to !indent do output_string oc "\t" done;;
let indentation_fonc = function () -> for i=1 to !indent_func do output_string oc "\t" done;;
let return_type = function
| Tint(v) -> v;
| Tdouble(v) -> v;
| Tstring(v) -> v;
| Empty -> "";
;;
let return_terminal = function
| Ident(id) -> id;
| Double(v) -> v;
| Integer(v) -> v;
| String(v) -> v;
| True -> "true";
| False -> "false";
| As -> "";
| To -> "";
| Step -> "";
| Empty -> "";
;;
let return_type_terminal = function
| Ident(id) -> id;
| Double(v) -> "double";
| Integer(v) -> "int";
| String(v) -> "char*";
| True -> "true";
| False -> "false";
| As -> "";
| To -> "";
| Step -> "";
| Empty -> "";
;;
let return_math = function
| Minus -> "-";
| Plus -> "+";
| Mul -> "*";
| Div -> "/";
;;
let rec hasher = function
| (t, hd::[]) -> Hashtbl.add hash_table hd (return_type t);
| (t, hd::tl) -> Hashtbl.add hash_table hd (return_type t); hasher(t, tl);
| (t, []) -> ();
;;
let verifTypeIdent typ retour = match Hashtbl.find hash_table_fonc retour with
| x when x = typ -> ();
| _ -> print_string("[Erreur] La fonction \""^(!nameFunc)^"\" devrait retourner : "^typ^"\n");
;;
let verifType typ retour = match retour with
| "int" when "int" = typ -> ();
| "int" when "int" <> typ -> print_string("[Erreur] La fonction \""^(!nameFunc)^"\" devrait retourner : "^typ^"\n");
| "double" when "double" = typ -> ();
| "double" when "double" <> typ -> print_string("[Erreur] La fonction \""^(!nameFunc)^"\" devrait retourner : "^typ^"\n");
| "char*" when "char*" = typ -> ();
| "char*" when "char*" <> typ -> print_string("[Erreur] La fonction \""^(!nameFunc)^"\" devrait retourner : "^typ^"\n");
| _ -> verifTypeIdent typ retour;
;;
let rec print_terminal t = match t with
| Ident(id) -> output_string oc (id);
| Double(v) -> output_string oc (v);
| Integer(v) -> output_string oc (v);
| String(v) -> output_string oc (v);
| True -> output_string oc ("true");
| False -> output_string oc ("false");
| Empty -> ();
| _ -> ();
and printf_terminal t = match t with
| Ident(id) -> print_ident id hash_table;
| Double(v) -> output_string oc ("printf(\"%lf\\n\", "^v^");\n");
| Integer(v) -> output_string oc ("printf(\"%d\\n\", "^v^");\n");
| String(v) -> output_string oc ("printf(\"%s\\n\", "^v^");\n");
| Empty -> ();
| _ -> ();
and printf_terminal_fonc t = match t with
| Ident(id) -> print_ident id hash_table_fonc;
| Double(v) -> output_string oc ("printf(\"%lf\\n\", "^v^");\n");
| Integer(v) -> output_string oc ("printf(\"%d\\n\", "^v^");\n");
| String(v) -> output_string oc ("printf(\"%s\\n\", "^v^");\n");
| Empty -> ();
| _ -> ();
and print_ident id hash = match Hashtbl.find hash id with
|"int" -> output_string oc ("printf(\"%d\\n\","^id^");\n");
|"double"-> output_string oc ("printf(\"%lf\\n\","^id^");\n");
|"char*"-> output_string oc ("printf(\"%s\\n\","^id^");\n");
| _ -> ();
and print_operateur = function
| Lesser -> output_string oc("<");
| Greater -> output_string oc(">");
| Equal -> output_string oc("==");
| Notequal -> output_string oc("!=");
| Lessequal -> output_string oc("<=");
| Greaterequal -> output_string oc(">=");
| Empty -> ();
and print_condition = function
| Conditionnelle(t_terma, t_ope ,t_termb) -> output_string oc ("("); print_terminal(t_terma);print_operateur(t_ope);print_terminal(t_termb); output_string oc (")");
and print_math = function
| Minus -> output_string oc ("-");
| Plus -> output_string oc ("+");
| Mul -> output_string oc ("*");
| Div -> output_string oc ("/");
and print_type = function
| Tint(v) -> output_string oc (v^" ");
| Tdouble(v) -> output_string oc (v^" ");
| Tstring(v) -> output_string oc (v^" ");
| Empty -> ();
;;
let rec print_instr structprog = match structprog with
| SCom(com)::tl -> indentation ();
output_string oc ("// "^(String.sub com 1 ((String.length com)-1))^"\n");
print_instr tl;
| MCom(com)::tl -> indentation ();
output_string oc ("/* "^(String.sub com 2 ((String.length com)-4))^"*/\n");
print_instr tl;
| DimMult(vars, typ)::tl -> hasher(typ, vars);
indentation ();
output_string oc ((return_type typ)^" "^(String.concat ", " vars)^";\n");
print_instr tl;
| DimAffect(id, typ, v)::tl -> Hashtbl.add hash_table id (return_type typ);
indentation ();
output_string oc ((return_type typ)^" "^id^" = "^v^";\n");
print_instr tl;
| Decl(id, typ)::tl -> Hashtbl.add hash_table id (return_type typ);
indentation ();
output_string oc ((return_type typ)^" "^id^";\n");
print_instr tl;
| Affect(id, v)::tl -> indentation();
output_string oc (id^" = "^v^";\n");
print_instr tl;
| Print(print)::tl -> indentation ();
printf_terminal(print);
print_instr tl;
| Func(f)::tl -> indentation ();
output_string oc (f^";\n");
print_instr tl;
| If(cond)::tl -> indentation ();
incr(indent);
output_string oc ("if ");
print_condition(cond);
print_instr tl;
| ElseIf(cond)::tl -> decr(indent);
indentation ();
incr(indent);
output_string oc ("}else if ");
print_condition(cond);
print_instr tl;
| Then::tl -> output_string oc(" { \n");
print_instr tl;
| Else::tl-> decr(indent);
indentation ();
output_string oc("} else { \n");
incr(indent);
print_instr tl;
| EndIf::tl -> decr(indent);
indentation ();
output_string oc("}\n");
print_instr tl;
| While(cond)::tl -> indentation ();
incr(indent);
output_string oc ("while ");
print_condition(cond);
output_string oc ("{ \n");
print_instr tl;
| Wend::tl -> decr(indent);
indentation ();
output_string oc("}\n");
print_instr tl;
| Do::tl -> indentation ();
incr(indent);
output_string oc("do {\n");
print_instr tl;
| DoWhile(cond)::tl -> output_string oc ("while ");
print_condition(cond);
output_string oc (";\n\n");
print_instr tl;
| Loop::tl -> decr(indent);
indentation ();
output_string oc ("} ");
print_instr tl;
| For(t_a,t_b,typ,t_d,t_e,t_f,t_g,math,t_i)::tl -> indentation ();
incr(indent);
output_string oc("for (");
print_type(typ);
print_terminal(t_a);
output_string oc("=");
print_terminal(t_d);
output_string oc (";");
print_terminal(t_a);
output_string oc (" <= ");
print_terminal(t_f);
output_string oc (";");
print_terminal(t_a);
print_math(math);
output_string oc("=");
print_terminal(t_i);
output_string oc("){\n");
print_instr tl;
| Next::tl -> decr(indent);
indentation ();
output_string oc ("}\n");
print_instr tl;
| Empty::tl -> print_instr tl;
| [] -> ();
;;
let rec print_fonc structprog = match structprog with
| SCom_fonc(com)::tl -> indentation_fonc ();
output_string oc ("// "^(String.sub com 1 ((String.length com)-1))^"\n");
print_fonc tl;
| MCom_fonc(com)::tl -> indentation_fonc ();
output_string oc ("/* "^(String.sub com 2 ((String.length com)-4))^"*/\n");
print_fonc tl;
| DimMult_fonc(vars, typ)::tl -> hasher(typ, vars);
indentation_fonc ();
output_string oc ((return_type typ)^" "^(String.concat ", " vars)^";\n");
print_fonc tl;
| DimAffect_fonc(id, typ, v)::tl -> Hashtbl.add hash_table_fonc id (return_type typ);
indentation_fonc ();
output_string oc ((return_type typ)^" "^id^" = "^v^";\n");
print_fonc tl;
| Decl_fonc(id, typ)::tl -> Hashtbl.add hash_table_fonc id (return_type typ);
output_string oc ((return_type typ)^" "^id^", ");
print_fonc tl;
| Declf(id, typ)::tl -> Hashtbl.add hash_table_fonc id (return_type typ);
output_string oc ((return_type typ)^" "^id^") {\n");
print_fonc tl;
| DeclE::tl -> output_string oc (") {\n");
print_fonc tl;
| Affect_fonc(id, v)::tl -> indentation_fonc ();
output_string oc (id^" = "^v^";\n");
print_fonc tl;
| Sub(nom)::tl -> indentation_fonc ();
incr(indent_func);
output_string oc ("\nvoid "^nom^"(");
print_fonc tl;
| EndSub::tl -> decr(indent_func);
indentation_fonc ();
output_string oc ("}\n\n");
print_fonc tl;
| Function(nom,type_retour)::tl -> typeFunc := type_retour;
nameFunc := nom;
indentation_fonc ();
incr(indent_func);
output_string oc ("\n"^type_retour^" "^nom^"(");
print_fonc tl;
| Return(retour)::tl -> verifType !typeFunc (return_type_terminal retour);
indentation_fonc ();
output_string oc ("return ");
print_terminal(retour);
output_string oc (";\n");
print_fonc tl;
| EndFunc::tl -> decr(indent_func);
indentation_fonc ();
output_string oc ("}\n\n");
print_fonc tl;
| Print_fonc(print)::tl -> indentation_fonc ();
printf_terminal_fonc(print);
print_fonc tl;
| Func_fonc(f)::tl -> indentation_fonc ();
output_string oc (f^";\n");
print_fonc tl;
| If_fonc(cond)::tl -> indentation_fonc ();
incr(indent_func);
output_string oc ("if ");
print_condition(cond);
print_fonc tl;
| ElseIf_fonc(cond)::tl -> decr(indent_func);
indentation_fonc ();
incr(indent_func);
output_string oc ("}else if ");
print_condition(cond);
print_fonc tl;
| Then_fonc::tl -> output_string oc(" { \n");
print_fonc tl;
| Else_fonc::tl-> decr(indent_func);
indentation_fonc ();
output_string oc("} else { \n");
incr(indent);
print_fonc tl;
| EndIf_fonc::tl -> decr(indent_func);
indentation_fonc ();
output_string oc("}\n");
print_fonc tl;
| While_fonc(cond)::tl -> indentation_fonc ();
incr(indent_func);
output_string oc ("while ");
print_condition(cond);
output_string oc ("{ \n");
print_fonc tl;
| Wend_fonc::tl -> decr(indent_func);
indentation_fonc ();
output_string oc("}\n");
print_fonc tl;
| Do_fonc::tl -> indentation_fonc ();
incr(indent_func);
output_string oc("do {\n");
print_fonc tl;
| DoWhile_fonc(cond)::tl -> output_string oc ("while ");
print_condition(cond);
output_string oc (";\n\n");
print_fonc tl;
| Loop_fonc::tl -> decr(indent_func);
indentation_fonc ();
output_string oc ("} ");
print_fonc tl;
| For_fonc(t_a,t_b,typ,t_d,t_e,t_f,t_g,math,t_i)::tl -> indentation_fonc ();
incr(indent_func);
output_string oc("for (");
print_type(typ);
print_terminal(t_a);
output_string oc("=");
print_terminal(t_d);
output_string oc (";");
print_terminal(t_a);
output_string oc (" <= ");
print_terminal(t_f);
output_string oc (";");
print_terminal(t_a);
print_math(math);
output_string oc("=");
print_terminal(t_i);
output_string oc("){\n");
print_fonc tl;
| Next_fonc::tl -> decr(indent_func);
indentation_fonc ();
output_string oc ("}\n");
print_fonc tl;
| Empty::tl -> print_fonc tl;
| [] -> ();
;;
let rec print_import structprog = match structprog with
| Include(print)::tl -> output_string oc (print^"\n"); print_import tl;
| [] -> if (!includeprintf=true) then output_string oc ("#include <stdio.h>\n");
;;
let rec lookingForIncludes structprog = match structprog with
| Print(print)::tl-> includeprintf:=true; lookingForIncludes tl;
| [] -> ();
| _::tl -> lookingForIncludes tl;
;;
let print_prog prog =
lookingForIncludes prog.struct_instr;
print_import prog.struct_import;
print_fonc prog.struct_fonc;
output_string oc "\nint main(){\n\n";
print_instr prog.struct_instr;
indentation(); output_string oc "return 0;\n";
output_string oc "}\n\n";
;;