Skip to content

Commit 778114c

Browse files
committed
Improve compiler error message
1 parent 7925154 commit 778114c

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

ncc/include/stdio.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ int getchar()
2626
}
2727
#endif
2828

29-
// Internal buffer used by printf
30-
char* __buffer[32];
31-
3229
int printf(char* format, ...)
3330
{
3431
unsigned int ch_written = 0;

ncc/src/symbols.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ impl Env
9393
offset
9494
}
9595

96+
/// Check if a local with this name is already defined
97+
fn local_defined(&self, name: &str) -> bool
98+
{
99+
let num_scopes = self.scopes.len();
100+
let top_scope = &self.scopes[num_scopes - 1];
101+
top_scope.decls.get(name).is_some()
102+
}
103+
96104
/// Define a new local variable in the topmost scope
97105
fn define_local(&mut self, name: &str, var_type: Type)
98106
{
@@ -330,6 +338,13 @@ impl Stmt
330338

331339
// Local variable declaration
332340
Stmt::VarDecl { var_type, var_name, init_expr } => {
341+
if env.local_defined(var_name) {
342+
return ParseError::msg_only(&format!(
343+
"local with name \"{}\" already exists",
344+
var_name
345+
));
346+
}
347+
333348
env.define_local(var_name, var_type.clone());
334349

335350
let decl = env.lookup(var_name).unwrap();

0 commit comments

Comments
 (0)