Skip to content

Commit

Permalink
cgen: fix sum type assign/push from in match branch & type mod
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-conigliaro committed May 11, 2020
1 parent 64ba595 commit 1b3cd7a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions vlib/v/ast/ast.v
Expand Up @@ -773,7 +773,7 @@ pub fn expr_is_call(expr Expr) bool {
}
}

fn (expr Expr) position() token.Position {
pub fn (expr Expr) position() token.Position {
// all uncommented have to be implemented
match mut expr {
ArrayInit {
Expand Down Expand Up @@ -867,7 +867,7 @@ fn (expr Expr) position() token.Position {
}
}

fn (stmt Stmt) position() token.Position {
pub fn (stmt Stmt) position() token.Position {
match mut stmt {
AssertStmt {
return it.pos
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Expand Up @@ -1821,7 +1821,7 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
}
// Non-anon-function object (not a call), e.g. `onclick(my_click)`
if func := c.table.find_fn(name) {
fn_type := table.new_type(c.table.find_or_register_fn_type(func, false, true))
fn_type := table.new_type(c.table.find_or_register_fn_type(ident.mod, func, false, true))
ident.name = name
ident.kind = .function
ident.info = ast.IdentFn{
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/gen/cgen.v
Expand Up @@ -979,10 +979,10 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
} else {
g.write(' = ')
if !is_decl {
g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
} else {
if is_decl {
g.expr(val)
} else {
g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
}
}
if gen_or {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/fn.v
Expand Up @@ -307,7 +307,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
}
name := 'anon_${p.tok.pos}_$func.signature()'
func.name = name
idx := p.table.find_or_register_fn_type(func, true, false)
idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx)
// name := p.table.get_type_name(typ)
return ast.AnonFn{
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/parse_type.v
Expand Up @@ -84,7 +84,7 @@ pub fn (mut p Parser) parse_fn_type(name string) table.Type {
is_variadic: is_variadic
return_type: return_type
}
idx := p.table.find_or_register_fn_type(func, false, false)
idx := p.table.find_or_register_fn_type(p.mod, func, false, false)
return table.new_type(idx)
}

Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/parser.v
Expand Up @@ -1211,6 +1211,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
p.table.register_type_symbol(table.TypeSymbol{
kind: .enum_
name: name
mod: p.mod
info: table.Enum{
vals: vals
}
Expand Down Expand Up @@ -1264,6 +1265,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type
name: p.prepend_mod(name)
mod: p.mod
info: table.SumType{
variants: sum_variants
}
Expand All @@ -1283,6 +1285,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
kind: .alias
name: p.prepend_mod(name)
parent_idx: pid
mod: p.mod
info: table.Alias{
foo: ''
}
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/struct.v
Expand Up @@ -275,6 +275,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
t := table.TypeSymbol{
kind: .interface_
name: interface_name
mod: p.mod
info: table.Interface{
types: []
}
Expand Down
25 changes: 25 additions & 0 deletions vlib/v/table/atypes.v
Expand Up @@ -357,106 +357,131 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(TypeSymbol{
kind: .void
name: 'void'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .voidptr
name: 'voidptr'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .byteptr
name: 'byteptr'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .charptr
name: 'charptr'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .i8
name: 'i8'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .i16
name: 'i16'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .int
name: 'int'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .i64
name: 'i64'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .byte
name: 'byte'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .u16
name: 'u16'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .u32
name: 'u32'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .u64
name: 'u64'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .f32
name: 'f32'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .f64
name: 'f64'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .char
name: 'char'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .bool
name: 'bool'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .none_
name: 'none'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .string
name: 'string'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .ustring
name: 'ustring'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .array
name: 'array'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .map
name: 'map'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .any
name: 'any'
mod: 'builtin'
})
t.register_type_symbol(TypeSymbol{
kind: .size_t
name: 'size_t'
mod: 'builtin'
})
// TODO: remove. for v1 map compatibility
map_string_string_idx := t.find_or_register_map(string_type, string_type)
map_string_int_idx := t.find_or_register_map(string_type, int_type)
t.register_type_symbol(TypeSymbol{
kind: .alias
name: 'map_string'
mod: 'builtin'
parent_idx: map_string_string_idx
})
t.register_type_symbol(TypeSymbol{
kind: .alias
name: 'map_int'
mod: 'builtin'
parent_idx: map_string_int_idx
})
}
Expand Down
9 changes: 6 additions & 3 deletions vlib/v/table/table.v
Expand Up @@ -374,12 +374,13 @@ pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int {
return t.register_type_symbol(mr_type)
}

pub fn (mut t Table) find_or_register_fn_type(f Fn, is_anon, has_decl bool) int {
pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon, has_decl bool) int {
name := if f.name.len == 0 { 'anon_fn_$f.signature()' } else { f.name }
anon := f.name.len == 0 || is_anon
return t.register_type_symbol(TypeSymbol{
kind: .function
name: name
mod: mod
info: FnType{
is_anon: anon
has_decl: has_decl
Expand Down Expand Up @@ -516,13 +517,15 @@ pub fn (t &Table) check(got, expected Type) bool {
// sum type
if got_type_sym.kind == .sum_type {
sum_info := got_type_sym.info as SumType
if expected in sum_info.variants {
// TODO: handle `match SumType { &PtrVariant {} }` currently just checking base
if expected.set_nr_muls(0) in sum_info.variants {
return true
}
}
if exp_type_sym.kind == .sum_type {
sum_info := exp_type_sym.info as SumType
if got in sum_info.variants {
// TODO: handle `match SumType { &PtrVariant {} }` currently just checking base
if got.set_nr_muls(0) in sum_info.variants {
return true
}
}
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/sum_type_test.v
Expand Up @@ -33,3 +33,21 @@ fn test_expr() {
assert handle(expr) == 'int'
// assert expr is IntegerLiteral // TODO
}

fn test_assignment_and_push() {
mut expr1 := Expr{}
mut arr1 := []Expr{}
expr := IntegerLiteral{
val: '111'
}
arr1 << expr
match arr1[0] {
IntegerLiteral {
arr1 << it
// should ref/dereference on assignent be made automatic?
// currently it is done for return stmt and fn args
expr1 = *it
}
else {}
}
}

0 comments on commit 1b3cd7a

Please sign in to comment.