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

parser: support mut static x := y in -translated mode

This commit is contained in:
Delyan Angelov 2020-10-09 22:20:58 +03:00
parent 04d3ca7dbe
commit e862fad917
4 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/static_vars_in_translated_mode.vv:2:13: error: static variables are supported only in -translated mode
1 | fn counter() int {
2 | mut static icounter := 0
| ~~~~~~~~
3 | icounter++
4 | return icounter

View File

@ -0,0 +1,12 @@
fn counter() int {
mut static icounter := 0
icounter++
return icounter
}
fn main() {
println(counter())
println(counter())
println(counter())
println(counter())
}

View File

@ -263,6 +263,9 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
if var_info.is_mut {
f.write(var_info.share.str() + ' ')
}
if var_info.is_static {
f.write('static ')
}
f.expr(left)
} else {
f.expr(left)

View File

@ -105,6 +105,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
}
}
}
mut is_static := false
for i, lx in left {
match mut lx {
ast.Ident {
@ -114,7 +115,14 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
}
mut share := table.ShareType(0)
if lx.info is ast.IdentVar {
share = (lx.info as ast.IdentVar).share
iv := lx.info as ast.IdentVar
share = iv.share
if iv.is_static {
if !p.pref.translated {
p.error_with_pos('static variables are supported only in -translated mode', lx.pos)
}
is_static = true
}
}
mut v := ast.Var{
name: lx.name
@ -157,5 +165,6 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
pos: pos
has_cross_var: has_cross_var
is_simple: p.inside_for && p.tok.kind == .lcbr
is_static: is_static
}
}