parser: parse non-identifier expressions for sizeof too (#7781)
							parent
							
								
									5ae3637d27
								
							
						
					
					
						commit
						d15d13674c
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -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] | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue