cgen: prepare any_in, any_float on v side

pull/5009/head
Uwe Krüger 2020-05-24 14:26:43 +02:00 committed by GitHub
parent ed7ed6262f
commit 7c4f2b535b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 10 deletions

View File

@ -2654,10 +2654,13 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
fields := fmt.split('.')
// validate format
// only floats should have precision specifier
if fields.len > 2 || fields.len == 2 && !(node.expr_types[i].is_float()) || node.expr_types[i].is_signed() &&
!(fspec in [`d`, `c`, `x`, `X`, `o`]) || node.expr_types[i].is_unsigned() && !(fspec in [`u`,
`x`, `X`, `o`, `c`]) || node.expr_types[i].is_float() && !(fspec in [`E`, `F`, `G`,
`e`, `f`, `g`]) || node.expr_types[i].is_pointer() && !(fspec in [`p`, `x`, `X`]) {
if fields.len > 2 ||
fields.len == 2 && !(node.expr_types[i].is_float()) ||
node.expr_types[i].is_signed() && fspec !in [`d`, `c`, `x`, `X`, `o`] ||
node.expr_types[i].is_unsigned() && fspec !in [`u`, `x`, `X`, `o`, `c`] ||
node.expr_types[i].is_any_int() && fspec !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`, `c`] ||
node.expr_types[i].is_float() && fspec !in [`E`, `F`, `G`, `e`, `f`, `g`] ||
node.expr_types[i].is_pointer() && fspec !in [`p`, `x`, `X`] {
verror('illegal format specifier ${fspec:c} for type ${g.table.get_type_name(node.expr_types[i])}')
}
// make sure that format paramters are valid numbers

View File

@ -163,6 +163,11 @@ pub fn (typ Type) is_unsigned() bool {
return typ.idx() in unsigned_integer_type_idxs
}
[inline]
pub fn (typ Type) is_any_int() bool {
return typ.idx() == any_int_type_idx
}
[inline]
pub fn (typ Type) is_number() bool {
return typ.idx() in number_type_idxs
@ -192,24 +197,29 @@ pub const (
map_type_idx = 21
any_type_idx = 22
t_type_idx = 23
any_flt_type_idx = 24
any_int_type_idx = 25
)
pub const (
integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx,
u16_type_idx,
u32_type_idx,
u64_type_idx
u64_type_idx,
any_int_type_idx
]
signed_integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx]
unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx, any_flt_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx,
i64_type_idx, byte_type_idx,
u16_type_idx,
u32_type_idx,
u64_type_idx,
f32_type_idx,
f64_type_idx
f64_type_idx,
any_int_type_idx,
any_flt_type_idx
]
pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx]
string_type_idxs = [string_type_idx, ustring_type_idx]
@ -239,13 +249,16 @@ pub const (
map_type = new_type(map_type_idx)
any_type = new_type(any_type_idx)
t_type = new_type(t_type_idx)
any_flt_type = new_type(any_flt_type_idx)
any_int_type = new_type(any_int_type_idx)
)
pub const (
builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64',
'u16',
'u32',
'u64', 'f32', 'f64', 'string', 'ustring', 'char', 'byte', 'bool', 'none', 'array', 'array_fixed',
'u64', 'any_int', 'f32', 'f64', 'any_float', 'string', 'ustring', 'char', 'byte',
'bool', 'none', 'array', 'array_fixed',
'map', 'any', 'struct',
'mapnode', 'size_t']
)
@ -297,6 +310,8 @@ pub enum Kind {
enum_
function
interface_
any_float
any_int
}
pub fn (t &TypeSymbol) str() string {
@ -478,6 +493,16 @@ pub fn (mut t Table) register_builtin_type_symbols() {
name: 'T'
mod: 'builtin'
})
t.register_type_symbol({
kind: .any_float
name: 'any_float'
mod: 'builtin'
})
t.register_type_symbol({
kind: .any_int
name: 'any_int'
mod: 'builtin'
})
t.register_type_symbol({
kind: .size_t
name: 'size_t'
@ -507,12 +532,12 @@ pub fn (t &TypeSymbol) is_pointer() bool {
[inline]
pub fn (t &TypeSymbol) is_int() bool {
return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64]
return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .any_int]
}
[inline]
pub fn (t &TypeSymbol) is_float() bool {
return t.kind in [.f32, .f64]
return t.kind in [.f32, .f64, .any_float]
}
[inline]
@ -537,8 +562,10 @@ pub fn (k Kind) str() string {
.u16 { 'u16' }
.u32 { 'u32' }
.u64 { 'u64' }
.any_int { 'any_int' }
.f32 { 'f32' }
.f64 { 'f64' }
.any_float { 'any_float' }
.string { 'string' }
.char { 'char' }
.bool { 'bool' }