8000 [fix] Function definitions must have a type. · UnBCIC-TP2/r-python@b51c8b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit b51c8b6

Browse files
committed
[fix] Function definitions must have a type.
1 parent 62295e4 commit b51c8b6

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/environment/environment.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::ir::ast::Function;
22
use crate::ir::ast::Name;
3+
use crate::ir::ast::Type;
34

45
use std::collections::HashMap;
56
use std::collections::LinkedList;
@@ -165,14 +166,14 @@ mod tests {
165166

166167
let global_func = Function {
167168
name: "global".to_string(),
168-
kind: None,
169+
kind: Type::TVoid,
169170
params: None,
170171
body: None,
171172
};
172173

173174
let local_func = Function {
174175
name: "local".to_string(),
175-
kind: None,
176+
kind: Type::TVoid,
176177
params: None,
177178
body: None,
178179
};

src/ir/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<A> Environment<A> {
114114
#[derive(Clone, Debug, PartialEq)]
115115
pub struct Function {
116116
pub name: Name,
117-
pub kind: Option<Type>,
117+
pub kind: Type,
118118
pub params: Option<Vec<(Name, Type)>>,
119119
pub body: Option<Box<Statement>>,
120120
}
@@ -123,7 +123,7 @@ impl Function {
123123
pub fn new() -> Function {
124124
return Function {
125125
name: "__main__".to_string(),
126-
kind: None,
126+
kind: Type::TVoid,
127127
params: None,
128128
body: None,
129129
};

src/parser/parser_stmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
139139

140140
Statement::FuncDef(Function {
141141
name: name.to_string(),
142-
kind: Some(t),
142+
kind: t,
143143
params: Some(params),
144144
body: Some(Box::new(block))
145145
})
@@ -259,7 +259,7 @@ mod tests {
259259
let input = "def f(x: Int) -> Int: x = 1; end";
260260
let expected = Statement::FuncDef(Function {
261261
name: "f".to_string(),
262-
kind: Some(Type::TInteger),
262+
kind: Type::TInteger,
263263
params: Some(vec![("x".to_string(), Type::TInteger)]),
264264
body: Some(Box::new(Statement::Block(vec![
265265
Statement::Assignment("x".to_string(), Box::new(Expression::CInt(1)))

src/tc/type_checker.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ mod tests {
772772

773773
let func = FuncDef(Function {
774774
name: "add".to_string(),
775-
kind: Some(TInteger),
775+
kind: Type::TInteger,
776776
params: Some(vec![
777777
("a".to_string(), TInteger),
778778
("b".to_string(), TInteger),
@@ -908,4 +908,27 @@ mod tests {
908908
// Should fail - trying to reassign different type
909909
assert!(check_stmt(stmt, &env).is_err());
910910
}
911+
912+
#[test]
913+
fn test_function_scoping() {
914+
let mut env: Environment<i32> = Environment::new();
915+
916+
let global_func = Function {
917+
name: "global".to_string(),
918+
kind: Type::TVoid,
919+
params: None,
920+
body: None,
921+
};
922+
923+
let local_func = Function {
924+
name: "local".to_string(),
925+
kind: Type::TVoid,
926+
params: None,
927+
body: None,
928+
};
929+
930+
// Test function scoping
931+
env.map_function(global_func.clone());
932+
assert!(env.lookup_function(&"global".to_string()).is_some());
933+
}
911934
}

0 commit comments

Comments
 (0)
0