From 083964522bbc30c9dd8ab957f794cfc6626922b4 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Wed, 11 Mar 2020 11:31:24 +1100 Subject: [PATCH] cgen/fmt: fix assign_stmt fix & cgen test & hash tests & fmt --- vlib/hash/crc32/crc32.v | 6 +++--- vlib/hash/crc32/crc32_test.v | 4 ++-- vlib/hash/fnv1a/fnv1a_test.v | 4 ++-- vlib/strings/builder_c.v | 4 ++-- vlib/v/ast/str.v | 4 ++++ vlib/v/gen/cgen.v | 18 ++++++++++++------ vlib/v/gen/tests/1.c | 2 +- vlib/v/gen/tests/1.vv | 1 - vlib/v/gen/tests/4.c | 6 +++--- 9 files changed, 29 insertions(+), 20 deletions(-) diff --git a/vlib/hash/crc32/crc32.v b/vlib/hash/crc32/crc32.v index 7c116f2abe..6020df563d 100644 --- a/vlib/hash/crc32/crc32.v +++ b/vlib/hash/crc32/crc32.v @@ -7,7 +7,7 @@ module crc32 // polynomials -const ( +pub const ( ieee = 0xedb88320 castagnoli = 0x82f63b78 koopman = 0xeb31d82e @@ -26,7 +26,7 @@ mut: fn(c mut Crc32) generate_table(poly int) { for i in 0..256 { mut crc := u32(i) - for j in 0..8 { + for _ in 0..8 { if crc & u32(1) == u32(1) { crc = (crc >> 1) ^ u32(poly) } else { @@ -40,7 +40,7 @@ fn(c mut Crc32) generate_table(poly int) { fn(c &Crc32) sum32(b []byte) u32 { mut crc := ~u32(0) for i in 0..b.len { - crc = c.table[byte(crc)^b[i]] ^ u32(crc >> u32(8)) + crc = c.table[byte(crc)^b[i]] ^ (crc >> 8) } return ~crc } diff --git a/vlib/hash/crc32/crc32_test.v b/vlib/hash/crc32/crc32_test.v index 44961c4032..777dd10fb2 100644 --- a/vlib/hash/crc32/crc32_test.v +++ b/vlib/hash/crc32/crc32_test.v @@ -4,12 +4,12 @@ fn test_hash_crc32() { b1 := 'testing crc32'.bytes() sum1 := crc32.sum(b1) assert sum1 == u32(1212124400) - assert sum1.hex() == '0x483f8cf0' + assert sum1.hex() == '483f8cf0' c := crc32.new(crc32.ieee) b2 := 'testing crc32 again'.bytes() sum2 := c.checksum(b2) assert sum2 == u32(1420327025) - assert sum2.hex() == '0x54a87871' + assert sum2.hex() == '54a87871' } diff --git a/vlib/hash/fnv1a/fnv1a_test.v b/vlib/hash/fnv1a/fnv1a_test.v index 216159d87d..f775ab1b5a 100644 --- a/vlib/hash/fnv1a/fnv1a_test.v +++ b/vlib/hash/fnv1a/fnv1a_test.v @@ -7,6 +7,6 @@ fn test_fnv1a() { a := 'apple' b := fnv1a.sum64_string(a) c := fnv1a.sum64(a.bytes()) - assert b.hex() == '0xf74a62a458befdbf' - assert c.hex() == '0xf74a62a458befdbf' + assert b.hex() == 'f74a62a458befdbf' + assert c.hex() == 'f74a62a458befdbf' } diff --git a/vlib/strings/builder_c.v b/vlib/strings/builder_c.v index 2b75de1582..8a3a6d7846 100644 --- a/vlib/strings/builder_c.v +++ b/vlib/strings/builder_c.v @@ -42,8 +42,8 @@ pub fn (b mut Builder) write(s string) { pub fn (b mut Builder) go_back(n int) { b.buf.trim(b.buf.len-n) - //b.len -= n - } + b.len -= n +} pub fn (b mut Builder) writeln(s string) { // for c in s { diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index ff9b3f87f3..1f6e3d98ba 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -27,6 +27,10 @@ pub fn (node &FnDecl) str(t &table.Table) string { name := node.name.after('.') f.write('fn ${receiver}${name}(') for i, arg in node.args { + // skip receiver + if node.is_method && i == 0 { + continue + } is_last_arg := i == node.args.len - 1 should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i == node.args.len - 2) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 16169825bc..9db65ef5a6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -302,14 +302,16 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(assign_stmt.right[0]) g.writeln(';') for i, ident in assign_stmt.left { + if ident.kind == .blank_ident { + continue + } ident_var_info := ident.var_info() styp := g.typ(ident_var_info.typ) - if ident.kind == .blank_ident { - g.writeln('{ $styp _ = ${mr_var_name}.arg$i};') - } - else { - g.writeln('$styp $ident.name = ${mr_var_name}.arg$i;') + if assign_stmt.op == .decl_assign { + g.write('$styp ') } + g.expr(ident) + g.writeln(' = ${mr_var_name}.arg$i;') } } // `a := 1` | `a,b := 1,2` @@ -339,7 +341,11 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } } else { - g.write('$styp $ident.name = ') + if assign_stmt.op == .decl_assign { + g.write('$styp ') + } + g.expr(ident) + g.write(' = ') g.expr(val) } g.writeln(';') diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index bad0fac993..8b7cf64133 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -90,7 +90,7 @@ i < 10; i++) { bool q = true || false; bool b2 = array_get(bools, 0) || true; bool b3 = get_bool() || true; - int f = array_int_first(nums); + int f = array_first(nums); string d = tos3("d"); println(string_add(s, d)); } diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index d1854e0835..0aabba38da 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -175,7 +175,6 @@ fn end() { e := 2 + 3 * 4 //mut a := [1,2,3] //(a << 4) + 2 - } diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index a45cd600b3..b2b3b730b3 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -33,7 +33,7 @@ int main() { a.a = tos3("da"); a.b.a = 111; string a1 = a.a; - int a2 = Bar_testa(b); + int a2 = Bar_testa(&b); int c = testa(); c = 1; string d = testb(1); @@ -57,8 +57,8 @@ int main() { string ma1 = tos3("hello"); string ma2 = tos3("vlang"); multi_return_int_string mr_578 = mr_test(); - int mr1 = mr_578->arg[0]; - string mr2 = mr_578->arg[1]; + int mr1 = mr_578.arg0; + string mr2 = mr_578.arg1; return 0; }