parser: perform constant folding before checking size of fixed array (#12126)
							parent
							
								
									093cab6f56
								
							
						
					
					
						commit
						3647fc6633
					
				|  | @ -3,6 +3,7 @@ | |||
| // that can be found in the LICENSE file.
 | ||||
| module parser | ||||
| 
 | ||||
| import v.transformer | ||||
| import v.ast | ||||
| import v.util | ||||
| import v.token | ||||
|  | @ -26,7 +27,18 @@ pub fn (mut p Parser) parse_array_type() ast.Type { | |||
| 						if const_field.expr is ast.IntegerLiteral { | ||||
| 							fixed_size = const_field.expr.val.int() | ||||
| 						} else { | ||||
| 							show_non_const_error = true | ||||
| 							if const_field.expr is ast.InfixExpr { | ||||
| 								t := transformer.new_transformer(p.pref) | ||||
| 								folded_expr := t.infix_expr(const_field.expr) | ||||
| 
 | ||||
| 								if folded_expr is ast.IntegerLiteral { | ||||
| 									fixed_size = folded_expr.val.int() | ||||
| 								} else { | ||||
| 									show_non_const_error = true | ||||
| 								} | ||||
| 							} else { | ||||
| 								show_non_const_error = true | ||||
| 							} | ||||
| 						} | ||||
| 					} else { | ||||
| 						if p.pref.is_fmt { | ||||
|  |  | |||
|  | @ -0,0 +1,12 @@ | |||
| const ( | ||||
| 	c_a_s = 1 | ||||
| 	c_b_s = 1 + 1 | ||||
| 	c_c_s = c_b_s + 1 // this should be also fold by transformer since it's a constant
 | ||||
| ) | ||||
| 
 | ||||
| fn test_consant_array_size() { | ||||
| 	mut a := [c_a_s]int{} | ||||
| 	a = [1]! | ||||
| 	mut b := [c_b_s]int{} | ||||
| 	b = [1, 2]! | ||||
| } | ||||
|  | @ -26,7 +26,7 @@ pub fn (t Transformer) transform(ast_file &ast.File) { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn (t Transformer) stmt(mut node ast.Stmt) { | ||||
| pub fn (t Transformer) stmt(mut node ast.Stmt) { | ||||
| 	match mut node { | ||||
| 		ast.EmptyStmt {} | ||||
| 		ast.NodeError {} | ||||
|  | @ -88,14 +88,14 @@ fn (t Transformer) stmt(mut node ast.Stmt) { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn (t Transformer) expr(node ast.Expr) ast.Expr { | ||||
| pub fn (t Transformer) expr(node ast.Expr) ast.Expr { | ||||
| 	match node { | ||||
| 		ast.InfixExpr { return t.infix_expr(node) } | ||||
| 		else { return node } | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| fn (t Transformer) infix_expr(original ast.InfixExpr) ast.Expr { | ||||
| pub fn (t Transformer) infix_expr(original ast.InfixExpr) ast.Expr { | ||||
| 	mut node := original | ||||
| 	node.left = t.expr(node.left) | ||||
| 	node.right = t.expr(node.right) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue