gen: fix multiple call of expression in match (#6324)
							parent
							
								
									5258f52497
								
							
						
					
					
						commit
						18034bb95c
					
				|  | @ -7,13 +7,10 @@ fn main() { | ||||||
| 	args := os.args | 	args := os.args | ||||||
| 	args_string := args[1..].join(' ') | 	args_string := args[1..].join(' ') | ||||||
| 	params := args_string.all_before('build-examples') | 	params := args_string.all_before('build-examples') | ||||||
| 
 |  | ||||||
| 	if testing.v_build_failing(params, 'examples') { | 	if testing.v_build_failing(params, 'examples') { | ||||||
| 		exit(1) | 		exit(1) | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	if testing.v_build_failing(params + '-live', os.join_path('examples', 'hot_reload')) { | 	if testing.v_build_failing(params + '-live', os.join_path('examples', 'hot_reload')) { | ||||||
| 		exit(1) | 		exit(1) | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -180,7 +180,9 @@ fn (t TileLine) to_left() TileLine { | ||||||
| fn (b Board) to_left() Board { | fn (b Board) to_left() Board { | ||||||
| 	mut res := b | 	mut res := b | ||||||
| 	for y := 0; y < 4; y++ { | 	for y := 0; y < 4; y++ { | ||||||
| 		mut hline := TileLine{ypos: y} | 		mut hline := TileLine{ | ||||||
|  | 			ypos: y | ||||||
|  | 		} | ||||||
| 		for x := 0; x < 4; x++ { | 		for x := 0; x < 4; x++ { | ||||||
| 			hline.field[x] = b.field[y][x] | 			hline.field[x] = b.field[y][x] | ||||||
| 		} | 		} | ||||||
|  | @ -379,7 +381,8 @@ fn (mut app App) game_over() { | ||||||
| 	app.state = .over | 	app.state = .over | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type BoardMoveFN fn(b Board) Board | type BoardMoveFN = fn (b Board) Board | ||||||
|  | 
 | ||||||
| fn (mut app App) move(move_fn BoardMoveFN) { | fn (mut app App) move(move_fn BoardMoveFN) { | ||||||
| 	old := app.board | 	old := app.board | ||||||
| 	new := move_fn(old) | 	new := move_fn(old) | ||||||
|  | @ -36,7 +36,10 @@ mut: | ||||||
| fn (mut a App) init() { | fn (mut a App) init() { | ||||||
| 	a.frame = 0 | 	a.frame = 0 | ||||||
| 	a.last = time.ticks() | 	a.last = time.ticks() | ||||||
| 	a.ps = particle.System{width: a.width, height: a.height} | 	a.ps = particle.System{ | ||||||
|  | 		width: a.width | ||||||
|  | 		height: a.height | ||||||
|  | 	} | ||||||
| 	a.ps.init(particle.SystemConfig{ | 	a.ps.init(particle.SystemConfig{ | ||||||
| 		pool: 20000 | 		pool: 20000 | ||||||
| 	}) | 	}) | ||||||
|  | @ -59,7 +62,6 @@ fn (mut a App) run() { | ||||||
| 		html5_canvas_name: title.str | 		html5_canvas_name: title.str | ||||||
| 		cleanup_userdata_cb: cleanup | 		cleanup_userdata_cb: cleanup | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	sapp.run(&desc) | 	sapp.run(&desc) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -119,13 +121,10 @@ fn event(ev &C.sapp_event, user_data voidptr) { | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	if ev.@type == .touches_began || ev.@type == .touches_moved { | 	if ev.@type == .touches_began || ev.@type == .touches_moved { | ||||||
| 		if ev.num_touches > 0 { | 		if ev.num_touches > 0 { | ||||||
| 
 |  | ||||||
| 			touch_point := ev.touches[0] | 			touch_point := ev.touches[0] | ||||||
| 			app.ps.explode(touch_point.pos_x, touch_point.pos_y) | 			app.ps.explode(touch_point.pos_x, touch_point.pos_y) | ||||||
| 
 |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -27,18 +27,17 @@ pub fn (mut app App) init_once() { | ||||||
| 	// app.vweb.handle_static('.')
 | 	// app.vweb.handle_static('.')
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut app App) init() {} | pub fn (mut app App) init() { | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut app App) index() { | pub fn (mut app App) index() { | ||||||
| 	// We can dynamically specify which assets are to be used in template.
 | 	// We can dynamically specify which assets are to be used in template.
 | ||||||
| 	mut am := assets.new_manager() | 	mut am := assets.new_manager() | ||||||
| 	am.add_css('assets/index.css') | 	am.add_css('assets/index.css') | ||||||
| 
 |  | ||||||
| 	css := am.include_css(false) | 	css := am.include_css(false) | ||||||
| 	title := 'VWeb Assets Example' | 	title := 'VWeb Assets Example' | ||||||
| 	subtitle := 'VWeb can serve static assets too!' | 	subtitle := 'VWeb can serve static assets too!' | ||||||
| 	message := 'It also has an Assets Manager that allows dynamically specifying which CSS and JS files to be used.' | 	message := 'It also has an Assets Manager that allows dynamically specifying which CSS and JS files to be used.' | ||||||
| 
 |  | ||||||
| 	$vweb.html() | 	$vweb.html() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -2561,15 +2561,17 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { | ||||||
| 			g.match_sumtype_syms.pop() | 			g.match_sumtype_syms.pop() | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	mut tmp := '' | 	cur_line := if is_expr { | ||||||
| 	if type_sym.kind != .void { | 		g.empty_line = true | ||||||
| 		tmp = g.new_tmp_var() | 		g.go_before_stmt(0) | ||||||
|  | 	} else { | ||||||
|  | 		'' | ||||||
| 	} | 	} | ||||||
| 	// styp := g.typ(node.expr_type)
 | 	cond_var := g.new_tmp_var() | ||||||
| 	// g.write('$styp $tmp = ')
 | 	g.write('${g.typ(node.cond_type)} $cond_var = ') | ||||||
| 	// g.expr(node.cond)
 | 	g.expr(node.cond) | ||||||
| 	// g.writeln(';') // $it.blocks.len')
 | 	g.writeln(';') | ||||||
| 	// mut sum_type_str = ''
 | 	g.write(cur_line) | ||||||
| 	for j, branch in node.branches { | 	for j, branch in node.branches { | ||||||
| 		is_last := j == node.branches.len - 1 | 		is_last := j == node.branches.len - 1 | ||||||
| 		if branch.is_else || (node.is_expr && is_last) { | 		if branch.is_else || (node.is_expr && is_last) { | ||||||
|  | @ -2596,7 +2598,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { | ||||||
| 			} | 			} | ||||||
| 			for i, expr in branch.exprs { | 			for i, expr in branch.exprs { | ||||||
| 				if node.is_sum_type { | 				if node.is_sum_type { | ||||||
| 					g.expr(node.cond) | 					g.write(cond_var) | ||||||
| 					sym := g.table.get_type_symbol(node.cond_type) | 					sym := g.table.get_type_symbol(node.cond_type) | ||||||
| 					// branch_sym := g.table.get_type_symbol(branch.typ)
 | 					// branch_sym := g.table.get_type_symbol(branch.typ)
 | ||||||
| 					if sym.kind == .sum_type { | 					if sym.kind == .sum_type { | ||||||
|  | @ -2610,26 +2612,23 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { | ||||||
| 					g.expr(expr) | 					g.expr(expr) | ||||||
| 				} else if type_sym.kind == .string { | 				} else if type_sym.kind == .string { | ||||||
| 					g.write('string_eq(') | 					g.write('string_eq(') | ||||||
| 					//
 | 					g.write(cond_var) | ||||||
| 					g.expr(node.cond) |  | ||||||
| 					g.write(', ') | 					g.write(', ') | ||||||
| 					// g.write('string_eq($tmp, ')
 |  | ||||||
| 					g.expr(expr) | 					g.expr(expr) | ||||||
| 					g.write(')') | 					g.write(')') | ||||||
| 				} else if expr is ast.RangeExpr { | 				} else if expr is ast.RangeExpr { | ||||||
| 					g.write('(') | 					g.write('(') | ||||||
| 					g.expr(node.cond) | 					g.write(cond_var) | ||||||
| 					g.write(' >= ') | 					g.write(' >= ') | ||||||
| 					g.expr(expr.low) | 					g.expr(expr.low) | ||||||
| 					g.write(' && ') | 					g.write(' && ') | ||||||
| 					g.expr(node.cond) | 					g.write(cond_var) | ||||||
| 					g.write(' <= ') | 					g.write(' <= ') | ||||||
| 					g.expr(expr.high) | 					g.expr(expr.high) | ||||||
| 					g.write(')') | 					g.write(')') | ||||||
| 				} else { | 				} else { | ||||||
| 					g.expr(node.cond) | 					g.write(cond_var) | ||||||
| 					g.write(' == ') | 					g.write(' == ') | ||||||
| 					// g.write('$tmp == ')
 |  | ||||||
| 					g.expr(expr) | 					g.expr(expr) | ||||||
| 				} | 				} | ||||||
| 				if i < branch.exprs.len - 1 { | 				if i < branch.exprs.len - 1 { | ||||||
|  | @ -2652,7 +2651,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { | ||||||
| 					it_type := g.typ(first_expr.typ) | 					it_type := g.typ(first_expr.typ) | ||||||
| 					// g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it')
 | 					// g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it')
 | ||||||
| 					g.write('\t$it_type* it = ($it_type*)') | 					g.write('\t$it_type* it = ($it_type*)') | ||||||
| 					g.expr(node.cond) | 					g.write(cond_var) | ||||||
| 					dot_or_ptr := if node.cond_type.is_ptr() { '->' } else { '.' } | 					dot_or_ptr := if node.cond_type.is_ptr() { '->' } else { '.' } | ||||||
| 					g.write(dot_or_ptr) | 					g.write(dot_or_ptr) | ||||||
| 					g.writeln('_object; // ST it') | 					g.writeln('_object; // ST it') | ||||||
|  |  | ||||||
|  | @ -118,6 +118,28 @@ fn test_match_enums() { | ||||||
| 	assert b == .blue | 	assert b == .blue | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | struct Counter { | ||||||
|  | mut: | ||||||
|  | 	val int | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn (mut c Counter) next() int { | ||||||
|  | 	c.val++ | ||||||
|  | 	return c.val | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn test_method_call() { | ||||||
|  | 	mut c := Counter{ | ||||||
|  | 		val: 1 | ||||||
|  | 	} | ||||||
|  | 	assert match c.next() { | ||||||
|  | 		1 { false } | ||||||
|  | 		2 { true } | ||||||
|  | 		3 { false } | ||||||
|  | 		else { false } | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| type Sum = A1 | B1 | type Sum = A1 | B1 | ||||||
| 
 | 
 | ||||||
| struct A1 { | struct A1 { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue