authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-08 01:34:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-08 01:34:00-07:00
log0c32b0b4ad32549193b46d33026d72b5e4a17bd9
tree31070922081ac6517013ca8a76991f79456364e7
parent18ed87c695398a1fbe972d0f8d212365a9832883

add list implementation to standard library


7 files changed, 112 insertions(+), 2 deletions(-)

CMakeLists.txt+2
...@@ -214,6 +214,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}")...@@ -214,6 +214,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}")
214install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}")214install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}")
215install(FILES "${CMAKE_SOURCE_DIR}/std/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}")215install(FILES "${CMAKE_SOURCE_DIR}/std/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}")
216install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}")216install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}")
217install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
218install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
217219
218add_executable(run_tests ${TEST_SOURCES})220add_executable(run_tests ${TEST_SOURCES})
219target_link_libraries(run_tests)221target_link_libraries(run_tests)
src/codegen.cpp+3
...@@ -2386,6 +2386,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV...@@ -2386,6 +2386,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV
2386 LLVMPositionBuilderAtEnd(g->builder, then_block);2386 LLVMPositionBuilderAtEnd(g->builder, then_block);
2387 LLVMValueRef then_expr_result = gen_expr(g, then_node);2387 LLVMValueRef then_expr_result = gen_expr(g, then_node);
2388 if (then_endif_reachable) {2388 if (then_endif_reachable) {
2389 clear_debug_source_node(g);
2389 LLVMBuildBr(g->builder, endif_block);2390 LLVMBuildBr(g->builder, endif_block);
2390 }2391 }
2391 LLVMBasicBlockRef after_then_block = LLVMGetInsertBlock(g->builder);2392 LLVMBasicBlockRef after_then_block = LLVMGetInsertBlock(g->builder);
...@@ -2393,6 +2394,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV...@@ -2393,6 +2394,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV
2393 LLVMPositionBuilderAtEnd(g->builder, else_block);2394 LLVMPositionBuilderAtEnd(g->builder, else_block);
2394 LLVMValueRef else_expr_result = gen_expr(g, else_node);2395 LLVMValueRef else_expr_result = gen_expr(g, else_node);
2395 if (else_endif_reachable) {2396 if (else_endif_reachable) {
2397 clear_debug_source_node(g);
2396 LLVMBuildBr(g->builder, endif_block);2398 LLVMBuildBr(g->builder, endif_block);
2397 }2399 }
2398 LLVMBasicBlockRef after_else_block = LLVMGetInsertBlock(g->builder);2400 LLVMBasicBlockRef after_else_block = LLVMGetInsertBlock(g->builder);
...@@ -2400,6 +2402,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV...@@ -2400,6 +2402,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV
2400 if (then_endif_reachable || else_endif_reachable) {2402 if (then_endif_reachable || else_endif_reachable) {
2401 LLVMPositionBuilderAtEnd(g->builder, endif_block);2403 LLVMPositionBuilderAtEnd(g->builder, endif_block);
2402 if (use_then_value && use_else_value) {2404 if (use_then_value && use_else_value) {
2405 set_debug_source_node(g, source_node);
2403 LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMTypeOf(then_expr_result), "");2406 LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMTypeOf(then_expr_result), "");
2404 LLVMValueRef incoming_values[2] = {then_expr_result, else_expr_result};2407 LLVMValueRef incoming_values[2] = {then_expr_result, else_expr_result};
2405 LLVMBasicBlockRef incoming_blocks[2] = {after_then_block, after_else_block};2408 LLVMBasicBlockRef incoming_blocks[2] = {after_then_block, after_else_block};
std/index.zig+2
...@@ -5,6 +5,8 @@ pub const math = @import("math.zig");...@@ -5,6 +5,8 @@ pub const math = @import("math.zig");
5pub const str = @import("str.zig");5pub const str = @import("str.zig");
6pub const cstr = @import("cstr.zig");6pub const cstr = @import("cstr.zig");
7pub const net = @import("net.zig");7pub const net = @import("net.zig");
8pub const list = @import("list.zig");
9pub const mem = @import("mem.zig");
810
9pub fn assert(b: bool) {11pub fn assert(b: bool) {
10 if (!b) unreachable{}12 if (!b) unreachable{}
std/list.zig created+92
...@@ -0,0 +1,92 @@
1const assert = @import("index.zig").assert;
2const mem = @import("mem.zig");
3const Allocator = mem.Allocator;
4
5/*
6fn List(T: type) -> type {
7 List(T, 8)
8}
9*/
10
11pub struct SmallList(T: type, STATIC_SIZE: isize) {
12 items: []T,
13 length: isize,
14 prealloc_items: [STATIC_SIZE]T,
15 allocator: &Allocator,
16
17 pub fn init(l: &SmallList(T, STATIC_SIZE), allocator: &Allocator) {
18 l.items = l.prealloc_items[0...];
19 l.length = 0;
20 l.allocator = allocator;
21 }
22
23 pub fn deinit(l: &SmallList(T, STATIC_SIZE)) {
24 if (l.items.ptr == &l.prealloc_items[0]) {
25 l.allocator.free(l.allocator, ([]u8)(l.items));
26 }
27 }
28
29 pub fn append(l: &SmallList(T, STATIC_SIZE), item: T) -> %void {
30 const new_length = l.length + 1;
31 %return l.ensure_capacity(new_length);
32 l.items[l.length] = item;
33 l.length = new_length;
34 }
35
36 pub fn ensure_capacity(l: &SmallList(T, STATIC_SIZE), new_capacity: isize) -> %void {
37 const old_capacity = l.items.len;
38 var better_capacity = old_capacity;
39 while (better_capacity < new_capacity) {
40 better_capacity *= 2;
41 }
42 if (better_capacity != old_capacity) {
43 const alloc_bytes = better_capacity * @sizeof(T);
44 if (l.items.ptr == &l.prealloc_items[0]) {
45 l.items = ([]T)(%return l.allocator.alloc(l.allocator, alloc_bytes));
46 @memcpy(l.items.ptr, &l.prealloc_items[0], old_capacity * @sizeof(T));
47 } else {
48 l.items = ([]T)(%return l.allocator.realloc(l.allocator, ([]u8)(l.items), alloc_bytes));
49 }
50 }
51 }
52}
53
54var global_allocator = Allocator {
55 .alloc = global_alloc,
56 .realloc = global_realloc,
57 .free = global_free,
58 .context = null,
59};
60
61var some_mem: [200]u8 = undefined;
62var some_mem_index: isize = 0;
63
64fn global_alloc(self: &Allocator, n: isize) -> %[]u8 {
65 const result = some_mem[some_mem_index ... some_mem_index + n];
66 some_mem_index += n;
67 return result;
68}
69
70fn global_realloc(self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8 {
71 const result = %return global_alloc(self, new_size);
72 @memcpy(result.ptr, old_mem.ptr, old_mem.len);
73 return result;
74}
75
76fn global_free(self: &Allocator, old_mem: []u8) {
77}
78
79#attribute("test")
80fn basic_list_test() {
81 var list: SmallList(i32, 4) = undefined;
82 list.init(&global_allocator);
83 defer list.deinit();
84
85 {var i: isize = 0; while (i < 10; i += 1) {
86 %%list.append(i32(i + 1));
87 }}
88
89 {var i: isize = 0; while (i < 10; i += 1) {
90 assert(list.items[i] == i32(i + 1));
91 }}
92}
std/mem.zig created+10
...@@ -0,0 +1,10 @@
1pub error NoMem;
2
3pub type Context = u8;
4pub struct Allocator {
5 alloc: fn (self: &Allocator, n: isize) -> %[]u8,
6 realloc: fn (self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8,
7 free: fn (self: &Allocator, mem: []u8),
8 context: ?&Context,
9}
10
test/run_tests.cpp+1-1
...@@ -1666,7 +1666,7 @@ static void run_self_hosted_test(bool is_release_mode) {...@@ -1666,7 +1666,7 @@ static void run_self_hosted_test(bool is_release_mode) {
1666 Termination term;1666 Termination term;
1667 os_exec_process(zig_exe, args, &term, &zig_stderr, &zig_stdout);1667 os_exec_process(zig_exe, args, &term, &zig_stderr, &zig_stdout);
16681668
1669 if (term.how != TerminationIdClean) {1669 if (term.how != TerminationIdClean || term.code != 0) {
1670 printf("\nSelf-hosted tests failed:\n");1670 printf("\nSelf-hosted tests failed:\n");
1671 printf("./zig");1671 printf("./zig");
1672 for (int i = 0; i < args.length; i += 1) {1672 for (int i = 0; i < args.length; i += 1) {
test/self_hosted.zig+2-1
...@@ -1622,7 +1622,8 @@ fn div_exact(a: u32, b: u32) -> u32 {...@@ -1622,7 +1622,8 @@ fn div_exact(a: u32, b: u32) -> u32 {
16221622
1623#attribute("test")1623#attribute("test")
1624fn null_literal_outside_function() {1624fn null_literal_outside_function() {
1625 assert(here_is_a_null_literal.context == null);1625 const is_null = if (const _ ?= here_is_a_null_literal.context) false else true;
1626 assert(is_null);
1626}1627}
1627struct SillyStruct {1628struct SillyStruct {
1628 context: ?i32,1629 context: ?i32,