8000 Typing: add throw and try statement checking · compiler-experts/miniJava@cbe079e · GitHub
[go: up one dir, main page]

Skip to content

Commit cbe079e

Browse files
committed
Typing: add throw and try statement checking
1 parent 29f7958 commit cbe079e

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ If you are a team member of the project, please review the [Guidelines for Contr
140140
- attributes: a `Hashtbl` that maps from attribute name to attribute type (declared type)
141141
- parent: a class reference type that refers to its class
142142
- [x] create a `Hashtbl` that maps from class
143-
- [ ] The second phase is concerned with ve 8000 rifying that the inside of classes is correct (mainly the body of methods). She will also make sure of the correction of the higher level expression.
143+
- [x] The second phase is concerned with verifying that the inside of classes is correct (mainly the body of methods). She will also make sure of the correction of the higher level expression.
144144
- [x] create 3 verification method that verifies the following aspects of the program
145145
- [x] `verify_methods` that checks the type of methods
146146
- [x] create a local definition environment type called `current_env` it contains 3 fields as follows
@@ -150,18 +150,18 @@ If you are a team member of the project, please review the [Guidelines for Contr
150150
- env_type: a string that identifies the type of the local definition environment, it could be `constructor`, `method` or `attribute`, in this case, the `env_type` is `method`
151151
- [x] write a verification method (`verify_declared_args`) that checks the declared type of variables in the method arguments
152152
- [x] check if there exists Duplicate Local Variable
153-
- [ ] write a verification method (`verify_statement`) that checks the body of the method
153+
- [x] write a verification method (`verify_statement`) that checks the body of the method
154154
- [x] check declared variables
155155
- [x] check block of statement
156156
- [x] check expression
157157
- [x] check return statement when it's none, ex: `return;`
158158
- [x] check return statement when it's not none, ex: `return x;`
159-
- [ ] check throw statement
159+
- [x] check throw statement
160160
- [x] check while statement
161161
- [x] check if statement when it doesn't have `else`
162162
- [x] check if statement when it has `else`
163163
- [x] check for statement
164-
- [ ] check try statement
164+
- [x] check try statement
165165
- [x] `verify_constructors` that checks the type of constructors
166166
- [x] `verify_attributes` that checks the type of attributes
167167

@@ -213,6 +213,7 @@ If you are a team member of the project, please review the [Guidelines for Contr
213213
- errors related to overloading
214214
- errors related to overriding
215215

216+
216217
#### Execution
217218

218219
Evaluation and execute by certain means

phase2/Main/Compile.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ let execute lexbuf verbose =
44
try
55
let ast = compilationUnit Lexer.token lexbuf in
66
print_endline "successfull parsing";
7-
87
if verbose then AST.print_program ast;
98
print_endline "===================";
109
Typing.typing ast;

phase2/Typing/Typing.ml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ open Type
44

55
type func_info = {
66
ftype: Type.t;
7-
fargs: argument list
7+
fargs: argument list;
8+
fthrows: Type.ref_type list
89
}
910

1011
type curr_env = {
@@ -348,7 +349,7 @@ let rec verify_statement current_env envs statement =
348349
then raise(IncompatibleTypes((stringOf actual_t)^" cannot be converted to "^(stringOf declared_t)))
349350
| _, None -> raise(InvalidMethodDeclaration("return type required"))
350351
)
351-
| Throw e -> () (*TODO*)
352+
| Throw e -> verify_expression envs current_env e
352353
| While(e,s) -> (verify_expression envs current_env e;
353354
verify_actual_type_with_boolean e "unknow type in while condition");
354355
verify_statement current_env envs s
@@ -380,7 +381,19 @@ let rec verify_statement current_env envs statement =
380381
);
381382
List.iter (verify_expression envs for_env) el;
382383
verify_statement for_env envs s;
383-
| Try(body,catch,finally) -> () (*TODO*)
384+
| Try(body,catch,finally) ->
385+
let catch_env = { returntype = current_env.returntype;
386+
variables = Hashtbl.copy current_env.variables;
387+
this_class = current_env.this_class;
388+
env_type = current_env.env_type} in
389+
List.iter (verify_statement current_env envs) body;
390+
List.iter (fun (a,sl) ->
391+
verify_declared_args catch_env a;
392+
List.iter (verify_statement catch_env envs) sl) catch;
393+
(if finally != [] then
394+
begin
395+
List.iter (verify_statement current_env envs) finally
396+
end)
384397

385398
(* check type for constructors *)
386399
let verify_constructors envs current_class consts =
@@ -394,7 +407,8 @@ let verify_constructors envs current_class consts =
394407
this_class = current_class;
395408
env_type = "constructor"} in
396409
List.iter (verify_declared_args current_env) consts.cargstype;
397-
List.iter (verify_statement current_env envs) consts.cbody (*TODO: I am constructor*)
410+
List.iter (verify_statement current_env envs) consts.cbody
411+
(* TODO: List.iter (verify_throws current_env envs) consts.mthrows*)
398412

399413
let verify_methods envs current_class meths =
400414
let current_env = {
@@ -404,6 +418,7 @@ let verify_methods envs current_class meths =
404418
env_type = "method"} in
405419
List.iter (verify_declared_args current_env) meths.margstype;
406420
List.iter (verify_statement current_env envs) meths.mbody
421+
(* TODO: List.iter (verify_throws current_env envs) meths.mthrows*)
407422

408423
(* check the type of the attibutes *)
409424
let verify_attributes envs current_class attrs =
@@ -440,7 +455,8 @@ let add_constructors env current_class consts =
440455
(* print_const " " consts; *)
441456
Hashtbl.add consts_table consts.cname
442457
{ftype = Type.Ref {tpath=[]; tid=current_class};
443-
fargs = consts.cargstype }
458+
fargs = consts.cargstype;
459+
fthrows = consts.cthrows }
444460
)
445461
else raise(ConstructorAlreadyExists(consts.cname))
446462

@@ -452,7 +468,8 @@ let add_methods env current_class meths =
452468
(* print_method " " meths; *)
453469
Hashtbl.add meths_table meths.mname
454470
{ftype = meths.mreturntype;
455-
fargs = meths.margstype }
471+
fargs = meths.margstype;
472+
fthrows = meths.mthrows }
456473
)
457474
(*TODO: check override and overload*)
458475
else raise(MethodAlreadyExists(meths.mname))

phase2/tests/TestsUnitairesTypingStatement.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// import java.util.EmptyStackException;
2+
3+
class EmptyStackException{}
4+
15
class I {
26
boolean a = (56 == 5);
37
float b = 8.5f;
@@ -77,4 +81,22 @@ private void testWhile(){
7781
i++;
7882
}
7983
}
84+
85+
// check throws, it has error but it will pas type checking
86+
public void testThrow() throws NullPointerException {
87+
int i = 0;
88+
if (i == 1) throw new EmptyStackException();
89+
}
90+
91+
public void testTry() {
92+
try {
93+
testThrow();
94+
}
95+
catch(EmptyStackException e) {
96+
int i = 0;
97+
}
98+
finally {
99+
int j = 0;
100+
}
101+
}
80102
}

0 commit comments

Comments
 (0)
0