cgen/fmt: fix assign_stmt fix & cgen test & hash tests & fmt

pull/3949/head
Joe Conigliaro 2020-03-11 11:31:24 +11:00
parent 630913d872
commit 083964522b
9 changed files with 29 additions and 20 deletions

View File

@ -7,7 +7,7 @@
module crc32 module crc32
// polynomials // polynomials
const ( pub const (
ieee = 0xedb88320 ieee = 0xedb88320
castagnoli = 0x82f63b78 castagnoli = 0x82f63b78
koopman = 0xeb31d82e koopman = 0xeb31d82e
@ -26,7 +26,7 @@ mut:
fn(c mut Crc32) generate_table(poly int) { fn(c mut Crc32) generate_table(poly int) {
for i in 0..256 { for i in 0..256 {
mut crc := u32(i) mut crc := u32(i)
for j in 0..8 { for _ in 0..8 {
if crc & u32(1) == u32(1) { if crc & u32(1) == u32(1) {
crc = (crc >> 1) ^ u32(poly) crc = (crc >> 1) ^ u32(poly)
} else { } else {
@ -40,7 +40,7 @@ fn(c mut Crc32) generate_table(poly int) {
fn(c &Crc32) sum32(b []byte) u32 { fn(c &Crc32) sum32(b []byte) u32 {
mut crc := ~u32(0) mut crc := ~u32(0)
for i in 0..b.len { 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 return ~crc
} }

View File

@ -4,12 +4,12 @@ fn test_hash_crc32() {
b1 := 'testing crc32'.bytes() b1 := 'testing crc32'.bytes()
sum1 := crc32.sum(b1) sum1 := crc32.sum(b1)
assert sum1 == u32(1212124400) assert sum1 == u32(1212124400)
assert sum1.hex() == '0x483f8cf0' assert sum1.hex() == '483f8cf0'
c := crc32.new(crc32.ieee) c := crc32.new(crc32.ieee)
b2 := 'testing crc32 again'.bytes() b2 := 'testing crc32 again'.bytes()
sum2 := c.checksum(b2) sum2 := c.checksum(b2)
assert sum2 == u32(1420327025) assert sum2 == u32(1420327025)
assert sum2.hex() == '0x54a87871' assert sum2.hex() == '54a87871'
} }

View File

@ -7,6 +7,6 @@ fn test_fnv1a() {
a := 'apple' a := 'apple'
b := fnv1a.sum64_string(a) b := fnv1a.sum64_string(a)
c := fnv1a.sum64(a.bytes()) c := fnv1a.sum64(a.bytes())
assert b.hex() == '0xf74a62a458befdbf' assert b.hex() == 'f74a62a458befdbf'
assert c.hex() == '0xf74a62a458befdbf' assert c.hex() == 'f74a62a458befdbf'
} }

View File

@ -42,8 +42,8 @@ pub fn (b mut Builder) write(s string) {
pub fn (b mut Builder) go_back(n int) { pub fn (b mut Builder) go_back(n int) {
b.buf.trim(b.buf.len-n) b.buf.trim(b.buf.len-n)
//b.len -= n b.len -= n
} }
pub fn (b mut Builder) writeln(s string) { pub fn (b mut Builder) writeln(s string) {
// for c in s { // for c in s {

View File

@ -27,6 +27,10 @@ pub fn (node &FnDecl) str(t &table.Table) string {
name := node.name.after('.') name := node.name.after('.')
f.write('fn ${receiver}${name}(') f.write('fn ${receiver}${name}(')
for i, arg in node.args { for i, arg in node.args {
// skip receiver
if node.is_method && i == 0 {
continue
}
is_last_arg := i == node.args.len - 1 is_last_arg := i == node.args.len - 1
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ ||
(node.is_variadic && i == node.args.len - 2) (node.is_variadic && i == node.args.len - 2)

View File

@ -302,14 +302,16 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.expr(assign_stmt.right[0]) g.expr(assign_stmt.right[0])
g.writeln(';') g.writeln(';')
for i, ident in assign_stmt.left { for i, ident in assign_stmt.left {
if ident.kind == .blank_ident {
continue
}
ident_var_info := ident.var_info() ident_var_info := ident.var_info()
styp := g.typ(ident_var_info.typ) styp := g.typ(ident_var_info.typ)
if ident.kind == .blank_ident { if assign_stmt.op == .decl_assign {
g.writeln('{ $styp _ = ${mr_var_name}.arg$i};') g.write('$styp ')
}
else {
g.writeln('$styp $ident.name = ${mr_var_name}.arg$i;')
} }
g.expr(ident)
g.writeln(' = ${mr_var_name}.arg$i;')
} }
} }
// `a := 1` | `a,b := 1,2` // `a := 1` | `a,b := 1,2`
@ -339,7 +341,11 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
} }
else { else {
g.write('$styp $ident.name = ') if assign_stmt.op == .decl_assign {
g.write('$styp ')
}
g.expr(ident)
g.write(' = ')
g.expr(val) g.expr(val)
} }
g.writeln(';') g.writeln(';')

View File

@ -90,7 +90,7 @@ i < 10; i++) {
bool q = true || false; bool q = true || false;
bool b2 = array_get(bools, 0) || true; bool b2 = array_get(bools, 0) || true;
bool b3 = get_bool() || true; bool b3 = get_bool() || true;
int f = array_int_first(nums); int f = array_first(nums);
string d = tos3("d"); string d = tos3("d");
println(string_add(s, d)); println(string_add(s, d));
} }

View File

@ -175,7 +175,6 @@ fn end() {
e := 2 + 3 * 4 e := 2 + 3 * 4
//mut a := [1,2,3] //mut a := [1,2,3]
//(a << 4) + 2 //(a << 4) + 2
} }

View File

@ -33,7 +33,7 @@ int main() {
a.a = tos3("da"); a.a = tos3("da");
a.b.a = 111; a.b.a = 111;
string a1 = a.a; string a1 = a.a;
int a2 = Bar_testa(b); int a2 = Bar_testa(&b);
int c = testa(); int c = testa();
c = 1; c = 1;
string d = testb(1); string d = testb(1);
@ -57,8 +57,8 @@ int main() {
string ma1 = tos3("hello"); string ma1 = tos3("hello");
string ma2 = tos3("vlang"); string ma2 = tos3("vlang");
multi_return_int_string mr_578 = mr_test(); multi_return_int_string mr_578 = mr_test();
int mr1 = mr_578->arg[0]; int mr1 = mr_578.arg0;
string mr2 = mr_578->arg[1]; string mr2 = mr_578.arg1;
return 0; return 0;
} }