v2: checker: handle clone()

pull/3866/head
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
}
mut compilation_options := os.args[1..tname_index].clone()
//f := os.args.join(' ')
tool_args := os.args[1..].join(' ')
tool_exe := path_of_executable(os.realpath('$vroot/cmd/tools/$tname'))
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 {
typ := c.expr(method_call_expr.expr)
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
}
if typ_sym.kind == .array && name in ['filter', 'clone'] {
// info := typ_sym.info as table.Array
return typ // info.elem_type
}
// check parent
if typ_sym.parent_idx != 0 {
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
}
}
if typ_sym.kind == .array && method_call_expr.name == 'filter' {
// 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)
c.error('type `$typ_sym.name` has no method `$name`', method_call_expr.pos)
return table.void_type
}

View File

@ -71,6 +71,9 @@ fn foo(a int) {
q := true || false
b2 := bools[0] || true
b3 := get_bool() || true
//cloned = nums.clone()
//cloned1 := cloned[0]
//println(cloned1 == 1)
}
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
args: args
// tok: tok
pos: tok.position()
is_c: is_c
}
@ -131,8 +132,10 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
typ = p.parse_type()
}
p.return_type = typ
// Register
if is_method {
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{
name: name
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 {
// println('register method `$new_fn.name` type=$typ.name idx=$typ.idx')
// println('register method `$new_fn.name` type=$typ.name')
// TODO mut << bug
mut t1 := typ
mut methods := typ.methods
methods << new_fn