From 4c50606b9d41c2c3d9f25dc8bf0848e30e338f6e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 18 Jan 2016 07:04:57 -0700 Subject: [PATCH] refactor std to use for loop --- README.md | 2 +- src/codegen.cpp | 2 +- std/rand.zig | 9 ++++----- std/std.zig | 6 +----- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2c30ca9b70..7732173b55 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ compromises backward compatibility. * Have a look in the examples/ folder to see some code examples. * Basic language features available such as loops, inline assembly, - expressions, literals, functions, importing, structs, enums. + expressions, literals, functions, importing, structs, tagged unions. * Linux x86_64 is supported. * Building for the native target is supported. * Optimized machine code that Zig produces is indistinguishable from diff --git a/src/codegen.cpp b/src/codegen.cpp index fce29f5d3e..72c5017d29 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1790,7 +1790,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) { LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody"); LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd"); - LLVMValueRef array_val = gen_expr(g, node->data.for_expr.array_expr); + LLVMValueRef array_val = gen_array_base_ptr(g, node->data.for_expr.array_expr); add_debug_source_node(g, node); LLVMBuildStore(g->builder, LLVMConstNull(index_var->type->type_ref), index_ptr); LLVMValueRef len_val; diff --git a/std/rand.zig b/std/rand.zig index 642dc0960b..5c23158d94 100644 --- a/std/rand.zig +++ b/std/rand.zig @@ -11,9 +11,10 @@ pub struct Rand { r.index = 0; r.array[0] = seed; var i : @typeof(ARRAY_SIZE) = 1; + var prev_value: u64 = seed; while (i < ARRAY_SIZE) { - const prev_value : u64 = r.array[i - 1]; r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i); + prev_value = r.array[i]; i += 1; } } @@ -67,9 +68,8 @@ pub struct Rand { } fn generate_numbers(r: &Rand) => { - var i : @typeof(ARRAY_SIZE) = 0; - while (i < ARRAY_SIZE) { - const y : u32 = (r.array[i] & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff); + for (item, r.array, i) { + const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff); const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1); r.array[i] = if ((y % 2) == 0) { untempered @@ -77,7 +77,6 @@ pub struct Rand { // y is odd untempered ^ 0x9908b0df }; - i += 1; } } diff --git a/std/std.zig b/std/std.zig index 28e626cd88..f6f21029ad 100644 --- a/std/std.zig +++ b/std/std.zig @@ -51,9 +51,7 @@ pub fn readline(buf: []u8, out_len: &usize) bool => { pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => { var x : u64 = 0; - var i : @typeof(buf.len) = 0; - while (i < buf.len) { - const c = buf[i]; + for (c, buf) { const digit = char_to_digit(c); if (digit > radix) { @@ -69,8 +67,6 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => { if (@add_with_overflow(u64, x, digit, &x)) { return true; } - - i += 1; } *result = x;