| author | |
| committer | |
| log | f210f17d306ed1298901d38a29c1e50a1ee38ae5 |
| tree | 77e220c6b51859d1c808a5fbcab82c97e1705fc3 |
| parent | 4b1d120f5874a3cad4d05e21e6a3219e48f9b056 |
6 files changed, 72 insertions(+), 7 deletions(-)
build.zig+4| ... | ... | @@ -53,6 +53,10 @@ pub fn build(b: &Builder) { |
| 53 | 53 | "std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests", |
| 54 | 54 | with_lldb)); |
| 55 | 55 | |
| 56 | test_step.dependOn(tests.addPkgTests(b, test_filter, | |
| 57 | "src-self-hosted/main.zig", "fmt", "Run the fmt tests", | |
| 58 | with_lldb)); | |
| 59 | ||
| 56 | 60 | test_step.dependOn(tests.addCompareOutputTests(b, test_filter)); |
| 57 | 61 | test_step.dependOn(tests.addBuildExampleTests(b, test_filter)); |
| 58 | 62 | test_step.dependOn(tests.addCompileErrorTests(b, test_filter)); |
src-self-hosted/main.zig+35| ... | ... | @@ -1387,6 +1387,7 @@ const Parser = struct { |
| 1387 | 1387 | |
| 1388 | 1388 | %return stream.print("("); |
| 1389 | 1389 | |
| 1390 | %return stack.append(RenderState { .Text = "\n" }); | |
| 1390 | 1391 | if (fn_proto.fn_def_node == null) { |
| 1391 | 1392 | %return stack.append(RenderState { .Text = ";" }); |
| 1392 | 1393 | } |
| ... | ... | @@ -1536,3 +1537,37 @@ fn removeNullCast(x: var) -> {const InnerPtr = @typeOf(x).Child.Child; &InnerPtr |
| 1536 | 1537 | const InnerPtr = @typeOf(x).Child.Child; |
| 1537 | 1538 | return @ptrCast(&InnerPtr, x); |
| 1538 | 1539 | } |
| 1540 | ||
| 1541 | ||
| 1542 | ||
| 1543 | fn testCanonical(source: []const u8) { | |
| 1544 | const allocator = std.debug.global_allocator; | |
| 1545 | std.debug.global_allocator_index = 0; | |
| 1546 | ||
| 1547 | var tokenizer = Tokenizer.init(source); | |
| 1548 | var parser = Parser.init(&tokenizer, allocator, "(memory buffer)"); | |
| 1549 | defer parser.deinit(); | |
| 1550 | ||
| 1551 | const root_node = parser.parse() %% unreachable; | |
| 1552 | defer parser.freeAst(root_node); | |
| 1553 | ||
| 1554 | var buffer = std.Buffer.initSize(allocator, 0) %% unreachable; | |
| 1555 | var buffer_out_stream = io.BufferOutStream.init(&buffer); | |
| 1556 | parser.renderSource(&buffer_out_stream.stream, root_node) %% unreachable; | |
| 1557 | ||
| 1558 | if (!mem.eql(u8, buffer.toSliceConst(), source)) { | |
| 1559 | warn("\n====== expected this output: =========\n"); | |
| 1560 | warn("{}", source); | |
| 1561 | warn("\n======== instead found this: =========\n"); | |
| 1562 | warn("{}", buffer.toSliceConst()); | |
| 1563 | warn("\n======================================\n"); | |
| 1564 | @panic("test failed"); | |
| 1565 | } | |
| 1566 | } | |
| 1567 | ||
| 1568 | test "zig fmt" { | |
| 1569 | testCanonical( | |
| 1570 | \\extern fn puts(s: &const u8) -> c_int; | |
| 1571 | \\ | |
| 1572 | ); | |
| 1573 | } |
std/buffer.zig+3| ... | ... | @@ -98,14 +98,17 @@ pub const Buffer = struct { |
| 98 | 98 | mem.copy(u8, self.list.toSlice()[old_len..], m); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | // TODO: remove, use OutStream for this | |
| 101 | 102 | pub fn appendFormat(self: &Buffer, comptime format: []const u8, args: ...) -> %void { |
| 102 | 103 | return fmt.format(self, append, format, args); |
| 103 | 104 | } |
| 104 | 105 | |
| 106 | // TODO: remove, use OutStream for this | |
| 105 | 107 | pub fn appendByte(self: &Buffer, byte: u8) -> %void { |
| 106 | 108 | return self.appendByteNTimes(byte, 1); |
| 107 | 109 | } |
| 108 | 110 | |
| 111 | // TODO: remove, use OutStream for this | |
| 109 | 112 | pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) -> %void { |
| 110 | 113 | var prev_size: usize = self.len(); |
| 111 | 114 | %return self.resize(prev_size + count); |
std/debug.zig+8-7| ... | ... | @@ -966,28 +966,29 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { |
| 966 | 966 | } |
| 967 | 967 | |
| 968 | 968 | pub const global_allocator = &global_allocator_state; |
| 969 | ||
| 969 | 970 | var global_allocator_state = mem.Allocator { |
| 970 | 971 | .allocFn = globalAlloc, |
| 971 | 972 | .reallocFn = globalRealloc, |
| 972 | 973 | .freeFn = globalFree, |
| 973 | 974 | }; |
| 974 | 975 | |
| 975 | var some_mem: [100 * 1024]u8 = undefined; | |
| 976 | var some_mem_index: usize = 0; | |
| 976 | pub var global_allocator_mem: [100 * 1024]u8 = undefined; | |
| 977 | pub var global_allocator_index: usize = 0; | |
| 977 | 978 | |
| 978 | 979 | error OutOfMemory; |
| 979 | 980 | |
| 980 | 981 | fn globalAlloc(self: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 { |
| 981 | const addr = @ptrToInt(&some_mem[some_mem_index]); | |
| 982 | const addr = @ptrToInt(&global_allocator_mem[global_allocator_index]); | |
| 982 | 983 | const rem = @rem(addr, alignment); |
| 983 | 984 | const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); |
| 984 | const adjusted_index = some_mem_index + march_forward_bytes; | |
| 985 | const adjusted_index = global_allocator_index + march_forward_bytes; | |
| 985 | 986 | const end_index = adjusted_index + n; |
| 986 | if (end_index > some_mem.len) { | |
| 987 | if (end_index > global_allocator_mem.len) { | |
| 987 | 988 | return error.OutOfMemory; |
| 988 | 989 | } |
| 989 | const result = some_mem[adjusted_index .. end_index]; | |
| 990 | some_mem_index = end_index; | |
| 990 | const result = global_allocator_mem[adjusted_index .. end_index]; | |
| 991 | global_allocator_index = end_index; | |
| 991 | 992 | return result; |
| 992 | 993 | } |
| 993 | 994 |
std/index.zig+1| ... | ... | @@ -3,6 +3,7 @@ pub const AlignedArrayList = @import("array_list.zig").AlignedArrayList; |
| 3 | 3 | pub const BufMap = @import("buf_map.zig").BufMap; |
| 4 | 4 | pub const BufSet = @import("buf_set.zig").BufSet; |
| 5 | 5 | pub const Buffer = @import("buffer.zig").Buffer; |
| 6 | pub const BufferOutStream = @import("buffer.zig").BufferOutStream; | |
| 6 | 7 | pub const HashMap = @import("hash_map.zig").HashMap; |
| 7 | 8 | pub const LinkedList = @import("linked_list.zig").LinkedList; |
| 8 | 9 |
std/io.zig+21| ... | ... | @@ -633,3 +633,24 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { |
| 633 | 633 | } |
| 634 | 634 | }; |
| 635 | 635 | } |
| 636 | ||
| 637 | /// Implementation of OutStream trait for Buffer | |
| 638 | pub const BufferOutStream = struct { | |
| 639 | buffer: &Buffer, | |
| 640 | stream: OutStream, | |
| 641 | ||
| 642 | pub fn init(buffer: &Buffer) -> BufferOutStream { | |
| 643 | return BufferOutStream { | |
| 644 | .buffer = buffer, | |
| 645 | .stream = OutStream { | |
| 646 | .writeFn = writeFn, | |
| 647 | }, | |
| 648 | }; | |
| 649 | } | |
| 650 | ||
| 651 | fn writeFn(out_stream: &OutStream, bytes: []const u8) -> %void { | |
| 652 | const self = @fieldParentPtr(BufferOutStream, "stream", out_stream); | |
| 653 | return self.buffer.append(bytes); | |
| 654 | } | |
| 655 | }; | |
| 656 |