parser: handle operator overloading type errors

pull/3142/head
Alexander Medvednikov 2019-12-17 00:50:41 +03:00
parent 560d1383d1
commit 5a56ca0892
1 changed files with 11 additions and 1 deletions

View File

@ -284,10 +284,12 @@ fn (p mut Parser) fn_decl() {
f.args << receiver
p.register_var(receiver)
}
// +-/* methods
// +-/* methods (operator overloading)
mut is_op := false
if p.tok in [.plus, .minus, .mul, .div, .mod] {
f.name = p.tok.str()
p.next()
is_op = true
}
else {
f.name = p.check_name()
@ -364,6 +366,14 @@ fn (p mut Parser) fn_decl() {
}
// Args (...)
p.fn_args(mut f)
if is_op {
if f.args.len != 1 + 1 { // +1 is for the receiver
p.error('operator overloading methods must have only 1 argument')
}
if f.args[0].typ != f.args[1].typ {
p.error('operators must have the same types on both sides')
}
}
// Returns an error?
if p.tok == .not {
p.next()