all: make __global behave consistent with const (#9711)
parent
49330af715
commit
70c651ff17
|
@ -66,7 +66,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// g_original_codepage - used to restore the original windows console code page when exiting
|
// g_original_codepage - used to restore the original windows console code page when exiting
|
||||||
__global ( g_original_codepage = u32(0))
|
__global (
|
||||||
|
g_original_codepage = u32(0)
|
||||||
|
)
|
||||||
|
|
||||||
// utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8)
|
// utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8)
|
||||||
fn C.GetConsoleOutputCP() u32
|
fn C.GetConsoleOutputCP() u32
|
||||||
|
|
|
@ -1285,7 +1285,9 @@ pub fn (s string) ustring() ustring {
|
||||||
// A hack that allows to create ustring without allocations.
|
// A hack that allows to create ustring without allocations.
|
||||||
// It's called from functions like draw_text() where we know that the string is going to be freed
|
// It's called from functions like draw_text() where we know that the string is going to be freed
|
||||||
// right away. Uses global buffer for storing runes []int array.
|
// right away. Uses global buffer for storing runes []int array.
|
||||||
__global ( g_ustring_runes []int )
|
__global (
|
||||||
|
g_ustring_runes []int
|
||||||
|
)
|
||||||
|
|
||||||
pub fn (s string) ustring_tmp() ustring {
|
pub fn (s string) ustring_tmp() ustring {
|
||||||
if g_ustring_runes.len == 0 {
|
if g_ustring_runes.len == 0 {
|
||||||
|
|
|
@ -527,6 +527,7 @@ pub mut:
|
||||||
pub struct GlobalDecl {
|
pub struct GlobalDecl {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
is_block bool // __global() block
|
||||||
pub mut:
|
pub mut:
|
||||||
fields []GlobalField
|
fields []GlobalField
|
||||||
end_comments []Comment
|
end_comments []Comment
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
vlib/v/checker/tests/globals/assign_no_type.vv:1:21: error: global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )`
|
vlib/v/checker/tests/globals/assign_no_type.vv:1:21: error: global assign must have a type and value, use `name = type(value)` or `name type`
|
||||||
1 | __global ( test = 0 )
|
1 | __global ( test = 0 )
|
||||||
| ^
|
| ^
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
vlib/v/checker/tests/globals/assign_no_value.vv:1:23: error: global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )`
|
vlib/v/checker/tests/globals/assign_no_value.vv:1:23: error: global assign must have a type and value, use `name = type(value)` or `name type`
|
||||||
1 | __global ( test = int )
|
1 | __global ( test = int )
|
||||||
| ^
|
| ^
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
vlib/v/checker/tests/globals/unexpected_eof.vv:3:1: error: unexpected eof, expecting ´)´
|
||||||
|
1 | __global (
|
||||||
|
2 | x string
|
|
@ -0,0 +1,2 @@
|
||||||
|
__global (
|
||||||
|
x string
|
|
@ -1083,15 +1083,16 @@ pub fn (mut f Fmt) for_stmt(node ast.ForStmt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
|
pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
|
||||||
single := node.fields.len == 1
|
if node.fields.len == 0 && node.pos.line_nr == node.pos.last_line {
|
||||||
if single {
|
f.writeln('__global ()')
|
||||||
f.write('__global ( ')
|
return
|
||||||
} else {
|
|
||||||
f.writeln('__global (')
|
|
||||||
f.indent++
|
|
||||||
}
|
}
|
||||||
|
f.write('__global ')
|
||||||
mut max := 0
|
mut max := 0
|
||||||
mut has_assign := false
|
mut has_assign := false
|
||||||
|
if node.is_block {
|
||||||
|
f.writeln('(')
|
||||||
|
f.indent++
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
if field.name.len > max {
|
if field.name.len > max {
|
||||||
max = field.name.len
|
max = field.name.len
|
||||||
|
@ -1100,6 +1101,7 @@ pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
|
||||||
has_assign = true
|
has_assign = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
f.comments(field.comments, inline: true)
|
f.comments(field.comments, inline: true)
|
||||||
f.write('$field.name ')
|
f.write('$field.name ')
|
||||||
|
@ -1111,20 +1113,19 @@ pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
|
||||||
f.expr(field.expr)
|
f.expr(field.expr)
|
||||||
f.write(')')
|
f.write(')')
|
||||||
} else {
|
} else {
|
||||||
if !single && has_assign {
|
|
||||||
f.write(' ')
|
|
||||||
}
|
|
||||||
f.write('${f.table.type_to_str_using_aliases(field.typ, f.mod2alias)}')
|
f.write('${f.table.type_to_str_using_aliases(field.typ, f.mod2alias)}')
|
||||||
}
|
}
|
||||||
if !single {
|
if node.is_block {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !single {
|
|
||||||
f.indent--
|
|
||||||
}
|
|
||||||
f.comments_after_last_field(node.end_comments)
|
f.comments_after_last_field(node.end_comments)
|
||||||
f.writeln(')\n')
|
if node.is_block {
|
||||||
|
f.indent--
|
||||||
|
f.writeln(')')
|
||||||
|
} else {
|
||||||
|
f.writeln('')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) go_expr(node ast.GoExpr) {
|
pub fn (mut f Fmt) go_expr(node ast.GoExpr) {
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
__global x = bool(true)
|
||||||
|
__global y f32
|
||||||
|
__global ()
|
||||||
|
__global (
|
||||||
|
spam string
|
||||||
|
foo = int(5)
|
||||||
|
)
|
||||||
|
__global (
|
||||||
|
a int
|
||||||
|
)
|
|
@ -2818,17 +2818,19 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
|
||||||
return ast.GlobalDecl{}
|
return ast.GlobalDecl{}
|
||||||
}
|
}
|
||||||
start_pos := p.tok.position()
|
start_pos := p.tok.position()
|
||||||
end_pos := p.tok.position()
|
|
||||||
p.check(.key_global)
|
p.check(.key_global)
|
||||||
if p.tok.kind != .lpar {
|
is_block := p.tok.kind == .lpar
|
||||||
p.error('globals must be grouped, e.g. `__global ( a = int(1) )`')
|
if is_block {
|
||||||
return ast.GlobalDecl{}
|
|
||||||
}
|
|
||||||
p.next() // (
|
p.next() // (
|
||||||
|
}
|
||||||
mut fields := []ast.GlobalField{}
|
mut fields := []ast.GlobalField{}
|
||||||
mut comments := []ast.Comment{}
|
mut comments := []ast.Comment{}
|
||||||
for {
|
for {
|
||||||
comments = p.eat_comments({})
|
comments = p.eat_comments({})
|
||||||
|
if is_block && p.tok.kind == .eof {
|
||||||
|
p.error('unexpected eof, expecting ´)´')
|
||||||
|
return ast.GlobalDecl{}
|
||||||
|
}
|
||||||
if p.tok.kind == .rpar {
|
if p.tok.kind == .rpar {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -2840,13 +2842,13 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
|
||||||
}
|
}
|
||||||
typ := p.parse_type()
|
typ := p.parse_type()
|
||||||
if p.tok.kind == .assign {
|
if p.tok.kind == .assign {
|
||||||
p.error('global assign must have the type around the value, use `__global ( name = type(value) )`')
|
p.error('global assign must have the type around the value, use `name = type(value)`')
|
||||||
return ast.GlobalDecl{}
|
return ast.GlobalDecl{}
|
||||||
}
|
}
|
||||||
mut expr := ast.empty_expr()
|
mut expr := ast.empty_expr()
|
||||||
if has_expr {
|
if has_expr {
|
||||||
if p.tok.kind != .lpar {
|
if p.tok.kind != .lpar {
|
||||||
p.error('global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )`')
|
p.error('global assign must have a type and value, use `name = type(value)` or `name type`')
|
||||||
return ast.GlobalDecl{}
|
return ast.GlobalDecl{}
|
||||||
}
|
}
|
||||||
p.next() // (
|
p.next() // (
|
||||||
|
@ -2864,12 +2866,18 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
|
||||||
fields << field
|
fields << field
|
||||||
p.global_scope.register(field)
|
p.global_scope.register(field)
|
||||||
comments = []
|
comments = []
|
||||||
|
if !is_block {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if is_block {
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
|
}
|
||||||
return ast.GlobalDecl{
|
return ast.GlobalDecl{
|
||||||
pos: start_pos.extend(end_pos)
|
pos: start_pos.extend(p.prev_tok.position())
|
||||||
fields: fields
|
fields: fields
|
||||||
end_comments: comments
|
end_comments: comments
|
||||||
|
is_block: is_block
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue