From d47e2f113fb419e8a4309364d4c53e34f26045ed Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 5 Jul 2019 02:44:22 +0200 Subject: [PATCH] Revert "make function arguments immutable" This reverts commit 0f0ed8d716485b2a8bc8c5bbac9a01f9ba4bbdbc. --- compiler/fn.v | 4 ++-- compiler/jsgen.v | 8 ++++---- compiler/main.v | 2 +- compiler/parser.v | 27 +++++++++++---------------- compiler/table.v | 14 +++++--------- vlib/builtin/string.v | 10 +++++----- vlib/os/os.v | 8 ++++---- vlib/strings/builder.v | 2 +- 8 files changed, 33 insertions(+), 42 deletions(-) diff --git a/compiler/fn.v b/compiler/fn.v index ea6250fa9e..7e761640f2 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -443,8 +443,8 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type // Normal function => just its name, method => TYPE_FNNAME mut fn_name := f.name if f.is_method { - _receiver_type := receiver_type.replace('*', '') - fn_name = '${_receiver_type}_${f.name}' + receiver_type = receiver_type.replace('*', '') + fn_name = '${receiver_type}_${f.name}' } // Generate tmp struct with args arg_struct_name := 'thread_arg_$fn_name' diff --git a/compiler/jsgen.v b/compiler/jsgen.v index bbe4972715..5b62c098ce 100644 --- a/compiler/jsgen.v +++ b/compiler/jsgen.v @@ -115,8 +115,8 @@ fn is_js_prim(typ string) bool { typ == 'i8' || typ == 'i16' || typ == 'i32' || typ == 'i64' } -fn (p mut Parser) decode_array(_typ string) string { - typ := _typ.replace('array_', '') +fn (p mut Parser) decode_array(typ string) string { + typ = typ.replace('array_', '') t := p.table.find_type(typ) fn_name := js_dec_name(typ) // If we have `[]Profile`, have to register a Profile en(de)coder first @@ -149,8 +149,8 @@ fn js_dec_name(typ string) string { return name } -fn (p &Parser) encode_array(_typ string) string { - typ := _typ.replace('array_', '') +fn (p &Parser) encode_array(typ string) string { + typ = typ.replace('array_', '') fn_name := js_enc_name(typ) return ' o = cJSON_CreateArray(); diff --git a/compiler/main.v b/compiler/main.v index dfd567025c..93a9df14ca 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -418,7 +418,7 @@ string _STR_TMP(const char *fmt, ...) { } } -fn (c mut V) cc_windows_cross() { +fn (c &V) cc_windows_cross() { if !c.out_name.ends_with('.exe') { c.out_name = c.out_name + '.exe' } diff --git a/compiler/parser.v b/compiler/parser.v index 529a43d1c4..a6101d24e5 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1044,8 +1044,7 @@ fn (p mut Parser) statement(add_semi bool) string { fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) { p.log('assign_statement() name=$v.name tok=') tok := p.tok - //if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{ - if !v.is_mut && !p.pref.translated && !v.is_global{ + if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{ p.error('`$v.name` is immutable') } is_str := v.typ == 'string' @@ -1455,7 +1454,7 @@ fn (p mut Parser) var_expr(v Var) string { } // a++ and a-- if p.tok == INC || p.tok == DEC { - if !v.is_mut && !p.pref.translated { + if !v.is_mut && !v.is_arg && !p.pref.translated { p.error('`$v.name` is immutable') } if typ != 'int' { @@ -1582,12 +1581,11 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string { return method.typ } -fn (p mut Parser) index_expr(_typ string, fn_ph int) string { +fn (p mut Parser) index_expr(typ string, fn_ph int) string { //if p.fileis('main.v') { //println('index expr typ=$typ') //} // a[0] - mut typ := _typ v := p.expr_var is_map := typ.starts_with('map_') is_str := typ == 'string' @@ -2135,8 +2133,8 @@ fn (p mut Parser) typ_to_fmt(typ string) string { return '' } -fn format_str(_str string) string { - mut str := _str.replace('"', '\\"') +fn format_str(str string) string { + str = str.replace('"', '\\"') $if windows { str = str.replace('\r\n', '\\n') } @@ -3079,8 +3077,7 @@ fn (p mut Parser) go_statement() { } } -fn (p mut Parser) register_var(var Var) { - mut v := var +fn (p mut Parser) register_var(v Var) { if v.line_nr == 0 { v.line_nr = p.scanner.line_nr } @@ -3144,8 +3141,8 @@ fn (p mut Parser) js_decode() string { return '' } -fn is_compile_time_const(_s string) bool { - s := _s.trim_space() +fn is_compile_time_const(s string) bool { + s = s.trim_space() if s == '' { return false } @@ -3169,17 +3166,15 @@ fn (p &Parser) building_v() bool { /////////////////////////////////////////////////////////////////////////////// // fmt helpers -fn (scanner mut Scanner) fgen(_s string) { - mut s := _s +fn (scanner mut Scanner) fgen(s string) { if scanner.fmt_line_empty { - s = strings.repeat(`\t`, scanner.fmt_indent) + s + s = strings.repeat(`\t`, scanner.fmt_indent) + s } scanner.fmt_out.write(s) scanner.fmt_line_empty = false } -fn (scanner mut Scanner) fgenln(_s string) { - mut s := _s +fn (scanner mut Scanner) fgenln(s string) { if scanner.fmt_line_empty { s = strings.repeat(`\t`, scanner.fmt_indent) + s } diff --git a/compiler/table.v b/compiler/table.v index 209d9c8a8e..1de6cbcfb5 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -184,8 +184,7 @@ fn (t mut Table) register_fn(new_fn Fn) { t.fns[new_fn.name] = new_fn } -fn (table &Table) known_type(_typ string) bool { - mut typ := _typ +fn (table &Table) known_type(typ string) bool { // 'byte*' => look up 'byte', but don't mess up fns if typ.ends_with('*') && !typ.contains(' ') { typ = typ.left(typ.len - 1) @@ -381,8 +380,7 @@ fn (p &Parser) find_type(name string) *Type { return typ } -fn (t &Table) find_type(_name string) *Type { - mut name := _name +fn (t &Table) find_type(name string) *Type { if name.ends_with('*') && !name.contains(' ') { name = name.left(name.len - 1) } @@ -395,9 +393,7 @@ fn (t &Table) find_type(_name string) *Type { return &Type{} } -fn (p mut Parser) _check_types(_got, _expected string, throw bool) bool { - mut expected := _expected - mut got := _got +fn (p mut Parser) _check_types(got, expected string, throw bool) bool { p.log('check types got="$got" exp="$expected" ') if p.pref.translated { return true @@ -522,8 +518,8 @@ fn (p mut Parser) satisfies_interface(interface_name, _typ string, throw bool) b fn type_default(typ string) string { if typ.starts_with('array_') { - elm_type := typ.right(6) - return 'new_array(0, 1, sizeof($elm_type))' + typ = typ.right(6) + return 'new_array(0, 1, sizeof($typ))' } // Always set pointers to 0 if typ.ends_with('*') { diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index add140a477..eade49afe7 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -642,14 +642,14 @@ pub fn (s string) ustring_tmp() ustring { } fn (u ustring) substr(start, end int) string { - _start := u.runes[start] - _end := if end >= u.runes.len { - u.s.len + start = u.runes[start] + if end >= u.runes.len { + end = u.s.len } else { - u.runes[end] + end = u.runes[end] } - return u.s.substr(_start, _end) + return u.s.substr(start, end) } fn (u ustring) left(pos int) string { diff --git a/vlib/os/os.v b/vlib/os/os.v index 92a919ffac..83c1ffe043 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -262,8 +262,8 @@ fn popen(path string) *FILE { } // exec starts the specified command, waits for it to complete, and returns its output. -pub fn exec(_cmd string) string { - cmd := '$_cmd 2>&1' +pub fn exec(cmd string) string { + cmd = '$cmd 2>&1' f := popen(cmd) if isnil(f) { // TODO optional or error code @@ -331,8 +331,8 @@ pub fn dir_exists(path string) bool { // mkdir creates a new directory with the specified path. pub fn mkdir(path string) { $if windows { - _path := path.replace('/', '\\') - C.CreateDirectory(_path.cstr(), 0) + path = path.replace('/', '\\') + C.CreateDirectory(path.cstr(), 0) } $else { C.mkdir(path.cstr(), 511)// S_IRWXU | S_IRWXG | S_IRWXO diff --git a/vlib/strings/builder.v b/vlib/strings/builder.v index ecc55c1cf5..11f34c6127 100644 --- a/vlib/strings/builder.v +++ b/vlib/strings/builder.v @@ -33,7 +33,7 @@ pub fn (b Builder) str() string { return tos(b.buf.data, b.len) } -pub fn (b mut Builder) cut(n int) { +pub fn (b Builder) cut(n int) { b.len -= n }