table: add tests and improve performance

pull/5288/head
Adam Stankiewicz 2020-06-08 15:36:59 +02:00 committed by GitHub
parent 09fa0f1540
commit 586a886975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 8 deletions

View File

@ -76,7 +76,7 @@ pub fn (t Type) set_nr_muls(nr_muls int) Type {
if nr_muls < 0 || nr_muls > 255 {
panic('set_nr_muls: nr_muls must be between 0 & 255')
}
return (((int(t) >> 24) & 0xff) << 24) | (nr_muls << 16) | (u16(t) & 0xffff)
return int(t) & 0xff00ffff | (nr_muls << 16)
}
// increments nr_nuls on `t` and return it
@ -86,7 +86,8 @@ pub fn (t Type) to_ptr() Type {
if nr_muls == 255 {
panic('to_ptr: nr_muls is already at max of 255')
}
return (((int(t) >> 24) & 0xff) << 24) | ((nr_muls + 1) << 16) | (u16(t) & 0xffff)
return int(t) & 0xff00ffff | ((nr_muls + 1) << 16)
}
// decrement nr_muls on `t` and return it
@ -96,37 +97,37 @@ pub fn (t Type) deref() Type {
if nr_muls == 0 {
panic('deref: type `$t` is not a pointer')
}
return (((int(t) >> 24) & 0xff) << 24) | ((nr_muls - 1) << 16) | (u16(t) & 0xffff)
return int(t) & 0xff00ffff | ((nr_muls - 1) << 16)
}
// set `flag` on `t` and return `t`
[inline]
pub fn (t Type) set_flag(flag TypeFlag) Type {
return ((((int(t) >> 24) & 0xff) | 1 << int(flag)) << 24) | (((int(t) >> 16) & 0xff) << 16) | (u16(t) & 0xffff)
return int(t) | (1 << (int(flag) + 24))
}
// clear `flag` on `t` and return `t`
[inline]
pub fn (t Type) clear_flag(flag TypeFlag) Type {
return ((((int(t) >> 24) & 0xff) & ~(1 << int(flag))) << 24) | (((int(t) >> 16) & 0xff) << 16) | (u16(t) & 0xffff)
return int(t) & ~(1 << (int(flag) + 24))
}
// clear all flags
[inline]
pub fn (t Type) clear_flags() Type {
return 0 | (((int(t) >> 16) & 0xff) << 16) | (u16(t) & 0xffff)
return int(t) & 0xffffff
}
// return true if `flag` is set on `t`
[inline]
pub fn (t Type) has_flag(flag TypeFlag) bool {
return (((int(t) >> 24) & 0xff) >> int(flag)) & 1 == 1
return int(t) & (1 << (int(flag) + 24)) > 0
}
// copy flags & nr_muls from `t_from` to `t` and return `t`
[inline]
pub fn (t Type) derive(t_from Type) Type {
return (((int(t_from) >> 24) & 0xff) << 24) | (((int(t_from) >> 16) & 0xff) << 16) | (u16(t) & 0xffff)
return (0xffff0000 & t_from) | u16(t)
}
// return new type with TypeSymbol idx set to `idx`

View File

@ -0,0 +1,102 @@
import table
fn test_idx() {
mut t := table.new_type(table.void_type_idx)
assert t.idx() == table.void_type_idx
t = table.new_type(table.i8_type_idx)
assert t.idx() == table.i8_type_idx
}
fn test_muls() {
mut t := table.new_type(table.void_type_idx)
idx := t.idx()
assert t.nr_muls() == 0
for i in 0..32 {
t = t.set_nr_muls(i)
assert t.nr_muls() == i
}
t = t.set_nr_muls(0)
assert t.nr_muls() == 0
assert t.is_ptr() == false
t = t.to_ptr()
assert t.nr_muls() == 1
assert t.is_ptr() == true
t = t.to_ptr()
assert t.nr_muls() == 2
assert t.is_ptr() == true
t = t.deref()
assert t.nr_muls() == 1
assert t.is_ptr() == true
t = t.deref()
assert t.nr_muls() == 0
assert t.is_ptr() == false
assert t.idx() == idx
}
fn test_flags() {
mut t := table.new_type(table.void_type_idx)
idx := t.idx()
nr_muls := t.nr_muls()
t = t.set_flag(table.TypeFlag.optional)
assert t.has_flag(table.TypeFlag.optional) == true
assert t.has_flag(table.TypeFlag.variadic) == false
assert t.has_flag(table.TypeFlag.generic) == false
t = t.set_flag(table.TypeFlag.variadic)
assert t.has_flag(table.TypeFlag.optional) == true
assert t.has_flag(table.TypeFlag.variadic) == true
assert t.has_flag(table.TypeFlag.generic) == false
t = t.set_flag(table.TypeFlag.generic)
assert t.has_flag(table.TypeFlag.optional) == true
assert t.has_flag(table.TypeFlag.variadic) == true
assert t.has_flag(table.TypeFlag.generic) == true
assert t.idx() == idx
assert t.nr_muls() == nr_muls
t = t.clear_flag(table.TypeFlag.optional)
assert t.has_flag(table.TypeFlag.optional) == false
assert t.has_flag(table.TypeFlag.variadic) == true
assert t.has_flag(table.TypeFlag.generic) == true
t = t.clear_flag(table.TypeFlag.variadic)
assert t.has_flag(table.TypeFlag.optional) == false
assert t.has_flag(table.TypeFlag.variadic) == false
assert t.has_flag(table.TypeFlag.generic) == true
t = t.clear_flag(table.TypeFlag.generic)
assert t.has_flag(table.TypeFlag.optional) == false
assert t.has_flag(table.TypeFlag.variadic) == false
assert t.has_flag(table.TypeFlag.generic) == false
assert t.idx() == idx
assert t.nr_muls() == nr_muls
}
fn test_derive() {
mut t := table.new_type(table.i8_type_idx)
t = t.set_flag(table.TypeFlag.generic)
t = t.set_flag(table.TypeFlag.variadic)
t = t.set_nr_muls(10)
mut t2 := table.new_type(table.i16_type_idx)
t2 = t2.derive(t)
assert t2.has_flag(table.TypeFlag.optional) == false
assert t2.has_flag(table.TypeFlag.variadic) == true
assert t2.has_flag(table.TypeFlag.generic) == true
assert t2.nr_muls() == 10
}