v/vlib/v/parser/fn.v

252 lines
5.1 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2020-01-02 08:30:15 +01:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module parser
import (
v.ast
v.table
)
pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
2020-01-02 08:30:15 +01:00
tok := p.tok
name := p.check_name()
fn_name := if is_c { 'C.$name' } else if mod.len > 0 { '${mod}.$name' } else { name }
2020-01-02 08:30:15 +01:00
p.check(.lpar)
args := p.call_args()
2020-02-29 15:03:32 +01:00
mut or_stmts := []ast.Stmt
if p.tok.kind == .key_orelse {
p.next()
or_stmts = p.parse_block()
}
2020-01-02 08:30:15 +01:00
node := ast.CallExpr{
name: fn_name
args: args
// tok: tok
pos: tok.position()
2020-02-18 18:13:34 +01:00
is_c: is_c
2020-02-29 15:03:32 +01:00
or_block: ast.OrExpr{
stmts: or_stmts
}
2020-02-04 17:44:39 +01:00
}
return node
2020-01-02 08:30:15 +01:00
}
pub fn (p mut Parser) call_args() []ast.CallArg {
mut args := []ast.CallArg
for p.tok.kind != .rpar {
mut is_mut := false
2020-02-04 12:50:58 +01:00
if p.tok.kind == .key_mut {
p.check(.key_mut)
is_mut = true
2020-02-04 12:50:58 +01:00
}
2020-03-05 12:13:45 +01:00
e := p.expr(0)
args << ast.CallArg{
is_mut: is_mut
expr: e
}
if p.tok.kind != .rpar {
p.check(.comma)
}
}
p.check(.rpar)
return args
}
2020-02-04 09:54:15 +01:00
fn (p mut Parser) fn_decl() ast.FnDecl {
2020-02-18 20:20:15 +01:00
// p.table.clear_vars()
p.open_scope()
2020-03-05 16:13:14 +01:00
is_deprecated := p.attr == 'deprecated'
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
2020-01-02 08:30:15 +01:00
p.check(.key_fn)
2020-02-04 09:54:15 +01:00
// C.
is_c := p.tok.kind == .name && p.tok.lit == 'C'
if is_c {
p.next()
p.check(.dot)
}
// Receiver?
mut rec_name := ''
2020-01-07 13:10:05 +01:00
mut is_method := false
mut rec_type := table.void_type
2020-02-18 20:20:15 +01:00
mut rec_mut := false
mut args := []table.Arg
if p.tok.kind == .lpar {
2020-01-07 13:10:05 +01:00
is_method = true
p.next()
rec_name = p.check_name()
rec_mut = p.tok.kind == .key_mut
// if rec_mut {
2020-03-11 21:11:27 +01:00
// p.check(.key_mut)
// }
// TODO: talk to alex, should mut be parsed with the type like this?
// or should it be a property of the arg, like this ptr/mut becomes indistinguishable
rec_type = p.parse_type()
args << table.Arg{
2020-03-10 23:21:26 +01:00
name: rec_name
is_mut: rec_mut
2020-03-10 23:21:26 +01:00
typ: rec_type
}
p.check(.rpar)
}
mut name := ''
if p.tok.kind == .name {
2020-03-11 21:11:27 +01:00
// TODO high order fn
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()
}
2020-02-03 07:44:52 +01:00
// <T>
if p.tok.kind == .lt {
p.next()
p.next()
p.check(.gt)
}
2020-01-02 08:30:15 +01:00
// println('fn decl $name')
// Args
args2,is_variadic := p.fn_args()
args << args2
for arg in args {
p.scope.register_var(ast.Var{
name: arg.name
typ: arg.typ
})
2020-02-11 13:03:10 +01:00
}
2020-01-02 08:30:15 +01:00
// Return type
mut return_type := table.void_type
2020-02-11 13:21:41 +01:00
if p.tok.kind.is_start_of_type() {
return_type = p.parse_type()
2020-01-02 08:30:15 +01:00
}
2020-02-27 17:21:13 +01:00
// Register
2020-01-08 10:19:12 +01:00
if is_method {
mut type_sym := p.table.get_type_symbol(rec_type)
2020-02-27 17:21:13 +01:00
// p.warn('reg method $type_sym.name . $name ()')
type_sym.register_method(table.Fn{
2020-01-08 10:19:12 +01:00
name: name
args: args
return_type: return_type
2020-01-08 10:19:12 +01:00
})
}
else {
if is_c {
name = 'C.$name'
}
else {
name = p.prepend_mod(name)
}
2020-01-07 13:10:05 +01:00
p.table.register_fn(table.Fn{
name: name
2020-01-07 13:10:05 +01:00
args: args
return_type: return_type
is_variadic: is_variadic
2020-02-07 07:34:18 +01:00
is_c: is_c
2020-01-07 13:10:05 +01:00
})
}
2020-02-04 09:54:15 +01:00
mut stmts := []ast.Stmt
2020-03-06 16:31:40 +01:00
no_body := p.tok.kind != .lcbr
2020-02-04 09:54:15 +01:00
if p.tok.kind == .lcbr {
stmts = p.parse_block()
}
p.close_scope()
2020-03-05 16:13:14 +01:00
p.attr = ''
2020-01-02 08:30:15 +01:00
return ast.FnDecl{
name: name
stmts: stmts
return_type: return_type
args: args
2020-03-05 16:13:14 +01:00
is_deprecated: is_deprecated
is_pub: is_pub
is_variadic: is_variadic
receiver: ast.Field{
name: rec_name
typ: rec_type
}
2020-02-18 20:20:15 +01:00
is_method: is_method
rec_mut: rec_mut
is_c: is_c
2020-03-06 16:31:40 +01:00
no_body: no_body
2020-01-02 08:30:15 +01:00
}
}
2020-02-11 13:03:10 +01:00
fn (p mut Parser) fn_args() ([]table.Arg,bool) {
2020-02-11 13:21:41 +01:00
p.check(.lpar)
mut args := []table.Arg
2020-02-11 13:03:10 +01:00
mut is_variadic := false
// `int, int, string` (no names, just types)
types_only := p.tok.kind in [.amp] || (p.peek_tok.kind == .comma && p.table.known_type(p.tok.lit)) || p.peek_tok.kind == .rpar
if types_only {
2020-02-19 16:12:39 +01:00
// p.warn('types only')
2020-02-11 13:03:10 +01:00
mut arg_no := 1
for p.tok.kind != .rpar {
arg_name := 'arg_$arg_no'
is_mut := p.tok.kind == .key_mut
if is_mut {
p.check(.key_mut)
}
2020-02-11 13:03:10 +01:00
if p.tok.kind == .ellipsis {
p.check(.ellipsis)
is_variadic = true
}
2020-02-29 09:04:47 +01:00
mut arg_type := p.parse_type()
if is_variadic {
arg_type = table.type_to_variadic(arg_type)
}
2020-02-11 13:03:10 +01:00
if p.tok.kind == .comma {
if is_variadic {
p.error('cannot use ...(variadic) with non-final parameter no $arg_no')
}
p.next()
}
args << table.Arg{
2020-02-11 13:03:10 +01:00
name: arg_name
is_mut: is_mut
2020-02-11 13:03:10 +01:00
typ: arg_type
}
arg_no++
2020-02-11 13:03:10 +01:00
}
}
else {
for p.tok.kind != .rpar {
mut arg_names := [p.check_name()]
// `a, b, c int`
for p.tok.kind == .comma {
p.check(.comma)
arg_names << p.check_name()
}
is_mut := p.tok.kind == .key_mut
2020-03-11 21:11:27 +01:00
// if is_mut {
// p.check(.key_mut)
// }
2020-02-11 13:03:10 +01:00
if p.tok.kind == .ellipsis {
p.check(.ellipsis)
is_variadic = true
}
2020-02-29 09:04:47 +01:00
mut typ := p.parse_type()
if is_variadic {
typ = table.type_to_variadic(typ)
}
2020-02-11 13:03:10 +01:00
for arg_name in arg_names {
args << table.Arg{
2020-02-11 13:03:10 +01:00
name: arg_name
is_mut: is_mut
2020-02-11 13:03:10 +01:00
typ: typ
}
// if typ.typ.kind == .variadic && p.tok.kind == .comma {
if is_variadic && p.tok.kind == .comma {
p.error('cannot use ...(variadic) with non-final parameter $arg_name')
}
}
if p.tok.kind != .rpar {
p.check(.comma)
}
}
}
p.check(.rpar)
return args,is_variadic
}