gen: fix multiple call of expression in match (#6324)

pull/6358/head
Enzo 2020-09-08 00:38:24 +02:00 committed by GitHub
parent 5258f52497
commit 18034bb95c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 37 deletions

View File

@ -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') {
exit(1)
}
if testing.v_build_failing(params + '-live', os.join_path('examples', 'hot_reload')) {
exit(1)
}
}

View File

@ -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]
}
@ -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)

View File

@ -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)
}
}
}

View File

@ -27,18 +27,17 @@ pub fn (mut app App) init_once() {
// 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()
}

View File

@ -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')

View File

@ -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 {