Browse Source

Bug correction in typing error localisation

Phyks (Lucas Verney) 5 years ago
parent
commit
2678c933c8
15 changed files with 1391 additions and 1007 deletions
  1. 1
    2
      TODO
  2. 29
    7
      compiler.annot
  3. BIN
      compiler.cmo
  4. 1
    1
      compiler.ml
  5. 3
    0
      hello.s
  6. 31
    38
      main.annot
  7. BIN
      main.cmi
  8. BIN
      main.cmo
  9. 3
    3
      main.ml
  10. BIN
      minic++
  11. 1
    1
      test.py
  12. 1299
    932
      typer.annot
  13. BIN
      typer.cmi
  14. BIN
      typer.cmo
  15. 23
    23
      typer.ml

+ 1
- 2
TODO View File

@@ -1,5 +1,4 @@
1
-* Si main n'est pas la première fonction ?!?
2
-* Localisation pour erreurs de typage
1
+* Makefile pourri
3 2
 * Pointeurs
4 3
 * Fonctions
5 4
 * Classes

+ 29
- 7
compiler.annot View File

@@ -7611,14 +7611,14 @@ type(
7611 7611
   Ast_typing.at_decl list -> Mips.program
7612 7612
 )
7613 7613
 ident(
7614
-  def program "compiler.ml" 350 11708 11749 "compiler.ml" 1 0 -1
7614
+  def program "compiler.ml" 350 11708 11771 "compiler.ml" 1 0 -1
7615 7615
 )
7616 7616
 "compiler.ml" 349 11691 11703 "compiler.ml" 349 11691 11704
7617 7617
 type(
7618 7618
   Ast_typing.at_decl list
7619 7619
 )
7620 7620
 ident(
7621
-  def x "compiler.ml" 350 11708 11712 "compiler.ml" 350 11708 11749
7621
+  def x "compiler.ml" 350 11708 11712 "compiler.ml" 350 11708 11771
7622 7622
 )
7623 7623
 "compiler.ml" 350 11708 11712 "compiler.ml" 350 11708 11726
7624 7624
 type(
@@ -7635,21 +7635,43 @@ type(
7635 7635
 ident(
7636 7636
   int_ref mips_decl "compiler.ml" 321 10815 10819 "compiler.ml" 321 10815 10828
7637 7637
 )
7638
-"compiler.ml" 350 11708 11737 "compiler.ml" 350 11708 11747
7638
+"compiler.ml" 350 11708 11746 "compiler.ml" 350 11708 11747
7639 7639
 type(
7640
-  Mips.program
7640
+  Mips.label -> Mips.text
7641 7641
 )
7642 7642
 ident(
7643
-  int_ref empty_mips "compiler.ml" 11 134 138 "compiler.ml" 11 134 148
7643
+  ext_ref Mips.b
7644
+)
7645
+"compiler.ml" 350 11708 11748 "compiler.ml" 350 11708 11754
7646
+type(
7647
+  Mips.label
7648
+)
7649
+"compiler.ml" 350 11708 11746 "compiler.ml" 350 11708 11754
7650
+call(
7651
+  stack
7652
+)
7653
+type(
7654
+  Mips.text
7655
+)
7656
+"compiler.ml" 350 11708 11763 "compiler.ml" 350 11708 11766
7657
+type(
7658
+  Mips.data
7659
+)
7660
+ident(
7661
+  ext_ref Mips.nop
7662
+)
7663
+"compiler.ml" 350 11708 11737 "compiler.ml" 350 11708 11769
7664
+type(
7665
+  Mips.program
7644 7666
 )
7645
-"compiler.ml" 350 11708 11748 "compiler.ml" 350 11708 11749
7667
+"compiler.ml" 350 11708 11770 "compiler.ml" 350 11708 11771
7646 7668
 type(
7647 7669
   Ast_typing.at_decl list
7648 7670
 )
7649 7671
 ident(
7650 7672
   int_ref x "compiler.ml" 349 11691 11703 "compiler.ml" 349 11691 11704
7651 7673
 )
7652
-"compiler.ml" 350 11708 11712 "compiler.ml" 350 11708 11749
7674
+"compiler.ml" 350 11708 11712 "compiler.ml" 350 11708 11771
7653 7675
 call(
7654 7676
   tail
7655 7677
 )

BIN
compiler.cmo View File


+ 1
- 1
compiler.ml View File

@@ -347,4 +347,4 @@ let mips_decl x y = match y with
347 347
     (* TODO *)
348 348
 
349 349
 let program x = 
350
-    List.fold_left mips_decl empty_mips x
350
+    List.fold_left mips_decl { text = b "main"; data = nop; } x

+ 3
- 0
hello.s View File

@@ -1,4 +1,5 @@
1 1
 .text
2
+	b main
2 3
 main:
3 4
 	li $a0, 3
4 5
 	sw $a0, -8($fp)
@@ -139,10 +140,12 @@ main:
139 140
 	la $a0, string_15
140 141
 	li $v0, 4
141 142
 	syscall
143
+	b end_if_1
142 144
 else_1:
143 145
 	la $a0, string_16
144 146
 	li $v0, 4
145 147
 	syscall
148
+end_if_1:
146 149
 	li $a0, 0
147 150
 	sw $a0, -16($fp)
148 151
 	lw $a0, -16($fp)

+ 31
- 38
main.annot View File

@@ -645,7 +645,7 @@ type(
645 645
   in_channel
646 646
 )
647 647
 ident(
648
-  def f "main.ml" 55 1803 1805 "main.ml" 97 3384 3398
648
+  def f "main.ml" 55 1803 1805 "main.ml" 97 3366 3380
649 649
 )
650 650
 "main.ml" 52 1636 1650 "main.ml" 52 1636 1657
651 651
 type(
@@ -757,7 +757,7 @@ type(
757 757
   Lexing.lexbuf
758 758
 )
759 759
 ident(
760
-  def buf "main.ml" 57 1841 1843 "main.ml" 97 3384 3398
760
+  def buf "main.ml" 57 1841 1843 "main.ml" 97 3366 3380
761 761
 )
762 762
 "main.ml" 55 1803 1815 "main.ml" 55 1803 1834
763 763
 type(
@@ -1256,119 +1256,112 @@ type(
1256 1256
   string
1257 1257
 )
1258 1258
 ident(
1259
-  def e "main.ml" 95 3282 3290 "main.ml" 97 3384 3398
1259
+  def e "main.ml" 95 3288 3296 "main.ml" 97 3366 3380
1260 1260
 )
1261
-"main.ml" 92 3145 3151 "main.ml" 92 3145 3165
1261
+"main.ml" 92 3145 3167 "main.ml" 92 3145 3170
1262 1262
 type(
1263
-  exn
1264
-)
1265
-"main.ml" 95 3282 3290 "main.ml" 95 3282 3302
1266
-type(
1267
-  Lexing.position -> unit
1263
+  Lexing.position
1268 1264
 )
1269 1265
 ident(
1270
-  int_ref localisation "main.ml" 32 952 956 "main.ml" 32 952 968
1266
+  def pos "main.ml" 95 3288 3296 "main.ml" 97 3366 3380
1271 1267
 )
1272
-"main.ml" 95 3282 3304 "main.ml" 95 3282 3325
1268
+"main.ml" 92 3145 3151 "main.ml" 92 3145 3171
1273 1269
 type(
1274
-  Lexing.lexbuf -> Lexing.position
1275
-)
1276
-ident(
1277
-  ext_ref Lexing.lexeme_start_p
1270
+  exn
1278 1271
 )
1279
-"main.ml" 95 3282 3326 "main.ml" 95 3282 3329
1272
+"main.ml" 95 3288 3296 "main.ml" 95 3288 3308
1280 1273
 type(
1281
-  Lexing.lexbuf
1274
+  Lexing.position -> unit
1282 1275
 )
1283 1276
 ident(
1284
-  int_ref buf "main.ml" 55 1803 1809 "main.ml" 55 1803 1812
1285
-)
1286
-"main.ml" 95 3282 3303 "main.ml" 95 3282 3330
1287
-call(
1288
-  stack
1277
+  int_ref localisation "main.ml" 32 952 956 "main.ml" 32 952 968
1289 1278
 )
1279
+"main.ml" 95 3288 3309 "main.ml" 95 3288 3312
1290 1280
 type(
1291 1281
   Lexing.position
1292 1282
 )
1293
-"main.ml" 95 3282 3290 "main.ml" 95 3282 3330
1283
+ident(
1284
+  int_ref pos "main.ml" 92 3145 3167 "main.ml" 92 3145 3170
1285
+)
1286
+"main.ml" 95 3288 3296 "main.ml" 95 3288 3312
1294 1287
 call(
1295 1288
   stack
1296 1289
 )
1297 1290
 type(
1298 1291
   unit
1299 1292
 )
1300
-"main.ml" 96 3332 3340 "main.ml" 96 3332 3347
1293
+"main.ml" 96 3314 3322 "main.ml" 96 3314 3329
1301 1294
 type(
1302 1295
   (string -> unit, Format.formatter, unit) format -> string -> unit
1303 1296
 )
1304 1297
 ident(
1305 1298
   ext_ref Format.eprintf
1306 1299
 )
1307
-"main.ml" 96 3332 3348 "main.ml" 96 3332 3380
1300
+"main.ml" 96 3314 3330 "main.ml" 96 3314 3362
1308 1301
 type(
1309 1302
   (string -> unit, Format.formatter, unit) format
1310 1303
 )
1311
-"main.ml" 96 3332 3381 "main.ml" 96 3332 3382
1304
+"main.ml" 96 3314 3363 "main.ml" 96 3314 3364
1312 1305
 type(
1313 1306
   string
1314 1307
 )
1315 1308
 ident(
1316 1309
   int_ref e "main.ml" 92 3145 3164 "main.ml" 92 3145 3165
1317 1310
 )
1318
-"main.ml" 96 3332 3340 "main.ml" 96 3332 3382
1311
+"main.ml" 96 3314 3322 "main.ml" 96 3314 3364
1319 1312
 call(
1320 1313
   stack
1321 1314
 )
1322 1315
 type(
1323 1316
   unit
1324 1317
 )
1325
-"main.ml" 97 3384 3392 "main.ml" 97 3384 3396
1318
+"main.ml" 97 3366 3374 "main.ml" 97 3366 3378
1326 1319
 type(
1327 1320
   int -> unit
1328 1321
 )
1329 1322
 ident(
1330 1323
   ext_ref Pervasives.exit
1331 1324
 )
1332
-"main.ml" 97 3384 3397 "main.ml" 97 3384 3398
1325
+"main.ml" 97 3366 3379 "main.ml" 97 3366 3380
1333 1326
 type(
1334 1327
   int
1335 1328
 )
1336
-"main.ml" 97 3384 3392 "main.ml" 97 3384 3398
1329
+"main.ml" 97 3366 3374 "main.ml" 97 3366 3380
1337 1330
 call(
1338 1331
   stack
1339 1332
 )
1340 1333
 type(
1341 1334
   unit
1342 1335
 )
1343
-"main.ml" 96 3332 3340 "main.ml" 97 3384 3398
1336
+"main.ml" 96 3314 3322 "main.ml" 97 3366 3380
1344 1337
 type(
1345 1338
   unit
1346 1339
 )
1347
-"main.ml" 95 3282 3290 "main.ml" 97 3384 3398
1340
+"main.ml" 95 3288 3296 "main.ml" 97 3366 3380
1348 1341
 type(
1349 1342
   unit
1350 1343
 )
1351
-"main.ml" 57 1841 1843 "main.ml" 97 3384 3398
1344
+"main.ml" 57 1841 1843 "main.ml" 97 3366 3380
1352 1345
 type(
1353 1346
   unit
1354 1347
 )
1355
-"main.ml" 55 1803 1805 "main.ml" 97 3384 3398
1348
+"main.ml" 55 1803 1805 "main.ml" 97 3366 3380
1356 1349
 type(
1357 1350
   unit
1358 1351
 )
1359
-"main.ml" 52 1636 1638 "main.ml" 97 3384 3398
1352
+"main.ml" 52 1636 1638 "main.ml" 97 3366 3380
1360 1353
 type(
1361 1354
   unit
1362 1355
 )
1363
-"main.ml" 45 1406 1408 "main.ml" 97 3384 3398
1356
+"main.ml" 45 1406 1408 "main.ml" 97 3366 3380
1364 1357
 type(
1365 1358
   unit
1366 1359
 )
1367
-"main.ml" 42 1278 1280 "main.ml" 97 3384 3398
1360
+"main.ml" 42 1278 1280 "main.ml" 97 3366 3380
1368 1361
 type(
1369 1362
   unit
1370 1363
 )
1371
-"main.ml" 39 1166 1168 "main.ml" 97 3384 3398
1364
+"main.ml" 39 1166 1168 "main.ml" 97 3366 3380
1372 1365
 type(
1373 1366
   unit
1374 1367
 )

BIN
main.cmi View File


BIN
main.cmo View File


+ 3
- 3
main.ml View File

@@ -89,9 +89,9 @@ let () =
89 89
         localisation (Lexing.lexeme_start_p buf);
90 90
         eprintf "Erreur dans l'analyse syntaxique@.";
91 91
         exit 1
92
-    | Typer.Error  e ->
92
+    | Typer.Error (e, pos) ->
93 93
         (* Erreur de typage. On récupère sa position absolue et on la
94
-         * convertir en numéro de ligne *)
95
-        localisation (Lexing.lexeme_start_p buf);
94
+         * convertit en numéro de ligne *)
95
+        localisation pos;
96 96
         eprintf "Erreur durant le typage : %s@." e;
97 97
         exit 1

BIN
minic++ View File


+ 1
- 1
test.py View File

@@ -16,7 +16,7 @@ files = [i for i in os.listdir(sys.argv[1]) if i.endswith(".cpp")]
16 16
 for i in files:
17 17
     try:
18 18
         subprocess.check_call(["./minic++",
19
-                               "--parse-only",
19
+                               "--type-only",
20 20
                                sys.argv[1]+"/"+i],
21 21
                               #stdout=FNULL,
22 22
                               #stderr=FNULL)

+ 1299
- 932
typer.annot
File diff suppressed because it is too large
View File


BIN
typer.cmi View File


BIN
typer.cmo View File


+ 23
- 23
typer.ml View File

@@ -2,7 +2,7 @@
2 2
 open Ast
3 3
 open Ast_typing
4 4
 
5
-exception Error of string
5
+exception Error of string*Lexing.position
6 6
 
7 7
 let includes = ref false
8 8
 let is_main_here = ref false
@@ -62,12 +62,12 @@ let type_incr = function
62 62
     | DecrL -> ATDecrL
63 63
     | DecrR -> ATDecrR
64 64
 
65
-let rec type_expr locals = function
65
+let rec type_expr pos locals = function
66 66
     | EInt n -> ATEInt n
67
-    | Assign (e1, e2) when is_left_value e1 -> ATAssign (type_expr locals e1, type_expr locals e2)
68
-    | Assign (e1, e2) -> raise (Error "Valeur gauche attendue.")
69
-    | Op (op, e1, e2) -> ATOp (op_ast_to_atast op, type_expr locals e1, type_expr locals e2)
70
-    | UOp (uop, e) -> ATUOp (uop_ast_to_atast uop, type_expr locals e)
67
+    | Assign (e1, e2) when is_left_value e1 -> ATAssign (type_expr pos locals e1, type_expr pos locals e2)
68
+    | Assign (e1, e2) -> raise (Error ("Valeur gauche attendue.", pos))
69
+    | Op (op, e1, e2) -> ATOp (op_ast_to_atast op, type_expr pos locals e1, type_expr pos locals e2)
70
+    | UOp (uop, e) -> ATUOp (uop_ast_to_atast uop, type_expr pos locals e)
71 71
     | EQident qident -> begin
72 72
         match qident with
73 73
         | Ident ident ->
@@ -77,58 +77,58 @@ let rec type_expr locals = function
77 77
                     if Hashtbl.mem globals (ATVIdent ident) then
78 78
                         ATEQident (ATIdent ident, false)
79 79
                     else
80
-                        raise (Error ("Unbound variable "^ident))
80
+                        raise (Error ("Unbound variable "^ident, pos))
81 81
         | Tident (tid1, tid2) -> assert false
82 82
     end
83
-    | Incr (incr, expr) when is_left_value expr -> ATIncr (type_incr incr, type_expr locals expr)
84
-    | Incr (incr, expr) -> raise (Error "Valeur gauche attendue.")
83
+    | Incr (incr, expr) when is_left_value expr -> ATIncr (type_incr incr, type_expr pos locals expr)
84
+    | Incr (incr, expr) -> raise (Error ("Valeur gauche attendue.", pos))
85 85
     | ETrue -> ATEInt 1
86 86
     | EFalse -> ATEInt 0
87 87
     | _ -> assert false
88 88
     (* TODO *)
89 89
 
90
-let type_expr_string locals = function
90
+let type_expr_string pos locals = function
91 91
     | String s -> ATString s
92
-    | Expr e -> ATExpr (type_expr locals e)
92
+    | Expr e -> ATExpr (type_expr pos locals e)
93 93
 
94 94
 let rec type_instruction locals x = match x.instruction_content with
95 95
     | Nop -> ATNop
96 96
     | Cout expr ->
97 97
             if !includes then
98
-                ATCout (List.map (type_expr_string locals) expr)
98
+                ATCout (List.map (type_expr_string (fst x.instruction_loc) locals) expr)
99 99
             else
100
-                raise (Error "iostream not included.")
101
-    | IExpr expr -> ATIExpr (type_expr locals expr)
100
+                raise (Error ("iostream not included.", (fst x.instruction_loc)))
101
+    | IExpr expr -> ATIExpr (type_expr (fst x.instruction_loc) locals expr)
102 102
     | Return some_expr -> begin
103 103
         match some_expr with
104 104
             | None -> ATReturn None
105
-            | Some expr -> ATReturn (Some (type_expr locals expr))
105
+            | Some expr -> ATReturn (Some (type_expr (fst x.instruction_loc) locals expr))
106 106
     end
107 107
     | IVar (ast_type, VIdent ident, assign) -> begin
108 108
         if Hashtbl.mem locals (ATVIdent ident) then
109
-            raise (Error ("redeclaration of "^ident));
109
+            raise (Error ("redeclaration of "^ident, (fst x.instruction_loc)));
110 110
 
111 111
         Hashtbl.add locals (ATVIdent ident) (4 * Hashtbl.length locals);
112 112
         match assign with
113 113
         | NoAssign -> ATIVar (ATVIdent ident, ATNoAssign)
114
-        | SAExpr e -> ATIVar (ATVIdent ident, ATSAExpr (type_expr locals e))
114
+        | SAExpr e -> ATIVar (ATVIdent ident, ATSAExpr (type_expr (fst x.instruction_loc) locals e))
115 115
         | SATident (ident, expr_list) -> assert false (* TODO *)
116 116
     end
117 117
     | IVar (ast_type, ident, assign) -> assert false (* TODO *)
118
-    | If (e, instr) -> let if_locals = Hashtbl.copy locals in ATIfElse (type_expr if_locals e, type_instruction if_locals instr, ATNop, if_locals)
119
-    | IfElse (e, instr1, instr2)  -> let if_locals = Hashtbl.copy locals in ATIfElse (type_expr if_locals e, type_instruction if_locals instr1, type_instruction if_locals instr2, if_locals)
118
+    | If (e, instr) -> let if_locals = Hashtbl.copy locals in ATIfElse (type_expr (fst x.instruction_loc) if_locals e, type_instruction if_locals instr, ATNop, if_locals)
119
+    | IfElse (e, instr1, instr2)  -> let if_locals = Hashtbl.copy locals in ATIfElse (type_expr (fst x.instruction_loc) if_locals e, type_instruction if_locals instr1, type_instruction if_locals instr2, if_locals)
120 120
     | IBloc bloc -> let bloc_locals = Hashtbl.copy locals in ATIBloc (type_bloc bloc_locals bloc, bloc_locals)
121
-    | While (e, instr) -> let while_locals = Hashtbl.copy locals in ATWhile (type_expr while_locals e, type_instruction while_locals instr, while_locals)
121
+    | While (e, instr) -> let while_locals = Hashtbl.copy locals in ATWhile (type_expr (fst x.instruction_loc) while_locals e, type_instruction while_locals instr, while_locals)
122 122
     | For (e1, se2, e3, i) ->
123 123
             let for_locals = Hashtbl.copy locals in
124 124
             let some_expr2 = 
125 125
                 match se2 with
126 126
                 | None -> ATEInt 1
127
-                | Some expr -> type_expr for_locals expr
127
+                | Some expr -> type_expr (fst x.instruction_loc) for_locals expr
128 128
             in
129 129
 
130
-            let expr1 = List.map (type_expr for_locals) e1 in
131
-            let expr3 = List.map (type_expr for_locals) e3 in
130
+            let expr1 = List.map (type_expr (fst x.instruction_loc) for_locals) e1 in
131
+            let expr3 = List.map (type_expr (fst x.instruction_loc) for_locals) e3 in
132 132
 
133 133
             ATFor(expr1, some_expr2, expr3, type_instruction for_locals i, for_locals)
134 134