Skip to content

Commit

Permalink
Improve compiler error message
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Sep 13, 2024
1 parent 7925154 commit 778114c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 0 additions & 3 deletions ncc/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ int getchar()
}
#endif

// Internal buffer used by printf
char* __buffer[32];

int printf(char* format, ...)
{
unsigned int ch_written = 0;
Expand Down
15 changes: 15 additions & 0 deletions ncc/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 778114c

Please sign in to comment.