1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: fix `for i, mut val in vals {; examples: vfmt flappybird

This commit is contained in:
Alexander Medvednikov
2020-12-27 12:02:01 +01:00
parent bcdf3ca0cf
commit 0d43ff2453
4 changed files with 96 additions and 129 deletions

View File

@@ -51,6 +51,31 @@ pub fn (mut ctx Context) create_image(file string) Image {
return img
}
// TODO copypasta
pub fn (mut ctx Context) create_image_with_size(file string, width int, height int) Image {
if !C.sg_isvalid() {
// Sokol is not initialized yet, add stbi object to a queue/cache
// ctx.image_queue << file
stb_img := stbi.load(file) or { return Image{} }
img := Image{
width: width
height: height
nr_channels: stb_img.nr_channels
ok: false
data: stb_img.data
ext: stb_img.ext
path: file
id: ctx.image_cache.len
}
ctx.image_cache << img
return img
}
mut img := create_image(file)
img.id = ctx.image_cache.len
ctx.image_cache << img
return img
}
// TODO remove this
fn create_image(file string) Image {
if !os.exists(file) {
@@ -127,7 +152,11 @@ pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Imag
x0 := f32(x) * ctx.scale
y0 := f32(y) * ctx.scale
x1 := f32(x + width) * ctx.scale
y1 := f32(y + height) * ctx.scale
mut y1 := f32(y + height) * ctx.scale
if height == 0 {
scale := f32(img.width) / f32(width)
y1 = f32(y + int(f32(img.height) / scale)) * ctx.scale
}
//
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()

View File

@@ -80,8 +80,8 @@ fn (mut p Parser) for_stmt() ast.Stmt {
return for_c_stmt
} else if p.peek_tok.kind in [.key_in, .comma] ||
(p.tok.kind == .key_mut && p.peek_tok2.kind in [.key_in, .comma]) {
// `for i in vals`, `for i in start .. end`
val_is_mut := p.tok.kind == .key_mut
// `for i in vals`, `for i in start .. end`, `for mut user in users`, `for i, mut user in users`
mut val_is_mut := p.tok.kind == .key_mut
if val_is_mut {
p.next()
}
@@ -91,6 +91,11 @@ fn (mut p Parser) for_stmt() ast.Stmt {
mut val_var_name := p.check_name()
if p.tok.kind == .comma {
p.next()
if p.tok.kind == .key_mut {
// `for i, mut user in users {`
p.next()
val_is_mut = true
}
key_var_name = val_var_name
val_var_pos = p.tok.position()
val_var_name = p.check_name()