From fe6707b26dba6f594602443e3b40303d5dd85b0c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 18 Mar 2020 16:47:37 +0100 Subject: [PATCH] cgen: minor fixes --- vlib/builtin/array.v | 4 ++-- vlib/builtin/string.v | 4 ++-- vlib/v/checker/checker.v | 2 +- vlib/v/gen/cgen.v | 5 +++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 4c236afda5..27b03f606c 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -338,7 +338,7 @@ pub fn (a array) reverse() array { data: vcalloc(a.cap * a.element_size) } for i in 0..a.len { - C.memcpy(arr.data + i * arr.element_size, &a[a.len - 1 - i], arr.element_size) + C.memcpy(arr.data + i * arr.element_size, &a.data[a.len - 1 - i], arr.element_size) } return arr } @@ -412,7 +412,7 @@ pub fn (b []byte) hex() string { mut hex := malloc(b.len * 2 + 1) mut dst_i := 0 for i in b { - n0 := i >> 4 + n0 := i >> 4 hex[dst_i++] = if n0 < 10 { n0 + `0` } else { n0 + 87 } n1 := i & 0xF hex[dst_i++] = if n1 < 10 { n1 + `0` } else { n1 + 87 } diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index b18372a61a..a849541fbe 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -392,10 +392,10 @@ fn (s string) add(a string) string { str: malloc(new_len + 1) } for j in 0..s.len { - res[j] = s[j] + res.str[j] = s.str[j] } for j in 0..a.len { - res[s.len + j] = a[j] + res.str[s.len + j] = a.str[j] } res.str[new_len] = `\0` // V strings are not null terminated, but just in case return res diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 99576fd095..605f243f92 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -883,7 +883,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { } pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type { - if c.expected_type != 0 { + if c.expected_type != table.void_type { // sym := c.table.get_type_symbol(c.expected_type) // println('$c.file.path $node.pos.line_nr IF: checker exp type = ' + sym.name) node.is_expr = true diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 57898c3188..6edcb7c590 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1089,7 +1089,7 @@ fn (g mut Gen) ident(node ast.Ident) { // TODO `is` match node.info { ast.IdentVar { - if it.is_optional { + if it.is_optional && !g.is_assign_expr { g.write('/*opt*/') styp := g.typ(it.typ)[7..] // Option_int => int TODO perf? g.write('(*($styp*)${name}.data)') @@ -1103,6 +1103,7 @@ fn (g mut Gen) ident(node ast.Ident) { } fn (g mut Gen) if_expr(node ast.IfExpr) { + // g.writeln('/* if is_expr=$node.is_expr */') // If expression? Assign the value to a temp var. // Previously ?: was used, but it's too unreliable. type_sym := g.table.get_type_symbol(node.typ) @@ -1240,7 +1241,7 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) { g.write('))') } } - else if sym.kind == .string { + else if sym.kind == .string && !table.type_is_ptr(node.container_type) { g.write('string_at(') g.expr(node.left) g.write(', ')