Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callable type for functions #163

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions typechecker/src/type_check/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{

use super::{
type_inference::{self, bin_op_result_type, type_check_bin_op},
types::Type,
types::{CallableType, Type},
};

pub struct TypeChecker<'a> {
Expand Down Expand Up @@ -45,11 +45,20 @@ impl<'a> TypeChecker<'a> {
}
}
Declaration::Function(f) => {
if let Some(type_annotation) = f.function_node.returns.clone() {
let return_type = if let Some(type_annotation) = f.function_node.returns.clone() {
type_inference::get_type_from_annotation(&type_annotation)
} else {
panic!("Infer the type based on the return statement")
}
};

let arguments = f.function_node.args.clone();
let name = f.function_node.name.clone();

Type::Callable(Box::new(CallableType {
name,
arguments,
return_type,
}))
}
_ => panic!("TODO: infer type from declaration"),
}
Expand Down Expand Up @@ -85,7 +94,18 @@ impl<'a> TypeChecker<'a> {
ast::Expression::Call(call) => {
let func = *call.func.clone();
match func {
ast::Expression::Name(n) => self.infer_type_from_symbol_table(n.id.as_str()),
ast::Expression::Name(n) => {
let f_type = self.infer_type_from_symbol_table(n.id.as_str());
// we know this must be a callable type otherwise raise error cannot call
match f_type {
Type::Callable(callable_type) => callable_type.return_type,
_ => {
self.errors
.push(format!("Cannot call type '{}' as a function", f_type));
Type::Unknown
}
}
}
ast::Expression::Attribute(a) => panic!("TODO: infer type from attribute"),
_ => panic!("TODO: infer type from call"),
}
Expand Down
6 changes: 3 additions & 3 deletions typechecker/src/type_check/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub enum Type {

#[allow(unused)]
pub struct CallableType {
name: String,
arguments: ast::Arguments,
return_type: Type,
pub name: String,
pub arguments: ast::Arguments,
pub return_type: Type,
}

impl Display for Type {
Expand Down
2 changes: 2 additions & 0 deletions typechecker/testdata/inputs/type_check_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ def function() -> int:
b = function() + "1"
c = a + 1
d = function() + 1

function + 1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: typechecker/src/build.rs
description: "def function() -> int:\n return 1\n\na = function()\nb = function() + \"1\"\nc = a + 1\nd = function() + 1\n"
description: "def function() -> int:\n return 1\n\na = function()\nb = function() + \"1\"\nc = a + 1\nd = function() + 1\n\nfunction + 1\n"
expression: result
---
Operator '+' not supported for types 'Int' and 'Str'
Operator '+' not supported for types 'function' and 'Int'