From 46aed151abb1d933d2b711a9e05d905f09c2d93a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 4 Apr 2020 14:09:58 +0200 Subject: [PATCH] parser: snake_case check; array: mutable array test --- vlib/builtin/array_test.v | 21 +++++++++++++++++++++ vlib/v/parser/fn.v | 4 ++++ vlib/v/util/errors.v | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 89ad389047..61db9f1245 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -313,6 +313,27 @@ fn test_mut_slice() { */ } +fn double_up(a mut []int) { + for i := 0; i < a.len; i++ { + a[i] = a[i]*2 + } +} + +fn double_up_v2(a mut []int) { + for i, val in a { + a[i] = a[i]*2 // or val*2, doesn't matter + } +} + +fn test_mut_arg() { + mut arr := [1,2,3,4,5,6,7,8,9,10] + double_up(mut arr) + assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]' + arr = [1,2,3,4,5,6,7,8,9,10] + double_up_v2(mut arr) + assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]' +} + fn test_clone() { nums := [1, 2, 3, 4, 100] nums2 := nums.clone() diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index c78068d837..4a82632bd0 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -6,6 +6,7 @@ module parser import ( v.ast v.table + v.scanner ) pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr { @@ -103,6 +104,9 @@ fn (p mut Parser) fn_decl() ast.FnDecl { if p.tok.kind == .name { // TODO high order fn name = p.check_name() + if !is_c && !p.pref.translated && scanner.contains_capital(name) { + p.error('function names cannot contain uppercase letters, use snake_case instead') + } } if p.tok.kind in [.plus, .minus, .mul, .div, .mod] { name = p.tok.kind.str() // op_to_fn_name() diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index 24b8488826..05601587be 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -59,7 +59,7 @@ pub fn formated_error(kind string /*error or warn*/, emsg string, filepath strin mut source_context := '' // final_position := if emanager.support_color { - term.bold(term.white(position)) + term.bold(position) // term.white(position)) } else { position }