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
|
|
|
|
)
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (p mut Parser) call_expr() (ast.CallExpr,table.TypeRef) {
|
2020-01-02 08:30:15 +01:00
|
|
|
tok := p.tok
|
|
|
|
fn_name := p.check_name()
|
|
|
|
p.check(.lpar)
|
2020-01-18 23:26:14 +01:00
|
|
|
// mut return_ti := types.void_ti
|
2020-02-04 12:50:58 +01:00
|
|
|
args := p.call_args()
|
2020-01-02 08:30:15 +01:00
|
|
|
node := ast.CallExpr{
|
|
|
|
name: fn_name
|
|
|
|
args: args
|
2020-01-18 23:26:14 +01:00
|
|
|
// tok: tok
|
2020-02-07 07:34:18 +01:00
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
pos: tok.position()
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
2020-02-04 17:44:39 +01:00
|
|
|
if p.tok.kind == .key_orelse {
|
|
|
|
p.next()
|
|
|
|
p.parse_block()
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
if f := p.table.find_fn(fn_name) {
|
2020-02-04 12:03:12 +01:00
|
|
|
return node,f.return_type
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := p.add_unresolved('${fn_name}()', node)
|
2020-02-04 12:03:12 +01:00
|
|
|
return node,typ
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 01:08:24 +01:00
|
|
|
pub fn (p mut Parser) call_args() []ast.Expr {
|
|
|
|
mut args := []ast.Expr
|
|
|
|
for p.tok.kind != .rpar {
|
2020-02-04 12:50:58 +01:00
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
p.check(.key_mut)
|
|
|
|
}
|
2020-01-07 01:08:24 +01:00
|
|
|
e,_ := p.expr(0)
|
|
|
|
args << e
|
|
|
|
if p.tok.kind != .rpar {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
2020-01-22 21:34:38 +01:00
|
|
|
return args // ,table.void_ti
|
2020-01-07 01:08:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
fn (p mut Parser) fn_decl() ast.FnDecl {
|
2020-02-04 17:44:39 +01:00
|
|
|
p.table.clear_vars()
|
2020-01-02 20:09:15 +01:00
|
|
|
is_pub := p.tok.kind == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
p.table.clear_vars()
|
|
|
|
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)
|
|
|
|
}
|
2020-01-02 20:09:15 +01:00
|
|
|
// Receiver?
|
|
|
|
mut rec_name := ''
|
2020-01-07 13:10:05 +01:00
|
|
|
mut is_method := false
|
2020-02-06 13:57:35 +01:00
|
|
|
mut rec_type := p.table.type_ref(table.void_type_idx)
|
2020-01-02 20:09:15 +01:00
|
|
|
if p.tok.kind == .lpar {
|
2020-01-07 13:10:05 +01:00
|
|
|
is_method = true
|
2020-01-02 20:09:15 +01:00
|
|
|
p.next()
|
|
|
|
rec_name = p.check_name()
|
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
rec_type = p.parse_type()
|
2020-01-02 20:09:15 +01:00
|
|
|
p.table.register_var(table.Var{
|
|
|
|
name: rec_name
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: rec_type
|
2020-01-02 20:09:15 +01:00
|
|
|
})
|
|
|
|
p.check(.rpar)
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
mut name := ''
|
|
|
|
if p.tok.kind == .name {
|
2020-02-03 07:44:52 +01:00
|
|
|
// TODO high
|
2020-02-03 07:02:54 +01:00
|
|
|
name = p.check_name()
|
|
|
|
}
|
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')
|
|
|
|
p.check(.lpar)
|
|
|
|
// Args
|
|
|
|
mut args := []table.Var
|
|
|
|
mut ast_args := []ast.Arg
|
2020-02-04 09:54:15 +01:00
|
|
|
// `int, int, string` (no names, just types)
|
2020-02-04 12:50:58 +01:00
|
|
|
types_only := p.tok.kind in [.amp] || (p.peek_tok.kind == .comma && p.table.known_type(p.tok.lit)) ||
|
|
|
|
//
|
|
|
|
p.peek_tok.kind == .rpar
|
2020-02-04 09:54:15 +01:00
|
|
|
if types_only {
|
|
|
|
p.warn('types only')
|
|
|
|
for p.tok.kind != .rpar {
|
|
|
|
p.parse_type()
|
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.next()
|
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
2020-02-04 09:54:15 +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()
|
2020-01-02 20:09:15 +01:00
|
|
|
}
|
2020-02-04 12:50:58 +01:00
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
p.check(.key_mut)
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := p.parse_type()
|
2020-02-04 09:54:15 +01:00
|
|
|
for arg_name in arg_names {
|
|
|
|
arg := table.Var{
|
|
|
|
name: arg_name
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: typ
|
2020-02-04 09:54:15 +01:00
|
|
|
}
|
|
|
|
args << arg
|
|
|
|
p.table.register_var(arg)
|
|
|
|
ast_args << ast.Arg{
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: typ
|
2020-02-04 09:54:15 +01:00
|
|
|
name: arg_name
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind == .variadic && p.tok.kind == .comma {
|
2020-02-04 09:54:15 +01:00
|
|
|
p.error('cannot use ...(variadic) with non-final parameter $arg_name')
|
|
|
|
}
|
2020-01-02 20:09:15 +01:00
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
if p.tok.kind != .rpar {
|
|
|
|
p.check(.comma)
|
2020-01-07 12:10:07 +01:00
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
|
|
|
// Return type
|
2020-02-06 13:57:35 +01:00
|
|
|
mut typ := p.table.type_ref(table.void_type_idx)
|
2020-02-04 20:22:00 +01:00
|
|
|
if p.tok.kind in [.name, .lpar, .amp, .lsbr, .question] {
|
2020-02-03 07:02:54 +01:00
|
|
|
typ = p.parse_type()
|
2020-01-22 21:34:38 +01:00
|
|
|
p.return_type = typ
|
|
|
|
}
|
|
|
|
else {
|
2020-02-06 13:57:35 +01:00
|
|
|
// p.return_type = table.void_type
|
|
|
|
p.return_type = typ
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
if is_method {
|
2020-02-06 13:57:35 +01:00
|
|
|
ok := p.table.register_method(rec_type.typ, table.Fn{
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
|
|
|
args: args
|
2020-01-22 21:34:38 +01:00
|
|
|
return_type: typ
|
2020-01-08 10:19:12 +01:00
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
p.error('expected Struct')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-01-07 13:10:05 +01:00
|
|
|
p.table.register_fn(table.Fn{
|
|
|
|
name: name
|
|
|
|
args: args
|
2020-01-22 21:34:38 +01:00
|
|
|
return_type: typ
|
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
|
|
|
|
if p.tok.kind == .lcbr {
|
|
|
|
stmts = p.parse_block()
|
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
return ast.FnDecl{
|
|
|
|
name: name
|
|
|
|
stmts: stmts
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: typ
|
2020-01-02 08:30:15 +01:00
|
|
|
args: ast_args
|
2020-01-02 20:09:15 +01:00
|
|
|
is_pub: is_pub
|
|
|
|
receiver: ast.Field{
|
|
|
|
name: rec_name
|
2020-02-06 13:57:35 +01:00
|
|
|
typ: rec_type
|
2020-01-02 20:09:15 +01:00
|
|
|
}
|
2020-01-02 08:30:15 +01:00
|
|
|
}
|
|
|
|
}
|