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

force mut a := ..., do not allow mut a = ...

This commit is contained in:
Alexander Medvednikov 2019-06-28 14:19:46 +02:00
parent 806ad80360
commit 42bb85197a
6 changed files with 15 additions and 31 deletions

View File

@ -32,14 +32,14 @@ pub fn ptr_str(ptr voidptr) string {
// return i
// }
pub fn (nn int) str() string {
mut n = nn
mut n := nn
if n == 0 {
return '0'
}
max := 16
mut buf := malloc(max)
mut len := 0
mut is_neg = false
mut is_neg := false
if n < 0 {
n = -n
is_neg = true
@ -60,14 +60,14 @@ pub fn (nn int) str() string {
}
pub fn (nn u8) str() string {
mut n = nn
mut n := nn
if n == u8(0) {
return '0'
}
max := 5
mut buf := malloc(max)
mut len := 0
mut is_neg = false
mut is_neg := false
if n < u8(0) {
n = -n
is_neg = true
@ -88,14 +88,14 @@ pub fn (nn u8) str() string {
}
pub fn (nn i64) str() string {
mut n = nn
mut n := nn
if n == i64(0) {
return '0'
}
max := 32
mut buf := malloc(max)
mut len := 0
mut is_neg = false
mut is_neg := false
if n < i64(0) {
n = -n
is_neg = true

View File

@ -110,7 +110,7 @@ pub fn (s string) replace(rep, with string) string {
// Fill the new string
mut idx_pos := 0
mut cur_idx := idxs[idx_pos]
mut b_i = 0
mut b_i := 0
for i := 0; i < s.len; i++ {
// Reached the location of rep, replace it with "with"
if i == cur_idx {

View File

@ -531,7 +531,7 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
if !receiver.is_mut && receiver_type.contains('*') {
method_call += '*'
}
mut cast = ''
mut cast := ''
// Method returns (void*) => cast it to int, string, user etc
// number := *(int*)numbers.first()
if f.typ == 'void*' {

View File

@ -693,7 +693,7 @@ fn (p mut Parser) get_type() string {
debug := p.fileis('fn_test') && false
mut mul := false
mut nr_muls := 0
mut typ = ''
mut typ := ''
// fn type
if p.tok == FUNC {
if debug {
@ -941,6 +941,7 @@ fn (p mut Parser) statement(add_semi bool) string {
p.check(COLON)
return ''
}
// `a := 777`
else if p.peek() == DECL_ASSIGN {
p.log('var decl')
p.var_decl()
@ -949,7 +950,7 @@ fn (p mut Parser) statement(add_semi bool) string {
p.js_decode()
}
else {
// "a + 3", "a(7)" or maybe just "a"
// `a + 3`, `a(7)` or maybe just `a`
q = p.bool_expression()
}
case GOTO:
@ -1081,34 +1082,20 @@ fn (p mut Parser) var_decl() {
}
// println('var decl tok=${p.strtok()} ismut=$is_mut')
name := p.check_name()
p.fgen(' := ')
// Don't allow declaring a variable with the same name. Even in a child scope
// (shadowing is not allowed)
if !p.builtin_pkg && p.cur_fn.known_var(name) {
v := p.cur_fn.find_var(name)
p.error('redefinition of `$name`')
// Check if this variable has already been declared only in the first run.
// Otherwise the is_script code outside main will run in the first run
// since we can't skip the function body since there's no function.
// And the variable will be registered twice.
if p.is_play && p.first_run() && !p.builtin_pkg {
p.error('variable `$name` is already declared.')
}
}
// println('var_decl $name')
// p.assigned_var = name
p.next()// :=
p.check_space(DECL_ASSIGN) // :=
// Generate expression to tmp because we need its type first
// [TYP NAME =] bool_expression()
pos := p.cgen.add_placeholder()
// p.gen('typ $name = ')
// p.gen('/*^^^*/')
mut typ := p.bool_expression()
// p.gen('/*VVV*/')
// Option check ? or {
or_else := p.tok == OR_ELSE
tmp := p.get_tmp()
// assigned_var_copy := p.assigned_var
if or_else {
// Option_User tmp = get_user(1);
// if (!tmp.ok) { or_statement }
@ -1126,7 +1113,6 @@ fn (p mut Parser) var_decl() {
println(p.prev_tok2)
p.error('`or` statement must return/continue/break')
}
// p.assigned_var = assigned_var_copy
}
p.register_var(Var {
name: name
@ -1136,11 +1122,9 @@ fn (p mut Parser) var_decl() {
mut cgen_typ := typ
if !or_else {
gen_name := p.table.var_cgen_name(name)
// p.cgen.set_placeholder(pos, '$cgen_typ $gen_name = ')
mut nt_gen := p.table.cgen_name_type_pair(gen_name, cgen_typ) + '='
if is_static {
nt_gen = 'static $nt_gen'
// p.gen('static ')
}
p.cgen.set_placeholder(pos, nt_gen)
}
@ -1165,7 +1149,7 @@ fn (p mut Parser) bool_expression() string {
fn (p mut Parser) bterm() string {
ph := p.cgen.add_placeholder()
mut typ = p.expression()
mut typ := p.expression()
is_str := typ=='string'
tok := p.tok
// if tok in [ EQ, GT, LT, LE, GE, NE] {

View File

@ -129,7 +129,7 @@ fn (s mut Scanner) skip_whitespace() {
}
fn (s mut Scanner) get_var_name(pos int) string {
mut pos_start = pos
mut pos_start := pos
for ; pos_start >= 0 && s.text[pos_start] != `\n` && s.text[pos_start] != `;`; pos_start-- {}
pos_start++

View File

@ -100,7 +100,7 @@ fn (t Time) hhmm_tmp() string {
// 9:04pm
pub fn (t Time) hhmm12() string {
mut am := 'am'
mut hour = t.hour
mut hour := t.hour
if t.hour > 11 {
am = 'pm'
}