Skip to content

Commit 477a70e

Browse files
committed
Make duplicate function definitions as compile-time errors
1 parent c33cd90 commit 477a70e

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/ast.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! QAST is an abstract representation for quale language.
22
use crate::attributes::Attributes;
3-
use crate::error::{QccError, QccErrorKind};
3+
use crate::error::{QccError, QccErrorKind, Result};
44
use crate::lexer::Location;
55
use crate::mangle::mangle_fns;
66
use crate::types::Type;
@@ -190,8 +190,16 @@ impl ModuleAST {
190190
}
191191
}
192192

193-
pub(crate) fn append_function(&mut self, function: FunctionAST) {
194-
self.functions.push(std::rc::Rc::new(function.into()));
193+
pub(crate) fn append_function(&mut self, other: FunctionAST) -> Result<()> {
194+
// TODO: Use HashSet to store functions in ModuleAST.
195+
for function in &self.functions {
196+
if function.as_ref().borrow().name == other.name {
197+
return Err(QccErrorKind::DuplicateFunction.into());
198+
}
199+
}
200+
201+
self.functions.push(std::rc::Rc::new(other.into()));
202+
Ok(())
195203
}
196204

197205
#[inline]

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub enum QccErrorKind {
7272
ExpectedIf,
7373
ExpectedOpenCurly,
7474
ExpectedCloseCurly,
75+
DuplicateFunction,
7576
}
7677

7778
impl Display for QccErrorKind {
@@ -125,6 +126,7 @@ impl Display for QccErrorKind {
125126
ExpectedIf => "expected 'if'",
126127
ExpectedOpenCurly => "expected '{'",
127128
ExpectedCloseCurly => "expected '}'",
129+
DuplicateFunction => "duplicate function",
128130
}
129131
})(self))
130132
}

src/parser.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,19 @@ impl Parser {
751751
}
752752
} else if self.lexer.is_token(Token::Hash) || self.lexer.is_token(Token::Function) {
753753
match self.parse_function() {
754-
Ok(f) => this.append_function(f),
754+
Ok(f) => {
755+
let name = f.get_name().clone();
756+
let loc = f.get_loc().clone();
757+
758+
match this.append_function(f) {
759+
Ok(_) => {}
760+
Err(e) => {
761+
// TODO: Improve information.
762+
seen_errors = true;
763+
qcceprintln!("{} {} {}", e, name, loc);
764+
}
765+
}
766+
}
755767
Err(e) => {
756768
seen_errors = true;
757769

tests/test_duplicate_functions.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn double(x: f64) : f64 {
2+
return 2 * x;
3+
}
4+
5+
fn double(x: f64) : f64 { // ERROR
6+
return 2 * x;
7+
}
8+

tests/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ fn test_ast_gen() -> Result<(), Box<dyn std::error::Error>> {
298298
"|_ test_empty // @test_empty.ql:1:1
299299
");
300300

301+
// test!("tests/test_duplicate_functions.ql", "");
302+
301303
test!("examples/toss.ql",
302304
"|_ toss // @toss.ql:1:1
303305
|_ fn toss$sin (x: float64) : float64 // @toss.ql:2:4

0 commit comments

Comments
 (0)