From 54e838f482844369048d4f88e87a092a87162b72 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 17 Sep 2018 07:59:10 -0400 Subject: [PATCH] Fix - check number of arguments in function calls --- compiler/src/lib/tiger/tiger_error.ml | 22 +++++++++++++++++++ compiler/src/lib/tiger/tiger_error.mli | 1 + compiler/src/lib/tiger/tiger_semant.ml | 18 +++++++++++---- .../src/lib/tiger/tiger_test_cases_book.ml | 4 ++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/compiler/src/lib/tiger/tiger_error.ml b/compiler/src/lib/tiger/tiger_error.ml index e1b0736..88096a2 100644 --- a/compiler/src/lib/tiger/tiger_error.ml +++ b/compiler/src/lib/tiger/tiger_error.ml @@ -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 _ -> diff --git a/compiler/src/lib/tiger/tiger_error.mli b/compiler/src/lib/tiger/tiger_error.mli index d7c2e79..9090ad4 100644 --- a/compiler/src/lib/tiger/tiger_error.mli +++ b/compiler/src/lib/tiger/tiger_error.mli @@ -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 diff --git a/compiler/src/lib/tiger/tiger_semant.ml b/compiler/src/lib/tiger/tiger_semant.ml index 8d0ca11..a3893fa 100644 --- a/compiler/src/lib/tiger/tiger_semant.ml +++ b/compiler/src/lib/tiger/tiger_semant.ml @@ -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}) ) diff --git a/compiler/src/lib/tiger/tiger_test_cases_book.ml b/compiler/src/lib/tiger/tiger_test_cases_book.ml index 7502b47..76bb5af 100644 --- a/compiler/src/lib/tiger/tiger_test_cases_book.ml +++ b/compiler/src/lib/tiger/tiger_test_cases_book.ml @@ -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" -- 2.20.1