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

compiler: blank identifier for loops. dont declare any variables

This commit is contained in:
joe-conigliaro 2019-09-29 02:53:56 +10:00 committed by Alexander Medvednikov
parent 7952d591b9
commit ed3a4961d0
4 changed files with 35 additions and 38 deletions

View File

@ -543,11 +543,10 @@ fn (p mut Parser) check_unused_variables() {
if var.name == '' { if var.name == '' {
break break
} }
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated && var.name != '_' { if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated {
p.production_error('`$var.name` declared and not used', var.scanner_pos ) p.production_error('`$var.name` declared and not used', var.scanner_pos )
} }
if !var.is_changed && var.is_mut && !p.pref.is_repl && if !var.is_changed && var.is_mut && !p.pref.is_repl && !p.pref.translated {
!p.pref.translated && var.name != '_' {
p.error_with_position( '`$var.name` is declared as mutable, but it was never changed', var.scanner_pos ) p.error_with_position( '`$var.name` is declared as mutable, but it was never changed', var.scanner_pos )
} }
} }

View File

@ -256,17 +256,20 @@ fn (p mut Parser) gen_array_at(typ_ string, is_arr0 bool, fn_ph int) {
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) { fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
p.genln('for (int $i = 0; $i < ${tmp}.len; $i++) {') p.genln('for (int $i = 0; $i < ${tmp}.len; $i++) {')
if val == '_' { return }
p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];') p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];')
} }
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) { fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
p.genln('array_byte bytes_$tmp = string_bytes( $tmp );') p.genln('array_byte bytes_$tmp = string_bytes( $tmp );')
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {') p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
if val == '_' { return }
p.genln('$var_typ $val = (($var_typ *) bytes_$tmp . data)[$i];') p.genln('$var_typ $val = (($var_typ *) bytes_$tmp . data)[$i];')
} }
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) { fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {') p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {')
if val == '_' { return }
p.genln('$var_type $val = $i;') p.genln('$var_type $val = $i;')
} }
@ -277,6 +280,7 @@ fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
p.genln('string $i = ((string*)keys_$tmp .data)[l];') p.genln('string $i = ((string*)keys_$tmp .data)[l];')
// TODO don't call map_get() for each key, fetch values while traversing // TODO don't call map_get() for each key, fetch values while traversing
// the tree (replace `map_keys()` above with `map_key_vals()`) // the tree (replace `map_keys()` above with `map_key_vals()`)
if val == '_' { return }
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);') p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
} }

View File

@ -99,21 +99,25 @@ fn (p mut Parser) gen_array_at(typ string, is_arr0 bool, fn_ph int) {
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) { fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
p.genln('for (var $i = 0; $i < ${tmp}.length; $i++) {') p.genln('for (var $i = 0; $i < ${tmp}.length; $i++) {')
if val == '_' { return }
p.genln('var $val = $tmp [$i];') p.genln('var $val = $tmp [$i];')
} }
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) { fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
p.genln(';\nfor (var $i = $tmp; $i < $range_end; $i++) {') p.genln(';\nfor (var $i = $tmp; $i < $range_end; $i++) {')
if val == '_' { return }
p.genln('var /*$var_type*/ $val = $i;') p.genln('var /*$var_type*/ $val = $i;')
} }
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) { fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
p.genln('for (var $i = 0; $i < $tmp .length; $i ++) {') p.genln('for (var $i = 0; $i < $tmp .length; $i ++) {')
if val == '_' { return }
p.genln('var $val = $tmp[$i];') p.genln('var $val = $tmp[$i];')
} }
fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) { fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
p.genln('for (var $i in $tmp) {') p.genln('for (var $i in $tmp) {')
if val == '_' { return }
p.genln('var $val = $tmp[$i];') p.genln('var $val = $tmp[$i];')
} }

View File

@ -3275,44 +3275,33 @@ fn (p mut Parser) for_st() {
pad := if is_arr { 6 } else { 4 } pad := if is_arr { 6 } else { 4 }
var_typ := if is_str { 'byte' } else { typ.right(pad) } var_typ := if is_str { 'byte' } else { typ.right(pad) }
// typ = strings.Replace(typ, "_ptr", "*", -1) // typ = strings.Replace(typ, "_ptr", "*", -1)
// Register temp var mut i_var_type := 'int'
val_var := Var {
name: val
typ: var_typ
ptr: typ.contains('*')
}
p.register_var(val_var)
if is_arr { if is_arr {
i_var := Var {
name: i
typ: 'int'
// parent_fn: p.cur_fn
is_mut: true
is_changed: true
}
//p.genln(';\nfor ($i_type $i = 0; $i < $tmp .len; $i ++) {')
p.gen_for_header(i, tmp, var_typ, val) p.gen_for_header(i, tmp, var_typ, val)
p.register_var(i_var)
} }
else if is_map { else if is_map {
i_var := Var { i_var_type = 'string'
name: i
typ: 'string'
is_mut: true
is_changed: true
}
p.register_var(i_var)
p.gen_for_map_header(i, tmp, var_typ, val, typ) p.gen_for_map_header(i, tmp, var_typ, val, typ)
} }
else if is_str { else if is_str {
i_var := Var { i_var_type = 'byte'
p.gen_for_str_header(i, tmp, var_typ, val)
}
// Register temp vars
if i != '_' {
p.register_var(Var {
name: i name: i
typ: 'byte' typ: i_var_type
is_mut: true is_mut: true
is_changed: true is_changed: true
})
} }
p.register_var(i_var) if val != '_' {
p.gen_for_str_header(i, tmp, var_typ, val) p.register_var(Var {
name: val
typ: var_typ
ptr: typ.contains('*')
})
} }
} }
// `for val in vals` // `for val in vals`
@ -3357,13 +3346,14 @@ fn (p mut Parser) for_st() {
} }
// println('for typ=$typ vartyp=$var_typ') // println('for typ=$typ vartyp=$var_typ')
// Register temp var // Register temp var
val_var := Var { if val != '_' {
p.register_var(Var {
name: val name: val
typ: var_type typ: var_type
ptr: typ.contains('*') ptr: typ.contains('*')
is_changed: true is_changed: true
})
} }
p.register_var(val_var)
i := p.get_tmp() i := p.get_tmp()
if is_arr { if is_arr {
p.gen_for_header(i, tmp, var_type, val) p.gen_for_header(i, tmp, var_type, val)