mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: implement sym types & handle in table.check
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user