checker: is_number()

pull/3804/head
Alexander Medvednikov 2020-02-21 13:44:03 +01:00
parent 527377dc86
commit 6dac2edeef
2 changed files with 18 additions and 11 deletions

View File

@ -574,8 +574,7 @@ pub fn (c mut Checker) postfix_expr(node ast.PostfixExpr) table.Type {
} }
*/ */
typ := c.expr(node.expr) typ := c.expr(node.expr)
// if typ.typ.kind != .int { if !table.is_number(typ) {
if table.type_idx(typ) != table.int_type_idx {
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)', node.pos) c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)', node.pos)
} }
@ -601,7 +600,7 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
} }
if !is_range { if !is_range {
index_type := c.expr(node.index) index_type := c.expr(node.index)
if table.type_idx(index_type) != table.int_type_idx { if !(table.type_idx(index_type) in table.number_idxs) {
index_type_sym := c.table.get_type_symbol(index_type) index_type_sym := c.table.get_type_symbol(index_type)
c.error('non-integer index (type `$index_type_sym.name`)', node.pos) c.error('non-integer index (type `$index_type_sym.name`)', node.pos)
} }

View File

@ -13,17 +13,16 @@ pub fn type_idx(t Type) int {
return u16(t) & 0xffff return u16(t) & 0xffff
} }
// return nr_muls // return nr_muls
[inline] [inline]
pub fn type_nr_muls(t Type) int { pub fn type_nr_muls(t Type) int {
return (int(t) >> 16) & 0xff return (int(t)>>16) & 0xff
} }
// return extra // return extra
[inline] [inline]
pub fn type_extra(t Type) TypeExtra { pub fn type_extra(t Type) TypeExtra {
return ((int(t) >> 24) & 0xff) return ((int(t)>>24) & 0xff)
} }
// return true if pointer (nr_muls>0) // return true if pointer (nr_muls>0)
@ -38,7 +37,7 @@ pub fn type_set_nr_muls(t Type, nr_muls int) Type {
if nr_muls < 0 || nr_muls > 255 { if nr_muls < 0 || nr_muls > 255 {
panic('typ_set_nr_muls: nr_muls must be between 0 & 255') panic('typ_set_nr_muls: nr_muls must be between 0 & 255')
} }
return (int(type_extra(t)) << 24) | (nr_muls << 16) | u16(type_idx(t)) return (int(type_extra(t))<<24) | (nr_muls<<16) | u16(type_idx(t))
} }
// increments nr_nuls on Type and return it // increments nr_nuls on Type and return it
@ -48,7 +47,7 @@ pub fn type_to_ptr(t Type) Type {
if nr_muls == 255 { if nr_muls == 255 {
panic('type_to_pre: nr_muls is already at max of 255') panic('type_to_pre: nr_muls is already at max of 255')
} }
return (int(type_extra(t)) << 24) | ((nr_muls+1) << 16) | u16(type_idx(t)) return (int(type_extra(t))<<24) | ((nr_muls + 1)<<16) | u16(type_idx(t))
} }
// decrement nr_muls on Type and return it // decrement nr_muls on Type and return it
@ -58,7 +57,7 @@ pub fn type_deref(t Type) Type {
if nr_muls == 0 { if nr_muls == 0 {
panic('deref: type `$t` is not a pointer') panic('deref: type `$t` is not a pointer')
} }
return (int(type_extra(t)) << 24) | ((nr_muls-1) << 16) | u16(type_idx(t)) return (int(type_extra(t))<<24) | ((nr_muls - 1)<<16) | u16(type_idx(t))
} }
[inline] [inline]
@ -68,7 +67,7 @@ pub fn type_is_optional(t Type) bool {
[inline] [inline]
pub fn type_to_optional(t Type) Type { pub fn type_to_optional(t Type) Type {
return (int(TypeExtra.optional) << 24) | (type_nr_muls(t) << 16) | u16(type_idx(t)) return (int(TypeExtra.optional)<<24) | (type_nr_muls(t)<<16) | u16(type_idx(t))
} }
// new type with idx of TypeSymbol, not pointer (nr_muls=0) // new type with idx of TypeSymbol, not pointer (nr_muls=0)
@ -89,7 +88,16 @@ pub fn new_type_ptr(idx int, nr_muls int) Type {
if nr_muls < 0 || nr_muls > 255 { if nr_muls < 0 || nr_muls > 255 {
panic('typ_ptr: nr_muls must be between 0 & 255') panic('typ_ptr: nr_muls must be between 0 & 255')
} }
return (nr_muls << 16) | u16(idx) return (nr_muls<<16) | u16(idx)
}
pub const (
number_idxs = [int_type_idx, byte_type_idx, u64_type_idx]
)
pub fn is_number(typ Type) bool {
idx := type_idx(typ)
return idx in [int_type_idx, byte_type_idx, u64_type_idx]
} }
pub const ( pub const (