map/scope/checker: update just var type & ret zero in map.get3

pull/4146/head
joe-conigliaro 2020-03-29 00:00:28 +11:00
parent 9fb218d379
commit 718819eb7a
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 21 additions and 26 deletions

View File

@ -358,9 +358,7 @@ fn (m map) get3(key string, zero voidptr) voidptr {
index += 2
meta += probe_inc
}
out := malloc(m.value_bytes)
C.memcpy(out, zero, m.value_bytes)
return out
return zero
//return voidptr(0)
}

View File

@ -3,13 +3,14 @@
// that can be found in the LICENSE file.
module ast
import v.table
pub struct Scope {
mut:
parent &Scope
children []&Scope
start_pos int
end_pos int
// vars map[string]table.Var
vars map[string]Var
}
@ -73,6 +74,18 @@ pub fn (s mut Scope) override_var(var Var) {
s.vars[var.name] = var
}
pub fn (s mut Scope) update_var_type(name string, typ table.Type) {
mut x := s.vars[name]
// dont do an insert for no reason
if x.typ == typ {
return
}
x.typ = typ
s.vars[name] = x
// TODO
// s.vars[name].typ = typ
}
pub fn (s &Scope) outermost() &Scope {
mut sc := s
for !isnil(sc.parent) {

View File

@ -286,10 +286,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
if name in ['filter', 'map'] {
array_info := typ_sym.info as table.Array
mut scope := c.file.scope.innermost(method_call_expr.pos.pos)
scope.override_var(ast.Var{
name: 'it'
typ: array_info.elem_type
})
scope.update_var_type('it', array_info.elem_type)
}
for i, arg in method_call_expr.args {
c.expr(arg.expr)
@ -451,10 +448,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
}
}
assign_stmt.right_types << val_type
scope.override_var(ast.Var{
name: ident.name
typ: mr_info.types[i]
})
scope.update_var_type(ident.name, mr_info.types[i])
}
}
// `a := 1` | `a,b := 1,2`
@ -480,10 +474,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
ident_var_info.typ = val_type
ident.info = ident_var_info
assign_stmt.left[i] = ident
scope.override_var(ast.Var{
name: ident.name
typ: val_type
})
scope.update_var_type(ident.name, val_type)
}
}
c.expected_type = table.void_type
@ -619,10 +610,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
table.int_type}
}
it.key_type = key_type
scope.override_var(ast.Var{
name: it.key_var
typ: key_type
})
scope.update_var_type(it.key_var, key_type)
}
value_type := c.table.value_type(typ)
if value_type == table.void_type {
@ -632,10 +620,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
it.cond_type = typ
it.kind = sym.kind
it.val_type = value_type
scope.override_var(ast.Var{
name: it.val_var
typ: value_type
})
scope.update_var_type(it.val_var, value_type)
}
c.stmts(it.stmts)
}
@ -838,8 +823,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
// set var type on first use
if typ == 0 {
typ = c.expr(var.expr)
var.typ = typ
var_scope.override_var(var)
var_scope.update_var_type(var.name, typ)
}
// update ident
ident.kind = .variable