From 46758f1ddf23c3d2740c74d92e944245189437b3 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 27 Aug 2024 10:15:04 -0500 Subject: [PATCH] feat: implement semantic check for a function declaration --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/semantic/checks/function_declaration.rs | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58ab771..fe3023d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,7 +92,7 @@ dependencies = [ [[package]] name = "thp" -version = "0.1.0" +version = "0.1.1" dependencies = [ "colored", "serde", diff --git a/Cargo.toml b/Cargo.toml index 31aec6d..4f0095f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "thp" -version = "0.1.0" +version = "0.1.1" edition = "2021" diff --git a/src/semantic/checks/function_declaration.rs b/src/semantic/checks/function_declaration.rs index f05a6db..ccb877b 100644 --- a/src/semantic/checks/function_declaration.rs +++ b/src/semantic/checks/function_declaration.rs @@ -37,14 +37,24 @@ impl SemanticCheck for FunctionDeclaration<'_> { return Err(err); } } - BlockMember::Stmt(Statement::FnDecl(_)) => { - todo!("Function declaration: semantic check not implemented") + BlockMember::Stmt(Statement::FnDecl(f)) => { + // TODO: (for now) a function cannot be declared inside another function. + let error = SemanticError { + error_start: f.identifier.position, + error_end: f.identifier.get_end_position(), + reason: format!( + "A function cannot be defined inside another function." + ), + }; + + return Err(MistiError::Semantic(error)); } - _ => todo!("Expression: semantic check not implemented"), + BlockMember::Expr(e) => e.check_semantics(scope)?, } } - // TODO: Check the return type of the function + // TODO: Check that the return type of the function + // matches the return type of the last expression scope.insert(function_name, Type::Function(vec![], "Unit".into()));