Skip to content

Commit

Permalink
Merge pull request #92 from adi-lb-phoenix/type_checking
Browse files Browse the repository at this point in the history
Type checking for function parameters when called.
  • Loading branch information
chsasank authored Jul 29, 2024
2 parents fc683bb + 307c024 commit ed2c16d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/backend/c-lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,22 @@ def is_valid_expr(cls, expr):
def compile(self, expr):
instr_list = []
arg_syms = []
arg_types = []
name = expr[1]
if name not in self.ctx.function_types:
raise CodegenError(f"Call to undeclared function: {name}")

for arg in expr[2:]:
arg = super().compile(arg)
arg_types.append(arg.typ)
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}")
expected_arg_types = self.ctx.function_types[name][1]
if arg_types != expected_arg_types:
raise CodegenError(
f"Expected types: {expected_arg_types}, Received: {arg_types}"
)

res_sym = random_label(CLISP_PREFIX)
ret_type = self.ctx.function_types[name][0]
Expand Down
2 changes: 1 addition & 1 deletion src/backend/tests/c-lisp/errors/not-function.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CodegenError: Not a function: ['declare', 'a', 'int']
CodegenError: Neither function nor struct definition: ['declare', 'a', 'int']
2 changes: 2 additions & 0 deletions src/backend/tests/c-lisp/errors/ptradd-bad.sexp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(c-lisp
(define ((print int) (n int)))

(define ((main int))
(declare arr (ptr int))
(set arr (alloc int 20))
Expand Down
2 changes: 2 additions & 0 deletions src/backend/tests/c-lisp/errors/type-check-call-size.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error in statement: ['call', 'func_print', 'k', 'j', 'm']
CodegenError: Expected types: ['int', 'int'], Received: ['int', 'int', 'int']
15 changes: 15 additions & 0 deletions src/backend/tests/c-lisp/errors/type-check-call-size.sexp
Original file line number Diff line number Diff line change
@@ -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)))
2 changes: 2 additions & 0 deletions src/backend/tests/c-lisp/errors/type-check-call-type.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error in statement: ['call', 'print', 'm']
CodegenError: Expected types: ['int'], Received: ['float']
13 changes: 13 additions & 0 deletions src/backend/tests/c-lisp/errors/type-check-call-type.sexp
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit ed2c16d

Please sign in to comment.