authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 07:04:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 07:16:17-07:00
log4c50606b9d41c2c3d9f25dc8bf0848e30e338f6e
tree7bebbbac71fa1fde2f90213b7b0c60d6ef1a84ad
parentfbbef140130e8da13f1d58b884ec3a0225965531

refactor std to use for loop


4 files changed, 7 insertions(+), 12 deletions(-)

README.md+1-1
......@@ -52,7 +52,7 @@ compromises backward compatibility.
5252
5353 * Have a look in the examples/ folder to see some code examples.
5454 * Basic language features available such as loops, inline assembly,
55 expressions, literals, functions, importing, structs, enums.
55 expressions, literals, functions, importing, structs, tagged unions.
5656 * Linux x86_64 is supported.
5757 * Building for the native target is supported.
5858 * Optimized machine code that Zig produces is indistinguishable from
src/codegen.cpp+1-1
......@@ -1790,7 +1790,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
17901790 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
17911791 LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");
17921792
1793 LLVMValueRef array_val = gen_expr(g, node->data.for_expr.array_expr);
1793 LLVMValueRef array_val = gen_array_base_ptr(g, node->data.for_expr.array_expr);
17941794 add_debug_source_node(g, node);
17951795 LLVMBuildStore(g->builder, LLVMConstNull(index_var->type->type_ref), index_ptr);
17961796 LLVMValueRef len_val;
std/rand.zig+4-5
......@@ -11,9 +11,10 @@ pub struct Rand {
1111 r.index = 0;
1212 r.array[0] = seed;
1313 var i : @typeof(ARRAY_SIZE) = 1;
14 var prev_value: u64 = seed;
1415 while (i < ARRAY_SIZE) {
15 const prev_value : u64 = r.array[i - 1];
1616 r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i);
17 prev_value = r.array[i];
1718 i += 1;
1819 }
1920 }
......@@ -67,9 +68,8 @@ pub struct Rand {
6768 }
6869
6970 fn generate_numbers(r: &Rand) => {
70 var i : @typeof(ARRAY_SIZE) = 0;
71 while (i < ARRAY_SIZE) {
72 const y : u32 = (r.array[i] & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
71 for (item, r.array, i) {
72 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
7373 const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
7474 r.array[i] = if ((y % 2) == 0) {
7575 untempered
......@@ -77,7 +77,6 @@ pub struct Rand {
7777 // y is odd
7878 untempered ^ 0x9908b0df
7979 };
80 i += 1;
8180 }
8281 }
8382
std/std.zig+1-5
......@@ -51,9 +51,7 @@ pub fn readline(buf: []u8, out_len: &usize) bool => {
5151pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => {
5252 var x : u64 = 0;
5353
54 var i : @typeof(buf.len) = 0;
55 while (i < buf.len) {
56 const c = buf[i];
54 for (c, buf) {
5755 const digit = char_to_digit(c);
5856
5957 if (digit > radix) {
......@@ -69,8 +67,6 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => {
6967 if (@add_with_overflow(u64, x, digit, &x)) {
7068 return true;
7169 }
72
73 i += 1;
7470 }
7571
7672 *result = x;