v2: implement sym types & handle in table.check
parent
b65fad9ca8
commit
a8f07157dd
|
@ -1837,11 +1837,13 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
|
||||||
p.check(.key_type)
|
p.check(.key_type)
|
||||||
name := p.check_name()
|
name := p.check_name()
|
||||||
mut is_sum := false
|
mut is_sum := false
|
||||||
|
mut sum_variants := []table.Type
|
||||||
// type SumType = A | B | c
|
// type SumType = A | B | c
|
||||||
if p.tok.kind == .assign {
|
if p.tok.kind == .assign {
|
||||||
p.next()
|
p.next()
|
||||||
for {
|
for {
|
||||||
p.check_name()
|
variant_type := p.parse_type()
|
||||||
|
sum_variants << variant_type
|
||||||
if p.tok.kind != .pipe {
|
if p.tok.kind != .pipe {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -1852,13 +1854,23 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
|
||||||
else {
|
else {
|
||||||
p.parse_type()
|
p.parse_type()
|
||||||
}
|
}
|
||||||
|
if is_sum {
|
||||||
p.table.register_type_symbol(table.TypeSymbol{
|
p.table.register_type_symbol(table.TypeSymbol{
|
||||||
kind: .sum_type
|
kind: .sum_type
|
||||||
name: name
|
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{
|
info: table.Alias{
|
||||||
foo: ''
|
foo: ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
return ast.TypeDecl{
|
return ast.TypeDecl{
|
||||||
name: name
|
name: name
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
pub type TypeInfo = Array | ArrayFixed | Map | Struct |
|
pub type TypeInfo = Array | ArrayFixed | Map | Struct |
|
||||||
MultiReturn | Alias | Enum
|
MultiReturn | Alias | Enum | SumType
|
||||||
|
|
||||||
pub struct TypeSymbol {
|
pub struct TypeSymbol {
|
||||||
pub:
|
pub:
|
||||||
|
@ -399,6 +399,10 @@ pub mut:
|
||||||
value_type Type
|
value_type Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SumType {
|
||||||
|
variants []Type
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (table &Table) type_to_str(t Type) string {
|
pub fn (table &Table) type_to_str(t Type) string {
|
||||||
sym := table.get_type_symbol(t)
|
sym := table.get_type_symbol(t)
|
||||||
if sym.kind == .multi_return {
|
if sym.kind == .multi_return {
|
||||||
|
|
|
@ -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 {
|
if got_type_sym.kind == .array && got_type_sym.name == 'array_void' && exp_type_sym.kind == .array {
|
||||||
return true
|
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 {
|
if got_idx != exp_idx {
|
||||||
// && got.typ.name != expected.typ.name*/
|
// && got.typ.name != expected.typ.name*/
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -8,6 +8,15 @@ pub enum TypeExtra {
|
||||||
variadic
|
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
|
// return underlying TypeSymbol idx
|
||||||
[inline]
|
[inline]
|
||||||
pub fn type_idx(t Type) int {
|
pub fn type_idx(t Type) int {
|
||||||
|
|
Loading…
Reference in New Issue