1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: parse non-identifier expressions for sizeof too (#7781)

This commit is contained in:
Nick Treleaven 2021-01-01 13:38:11 +00:00 committed by GitHub
parent 5ae3637d27
commit d15d13674c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 4 deletions

View File

@ -142,9 +142,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
pos := p.tok.position()
p.next() // sizeof
p.check(.lpar)
is_known_var := p.mark_var_as_used(p.tok.lit)
if is_known_var {
expr := p.parse_ident(table.Language.v)
if !p.tok.can_start_type(table.builtin_type_names) {
if p.tok.kind == .name {
p.mark_var_as_used(p.tok.lit)
}
expr := p.expr(0)
node = ast.SizeOf{
is_type: false
expr: expr

View File

@ -1,6 +1,17 @@
import math
fn test_sizeof() {
struct S1 {
i voidptr
}
fn test_math_sizeof() {
r := math.f32_from_bits(sizeof(int))
assert r > 5.6e-45 && r < 5.7e-45
}
fn test_sizeof() {
// depends on compiler
assert sizeof(``) in [u32(2), 4]
// depends on -m32/64
assert sizeof(S1) in [u32(4), 8]
}

View File

@ -431,3 +431,14 @@ pub fn (kind Kind) is_infix() bool {
return kind in
[.plus, .minus, .mod, .mul, .div, .eq, .ne, .gt, .lt, .key_in, /* */.key_as, .ge, .le, .logical_or, .xor, .not_in, .key_is, .not_is, /* */.and, .dot, .pipe, .amp, .left_shift, .right_shift, .arrow]
}
// Pass table.builtin_type_names
// Note: can't import table here due to circular module dependency
pub fn (tok &Token) can_start_type(builtin_type_names []string) bool {
match tok.kind {
.name { return tok.lit[0].is_capital() || tok.lit in builtin_type_names }
.amp, .lsbr, .question { return true }
else {}
}
return false
}