type alias check

pull/3091/head
BigBlack 2019-12-15 03:01:20 +08:00 committed by Alexander Medvednikov
parent 2e23592264
commit fc33f9d49c
3 changed files with 30 additions and 4 deletions

View File

@ -49,11 +49,12 @@ fn (p mut Parser) bterm() string {
p.expected_type = typ
is_str := typ=='string' && !p.is_sql
is_ustr := typ=='ustring'
is_float := typ[0] == `f` && (typ in ['f64', 'f32']) &&
base := p.base_type(typ)
is_float := base[0] == `f` && (base in ['f64', 'f32']) &&
!(p.cur_fn.name in ['f64_abs', 'f32_abs']) &&
p.cur_fn.name != 'eq'
is_array := typ.starts_with('array_')
expr_type := typ
expr_type := base
tok := p.tok
/*
if tok == .assign {
@ -472,7 +473,7 @@ fn (p mut Parser) expression() string {
if typ == 'bool' {
p.error('operator ${p.tok.str()} not defined on bool ')
}
is_num := typ.contains('*') || is_number_type(typ)
is_num := typ.contains('*') || is_number_type(typ) || is_number_type(p.base_type(typ))
p.check_space(p.tok)
if is_str && tok_op == .plus && !p.is_js {
p.is_alloc = true

View File

@ -601,7 +601,9 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
p.cur_fn.typ = got
return true
}
if throw && p.base_type(got) == p.base_type(expected) {
return true
}
// variadic
if expected.starts_with('varg_') {
expected = expected[5..]
@ -728,6 +730,14 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
return true
}
fn (p mut Parser) base_type(name string) string {
typ := p.find_type(name)
if typ.parent != '' {
return p.base_type(typ.parent)
}
return name
}
// throw by default
fn (p mut Parser) check_types(got, expected string) bool {
if p.first_pass() { return true }

View File

@ -0,0 +1,15 @@
type Myint int
type Myf32 f32
type Myf64 f64
fn test_type_alias() {
i := Myint(10)
assert i + 100 == 110
f := Myf32(1.0)
assert f + 3.14 == 4.14
}