1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: handle operator methods and fix them in vdoc

This commit is contained in:
Alexander Medvednikov 2020-03-10 14:40:30 +01:00
parent 7036ca55e6
commit 2f0bb11a96
3 changed files with 32 additions and 3 deletions

View File

@ -176,7 +176,7 @@ pub fn (a Number) clone() Number {
return b return b
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
pub fn factorial(nn bignum.Number) bignum.Number { pub fn factorial(nn Number) Number {
mut n := nn.clone() mut n := nn.clone()
mut a := nn.clone() mut a := nn.clone()
n.dec() n.dec()
@ -190,6 +190,6 @@ pub fn factorial(nn bignum.Number) bignum.Number {
return a return a
} }
pub fn fact(n int) bignum.Number { pub fn fact(n int) Number {
return factorial( bignum.from_int(n) ) return factorial( bignum.from_int(n) )
} }

View File

@ -350,6 +350,9 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name
} }
name = name.replace('.', '__') name = name.replace('.', '__')
if name.starts_with('_op_') {
name = op_to_fn_name(name)
}
// type_name := g.table.type_to_str(it.typ) // type_name := g.table.type_to_str(it.typ)
type_name := g.typ(it.typ) type_name := g.typ(it.typ)
g.write('$type_name ${name}(') g.write('$type_name ${name}(')
@ -949,3 +952,25 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
} }
return types_sorted return types_sorted
} }
fn op_to_fn_name(name string) string {
return match name {
'+'{
'_op_plus'
}
'-'{
'_op_minus'
}
'*'{
'_op_mul'
}
'/'{
'_op_div'
}
'%'{
'_op_mod'
}
else {
'bad op $name'}
}
}

View File

@ -99,6 +99,10 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
// TODO high // TODO high
name = p.check_name() name = p.check_name()
} }
if p.tok.kind in [.plus, .minus, .mul, .div, .mod] {
name = p.tok.kind.str() // op_to_fn_name()
p.next()
}
// <T> // <T>
if p.tok.kind == .lt { if p.tok.kind == .lt {
p.next() p.next()