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:
parent
7036ca55e6
commit
2f0bb11a96
@ -176,7 +176,7 @@ pub fn (a Number) clone() Number {
|
||||
return b
|
||||
}
|
||||
////////////////////////////////////////////////////////////
|
||||
pub fn factorial(nn bignum.Number) bignum.Number {
|
||||
pub fn factorial(nn Number) Number {
|
||||
mut n := nn.clone()
|
||||
mut a := nn.clone()
|
||||
n.dec()
|
||||
@ -190,6 +190,6 @@ pub fn factorial(nn bignum.Number) bignum.Number {
|
||||
return a
|
||||
}
|
||||
|
||||
pub fn fact(n int) bignum.Number {
|
||||
pub fn fact(n int) Number {
|
||||
return factorial( bignum.from_int(n) )
|
||||
}
|
||||
|
@ -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 = 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.typ(it.typ)
|
||||
g.write('$type_name ${name}(')
|
||||
@ -949,3 +952,25 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
|
||||
}
|
||||
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'}
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,10 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
||||
// TODO high
|
||||
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>
|
||||
if p.tok.kind == .lt {
|
||||
p.next()
|
||||
|
Loading…
x
Reference in New Issue
Block a user