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

v2: checker: handle clone()

This commit is contained in:
Alexander Medvednikov 2020-02-27 17:21:13 +01:00
parent b1357d9641
commit 09b7a7c872
5 changed files with 16 additions and 8 deletions

View File

@ -20,7 +20,6 @@ fn launch_tool(is_verbose bool, tname string, cmdname string) {
tname_index = os.args.len tname_index = os.args.len
} }
mut compilation_options := os.args[1..tname_index].clone() mut compilation_options := os.args[1..tname_index].clone()
//f := os.args.join(' ')
tool_args := os.args[1..].join(' ') tool_args := os.args[1..].join(' ')
tool_exe := path_of_executable(os.realpath('$vroot/cmd/tools/$tname')) tool_exe := path_of_executable(os.realpath('$vroot/cmd/tools/$tname'))
tool_source := os.realpath('$vroot/cmd/tools/${tname}.v') tool_source := os.realpath('$vroot/cmd/tools/${tname}.v')

View File

@ -212,21 +212,23 @@ pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type {
pub fn (c mut Checker) check_method_call_expr(method_call_expr ast.MethodCallExpr) table.Type { pub fn (c mut Checker) check_method_call_expr(method_call_expr ast.MethodCallExpr) table.Type {
typ := c.expr(method_call_expr.expr) typ := c.expr(method_call_expr.expr)
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
if method := typ_sym.find_method(method_call_expr.name) { name := method_call_expr.name
if method := typ_sym.find_method(name) {
return method.return_type return method.return_type
} }
if typ_sym.kind == .array && name in ['filter', 'clone'] {
// info := typ_sym.info as table.Array
return typ // info.elem_type
}
// check parent // check parent
if typ_sym.parent_idx != 0 { if typ_sym.parent_idx != 0 {
parent := &c.table.types[typ_sym.parent_idx] parent := &c.table.types[typ_sym.parent_idx]
if method := parent.find_method(method_call_expr.name) { if method := parent.find_method(name) {
// println('got method $name, returning')
return method.return_type return method.return_type
} }
} }
if typ_sym.kind == .array && method_call_expr.name == 'filter' { c.error('type `$typ_sym.name` has no method `$name`', method_call_expr.pos)
// info := typ_sym.info as table.Array
return typ // info.elem_type
}
c.error('type `$typ_sym.name` has no method `$method_call_expr.name`', method_call_expr.pos)
return table.void_type return table.void_type
} }

View File

@ -71,6 +71,9 @@ fn foo(a int) {
q := true || false q := true || false
b2 := bools[0] || true b2 := bools[0] || true
b3 := get_bool() || true b3 := get_bool() || true
//cloned = nums.clone()
//cloned1 := cloned[0]
//println(cloned1 == 1)
} }
fn get_int(a string) int { fn get_int(a string) int {

View File

@ -18,6 +18,7 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
name: fn_name name: fn_name
args: args args: args
// tok: tok // tok: tok
pos: tok.position() pos: tok.position()
is_c: is_c is_c: is_c
} }
@ -131,8 +132,10 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
typ = p.parse_type() typ = p.parse_type()
} }
p.return_type = typ p.return_type = typ
// Register
if is_method { if is_method {
type_sym := p.table.get_type_symbol(rec_type) type_sym := p.table.get_type_symbol(rec_type)
// p.warn('reg method $type_sym.name . $name ()')
ok := p.table.register_method(type_sym, table.Fn{ ok := p.table.register_method(type_sym, table.Fn{
name: name name: name
args: args args: args

View File

@ -86,6 +86,7 @@ pub fn (t mut Table) register_fn(new_fn Fn) {
pub fn (t &Table) register_method(typ &TypeSymbol, new_fn Fn) bool { pub fn (t &Table) register_method(typ &TypeSymbol, new_fn Fn) bool {
// println('register method `$new_fn.name` type=$typ.name idx=$typ.idx') // println('register method `$new_fn.name` type=$typ.name idx=$typ.idx')
// println('register method `$new_fn.name` type=$typ.name') // println('register method `$new_fn.name` type=$typ.name')
// TODO mut << bug
mut t1 := typ mut t1 := typ
mut methods := typ.methods mut methods := typ.methods
methods << new_fn methods << new_fn