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

cgen: union sum types implementation (#6745)

This commit is contained in:
Daniel Däschle
2020-11-11 09:18:15 +01:00
committed by GitHub
parent bb91dc90a5
commit 6271798ce3
17 changed files with 1156 additions and 188 deletions

View File

@ -8,11 +8,12 @@ import v.table
pub struct Scope {
pub mut:
// mut:
objects map[string]ScopeObject
parent &Scope
children []&Scope
start_pos int
end_pos int
objects map[string]ScopeObject
struct_fields []ScopeStructField
parent &Scope
children []&Scope
start_pos int
end_pos int
}
pub fn new_scope(parent &Scope, start_pos int) &Scope {
@ -48,6 +49,20 @@ pub fn (s &Scope) find(name string) ?ScopeObject {
return none
}
pub fn (s &Scope) find_struct_field(struct_type table.Type, field_name string) ?ScopeStructField {
for sc := s; true; sc = sc.parent {
for field in sc.struct_fields {
if field.struct_type == struct_type && field.name == field_name {
return field
}
}
if isnil(sc.parent) {
break
}
}
return none
}
pub fn (s &Scope) is_known(name string) bool {
if _ := s.find(name) {
return true
@ -96,6 +111,15 @@ pub fn (mut s Scope) update_var_type(name string, typ table.Type) {
}
}
pub fn (mut s Scope) register_struct_field(field ScopeStructField) {
for f in s.struct_fields {
if f.struct_type == field.struct_type && f.name == field.name {
return
}
}
s.struct_fields << field
}
pub fn (mut s Scope) register(name string, obj ScopeObject) {
if name == '_' {
return
@ -163,6 +187,9 @@ pub fn (sc &Scope) show(depth int, max_depth int) string {
else {}
}
}
for field in sc.struct_fields {
out += '$indent * struct_field: $field.struct_type $field.name - $field.typ\n'
}
if max_depth == 0 || depth < max_depth - 1 {
for i, _ in sc.children {
out += sc.children[i].show(depth + 1, max_depth)