Fix - check number of arguments in function calls
authorSiraaj Khandkar <siraaj@khandkar.net>
Mon, 17 Sep 2018 11:59:10 +0000 (07:59 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Mon, 17 Sep 2018 11:59:10 +0000 (07:59 -0400)
compiler/src/lib/tiger/tiger_error.ml
compiler/src/lib/tiger/tiger_error.mli
compiler/src/lib/tiger/tiger_semant.ml
compiler/src/lib/tiger/tiger_test_cases_book.ml

index e1b0736..88096a2 100644 (file)
@@ -214,6 +214,28 @@ let is_wrong_type t =
   | Different_operand_types _ ->
       false
 
+let is_wrong_number_of_args t =
+  match t with
+  | Wrong_number_of_args _ ->
+      true
+  | Wrong_type _
+  | Unknown_type _
+  | Unknown_id _
+  | Invalid_syntax _
+  | Id_is_a_function _
+  | Id_not_a_function _
+  | No_such_field_in_record _
+  | Exp_not_a_record _
+  | Exp_not_an_array _
+  | Wrong_type_of_expression_in_var_dec _
+  | Wrong_type_used_as_array _
+  | Wrong_type_used_as_record _
+  | Wrong_type_of_field_value _
+  | Wrong_type_of_arg _
+  | Invalid_operand_type _
+  | Different_operand_types _ ->
+      false
+
 let is_invalid_syntax t =
   match t with
   | Invalid_syntax _ ->
index d7c2e79..9090ad4 100644 (file)
@@ -73,6 +73,7 @@ val to_string : t -> string
 val is_unknown_id   : t -> bool
 val is_unknown_type : t -> bool
 val is_wrong_type   : t -> bool
+val is_wrong_number_of_args : t -> bool
 val is_invalid_syntax : t -> bool
 val is_not_a_record : t -> bool
 val is_not_an_array : t -> bool
index 8d0ca11..a3893fa 100644 (file)
@@ -90,10 +90,20 @@ end = struct
       | A.CallExp {func; args; pos} ->
           (match env_get_val ~sym:func ~env ~pos with
           | Value.Fun {formals; result} ->
-              List.iter2 formals args ~f:(fun ty_expected exp_given ->
-                check_same (return (actual_ty ~pos ty_expected)) (trexp exp_given) ~pos;
-              );
-              return (actual_ty ~pos result)
+              let expected = List.length formals in
+              let given    = List.length args in
+              if given = expected then
+                begin
+                  List.iter2 formals args ~f:(fun ty_expected exp_given ->
+                    check_same
+                      (return (actual_ty ~pos ty_expected))
+                      (trexp exp_given)
+                      ~pos;
+                  );
+                  return (actual_ty ~pos result)
+                end
+              else
+                E.raise (E.Wrong_number_of_args {func; expected; given; pos})
           | Value.Var _ ->
               E.raise (E.Id_not_a_function {id=func; pos})
           )
index 7502b47..76bb5af 100644 (file)
@@ -113,6 +113,10 @@ let is_error_expected_semant_of_filename =
   | "test25.tig" ->
       Some Error.is_not_a_record
       (* TODO: Be more specific *)
+  | "test35.tig"
+  | "test36.tig" ->
+      Some Error.is_wrong_number_of_args
+      (* TODO: Be more specific - how many expected, how many given? *)
   | "test09.tig"
   | "test11.tig"
   | "test13.tig"
This page took 0.023093 seconds and 4 git commands to generate.