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,7 +13,6 @@ 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 {
@ -92,6 +91,15 @@ pub fn new_type_ptr(idx int, nr_muls int) Type {
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 (
void_type = new_type(void_type_idx) void_type = new_type(void_type_idx)
voidptr_type = new_type(voidptr_type_idx) voidptr_type = new_type(voidptr_type_idx)