From 0f5a7a0cb12e9ec94cbfe2df2017fa0f4e6444b7 Mon Sep 17 00:00:00 2001 From: adi-lb-phoenix Date: Mon, 29 Jul 2024 17:49:45 +0530 Subject: [PATCH 1/3] made changes to c-lisp.py that detects for function parameter type and alng with it test cases --- src/backend/c-lisp.py | 15 ++++++++++----- .../tests/c-lisp/errors/type-check-func-para.out | 2 ++ .../tests/c-lisp/errors/type-check-func-para.sexp | 13 +++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para.out create mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para.sexp diff --git a/src/backend/c-lisp.py b/src/backend/c-lisp.py index 960c8d8e..313c89bb 100755 --- a/src/backend/c-lisp.py +++ b/src/backend/c-lisp.py @@ -413,15 +413,20 @@ def is_valid_expr(cls, expr): def compile(self, expr): instr_list = [] arg_syms = [] - for arg in expr[2:]: - arg = super().compile(arg) - arg_syms.append(arg.symbol) - instr_list.extend(arg.instructions) - name = expr[1] if name not in self.ctx.function_types: raise CodegenError(f"Call to undeclared function: {name}") + check_parm_types = self.ctx.function_types[name][1] + for check_parm_types_index, arg in enumerate(expr[2:]): + arg = super().compile(arg) + if arg.typ != check_parm_types[check_parm_types_index]: + raise CodegenError( + f"type mismatch, declared function parameter type is {check_parm_types[check_parm_types_index]}" + ) + arg_syms.append(arg.symbol) + instr_list.extend(arg.instructions) + res_sym = random_label(CLISP_PREFIX) ret_type = self.ctx.function_types[name][0] instr_list.append( diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para.out b/src/backend/tests/c-lisp/errors/type-check-func-para.out new file mode 100644 index 00000000..34e10163 --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-func-para.out @@ -0,0 +1,2 @@ +Error in statement: ['call', 'print', 'm'] +__main__.CodegenError: type mismatch, declared function parameter type is int diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para.sexp b/src/backend/tests/c-lisp/errors/type-check-func-para.sexp new file mode 100644 index 00000000..f1400a23 --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-func-para.sexp @@ -0,0 +1,13 @@ +(c-lisp + (define ((print int) (n int))) + + (define ((func_print void) (n int) (m float)) + (call print n) + (call print m)) + + (define ((main void)) + (declare k int) + (declare j float) + (set k 50) + (set j 10.0) + (call func_print k j))) \ No newline at end of file From a8432ca73d6c3ac17a01991fe0a1426d30b131e9 Mon Sep 17 00:00:00 2001 From: adi-lb-phoenix Date: Mon, 29 Jul 2024 18:51:41 +0530 Subject: [PATCH 2/3] checking for number of function parameters, changed variables names, added a new test case that checks that checks if the number of function parameters are the same --- src/backend/c-lisp.py | 20 +++++++++++++------ .../errors/type-check-func-para-num-size.out | 3 +++ .../errors/type-check-func-para-num-size.sexp | 15 ++++++++++++++ .../errors/type-check-func-para-type.out | 3 +++ ...ra.sexp => type-check-func-para-type.sexp} | 0 .../c-lisp/errors/type-check-func-para.out | 2 -- 6 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out create mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp create mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para-type.out rename src/backend/tests/c-lisp/errors/{type-check-func-para.sexp => type-check-func-para-type.sexp} (100%) delete mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para.out diff --git a/src/backend/c-lisp.py b/src/backend/c-lisp.py index 313c89bb..bc328357 100755 --- a/src/backend/c-lisp.py +++ b/src/backend/c-lisp.py @@ -413,19 +413,27 @@ def is_valid_expr(cls, expr): def compile(self, expr): instr_list = [] arg_syms = [] + result_flag=0 name = expr[1] if name not in self.ctx.function_types: raise CodegenError(f"Call to undeclared function: {name}") - - check_parm_types = self.ctx.function_types[name][1] + + expected_parm_types = self.ctx.function_types[name][1] + + if len(expected_parm_types) != len(expr[2:]): + result_flag=1 + for check_parm_types_index, arg in enumerate(expr[2:]): arg = super().compile(arg) - if arg.typ != check_parm_types[check_parm_types_index]: - raise CodegenError( - f"type mismatch, declared function parameter type is {check_parm_types[check_parm_types_index]}" - ) + if arg.typ != expected_parm_types[check_parm_types_index]: + result_flag=1 arg_syms.append(arg.symbol) instr_list.extend(arg.instructions) + + if result_flag==1: + raise CodegenError( + f"Expected types: {expected_parm_types}\nReceived: {expr[2:]}" + ) res_sym = random_label(CLISP_PREFIX) ret_type = self.ctx.function_types[name][0] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out b/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out new file mode 100644 index 00000000..d868036c --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out @@ -0,0 +1,3 @@ +Error in statement: ['call', 'func_print', 'k', 'j', 'm'] +__main__.CodegenError: Expected types: ['int', 'int'] +Received: ['k', 'j', 'm'] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp b/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp new file mode 100644 index 00000000..88695878 --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp @@ -0,0 +1,15 @@ +(c-lisp + (define ((print int) (n int))) + + (define ((func_print void) (n int) (m int)) + (call print n) + (call print m)) + + (define ((main void)) + (declare k int) + (declare j int) + (declare m int) + (set k 50) + (set j 20) + (set m 100) + (call func_print k j m))) \ No newline at end of file diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-type.out b/src/backend/tests/c-lisp/errors/type-check-func-para-type.out new file mode 100644 index 00000000..7898533b --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-func-para-type.out @@ -0,0 +1,3 @@ +Error in statement: ['call', 'print', 'm'] +__main__.CodegenError: Expected types: ['int'] +Received: ['m'] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para.sexp b/src/backend/tests/c-lisp/errors/type-check-func-para-type.sexp similarity index 100% rename from src/backend/tests/c-lisp/errors/type-check-func-para.sexp rename to src/backend/tests/c-lisp/errors/type-check-func-para-type.sexp diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para.out b/src/backend/tests/c-lisp/errors/type-check-func-para.out deleted file mode 100644 index 34e10163..00000000 --- a/src/backend/tests/c-lisp/errors/type-check-func-para.out +++ /dev/null @@ -1,2 +0,0 @@ -Error in statement: ['call', 'print', 'm'] -__main__.CodegenError: type mismatch, declared function parameter type is int From 307c024a4be70d423bfcd028066432aea9dc114d Mon Sep 17 00:00:00 2001 From: adi-lb-phoenix Date: Mon, 29 Jul 2024 19:21:16 +0530 Subject: [PATCH 3/3] changed and modified test cases --- src/backend/c-lisp.py | 23 ++++++++----------- .../tests/c-lisp/errors/not-function.out | 2 +- .../tests/c-lisp/errors/ptradd-bad.sexp | 2 ++ .../c-lisp/errors/type-check-call-size.out | 2 ++ ...um-size.sexp => type-check-call-size.sexp} | 0 .../c-lisp/errors/type-check-call-type.out | 2 ++ ...ra-type.sexp => type-check-call-type.sexp} | 0 .../errors/type-check-func-para-num-size.out | 3 --- .../errors/type-check-func-para-type.out | 3 --- 9 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 src/backend/tests/c-lisp/errors/type-check-call-size.out rename src/backend/tests/c-lisp/errors/{type-check-func-para-num-size.sexp => type-check-call-size.sexp} (100%) create mode 100644 src/backend/tests/c-lisp/errors/type-check-call-type.out rename src/backend/tests/c-lisp/errors/{type-check-func-para-type.sexp => type-check-call-type.sexp} (100%) delete mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out delete mode 100644 src/backend/tests/c-lisp/errors/type-check-func-para-type.out diff --git a/src/backend/c-lisp.py b/src/backend/c-lisp.py index bc328357..aa98234d 100755 --- a/src/backend/c-lisp.py +++ b/src/backend/c-lisp.py @@ -413,27 +413,22 @@ def is_valid_expr(cls, expr): def compile(self, expr): instr_list = [] arg_syms = [] - result_flag=0 + arg_types = [] name = expr[1] if name not in self.ctx.function_types: raise CodegenError(f"Call to undeclared function: {name}") - - expected_parm_types = self.ctx.function_types[name][1] - - if len(expected_parm_types) != len(expr[2:]): - result_flag=1 - - for check_parm_types_index, arg in enumerate(expr[2:]): + + for arg in expr[2:]: arg = super().compile(arg) - if arg.typ != expected_parm_types[check_parm_types_index]: - result_flag=1 + arg_types.append(arg.typ) arg_syms.append(arg.symbol) instr_list.extend(arg.instructions) - - if result_flag==1: + + expected_arg_types = self.ctx.function_types[name][1] + if arg_types != expected_arg_types: raise CodegenError( - f"Expected types: {expected_parm_types}\nReceived: {expr[2:]}" - ) + f"Expected types: {expected_arg_types}, Received: {arg_types}" + ) res_sym = random_label(CLISP_PREFIX) ret_type = self.ctx.function_types[name][0] diff --git a/src/backend/tests/c-lisp/errors/not-function.out b/src/backend/tests/c-lisp/errors/not-function.out index e5695c34..8482af1e 100644 --- a/src/backend/tests/c-lisp/errors/not-function.out +++ b/src/backend/tests/c-lisp/errors/not-function.out @@ -1 +1 @@ -CodegenError: Not a function: ['declare', 'a', 'int'] +CodegenError: Neither function nor struct definition: ['declare', 'a', 'int'] diff --git a/src/backend/tests/c-lisp/errors/ptradd-bad.sexp b/src/backend/tests/c-lisp/errors/ptradd-bad.sexp index 066a1df8..fa1da6d2 100644 --- a/src/backend/tests/c-lisp/errors/ptradd-bad.sexp +++ b/src/backend/tests/c-lisp/errors/ptradd-bad.sexp @@ -1,4 +1,6 @@ (c-lisp + (define ((print int) (n int))) + (define ((main int)) (declare arr (ptr int)) (set arr (alloc int 20)) diff --git a/src/backend/tests/c-lisp/errors/type-check-call-size.out b/src/backend/tests/c-lisp/errors/type-check-call-size.out new file mode 100644 index 00000000..06466c9a --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-call-size.out @@ -0,0 +1,2 @@ +Error in statement: ['call', 'func_print', 'k', 'j', 'm'] +CodegenError: Expected types: ['int', 'int'], Received: ['int', 'int', 'int'] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp b/src/backend/tests/c-lisp/errors/type-check-call-size.sexp similarity index 100% rename from src/backend/tests/c-lisp/errors/type-check-func-para-num-size.sexp rename to src/backend/tests/c-lisp/errors/type-check-call-size.sexp diff --git a/src/backend/tests/c-lisp/errors/type-check-call-type.out b/src/backend/tests/c-lisp/errors/type-check-call-type.out new file mode 100644 index 00000000..16a16813 --- /dev/null +++ b/src/backend/tests/c-lisp/errors/type-check-call-type.out @@ -0,0 +1,2 @@ +Error in statement: ['call', 'print', 'm'] +CodegenError: Expected types: ['int'], Received: ['float'] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-type.sexp b/src/backend/tests/c-lisp/errors/type-check-call-type.sexp similarity index 100% rename from src/backend/tests/c-lisp/errors/type-check-func-para-type.sexp rename to src/backend/tests/c-lisp/errors/type-check-call-type.sexp diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out b/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out deleted file mode 100644 index d868036c..00000000 --- a/src/backend/tests/c-lisp/errors/type-check-func-para-num-size.out +++ /dev/null @@ -1,3 +0,0 @@ -Error in statement: ['call', 'func_print', 'k', 'j', 'm'] -__main__.CodegenError: Expected types: ['int', 'int'] -Received: ['k', 'j', 'm'] diff --git a/src/backend/tests/c-lisp/errors/type-check-func-para-type.out b/src/backend/tests/c-lisp/errors/type-check-func-para-type.out deleted file mode 100644 index 7898533b..00000000 --- a/src/backend/tests/c-lisp/errors/type-check-func-para-type.out +++ /dev/null @@ -1,3 +0,0 @@ -Error in statement: ['call', 'print', 'm'] -__main__.CodegenError: Expected types: ['int'] -Received: ['m']