Skip to content

Commit 9164bcf

Browse files
committed
Typing: add check type of Attr expression & fix bug in verify_name function
1 parent bede4f3 commit 9164bcf

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

phase2/Typing/TypeError.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ exception ArgumentTypeNotMatch of string
1818
exception UnknownMethod of string
1919
exception UnknowActualType of string
2020
exception WrongTypePrefixOperation of string * string
21-
exception WrongTypePostfixOperation of string
21+
exception WrongTypePostfixOperation of string
22+
exception UnknownAttribute of string * string

phase2/Typing/Typing.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ let verify_name s env current_env =
8080
if (Hashtbl.mem current_env.variables s) <> true then
8181
begin
8282
(* second find in global env *)
83-
if (Hashtbl.mem (Hashtbl.find env current_env.this_class).attributes s) <> true
84-
then raise(UnknownVariable(s))
85-
else Some(Hashtbl.find (Hashtbl.find env current_env.this_class).attributes s)
83+
if (Hashtbl.mem (Hashtbl.find env current_env.this_class).attributes s) = true
84+
then Some(Hashtbl.find (Hashtbl.find env current_env.this_class).attributes s)
85+
else
86+
begin
87+
if (Hashtbl.mem env s) = true then
88+
Some(Ref({ tpath = []; tid = s }))
89+
else raise(UnknownVariable(s))
90+
end
8691
end
8792
else Some(Hashtbl.find current_env.variables s)
8893

@@ -169,10 +174,25 @@ let rec verify_expression env current_env e =
169174
e.etype <- verify_call_expr m al env current_env.this_class
170175
| Some c ->
171176
List.iter (verify_expression env current_env) al;
172-
let class_name = string_of_expression(c) in
173-
e.etype <- verify_call_expr m al env class_name
177+
verify_expression env current_env c;
178+
(match c.etype with
179+
| Some t ->
180+
let class_name = stringOf t in
181+
e.etype <- verify_call_expr m al env class_name
182+
| None -> ())
174183
)
175-
| Attr (r,a) -> () (*TODO*)
184+
| Attr (r,a) ->
185+
(* we consider only the case when r is the name of the class and r has no type for the moment *)
186+
verify_expression env current_env r;
187+
(match r.etype with
188+
| Some t ->
189+
let class_name = stringOf t in
190+
let attrs_table = (Hashtbl.find env class_name).attributes in
191+
if (Hashtbl.mem attrs_table a) <> true then
192+
raise(UnknownAttribute(a, class_name))
193+
else
194+
e.etype <- Some(Hashtbl.find attrs_table a)
195+
| None -> ())
176196
| If (e1, e2, e3) -> () (*TODO*)
177197
| Val v ->
178198
e.etype <- verify_value v

phase2/tests/test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public I(int e) {
1515
boolean f = !a;
1616
int g = ~5;
1717
float h = (-8.5f);
18+
boolean boo = I.a;
1819
private String func1 (int a) {
1920
int b = 1;
2021
b += (5+4);

0 commit comments

Comments
 (0)