v2: implement sym types & handle in table.check

pull/3906/head
joe-conigliaro 2020-03-02 16:40:18 +11:00
parent b65fad9ca8
commit a8f07157dd
4 changed files with 47 additions and 9 deletions

View File

@ -1837,11 +1837,13 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
p.check(.key_type)
name := p.check_name()
mut is_sum := false
mut sum_variants := []table.Type
// type SumType = A | B | c
if p.tok.kind == .assign {
p.next()
for {
p.check_name()
variant_type := p.parse_type()
sum_variants << variant_type
if p.tok.kind != .pipe {
break
}
@ -1852,13 +1854,23 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
else {
p.parse_type()
}
p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type
name: name
info: table.Alias{
foo: ''
}
})
if is_sum {
p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type
name: p.prepend_mod(name)
info: table.SumType{
variants: sum_variants
}
})
} else {
p.table.register_type_symbol(table.TypeSymbol{
kind: .alias
name: p.prepend_mod(name)
info: table.Alias{
foo: ''
}
})
}
return ast.TypeDecl{
name: name
}

View File

@ -8,7 +8,7 @@ import (
)
pub type TypeInfo = Array | ArrayFixed | Map | Struct |
MultiReturn | Alias | Enum
MultiReturn | Alias | Enum | SumType
pub struct TypeSymbol {
pub:
@ -399,6 +399,10 @@ pub mut:
value_type Type
}
pub struct SumType {
variants []Type
}
pub fn (table &Table) type_to_str(t Type) string {
sym := table.get_type_symbol(t)
if sym.kind == .multi_return {

View File

@ -413,6 +413,19 @@ pub fn (t &Table) check(got, expected Type) bool {
if got_type_sym.kind == .array && got_type_sym.name == 'array_void' && exp_type_sym.kind == .array {
return true
}
// sum type
if got_type_sym.kind == .sum_type {
sum_info := got_type_sym.info as SumType
if expected in sum_info.variants {
return true
}
}
else if exp_type_sym.kind == .sum_type {
sum_info := exp_type_sym.info as SumType
if got in sum_info.variants {
return true
}
}
if got_idx != exp_idx {
// && got.typ.name != expected.typ.name*/
return false

View File

@ -8,6 +8,15 @@ pub enum TypeExtra {
variadic
}
pub fn (types []Type) contains(typ Type) bool {
for t in types {
if int(typ) == int(t) {
return true
}
}
return false
}
// return underlying TypeSymbol idx
[inline]
pub fn type_idx(t Type) int {