From 778114c4adf9a928a77c3491bb8dd3c1e464fd84 Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Fri, 13 Sep 2024 15:00:16 -0400 Subject: [PATCH] Improve compiler error message --- ncc/include/stdio.h | 3 --- ncc/src/symbols.rs | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ncc/include/stdio.h b/ncc/include/stdio.h index 348e3cc..1437565 100644 --- a/ncc/include/stdio.h +++ b/ncc/include/stdio.h @@ -26,9 +26,6 @@ int getchar() } #endif -// Internal buffer used by printf -char* __buffer[32]; - int printf(char* format, ...) { unsigned int ch_written = 0; diff --git a/ncc/src/symbols.rs b/ncc/src/symbols.rs index 4e244bc..166308d 100644 --- a/ncc/src/symbols.rs +++ b/ncc/src/symbols.rs @@ -93,6 +93,14 @@ impl Env offset } + /// Check if a local with this name is already defined + fn local_defined(&self, name: &str) -> bool + { + let num_scopes = self.scopes.len(); + let top_scope = &self.scopes[num_scopes - 1]; + top_scope.decls.get(name).is_some() + } + /// Define a new local variable in the topmost scope fn define_local(&mut self, name: &str, var_type: Type) { @@ -330,6 +338,13 @@ impl Stmt // Local variable declaration Stmt::VarDecl { var_type, var_name, init_expr } => { + if env.local_defined(var_name) { + return ParseError::msg_only(&format!( + "local with name \"{}\" already exists", + var_name + )); + } + env.define_local(var_name, var_type.clone()); let decl = env.lookup(var_name).unwrap();