parser: handle operator methods and fix them in vdoc

pull/3984/head
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

@ -33,7 +33,7 @@ fn C.bignum_xor( a &Number, b &Number, c &Number) // c = a xor b
fn C.bignum_lshift( a &Number, b &Number, nbits int) // b = a << nbits
fn C.bignum_rshift( a &Number, b &Number, nbits int) // b = a >> nbits
fn C.bignum_cmp( a &Number, b &Number) int
fn C.bignum_cmp( a &Number, b &Number) int
fn C.bignum_is_zero( a &Number) int
fn C.bignum_inc(n &Number)
fn C.bignum_dec(n &Number)
@ -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) )
}

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 = 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'}
}
}

View File

@ -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()