From 18034bb95cf1708370a55e2879f1eccf16856b95 Mon Sep 17 00:00:00 2001 From: Enzo Date: Tue, 8 Sep 2020 00:38:24 +0200 Subject: [PATCH] gen: fix multiple call of expression in match (#6324) --- cmd/tools/vbuild-examples.v | 7 ++-- examples/2048/{main.v => 2048.v} | 15 +++++---- .../sokol/particles/{main.v => particles.v} | 9 +++-- .../vweb_assets/{main.v => vweb_assets.v} | 7 ++-- vlib/v/gen/cgen.v | 33 +++++++++---------- vlib/v/tests/match_test.v | 22 +++++++++++++ 6 files changed, 56 insertions(+), 37 deletions(-) rename examples/2048/{main.v => 2048.v} (96%) rename examples/sokol/particles/{main.v => particles.v} (97%) rename examples/vweb/vweb_assets/{main.v => vweb_assets.v} (94%) diff --git a/cmd/tools/vbuild-examples.v b/cmd/tools/vbuild-examples.v index b7e6bdbf88..fe5ecc3a61 100644 --- a/cmd/tools/vbuild-examples.v +++ b/cmd/tools/vbuild-examples.v @@ -7,13 +7,10 @@ fn main() { args := os.args args_string := args[1..].join(' ') params := args_string.all_before('build-examples') - - if testing.v_build_failing(params, 'examples'){ + if testing.v_build_failing(params, 'examples') { 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) } - } diff --git a/examples/2048/main.v b/examples/2048/2048.v similarity index 96% rename from examples/2048/main.v rename to examples/2048/2048.v index 90565f4d2f..f03455ced9 100644 --- a/examples/2048/main.v +++ b/examples/2048/2048.v @@ -94,7 +94,7 @@ fn new_board(sb []string) Board { mut b := Board{} for y := 0; y < 4; y++ { for x := 0; x < 4; x++ { - b.field[y][x] = sb[y][x] - 64 + b.field[y][x] = sb[y][x] - 64 } } return b @@ -180,7 +180,9 @@ fn (t TileLine) to_left() TileLine { fn (b Board) to_left() Board { mut res := b for y := 0; y < 4; y++ { - mut hline := TileLine{ypos: y} + mut hline := TileLine{ + ypos: y + } for x := 0; x < 4; x++ { hline.field[x] = b.field[y][x] } @@ -264,8 +266,8 @@ fn (app &App) draw() { fn (app &App) draw_background() { tw, th := 128, 128 - for y := 30; y <= window_height; y+=tw { - for x := 0; x <= window_width; x+=th { + for y := 30; y <= window_height; y += tw { + for x := 0; x <= window_width; x += th { app.gg.draw_image(x, y, tw, th, app.tiles[0].image) } } @@ -379,7 +381,8 @@ fn (mut app App) game_over() { app.state = .over } -type BoardMoveFN fn(b Board) Board +type BoardMoveFN = fn (b Board) Board + fn (mut app App) move(move_fn BoardMoveFN) { old := app.board new := move_fn(old) @@ -400,7 +403,7 @@ fn (mut app App) on_key_down(key sapp.KeyCode) { .n { app.new_game() } - //.t {/* fast setup for a victory situation: */ app.board = new_board(['JJ@@', '@@@@', '@@@@', '@@@@'])} + // .t {/* fast setup for a victory situation: */ app.board = new_board(['JJ@@', '@@@@', '@@@@', '@@@@'])} .backspace { if app.undo.len > 0 { app.state = .play diff --git a/examples/sokol/particles/main.v b/examples/sokol/particles/particles.v similarity index 97% rename from examples/sokol/particles/main.v rename to examples/sokol/particles/particles.v index b86d5ac169..28e8bc8607 100644 --- a/examples/sokol/particles/main.v +++ b/examples/sokol/particles/particles.v @@ -36,7 +36,10 @@ mut: fn (mut a App) init() { a.frame = 0 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{ pool: 20000 }) @@ -59,7 +62,6 @@ fn (mut a App) run() { html5_canvas_name: title.str cleanup_userdata_cb: cleanup } - 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.num_touches > 0 { - touch_point := ev.touches[0] app.ps.explode(touch_point.pos_x, touch_point.pos_y) - } } } diff --git a/examples/vweb/vweb_assets/main.v b/examples/vweb/vweb_assets/vweb_assets.v similarity index 94% rename from examples/vweb/vweb_assets/main.v rename to examples/vweb/vweb_assets/vweb_assets.v index c21eef83d4..d1da9eeaa4 100644 --- a/examples/vweb/vweb_assets/main.v +++ b/examples/vweb/vweb_assets/vweb_assets.v @@ -24,21 +24,20 @@ pub fn (mut app App) init_once() { app.vweb.handle_static('assets') // This would make available all known static mime types from current // directory and below. - //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() { // We can dynamically specify which assets are to be used in template. mut am := assets.new_manager() am.add_css('assets/index.css') - css := am.include_css(false) title := 'VWeb Assets Example' 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.' - $vweb.html() } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 8c520e4b99..c9fd9efb82 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2561,15 +2561,17 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { g.match_sumtype_syms.pop() } } - mut tmp := '' - if type_sym.kind != .void { - tmp = g.new_tmp_var() + cur_line := if is_expr { + g.empty_line = true + g.go_before_stmt(0) + } else { + '' } - // styp := g.typ(node.expr_type) - // g.write('$styp $tmp = ') - // g.expr(node.cond) - // g.writeln(';') // $it.blocks.len') - // mut sum_type_str = '' + cond_var := g.new_tmp_var() + g.write('${g.typ(node.cond_type)} $cond_var = ') + g.expr(node.cond) + g.writeln(';') + g.write(cur_line) for j, branch in node.branches { is_last := j == node.branches.len - 1 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 { if node.is_sum_type { - g.expr(node.cond) + g.write(cond_var) sym := g.table.get_type_symbol(node.cond_type) // branch_sym := g.table.get_type_symbol(branch.typ) if sym.kind == .sum_type { @@ -2610,26 +2612,23 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { g.expr(expr) } else if type_sym.kind == .string { g.write('string_eq(') - // - g.expr(node.cond) + g.write(cond_var) g.write(', ') - // g.write('string_eq($tmp, ') g.expr(expr) g.write(')') } else if expr is ast.RangeExpr { g.write('(') - g.expr(node.cond) + g.write(cond_var) g.write(' >= ') g.expr(expr.low) g.write(' && ') - g.expr(node.cond) + g.write(cond_var) g.write(' <= ') g.expr(expr.high) g.write(')') } else { - g.expr(node.cond) + g.write(cond_var) g.write(' == ') - // g.write('$tmp == ') g.expr(expr) } 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) // g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it') 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 { '.' } g.write(dot_or_ptr) g.writeln('_object; // ST it') diff --git a/vlib/v/tests/match_test.v b/vlib/v/tests/match_test.v index 77967d996c..80e234e215 100644 --- a/vlib/v/tests/match_test.v +++ b/vlib/v/tests/match_test.v @@ -118,6 +118,28 @@ fn test_match_enums() { 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 struct A1 {